#include <cstdint>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>

const int BLOCK_SIZE = 1024;

class PointSourcePollution {
  public:
    PointSourcePollution() = default;
    ~PointSourcePollution() = default;
    void end(const double* data, uint64_t cylinder_size);
};

void PointSourcePollution::end(const double* data, uint64_t cylinder_size) {
  std::ofstream payload;
  payload.open("output.txt");

  for (uint64_t i = 0; i < cylinder_size; ++i) {
    if (i != 0) {
      payload << " ";
    }

    payload << data[i];
  }

  payload.close();
}

__device__
void central_difference_theorem(
    double left,
    double right,
    double& out
    ) {
  out = (left + right) / 2.0;
}

__global__
void diffuse(
    double* cylinder,
    double* copy_cylinder,
    double* temp,
    uint64_t cylinder_size,
    uint64_t diffusion_time,
    uint64_t contaminant_concentration
    ) {
  double left, right, cdt_out;
  int i = blockIdx.x * BLOCK_SIZE + threadIdx.x;

  if (i < cylinder_size) {
    if (i > 0)
      left = cylinder[i - 1];
    else
      left = cylinder[i];
    right = cylinder[i + 1];

    central_difference_theorem(left, right, cdt_out);
    cylinder[i] = cdt_out;
    temp = cylinder;
    cylinder = copy_cylinder;
    copy_cylinder = temp;
  }
}


int main(int argc, char** argv) {
  uint64_t cylinder_size, slice_location, diffusion_time, contaminant_concentration;

  if (argc < 5) {
    std::cerr << "usage: psp cylinder_size slice_location diffusion_time contaminant_concentration" << std::endl;
    return EXIT_FAILURE;
  }

  for (int i = 0; i < argc; ++i) {
    if (atoi(argv[i]) < 0) {
      std::cerr << "All inputs must be greater than 0" << std::endl;
      return EXIT_FAILURE;
    }
  }

  PointSourcePollution psp;
  cylinder_size = atoi(argv[1]);
  slice_location = atoi(argv[2]);
  diffusion_time = atoi(argv[3]);
  contaminant_concentration = atoi(argv[4]);
  cudaError_t e;
  double *cylinder, *copy_cylinder, *temp;

  cudaMallocManaged(&cylinder, cylinder_size * sizeof(double));
  cudaMallocManaged(&copy_cylinder, cylinder_size * sizeof(double));
  cudaMallocManaged(&temp, cylinder_size * sizeof(double));

  // init our arrays
  for (int i = 0; i < cylinder_size; ++i) {
    if (i == 0) {
      cylinder[i] = contaminant_concentration;
      copy_cylinder[i] = contaminant_concentration;
    } else {
      cylinder[i] = 0.0;
      copy_cylinder[i] = 0.0;
    }
  }
  std::cout << cylinder[0] << copy_cylinder[0] << std::endl;

  e = cudaGetLastError();
  if (e != cudaSuccess) {
    std::cerr << "Error: " << cudaGetErrorString(e) << std::endl;
    return EXIT_FAILURE;
  }

  const uint64_t GRID_SIZE = ceil(cylinder_size / static_cast<double>(BLOCK_SIZE));
  for (int i = 0; i < diffusion_time; ++i) {
    diffuse<<<GRID_SIZE, BLOCK_SIZE>>>(
        cylinder,
        copy_cylinder,
        temp,
        cylinder_size,
        diffusion_time,
        contaminant_concentration);
  }
  cudaDeviceSynchronize();

  e = cudaGetLastError();
  if (e != cudaSuccess) {
    std::cerr << "Error2: " << cudaGetErrorString(e) << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "Answer at slice location: " << slice_location << " is " << cylinder[slice_location] << std::endl;
  std::cout << "Now visualizing results..." << std::endl;
  psp.end(cylinder, cylinder_size);

  cudaFree(cylinder);
  cudaFree(copy_cylinder);

  e = cudaGetLastError();
  if (e != cudaSuccess) {
    std::cerr << cudaGetErrorString(e) << std::endl;
    return EXIT_FAILURE;
  }


  system("python plot.py");
  return EXIT_SUCCESS;
}

	.file	"tmpxft_002dac6e_00000000-6_psp.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4134:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4134:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.type	_ZNSolsEPFRSoS_E.isra.0, @function
_ZNSolsEPFRSoS_E.isra.0:
.LFB4842:
	.cfi_startproc
	jmp	*%rsi
	.cfi_endproc
.LFE4842:
	.size	_ZNSolsEPFRSoS_E.isra.0, .-_ZNSolsEPFRSoS_E.isra.0
	.globl	_Z26central_difference_theoremddRd
	.type	_Z26central_difference_theoremddRd, @function
_Z26central_difference_theoremddRd:
.LFB4130:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE4130:
	.size	_Z26central_difference_theoremddRd, .-_Z26central_difference_theoremddRd
	.globl	_Z33__device_stub__Z7diffusePdS_S_mmmPdS_S_mmm
	.type	_Z33__device_stub__Z7diffusePdS_S_mmmPdS_S_mmm, @function
_Z33__device_stub__Z7diffusePdS_S_mmmPdS_S_mmm:
.LFB4156:
	.cfi_startproc
	endbr64
	subq	$184, %rsp
	.cfi_def_cfa_offset 192
	movq	%rdi, 40(%rsp)
	leaq	72(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	84(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	56(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	64(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%r9, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 168(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 80(%rsp)
	movq	%rax, 120(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 152(%rsp)
	movq	%rsp, %rax
	movq	%rax, 160(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 72(%rsp)
	movq	%rax, 84(%rsp)
	movl	$1, 92(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L5
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z7diffusePdS_S_mmm(%rip), %rdi
	pushq	64(%rsp)
	.cfi_def_cfa_offset 208
	movq	100(%rsp), %rcx
	movl	108(%rsp), %r8d
	movq	88(%rsp), %rsi
	movl	96(%rsp), %edx
	leaq	136(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 200
	popq	%rdx
	.cfi_def_cfa_offset 192
.L5:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L7
	call	__stack_chk_fail@PLT
.L7:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4156:
	.size	_Z33__device_stub__Z7diffusePdS_S_mmmPdS_S_mmm, .-_Z33__device_stub__Z7diffusePdS_S_mmmPdS_S_mmm
	.globl	_Z7diffusePdS_S_mmm
	.type	_Z7diffusePdS_S_mmm, @function
_Z7diffusePdS_S_mmm:
.LFB4157:
	.cfi_startproc
	endbr64
	jmp	_Z33__device_stub__Z7diffusePdS_S_mmmPdS_S_mmm
	.cfi_endproc
.LFE4157:
	.size	_Z7diffusePdS_S_mmm, .-_Z7diffusePdS_S_mmm
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"_Z7diffusePdS_S_mmm"
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4159:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC0(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z7diffusePdS_S_mmm(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE4159:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.rodata.str1.1
.LC1:
	.string	"output.txt"
.LC2:
	.string	" "
	.text
	.align 2
	.globl	_ZN20PointSourcePollution3endEPKdm
	.type	_ZN20PointSourcePollution3endEPKdm, @function
_ZN20PointSourcePollution3endEPKdm:
.LFB4129:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA4129
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rdx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$528, %rsp
	.cfi_def_cfa_offset 576
	movq	%fs:40, %rax
	movq	%rax, 520(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rbx
	movq	%rbx, %rdi
.LEHB0:
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1Ev@PLT
.LEHE0:
	movl	$16, %edx
	leaq	.LC1(%rip), %rsi
	movq	%rbx, %rdi
.LEHB1:
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode@PLT
	xorl	%ebp, %ebp
	leaq	.LC2(%rip), %r14
.L14:
	cmpq	%r12, %rbp
	je	.L28
	testq	%rbp, %rbp
	jne	.L15
.L17:
	movsd	0(%r13,%rbp,8), %xmm0
	movq	%rbx, %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	incq	%rbp
	jmp	.L14
.L15:
	movq	%r14, %rsi
	movq	%rbx, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	jmp	.L17
.L28:
	movq	%rbx, %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE5closeEv@PLT
.LEHE1:
	movq	%rbx, %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	520(%rsp), %rax
	subq	%fs:40, %rax
	je	.L21
	jmp	.L26
.L23:
	endbr64
	movq	%rax, %rbp
.L19:
	movq	%rbx, %rdi
	call	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev@PLT
	movq	520(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L26
	movq	%rbp, %rdi
.LEHB2:
	call	_Unwind_Resume@PLT
.LEHE2:
.L26:
	call	__stack_chk_fail@PLT
.L21:
	addq	$528, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4129:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA4129:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE4129-.LLSDACSB4129
.LLSDACSB4129:
	.uleb128 .LEHB0-.LFB4129
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB4129
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L23-.LFB4129
	.uleb128 0
	.uleb128 .LEHB2-.LFB4129
	.uleb128 .LEHE2-.LEHB2
	.uleb128 0
	.uleb128 0
.LLSDACSE4129:
	.text
	.size	_ZN20PointSourcePollution3endEPKdm, .-_ZN20PointSourcePollution3endEPKdm
	.section	.rodata.str1.1
.LC3:
	.string	"usage: psp cylinder_size slice_location diffusion_time contaminant_concentration"
.LC4:
	.string	"All inputs must be greater than 0"
.LC6:
	.string	"Error: "
.LC9:
	.string	"Error2: "
.LC10:
	.string	"Answer at slice location: "
.LC11:
	.string	" is "
.LC12:
	.string	"Now visualizing results..."
.LC13:
	.string	"python plot.py"
	.section	.text.startup
	.globl	main
	.type	main, @function
main:
.LFB4131:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	cmpl	$4, %edi
	jg	.L30
	leaq	.LC3(%rip), %rsi
	jmp	.L52
.L30:
	movslq	%edi, %rdi
	movq	%rsi, %rbx
	movq	%rsi, %rbp
	leaq	(%rsi,%rdi,8), %r12
.L34:
	movq	0(%rbp), %rdi
	call	atoi@PLT
	testl	%eax, %eax
	jns	.L32
	leaq	.LC4(%rip), %rsi
.L52:
	leaq	_ZSt4cerr(%rip), %rdi
.L53:
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	$1, %eax
	jmp	.L29
.L32:
	addq	$8, %rbp
	cmpq	%r12, %rbp
	jne	.L34
	movq	8(%rbx), %rdi
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movslq	%eax, %r12
	call	atoi@PLT
	movq	24(%rbx), %rdi
	leaq	0(,%r12,8), %r15
	movq	%r12, %r14
	movl	%eax, 12(%rsp)
	call	atoi@PLT
	movq	32(%rbx), %rdi
	movslq	%eax, %r13
	call	atoi@PLT
	leaq	24(%rsp), %rdi
	movl	$1, %edx
	movq	%r15, %rsi
	movslq	%eax, %rbx
	call	cudaMallocManaged@PLT
	leaq	32(%rsp), %rdi
	movl	$1, %edx
	movq	%r15, %rsi
	call	cudaMallocManaged@PLT
	leaq	40(%rsp), %rdi
	movl	$1, %edx
	movq	%r15, %rsi
	call	cudaMallocManaged@PLT
	movq	%rbx, %rdi
	movq	24(%rsp), %rdx
	xorl	%eax, %eax
	movq	32(%rsp), %rsi
	andl	$1, %edi
.L35:
	cmpq	%r12, %rax
	jnb	.L54
	testq	%rax, %rax
	jne	.L36
	testq	%rbx, %rbx
	js	.L37
	cvtsi2sdq	%rbx, %xmm0
	jmp	.L38
.L37:
	movq	%rbx, %rcx
	shrq	%rcx
	orq	%rdi, %rcx
	cvtsi2sdq	%rcx, %xmm0
	addsd	%xmm0, %xmm0
.L38:
	movsd	%xmm0, (%rdx)
	movsd	%xmm0, (%rsi)
	jmp	.L39
.L36:
	movq	$0x000000000, (%rdx,%rax,8)
	movq	$0x000000000, (%rsi,%rax,8)
.L39:
	incq	%rax
	jmp	.L35
.L54:
	movsd	(%rdx), %xmm0
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	%rax, %rdi
	movq	32(%rsp), %rax
	movsd	(%rax), %xmm0
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	call	cudaGetLastError@PLT
	movl	%eax, %r15d
	testl	%eax, %eax
	je	.L41
	leaq	.LC6(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%r15d, %edi
	movq	%rax, %rbx
	call	cudaGetErrorString@PLT
	movq	%rbx, %rdi
	movq	%rax, %rsi
	jmp	.L53
.L41:
	cvtsi2sdl	%r14d, %xmm0
	mulsd	.LC7(%rip), %xmm0
	call	ceil@PLT
	movsd	.LC8(%rip), %xmm1
	comisd	%xmm1, %xmm0
	jnb	.L42
	cvttsd2siq	%xmm0, %rbp
	jmp	.L43
.L42:
	subsd	%xmm1, %xmm0
	cvttsd2siq	%xmm0, %rbp
	btcq	$63, %rbp
.L43:
	movabsq	$4294968320, %r15
	xorl	%r14d, %r14d
.L44:
	cmpq	%r13, %r14
	jnb	.L55
	movl	%ebp, 48(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r15, %rdx
	movabsq	$4294967297, %rax
	movl	$1, %ecx
	movq	%r15, 60(%rsp)
	movq	%rax, 52(%rsp)
	movq	48(%rsp), %rdi
	movl	56(%rsp), %esi
	movl	$1, 68(%rsp)
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L45
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movq	%rbx, %r9
	movq	%r13, %r8
	movq	24(%rsp), %rdi
	movq	%r12, %rcx
	call	_Z33__device_stub__Z7diffusePdS_S_mmmPdS_S_mmm
.L45:
	incq	%r14
	jmp	.L44
.L55:
	call	cudaDeviceSynchronize@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L47
	leaq	.LC9(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	%ebx, %edi
	movq	%rax, %rbp
	call	cudaGetErrorString@PLT
	movq	%rbp, %rdi
	movq	%rax, %rsi
	jmp	.L53
.L47:
	movslq	12(%rsp), %rbp
	leaq	.LC10(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	movq	%rbp, %rsi
	call	_ZNSo9_M_insertImEERSoT_@PLT
	leaq	.LC11(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	movq	24(%rsp), %rax
	movsd	(%rax,%rbp,8), %xmm0
	call	_ZNSo9_M_insertIdEERSoT_@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	leaq	.LC12(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movq	24(%rsp), %rsi
	movq	%r12, %rdx
	leaq	60(%rsp), %rdi
	call	_ZN20PointSourcePollution3endEPKdm
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	call	cudaGetLastError@PLT
	movl	%eax, %edi
	testl	%eax, %eax
	je	.L48
	call	cudaGetErrorString@PLT
	movq	%rax, %rsi
	jmp	.L52
.L48:
	leaq	.LC13(%rip), %rdi
	call	system@PLT
	xorl	%eax, %eax
.L29:
	movq	72(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L49
	call	__stack_chk_fail@PLT
.L49:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4131:
	.size	main, .-main
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC7:
	.long	0
	.long	1062207488
	.align 8
.LC8:
	.long	0
	.long	1138753536
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <cstdint>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>

const int BLOCK_SIZE = 1024;

class PointSourcePollution {
  public:
    PointSourcePollution() = default;
    ~PointSourcePollution() = default;
    void end(const double* data, uint64_t cylinder_size);
};

void PointSourcePollution::end(const double* data, uint64_t cylinder_size) {
  std::ofstream payload;
  payload.open("output.txt");

  for (uint64_t i = 0; i < cylinder_size; ++i) {
    if (i != 0) {
      payload << " ";
    }

    payload << data[i];
  }

  payload.close();
}

__device__
void central_difference_theorem(
    double left,
    double right,
    double& out
    ) {
  out = (left + right) / 2.0;
}

__global__
void diffuse(
    double* cylinder,
    double* copy_cylinder,
    double* temp,
    uint64_t cylinder_size,
    uint64_t diffusion_time,
    uint64_t contaminant_concentration
    ) {
  double left, right, cdt_out;
  int i = blockIdx.x * BLOCK_SIZE + threadIdx.x;

  if (i < cylinder_size) {
    if (i > 0)
      left = cylinder[i - 1];
    else
      left = cylinder[i];
    right = cylinder[i + 1];

    central_difference_theorem(left, right, cdt_out);
    cylinder[i] = cdt_out;
    temp = cylinder;
    cylinder = copy_cylinder;
    copy_cylinder = temp;
  }
}


int main(int argc, char** argv) {
  uint64_t cylinder_size, slice_location, diffusion_time, contaminant_concentration;

  if (argc < 5) {
    std::cerr << "usage: psp cylinder_size slice_location diffusion_time contaminant_concentration" << std::endl;
    return EXIT_FAILURE;
  }

  for (int i = 0; i < argc; ++i) {
    if (atoi(argv[i]) < 0) {
      std::cerr << "All inputs must be greater than 0" << std::endl;
      return EXIT_FAILURE;
    }
  }

  PointSourcePollution psp;
  cylinder_size = atoi(argv[1]);
  slice_location = atoi(argv[2]);
  diffusion_time = atoi(argv[3]);
  contaminant_concentration = atoi(argv[4]);
  hipError_t e;
  double *cylinder, *copy_cylinder, *temp;

  hipMallocManaged(&cylinder, cylinder_size * sizeof(double));
  hipMallocManaged(&copy_cylinder, cylinder_size * sizeof(double));
  hipMallocManaged(&temp, cylinder_size * sizeof(double));

  // init our arrays
  for (int i = 0; i < cylinder_size; ++i) {
    if (i == 0) {
      cylinder[i] = contaminant_concentration;
      copy_cylinder[i] = contaminant_concentration;
    } else {
      cylinder[i] = 0.0;
      copy_cylinder[i] = 0.0;
    }
  }
  std::cout << cylinder[0] << copy_cylinder[0] << std::endl;

  e = hipGetLastError();
  if (e != hipSuccess) {
    std::cerr << "Error: " << hipGetErrorString(e) << std::endl;
    return EXIT_FAILURE;
  }

  const uint64_t GRID_SIZE = ceil(cylinder_size / static_cast<double>(BLOCK_SIZE));
  for (int i = 0; i < diffusion_time; ++i) {
    diffuse<<<GRID_SIZE, BLOCK_SIZE>>>(
        cylinder,
        copy_cylinder,
        temp,
        cylinder_size,
        diffusion_time,
        contaminant_concentration);
  }
  hipDeviceSynchronize();

  e = hipGetLastError();
  if (e != hipSuccess) {
    std::cerr << "Error2: " << hipGetErrorString(e) << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "Answer at slice location: " << slice_location << " is " << cylinder[slice_location] << std::endl;
  std::cout << "Now visualizing results..." << std::endl;
  psp.end(cylinder, cylinder_size);

  hipFree(cylinder);
  hipFree(copy_cylinder);

  e = hipGetLastError();
  if (e != hipSuccess) {
    std::cerr << hipGetErrorString(e) << std::endl;
    return EXIT_FAILURE;
  }


  system("python plot.py");
  return EXIT_SUCCESS;
}

	.text
	.file	"psp.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_ZN20PointSourcePollution3endEPKdm # -- Begin function _ZN20PointSourcePollution3endEPKdm
	.p2align	1, 0x90
	.type	_ZN20PointSourcePollution3endEPKdm,@function
_ZN20PointSourcePollution3endEPKdm:     # @_ZN20PointSourcePollution3endEPKdm
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$520, %rsp                      # imm = 0x208
	.cfi_def_cfa_offset 560
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdx, %rbx
	movq	%rsi, %r14
	leaq	8(%rsp), %r15
	movq	%r15, %rdi
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEEC1Ev
.Ltmp0:
	movl	$.L.str, %esi
	movq	%r15, %rdi
	movl	$16, %edx
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode
.Ltmp1:
# %bb.1:                                # %.preheader
	testq	%rbx, %rbx
	je	.LBB0_7
# %bb.2:                                # %.lr.ph.preheader
	xorl	%r12d, %r12d
	leaq	8(%rsp), %r15
.LBB0_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	testq	%r12, %r12
	je	.LBB0_5
# %bb.4:                                #   in Loop: Header=BB0_3 Depth=1
.Ltmp2:
	movl	$.L.str.1, %esi
	movl	$1, %edx
	movq	%r15, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
.Ltmp3:
.LBB0_5:                                # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
                                        #   in Loop: Header=BB0_3 Depth=1
	movsd	(%r14,%r12,8), %xmm0            # xmm0 = mem[0],zero
.Ltmp4:
	movq	%r15, %rdi
	callq	_ZNSo9_M_insertIdEERSoT_
.Ltmp5:
# %bb.6:                                # %_ZNSolsEd.exit
                                        #   in Loop: Header=BB0_3 Depth=1
	incq	%r12
	cmpq	%r12, %rbx
	jne	.LBB0_3
.LBB0_7:                                # %._crit_edge
.Ltmp7:
	leaq	8(%rsp), %rdi
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEE5closeEv
.Ltmp8:
# %bb.8:
	movq	_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE(%rip), %rax
	leaq	16(%rsp), %rdi
	movq	%rax, -8(%rdi)
	movq	_ZTTSt14basic_ofstreamIcSt11char_traitsIcEE+24(%rip), %rcx
	movq	-24(%rax), %rax
	movq	%rcx, 8(%rsp,%rax)
	callq	_ZNSt13basic_filebufIcSt11char_traitsIcEED2Ev
	leaq	256(%rsp), %rdi
	callq	_ZNSt8ios_baseD2Ev
	addq	$520, %rsp                      # imm = 0x208
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB0_9:
	.cfi_def_cfa_offset 560
.Ltmp9:
	jmp	.LBB0_10
.LBB0_11:
.Ltmp6:
.LBB0_10:
	movq	%rax, %rbx
	leaq	8(%rsp), %rdi
	callq	_ZNSt14basic_ofstreamIcSt11char_traitsIcEED1Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end0:
	.size	_ZN20PointSourcePollution3endEPKdm, .Lfunc_end0-_ZN20PointSourcePollution3endEPKdm
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table0:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Lfunc_begin0-.Lfunc_begin0    # >> Call Site 1 <<
	.uleb128 .Ltmp0-.Lfunc_begin0           #   Call between .Lfunc_begin0 and .Ltmp0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp1-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp1
	.uleb128 .Ltmp9-.Lfunc_begin0           #     jumps to .Ltmp9
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp2-.Lfunc_begin0           # >> Call Site 3 <<
	.uleb128 .Ltmp5-.Ltmp2                  #   Call between .Ltmp2 and .Ltmp5
	.uleb128 .Ltmp6-.Lfunc_begin0           #     jumps to .Ltmp6
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp7-.Lfunc_begin0           # >> Call Site 4 <<
	.uleb128 .Ltmp8-.Ltmp7                  #   Call between .Ltmp7 and .Ltmp8
	.uleb128 .Ltmp9-.Lfunc_begin0           #     jumps to .Ltmp9
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp8-.Lfunc_begin0           # >> Call Site 5 <<
	.uleb128 .Lfunc_end0-.Ltmp8             #   Call between .Ltmp8 and .Lfunc_end0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.text
	.globl	_Z22__device_stub__diffusePdS_S_mmm # -- Begin function _Z22__device_stub__diffusePdS_S_mmm
	.type	_Z22__device_stub__diffusePdS_S_mmm,@function
_Z22__device_stub__diffusePdS_S_mmm:    # @_Z22__device_stub__diffusePdS_S_mmm
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$144, %rsp
	.cfi_def_cfa_offset 192
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	24(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	16(%rsp), %r8
	movq	%r9, (%r8)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%rsp, %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z7diffusePdS_S_mmm, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z22__device_stub__diffusePdS_S_mmm, .Lfunc_end1-_Z22__device_stub__diffusePdS_S_mmm
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0                          # -- Begin function main
.LCPI2_0:
	.long	1127219200                      # 0x43300000
	.long	1160773632                      # 0x45300000
	.long	0                               # 0x0
	.long	0                               # 0x0
.LCPI2_1:
	.quad	0x4330000000000000              # double 4503599627370496
	.quad	0x4530000000000000              # double 1.9342813113834067E+25
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI2_2:
	.quad	0x3f50000000000000              # double 9.765625E-4
.LCPI2_3:
	.quad	0x43e0000000000000              # double 9.2233720368547758E+18
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$5, %edi
	jge	.LBB2_1
# %bb.2:
	movl	$_ZSt4cerr, %ebx
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.2, %esi
	movl	$80, %edx
	jmp	.LBB2_3
.LBB2_1:                                # %.preheader.preheader
	movq	%rsi, %r15
	movl	%edi, %ebx
	xorl	%r14d, %r14d
.LBB2_5:                                # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r15,%r14,8), %rdi
	callq	atoi
	testl	%eax, %eax
	js	.LBB2_6
# %bb.4:                                #   in Loop: Header=BB2_5 Depth=1
	incq	%r14
	cmpq	%r14, %rbx
	jne	.LBB2_5
# %bb.7:
	movq	8(%r15), %rdi
	callq	atoi
	movl	%eax, %r12d
	movslq	%eax, %rbx
	movq	16(%r15), %rdi
	callq	atoi
	movl	%eax, 28(%rsp)                  # 4-byte Spill
	movq	24(%r15), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	32(%r15), %rdi
	callq	atoi
	movslq	%eax, %r15
	leaq	(,%rbx,8), %r13
	leaq	8(%rsp), %rdi
	movq	%r13, %rsi
	movl	$1, %edx
	callq	hipMallocManaged
	leaq	16(%rsp), %rdi
	movq	%r13, %rsi
	movl	$1, %edx
	callq	hipMallocManaged
	leaq	32(%rsp), %rdi
	movq	%r13, %rsi
	movl	$1, %edx
	callq	hipMallocManaged
	testl	%ebx, %ebx
	je	.LBB2_12
# %bb.8:                                # %.lr.ph
	movq	8(%rsp), %rax
	movq	%r15, %xmm1
	punpckldq	.LCPI2_0(%rip), %xmm1   # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
	movq	16(%rsp), %rcx
	subpd	.LCPI2_1(%rip), %xmm1
	movapd	%xmm1, %xmm0
	unpckhpd	%xmm1, %xmm0                    # xmm0 = xmm0[1],xmm1[1]
	addsd	%xmm1, %xmm0
	movl	%r12d, %edx
	xorl	%esi, %esi
.LBB2_9:                                # =>This Inner Loop Header: Depth=1
	movapd	%xmm0, %xmm1
	testq	%rsi, %rsi
	je	.LBB2_11
# %bb.10:                               #   in Loop: Header=BB2_9 Depth=1
	xorpd	%xmm1, %xmm1
.LBB2_11:                               #   in Loop: Header=BB2_9 Depth=1
	movsd	%xmm1, (%rax,%rsi,8)
	movsd	%xmm1, (%rcx,%rsi,8)
	incq	%rsi
	cmpq	%rsi, %rdx
	jne	.LBB2_9
.LBB2_12:                               # %._crit_edge
	movq	8(%rsp), %rax
	movsd	(%rax), %xmm0                   # xmm0 = mem[0],zero
	movl	$_ZSt4cout, %edi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	16(%rsp), %rcx
	movsd	(%rcx), %xmm0                   # xmm0 = mem[0],zero
	movq	%rax, %rdi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %r12
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r12, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r12, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB2_20
# %bb.13:
	movl	%eax, %ebp
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.4, %esi
	movl	$7, %edx
	jmp	.LBB2_14
.LBB2_6:                                # %.thread
	movl	$_ZSt4cerr, %ebx
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.3, %esi
	movl	$33, %edx
.LBB2_3:
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cerr(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	jmp	.LBB2_19
.LBB2_20:
	testl	%r14d, %r14d
	je	.LBB2_25
# %bb.21:                               # %.lr.ph70
	movslq	%r14d, %r12
	movq	%rbx, %xmm1
	punpckldq	.LCPI2_0(%rip), %xmm1   # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
	movabsq	$4294967296, %r13               # imm = 0x100000000
	subpd	.LCPI2_1(%rip), %xmm1
	movapd	%xmm1, %xmm0
	unpckhpd	%xmm1, %xmm0                    # xmm0 = xmm0[1],xmm1[1]
	addsd	%xmm1, %xmm0
	mulsd	.LCPI2_2(%rip), %xmm0
	callq	ceil@PLT
	cvttsd2si	%xmm0, %rbp
	movq	%rbp, %rax
	sarq	$63, %rax
	subsd	.LCPI2_3(%rip), %xmm0
	cvttsd2si	%xmm0, %rcx
	andl	%eax, %ecx
	orl	%ecx, %ebp
	btsq	$32, %rbp
	addq	$1024, %r13                     # imm = 0x400
.LBB2_22:                               # =>This Inner Loop Header: Depth=1
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB2_24
# %bb.23:                               #   in Loop: Header=BB2_22 Depth=1
	movq	8(%rsp), %rdi
	movq	16(%rsp), %rsi
	movq	32(%rsp), %rdx
	movq	%rbx, %rcx
	movq	%r12, %r8
	movq	%r15, %r9
	callq	_Z22__device_stub__diffusePdS_S_mmm
.LBB2_24:                               #   in Loop: Header=BB2_22 Depth=1
	decl	%r14d
	jne	.LBB2_22
.LBB2_25:                               # %._crit_edge71
	callq	hipDeviceSynchronize
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB2_27
# %bb.26:
	movl	%eax, %ebp
	movl	$_ZSt4cerr, %edi
	movl	$.L.str.5, %esi
	movl	$8, %edx
.LBB2_14:
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	%ebp, %edi
.LBB2_15:
	callq	hipGetErrorString
	testq	%rax, %rax
	je	.LBB2_16
# %bb.17:
	movq	%rax, %rbx
	movq	%rax, %rdi
	callq	strlen
	movl	$_ZSt4cerr, %edi
	movq	%rbx, %rsi
	movq	%rax, %rdx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	jmp	.LBB2_18
.LBB2_16:
	movq	_ZSt4cerr(%rip), %rax
	movq	-24(%rax), %rax
	leaq	_ZSt4cerr(%rax), %rdi
	movl	32(%rdi), %esi
	orl	$1, %esi
	callq	_ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate
.LBB2_18:                               # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
	movq	_ZSt4cerr(%rip), %rax
	movl	$_ZSt4cerr, %ecx
	movq	-24(%rax), %rdi
	addq	%rcx, %rdi
.LBB2_19:
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cerr, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$1, %eax
.LBB2_30:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB2_27:
	.cfi_def_cfa_offset 96
	movslq	28(%rsp), %r14                  # 4-byte Folded Reload
	movl	$_ZSt4cout, %r12d
	movl	$_ZSt4cout, %edi
	movl	$.L.str.6, %esi
	movl	$26, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movl	$_ZSt4cout, %edi
	movq	%r14, %rsi
	callq	_ZNSo9_M_insertImEERSoT_
	movq	%rax, %r15
	movl	$.L.str.7, %esi
	movl	$4, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	8(%rsp), %rax
	movsd	(%rax,%r14,8), %xmm0            # xmm0 = mem[0],zero
	movq	%r15, %rdi
	callq	_ZNSo9_M_insertIdEERSoT_
	movq	%rax, %r14
	movq	(%rax), %rax
	movq	-24(%rax), %rdi
	addq	%r14, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%r14, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	$_ZSt4cout, %edi
	movl	$.L.str.8, %esi
	movl	$26, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r12, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	8(%rsp), %rsi
	movq	%rbx, %rdx
	callq	_ZN20PointSourcePollution3endEPKdm
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	callq	hipGetLastError
	testl	%eax, %eax
	je	.LBB2_29
# %bb.28:
	movl	%eax, %edi
	jmp	.LBB2_15
.LBB2_29:
	movl	$.L.str.9, %edi
	callq	system
	xorl	%eax, %eax
	jmp	.LBB2_30
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB3_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB3_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z7diffusePdS_S_mmm, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end3:
	.size	__hip_module_ctor, .Lfunc_end3-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB4_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB4_2:
	retq
.Lfunc_end4:
	.size	__hip_module_dtor, .Lfunc_end4-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"output.txt"
	.size	.L.str, 11

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	" "
	.size	.L.str.1, 2

	.type	_Z7diffusePdS_S_mmm,@object     # @_Z7diffusePdS_S_mmm
	.section	.rodata,"a",@progbits
	.globl	_Z7diffusePdS_S_mmm
	.p2align	3, 0x0
_Z7diffusePdS_S_mmm:
	.quad	_Z22__device_stub__diffusePdS_S_mmm
	.size	_Z7diffusePdS_S_mmm, 8

	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"usage: psp cylinder_size slice_location diffusion_time contaminant_concentration"
	.size	.L.str.2, 81

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"All inputs must be greater than 0"
	.size	.L.str.3, 34

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Error: "
	.size	.L.str.4, 8

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Error2: "
	.size	.L.str.5, 9

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Answer at slice location: "
	.size	.L.str.6, 27

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	" is "
	.size	.L.str.7, 5

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Now visualizing results..."
	.size	.L.str.8, 27

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"python plot.py"
	.size	.L.str.9, 15

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z7diffusePdS_S_mmm"
	.size	.L__unnamed_1, 20

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym _Z22__device_stub__diffusePdS_S_mmm
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _Z7diffusePdS_S_mmm
	.addrsig_sym _ZSt4cerr
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7diffusePdS_S_mmm     ; -- Begin function _Z7diffusePdS_S_mmm
	.globl	_Z7diffusePdS_S_mmm
	.p2align	8
	.type	_Z7diffusePdS_S_mmm,@function
_Z7diffusePdS_S_mmm:                    ; @_Z7diffusePdS_S_mmm
; %bb.0:
	s_load_b64 s[2:3], s[0:1], 0x18
	v_lshl_or_b32 v0, s15, 10, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v1, 31, v0
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_u64_e32 vcc_lo, s[2:3], v[0:1]
	s_and_saveexec_b32 s2, vcc_lo
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b64 s[0:1], s[0:1], 0x0
	v_dual_mov_b32 v3, 0 :: v_dual_mov_b32 v2, v0
	v_lshlrev_b64 v[4:5], 3, v[0:1]
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[2:3], 3, v[2:3]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v6, vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	v_add_co_u32 v1, vcc_lo, s0, v4
	v_add_co_ci_u32_e32 v2, vcc_lo, s1, v5, vcc_lo
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v5, vcc_lo, v6, -8
	v_add_co_ci_u32_e32 v3, vcc_lo, -1, v3, vcc_lo
	v_cmp_lt_i32_e32 vcc_lo, 0, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_dual_cndmask_b32 v4, v2, v3 :: v_dual_cndmask_b32 v3, v1, v5
	global_load_b64 v[3:4], v[3:4], off
	global_load_b64 v[5:6], v[1:2], off offset:8
	s_waitcnt vmcnt(0)
	v_add_f64 v[3:4], v[3:4], v[5:6]
	v_mul_f64 v[3:4], v[3:4], 0.5
	global_store_b64 v[1:2], v[3:4], off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7diffusePdS_S_mmm
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 48
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 7
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z7diffusePdS_S_mmm, .Lfunc_end0-_Z7diffusePdS_S_mmm
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 196
; NumSgprs: 18
; NumVgprs: 7
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 7
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .offset:         24
        .size:           8
        .value_kind:     by_value
      - .offset:         32
        .size:           8
        .value_kind:     by_value
      - .offset:         40
        .size:           8
        .value_kind:     by_value
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 48
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7diffusePdS_S_mmm
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z7diffusePdS_S_mmm.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     7
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
