#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <math.h>


#define DIV_ROUND_UP(n, d)  (((n) + (d) - 1) / (d))
#define cuda_check(ret) _cuda_check((ret), __FILE__, __LINE__)
inline void _cuda_check(cudaError_t ret, const char *file, int line)
{
  if (ret != cudaSuccess) {
    fprintf(stderr, "CudaErr: %s (%s:%d)\n", cudaGetErrorString(ret), file, line);
    exit(1);
  }
}
void print_matrix(float* matrix, int order){
  for (int row=0; row<order; row++)
  {
      for(int columns=0; columns<order; columns++)
          {
           printf("%f ", matrix[row*order + columns]);
          }
      printf("\n");
   }
}
float Gaussian(int x, int y, float sigma){
  return (1.0 / (2.0 * M_PI * sigma * sigma)) * exp(-((x*x+y*y)/(2*sigma*sigma)));
}

__global__ void calculation_kernel(float *Gaussian_matrix, unsigned char* image_buffer, unsigned char*output_matrix, int order, int width, int height)
{
  int row = blockIdx.y * blockDim.y + threadIdx.y;
  int col = blockIdx.x * blockDim.x + threadIdx.x;
      float sum = 0;
      //traversing the Gaussian_matrix
      for(unsigned int k = 0; k < order; k++)
      {
        for(unsigned int z = 0; z < order; z++)
        {
          int locX = (col - (floor((float)order / 2))) + z;
          int locY = (row - (floor((float)order / 2))) + k;
          // Getting nearest pixel
          if(locX < 0)
          {
            locX = 0;
          }
          else if(locX >= width)
          {
            locX = width-1;
          }

          if(locY < 0)
          {
            locY = 0;
          }
          else if(locY >= height)
          {
            locY = height-1;
          }
          float temp =  Gaussian_matrix[k*order+z] *image_buffer[(locY * width  + locX)];
          sum += temp;
        }
      }
  output_matrix[row*width+col] = (unsigned char)sum;
}

// Send every Pixel to GPU for computation
void calculation(float *Gaussian_matrix_h, unsigned char* image_buffer_h, unsigned char * output_matrix_h, int order, int width, int height)
{
  float * Gaussian_matrix_d;
  unsigned char *image_buffer_d;
  unsigned char *output_matrix_d;
  int kernel_size = order * order * sizeof(float);
  int image_size = width * height * sizeof(unsigned char);

  cuda_check(cudaMalloc(&Gaussian_matrix_d, kernel_size));
  cuda_check(cudaMalloc(&image_buffer_d, image_size));
  cuda_check(cudaMalloc(&output_matrix_d, image_size));
  cuda_check(cudaMemcpy(Gaussian_matrix_d, Gaussian_matrix_h, kernel_size, cudaMemcpyHostToDevice));
  cuda_check(cudaMemcpy(image_buffer_d, image_buffer_h, image_size, cudaMemcpyHostToDevice));
  dim3 block_dim(32,32);
  dim3 grid_dim(DIV_ROUND_UP(width, block_dim.x), DIV_ROUND_UP(height, block_dim.y));
  calculation_kernel<<<grid_dim, block_dim>>>(Gaussian_matrix_d, image_buffer_d, output_matrix_d, order, width, height);
  cuda_check(cudaPeekAtLastError());      /* Catch configuration errors */
  cuda_check(cudaDeviceSynchronize());    /* Catch execution errors */
  cuda_check(cudaMemcpy(output_matrix_h, output_matrix_d, image_size, cudaMemcpyDeviceToHost));
  cuda_check(cudaFree(Gaussian_matrix_d));
  cuda_check(cudaFree(image_buffer_d));
  cuda_check(cudaFree(output_matrix_d));
}

int main(int argc, char *argv[]){
  struct timespec start, end;
  if(argc != 4) {
    fprintf(stderr, "Usage: ./gaussian_blur_cuda <input_file> <output_file> <sigma>");
    return 1;
  }
  float sigma = atof(argv[3]);
  FILE *fp;
  fp = fopen(argv[1], "rb");
  char buffer[4];
  unsigned  int width;
  unsigned int height;
  int cutoff;

  if (!fp) {
    fprintf(stderr, "Error: cannot open file %s", argv[1]);
    return 1;
  }
  if(fscanf(fp,"%s\n",buffer) != 1){
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if(strcmp(buffer,"P5") != 0){
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if( fscanf(fp, "%d %d\n", &width, &height) != 2){         //taking in two values
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if(width < 256 || height < 256){
    fprintf(stderr, "Error: invalid PGM pixels");
    return 1;
  }
  if( fscanf(fp, "%d\n", &cutoff)!= 1){
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if(cutoff != 255){
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if(sigma == 0){
    fprintf(stderr, "Error: invalid sigma value");
    return 1;
  }
  unsigned  int order = ceil(6*sigma);
  if(order % 2 == 0){
    order++;
  }
  if(order > height ||order  > width){
     fprintf(stderr, "Error: sigma value too big for image size");
     return 1;
  }

  float *Gaussian_matrix;
  unsigned char *image_buffer;
  unsigned char *output_matrix;
  Gaussian_matrix = (float*)malloc(order * order * sizeof(float));
  image_buffer = (unsigned char*)malloc(width * height * sizeof(unsigned char));
  output_matrix = (unsigned char*)malloc(width * height * sizeof(unsigned char));
  for(unsigned int x = 0, i = 0; x <= 2*floor(order/2) && i < order; x++, i++){
    for(unsigned int y = 0, j = 0; y <= 2*floor(order/2) && j < order; y++, j++){
      Gaussian_matrix[i*order+j] = Gaussian(x-floor(order/2),y-floor(order/2),sigma);
    }
  }
  if(fread(image_buffer, sizeof(unsigned char), height * width, fp) != width*height){
    fprintf(stderr, "Error: invalid PGM pixels");
    return 1;
  }
  clock_gettime(CLOCK_MONOTONIC, &start);
  calculation(Gaussian_matrix, image_buffer, output_matrix, order, width, height);
  clock_gettime(CLOCK_MONOTONIC, &end);
  double diff = end.tv_sec - start.tv_sec + (end.tv_nsec - start.tv_nsec)/1000000000.0;
  printf("Running time: %f secs\n", diff);
  FILE* pgmimg;
  char filename[40];
  strcpy(filename, argv[2]);
  pgmimg = fopen( filename , "wb" );
  if(pgmimg == NULL){
    fprintf(stderr, "cannot open file to write");
    return 1;
  }
  fprintf(pgmimg, "P5\n");
  fprintf(pgmimg, "%d %d\n", width, height);
  fprintf(pgmimg, "%d\n", cutoff);
  fwrite(output_matrix, sizeof(unsigned char), height * width, pgmimg);
  fclose(pgmimg);
  fclose(fp);
  return 0;
}

	.file	"tmpxft_0034a4be_00000000-6_gaussian_blur_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata._Z11_cuda_check9cudaErrorPKci.str1.1,"aMS",@progbits,1
.LC0:
	.string	"CudaErr: %s (%s:%d)\n"
	.section	.text._Z11_cuda_check9cudaErrorPKci,"axG",@progbits,_Z11_cuda_check9cudaErrorPKci,comdat
	.weak	_Z11_cuda_check9cudaErrorPKci
	.type	_Z11_cuda_check9cudaErrorPKci, @function
_Z11_cuda_check9cudaErrorPKci:
.LFB2027:
	.cfi_startproc
	endbr64
	testl	%edi, %edi
	je	.L2
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	%edx, 12(%rsp)
	movq	%rsi, (%rsp)
	call	cudaGetErrorString@PLT
	movq	stderr(%rip), %rdi
	movq	(%rsp), %r8
	leaq	.LC0(%rip), %rdx
	movl	12(%rsp), %r9d
	movq	%rax, %rcx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L2:
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z11_cuda_check9cudaErrorPKci, .-_Z11_cuda_check9cudaErrorPKci
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%f "
.LC2:
	.string	"\n"
	.text
	.globl	_Z12print_matrixPfi
	.type	_Z12print_matrixPfi, @function
_Z12print_matrixPfi:
.LFB2028:
	.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
	leaq	.LC1(%rip), %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movslq	%esi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%r13, %rbp
	salq	$2, %r13
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	pushq	%rdx
	.cfi_def_cfa_offset 64
.L9:
	cmpl	%ebp, %r12d
	jge	.L14
	leaq	(%rbx,%r13), %r15
.L10:
	cvtss2sd	(%rbx), %xmm0
	movq	%r14, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	addq	$4, %rbx
	cmpq	%r15, %rbx
	jne	.L10
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	incl	%r12d
	call	__printf_chk@PLT
	jmp	.L9
.L14:
	popq	%rax
	.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
.LFE2028:
	.size	_Z12print_matrixPfi, .-_Z12print_matrixPfi
	.globl	_Z8Gaussianiif
	.type	_Z8Gaussianiif, @function
_Z8Gaussianiif:
.LFB2029:
	.cfi_startproc
	endbr64
	movaps	%xmm0, %xmm2
	imull	%esi, %esi
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	cvtss2sd	%xmm0, %xmm3
	imull	%edi, %edi
	addss	%xmm0, %xmm2
	movsd	%xmm3, 8(%rsp)
	mulss	%xmm0, %xmm2
	addl	%esi, %edi
	cvtsi2ssl	%edi, %xmm1
	divss	%xmm2, %xmm1
	movaps	%xmm1, %xmm0
	xorps	.LC3(%rip), %xmm0
	call	expf@PLT
	movsd	8(%rsp), %xmm3
	movsd	.LC4(%rip), %xmm2
	movaps	%xmm0, %xmm1
	movsd	.LC5(%rip), %xmm0
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	mulsd	%xmm3, %xmm2
	cvtss2sd	%xmm1, %xmm1
	mulsd	%xmm3, %xmm2
	divsd	%xmm2, %xmm0
	mulsd	%xmm1, %xmm0
	cvtsd2ss	%xmm0, %xmm0
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z8Gaussianiif, .-_Z8Gaussianiif
	.globl	_Z46__device_stub__Z18calculation_kernelPfPhS0_iiiPfPhS0_iii
	.type	_Z46__device_stub__Z18calculation_kernelPfPhS0_iiiPfPhS0_iii, @function
_Z46__device_stub__Z18calculation_kernelPfPhS0_iiiPfPhS0_iii:
.LFB2056:
	.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
	movl	%ecx, 20(%rsp)
	leaq	64(%rsp), %rcx
	movl	%r8d, 16(%rsp)
	movl	%r9d, 12(%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	20(%rsp), %rax
	movq	%rax, 144(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 152(%rsp)
	leaq	12(%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	.L17
	pushq	64(%rsp)
	.cfi_def_cfa_offset 200
	leaq	_Z18calculation_kernelPfPhS0_iii(%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
.L17:
	movq	168(%rsp), %rax
	subq	%fs:40, %rax
	je	.L19
	call	__stack_chk_fail@PLT
.L19:
	addq	$184, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z46__device_stub__Z18calculation_kernelPfPhS0_iiiPfPhS0_iii, .-_Z46__device_stub__Z18calculation_kernelPfPhS0_iiiPfPhS0_iii
	.globl	_Z18calculation_kernelPfPhS0_iii
	.type	_Z18calculation_kernelPfPhS0_iii, @function
_Z18calculation_kernelPfPhS0_iii:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z46__device_stub__Z18calculation_kernelPfPhS0_iiiPfPhS0_iii
	.cfi_endproc
.LFE2057:
	.size	_Z18calculation_kernelPfPhS0_iii, .-_Z18calculation_kernelPfPhS0_iii
	.section	.rodata.str1.1
.LC6:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/samanthatsoi/UC-Davis/master/ECS158/ECS158_p4/gaussian_blur_cuda.cu"
	.text
	.globl	_Z11calculationPfPhS0_iii
	.type	_Z11calculationPfPhS0_iii, @function
_Z11calculationPfPhS0_iii:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movl	%ecx, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%r9d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%r8d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	imull	%ecx, %ebp
	movl	%r8d, %ebx
	imull	%r9d, %ebx
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	sall	$2, %ebp
	movq	%rsi, (%rsp)
	movslq	%ebp, %rbp
	leaq	24(%rsp), %rdi
	movslq	%ebx, %rbx
	movq	%rdx, 8(%rsp)
	movq	%rbp, %rsi
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	cudaMalloc@PLT
	movl	$79, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	leaq	32(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movl	$80, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	leaq	40(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movl	$81, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	movq	24(%rsp), %rdi
	movq	%rbp, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$82, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	movq	(%rsp), %rsi
	movq	32(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	$83, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	leal	31(%r12), %eax
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	shrl	$5, %eax
	movl	$1, %ecx
	movl	$1, %esi
	movabsq	$137438953504, %rdx
	movl	%eax, 60(%rsp)
	leal	31(%r13), %eax
	shrl	$5, %eax
	movl	%eax, 64(%rsp)
	movq	60(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L23
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	movl	%r13d, %r9d
	movl	%r12d, %r8d
	movq	24(%rsp), %rdi
	movl	%r14d, %ecx
	call	_Z46__device_stub__Z18calculation_kernelPfPhS0_iiiPfPhS0_iii
.L23:
	call	cudaPeekAtLastError@PLT
	movl	$87, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	call	cudaDeviceSynchronize@PLT
	movl	$88, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	movq	40(%rsp), %rsi
	movq	8(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movl	$89, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movl	$90, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movl	$91, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movl	$92, %edx
	leaq	.LC6(%rip), %rsi
	movl	%eax, %edi
	call	_Z11_cuda_check9cudaErrorPKci
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L24
	call	__stack_chk_fail@PLT
.L24:
	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
.LFE2030:
	.size	_Z11calculationPfPhS0_iii, .-_Z11calculationPfPhS0_iii
	.section	.rodata.str1.1
.LC7:
	.string	"Usage: ./gaussian_blur_cuda <input_file> <output_file> <sigma>"
.LC8:
	.string	"rb"
.LC9:
	.string	"Error: cannot open file %s"
.LC10:
	.string	"%s\n"
.LC11:
	.string	"Error: invalid PGM information"
.LC12:
	.string	"P5"
.LC13:
	.string	"%d %d\n"
.LC14:
	.string	"Error: invalid PGM pixels"
.LC15:
	.string	"%d\n"
.LC17:
	.string	"Error: invalid sigma value"
.LC19:
	.string	"Error: sigma value too big for image size"
.LC21:
	.string	"Running time: %f secs\n"
.LC22:
	.string	"wb"
.LC23:
	.string	"cannot open file to write"
.LC24:
	.string	"P5\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.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	$200, %rsp
	.cfi_def_cfa_offset 256
	movq	%fs:40, %rax
	movq	%rax, 184(%rsp)
	xorl	%eax, %eax
	cmpl	$4, %edi
	je	.L27
	movq	stderr(%rip), %rdi
	leaq	.LC7(%rip), %rdx
	movl	$2, %esi
	jmp	.L69
.L27:
	movq	24(%rsi), %rdi
	movq	%rsi, %r12
	call	atof@PLT
	movq	8(%r12), %rdi
	leaq	.LC8(%rip), %rsi
	movq	%xmm0, %rbp
	call	fopen@PLT
	movq	%rax, 8(%rsp)
	testq	%rax, %rax
	jne	.L29
	movq	8(%r12), %rcx
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC9(%rip), %rdx
	call	__fprintf_chk@PLT
.L28:
	movl	$1, %r13d
	jmp	.L26
.L29:
	movq	8(%rsp), %rdi
	xorl	%eax, %eax
	leaq	140(%rsp), %r13
	leaq	.LC10(%rip), %rsi
	movq	%r13, %rdx
	call	__isoc23_fscanf@PLT
	decl	%eax
	jne	.L37
	movq	%r13, %rdi
	leaq	.LC12(%rip), %rsi
	call	strcmp@PLT
	movl	%eax, %r13d
	testl	%eax, %eax
	jne	.L37
	movq	8(%rsp), %rdi
	xorl	%eax, %eax
	leaq	96(%rsp), %rcx
	leaq	92(%rsp), %rdx
	leaq	.LC13(%rip), %rsi
	call	__isoc23_fscanf@PLT
	cmpl	$2, %eax
	jne	.L37
	cmpl	$255, 92(%rsp)
	jbe	.L68
	cmpl	$255, 96(%rsp)
	jbe	.L68
	movq	8(%rsp), %rdi
	xorl	%eax, %eax
	leaq	100(%rsp), %rdx
	leaq	.LC15(%rip), %rsi
	call	__isoc23_fscanf@PLT
	decl	%eax
	je	.L36
.L37:
	leaq	.LC11(%rip), %rdx
	jmp	.L67
.L36:
	cmpl	$255, 100(%rsp)
	jne	.L37
	movq	%rbp, %xmm5
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm5, %xmm4
	ucomiss	%xmm0, %xmm4
	movss	%xmm4, 20(%rsp)
	jp	.L38
	leaq	.LC17(%rip), %rdx
	je	.L67
.L38:
	movss	20(%rsp), %xmm0
	mulss	.LC18(%rip), %xmm0
	call	ceilf@PLT
	movl	96(%rsp), %ebp
	cvttss2siq	%xmm0, %r10
	movl	%r10d, %eax
	movl	%r10d, %ebx
	andl	$1, %eax
	cmpl	$1, %eax
	adcl	$0, %ebx
	cmpl	%ebx, %ebp
	jb	.L41
	movl	92(%rsp), %r15d
	cmpl	%ebx, %r15d
	jnb	.L42
.L41:
	leaq	.LC19(%rip), %rdx
	jmp	.L67
.L42:
	movl	%ebx, %edi
	imull	%r15d, %ebp
	xorl	%r15d, %r15d
	imull	%ebx, %edi
	salq	$2, %rdi
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, %r14
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, 24(%rsp)
	call	malloc@PLT
	xorl	%ecx, %ecx
	movq	%rax, 32(%rsp)
	movl	%ebx, %eax
	shrl	%eax
	cvtsi2sdl	%eax, %xmm1
	movaps	%xmm1, %xmm3
	addsd	%xmm1, %xmm3
.L43:
	movl	%r15d, %eax
	cvtsi2sdq	%rax, %xmm2
	cmpl	%ebx, %r15d
	jnb	.L56
	comisd	%xmm2, %xmm3
	jb	.L56
	xorl	%eax, %eax
.L47:
	movl	%eax, %esi
	cvtsi2sdq	%rsi, %xmm0
	cmpl	%ebx, %eax
	jnb	.L46
	comisd	%xmm0, %xmm3
	jb	.L46
	subsd	%xmm1, %xmm0
	leal	(%rax,%rcx), %esi
	movl	%ecx, 68(%rsp)
	leaq	(%r14,%rsi,4), %r8
	movl	%eax, 64(%rsp)
	movq	%r8, 56(%rsp)
	cvttsd2sil	%xmm0, %esi
	movaps	%xmm2, %xmm0
	movsd	%xmm3, 72(%rsp)
	subsd	%xmm1, %xmm0
	movsd	%xmm2, 48(%rsp)
	movsd	%xmm1, 40(%rsp)
	cvttsd2sil	%xmm0, %edi
	movss	20(%rsp), %xmm0
	call	_Z8Gaussianiif
	movq	56(%rsp), %r8
	movl	64(%rsp), %eax
	movsd	40(%rsp), %xmm1
	movsd	48(%rsp), %xmm2
	movl	68(%rsp), %ecx
	movsd	72(%rsp), %xmm3
	movss	%xmm0, (%r8)
	incl	%eax
	jmp	.L47
.L46:
	incl	%r15d
	addl	%ebx, %ecx
	jmp	.L43
.L56:
	movq	8(%rsp), %r8
	movq	24(%rsp), %rdi
	movq	%rbp, %rcx
	movq	%rbp, %rsi
	movl	$1, %edx
	call	__fread_chk@PLT
	movq	%rax, %rdx
	movl	92(%rsp), %eax
	imull	96(%rsp), %eax
	cmpq	%rdx, %rax
	je	.L51
.L68:
	leaq	.LC14(%rip), %rdx
.L67:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
.L69:
	call	__fprintf_chk@PLT
	jmp	.L28
.L51:
	leaq	104(%rsp), %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movl	96(%rsp), %r9d
	movl	%ebx, %ecx
	movq	%r14, %rdi
	movl	92(%rsp), %r8d
	movq	32(%rsp), %rdx
	movq	24(%rsp), %rsi
	call	_Z11calculationPfPhS0_iii
	leaq	120(%rsp), %rsi
	movl	$1, %edi
	call	clock_gettime@PLT
	movq	128(%rsp), %rax
	subq	112(%rsp), %rax
	leaq	.LC21(%rip), %rsi
	cvtsi2sdq	%rax, %xmm0
	movq	120(%rsp), %rax
	subq	104(%rsp), %rax
	divsd	.LC20(%rip), %xmm0
	cvtsi2sdq	%rax, %xmm1
	movl	$2, %edi
	movb	$1, %al
	addsd	%xmm1, %xmm0
	call	__printf_chk@PLT
	movq	16(%r12), %rsi
	movl	$40, %edx
	leaq	144(%rsp), %rdi
	call	__strcpy_chk@PLT
	leaq	.LC22(%rip), %rsi
	movq	%rax, %rdi
	call	fopen@PLT
	leaq	.LC23(%rip), %rdx
	movq	%rax, %rbp
	testq	%rax, %rax
	je	.L67
	movq	%rax, %rdi
	leaq	.LC24(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	96(%rsp), %r8d
	movl	92(%rsp), %ecx
	xorl	%eax, %eax
	leaq	.LC13(%rip), %rdx
	movl	$2, %esi
	movq	%rbp, %rdi
	call	__fprintf_chk@PLT
	movl	100(%rsp), %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC15(%rip), %rdx
	movq	%rbp, %rdi
	call	__fprintf_chk@PLT
	movl	96(%rsp), %edx
	imull	92(%rsp), %edx
	movq	%rbp, %rcx
	movq	32(%rsp), %rdi
	movl	$1, %esi
	call	fwrite@PLT
	movq	%rbp, %rdi
	call	fclose@PLT
	movq	8(%rsp), %rdi
	call	fclose@PLT
.L26:
	movq	184(%rsp), %rax
	subq	%fs:40, %rax
	je	.L53
	call	__stack_chk_fail@PLT
.L53:
	addq	$200, %rsp
	.cfi_def_cfa_offset 56
	movl	%r13d, %eax
	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
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC25:
	.string	"_Z18calculation_kernelPfPhS0_iii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.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	.LC25(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z18calculation_kernelPfPhS0_iii(%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
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.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.cst16,"aM",@progbits,16
	.align 16
.LC3:
	.long	-2147483648
	.long	0
	.long	0
	.long	0
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC4:
	.long	1413754136
	.long	1075388923
	.align 8
.LC5:
	.long	0
	.long	1072693248
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC18:
	.long	1086324736
	.section	.rodata.cst8
	.align 8
.LC20:
	.long	0
	.long	1104006501
	.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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <math.h>


#define DIV_ROUND_UP(n, d)  (((n) + (d) - 1) / (d))
#define cuda_check(ret) _cuda_check((ret), __FILE__, __LINE__)
inline void _cuda_check(hipError_t ret, const char *file, int line)
{
  if (ret != hipSuccess) {
    fprintf(stderr, "CudaErr: %s (%s:%d)\n", hipGetErrorString(ret), file, line);
    exit(1);
  }
}
void print_matrix(float* matrix, int order){
  for (int row=0; row<order; row++)
  {
      for(int columns=0; columns<order; columns++)
          {
           printf("%f ", matrix[row*order + columns]);
          }
      printf("\n");
   }
}
float Gaussian(int x, int y, float sigma){
  return (1.0 / (2.0 * M_PI * sigma * sigma)) * exp(-((x*x+y*y)/(2*sigma*sigma)));
}

__global__ void calculation_kernel(float *Gaussian_matrix, unsigned char* image_buffer, unsigned char*output_matrix, int order, int width, int height)
{
  int row = blockIdx.y * blockDim.y + threadIdx.y;
  int col = blockIdx.x * blockDim.x + threadIdx.x;
      float sum = 0;
      //traversing the Gaussian_matrix
      for(unsigned int k = 0; k < order; k++)
      {
        for(unsigned int z = 0; z < order; z++)
        {
          int locX = (col - (floor((float)order / 2))) + z;
          int locY = (row - (floor((float)order / 2))) + k;
          // Getting nearest pixel
          if(locX < 0)
          {
            locX = 0;
          }
          else if(locX >= width)
          {
            locX = width-1;
          }

          if(locY < 0)
          {
            locY = 0;
          }
          else if(locY >= height)
          {
            locY = height-1;
          }
          float temp =  Gaussian_matrix[k*order+z] *image_buffer[(locY * width  + locX)];
          sum += temp;
        }
      }
  output_matrix[row*width+col] = (unsigned char)sum;
}

// Send every Pixel to GPU for computation
void calculation(float *Gaussian_matrix_h, unsigned char* image_buffer_h, unsigned char * output_matrix_h, int order, int width, int height)
{
  float * Gaussian_matrix_d;
  unsigned char *image_buffer_d;
  unsigned char *output_matrix_d;
  int kernel_size = order * order * sizeof(float);
  int image_size = width * height * sizeof(unsigned char);

  cuda_check(hipMalloc(&Gaussian_matrix_d, kernel_size));
  cuda_check(hipMalloc(&image_buffer_d, image_size));
  cuda_check(hipMalloc(&output_matrix_d, image_size));
  cuda_check(hipMemcpy(Gaussian_matrix_d, Gaussian_matrix_h, kernel_size, hipMemcpyHostToDevice));
  cuda_check(hipMemcpy(image_buffer_d, image_buffer_h, image_size, hipMemcpyHostToDevice));
  dim3 block_dim(32,32);
  dim3 grid_dim(DIV_ROUND_UP(width, block_dim.x), DIV_ROUND_UP(height, block_dim.y));
  calculation_kernel<<<grid_dim, block_dim>>>(Gaussian_matrix_d, image_buffer_d, output_matrix_d, order, width, height);
  cuda_check(hipPeekAtLastError());      /* Catch configuration errors */
  cuda_check(hipDeviceSynchronize());    /* Catch execution errors */
  cuda_check(hipMemcpy(output_matrix_h, output_matrix_d, image_size, hipMemcpyDeviceToHost));
  cuda_check(hipFree(Gaussian_matrix_d));
  cuda_check(hipFree(image_buffer_d));
  cuda_check(hipFree(output_matrix_d));
}

int main(int argc, char *argv[]){
  struct timespec start, end;
  if(argc != 4) {
    fprintf(stderr, "Usage: ./gaussian_blur_cuda <input_file> <output_file> <sigma>");
    return 1;
  }
  float sigma = atof(argv[3]);
  FILE *fp;
  fp = fopen(argv[1], "rb");
  char buffer[4];
  unsigned  int width;
  unsigned int height;
  int cutoff;

  if (!fp) {
    fprintf(stderr, "Error: cannot open file %s", argv[1]);
    return 1;
  }
  if(fscanf(fp,"%s\n",buffer) != 1){
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if(strcmp(buffer,"P5") != 0){
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if( fscanf(fp, "%d %d\n", &width, &height) != 2){         //taking in two values
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if(width < 256 || height < 256){
    fprintf(stderr, "Error: invalid PGM pixels");
    return 1;
  }
  if( fscanf(fp, "%d\n", &cutoff)!= 1){
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if(cutoff != 255){
    fprintf(stderr, "Error: invalid PGM information");
    return 1;
  }
  if(sigma == 0){
    fprintf(stderr, "Error: invalid sigma value");
    return 1;
  }
  unsigned  int order = ceil(6*sigma);
  if(order % 2 == 0){
    order++;
  }
  if(order > height ||order  > width){
     fprintf(stderr, "Error: sigma value too big for image size");
     return 1;
  }

  float *Gaussian_matrix;
  unsigned char *image_buffer;
  unsigned char *output_matrix;
  Gaussian_matrix = (float*)malloc(order * order * sizeof(float));
  image_buffer = (unsigned char*)malloc(width * height * sizeof(unsigned char));
  output_matrix = (unsigned char*)malloc(width * height * sizeof(unsigned char));
  for(unsigned int x = 0, i = 0; x <= 2*floor(order/2) && i < order; x++, i++){
    for(unsigned int y = 0, j = 0; y <= 2*floor(order/2) && j < order; y++, j++){
      Gaussian_matrix[i*order+j] = Gaussian(x-floor(order/2),y-floor(order/2),sigma);
    }
  }
  if(fread(image_buffer, sizeof(unsigned char), height * width, fp) != width*height){
    fprintf(stderr, "Error: invalid PGM pixels");
    return 1;
  }
  clock_gettime(CLOCK_MONOTONIC, &start);
  calculation(Gaussian_matrix, image_buffer, output_matrix, order, width, height);
  clock_gettime(CLOCK_MONOTONIC, &end);
  double diff = end.tv_sec - start.tv_sec + (end.tv_nsec - start.tv_nsec)/1000000000.0;
  printf("Running time: %f secs\n", diff);
  FILE* pgmimg;
  char filename[40];
  strcpy(filename, argv[2]);
  pgmimg = fopen( filename , "wb" );
  if(pgmimg == NULL){
    fprintf(stderr, "cannot open file to write");
    return 1;
  }
  fprintf(pgmimg, "P5\n");
  fprintf(pgmimg, "%d %d\n", width, height);
  fprintf(pgmimg, "%d\n", cutoff);
  fwrite(output_matrix, sizeof(unsigned char), height * width, pgmimg);
  fclose(pgmimg);
  fclose(fp);
  return 0;
}

	.text
	.file	"gaussian_blur_cuda.hip"
	.globl	_Z12print_matrixPfi             # -- Begin function _Z12print_matrixPfi
	.type	_Z12print_matrixPfi,@function
_Z12print_matrixPfi:                    # @_Z12print_matrixPfi
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, (%rsp)                    # 8-byte Spill
	testl	%esi, %esi
	jle	.LBB0_5
# %bb.1:                                # %.preheader.lr.ph
	movl	%esi, %ebx
	movl	%esi, %r15d
	xorl	%r12d, %r12d
	xorl	%r13d, %r13d
.LBB0_2:                                # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_3 Depth 2
	movl	%r12d, %eax
	movq	(%rsp), %rcx                    # 8-byte Reload
	leaq	(%rcx,%rax,4), %rbp
	xorl	%r14d, %r14d
.LBB0_3:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbp,%r14,4), %xmm0
	movl	$.L.str, %edi
	movb	$1, %al
	callq	printf
	incq	%r14
	cmpq	%r14, %r15
	jne	.LBB0_3
# %bb.4:                                # %._crit_edge
                                        #   in Loop: Header=BB0_2 Depth=1
	movl	$10, %edi
	callq	putchar@PLT
	incq	%r13
	addl	%ebx, %r12d
	cmpq	%r15, %r13
	jne	.LBB0_2
.LBB0_5:                                # %._crit_edge13
	addq	$8, %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
.Lfunc_end0:
	.size	_Z12print_matrixPfi, .Lfunc_end0-_Z12print_matrixPfi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z8Gaussianiif
.LCPI1_0:
	.quad	0x401921fb54442d18              # double 6.2831853071795862
.LCPI1_1:
	.quad	0x3ff0000000000000              # double 1
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI1_2:
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.text
	.globl	_Z8Gaussianiif
	.type	_Z8Gaussianiif,@function
_Z8Gaussianiif:                         # @_Z8Gaussianiif
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	cvtss2sd	%xmm0, %xmm1
	movsd	.LCPI1_0(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm1, %xmm2
	mulsd	%xmm1, %xmm2
	imull	%edi, %edi
	imull	%esi, %esi
	addl	%edi, %esi
	xorps	%xmm1, %xmm1
	cvtsi2ss	%esi, %xmm1
	movsd	.LCPI1_1(%rip), %xmm3           # xmm3 = mem[0],zero
	divsd	%xmm2, %xmm3
	movsd	%xmm3, (%rsp)                   # 8-byte Spill
	movaps	%xmm0, %xmm2
	addss	%xmm0, %xmm2
	mulss	%xmm0, %xmm2
	xorps	.LCPI1_2(%rip), %xmm1
	divss	%xmm2, %xmm1
	movaps	%xmm1, %xmm0
	callq	expf
	cvtss2sd	%xmm0, %xmm0
	mulsd	(%rsp), %xmm0                   # 8-byte Folded Reload
	cvtsd2ss	%xmm0, %xmm0
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z8Gaussianiif, .Lfunc_end1-_Z8Gaussianiif
	.cfi_endproc
                                        # -- End function
	.globl	_Z33__device_stub__calculation_kernelPfPhS0_iii # -- Begin function _Z33__device_stub__calculation_kernelPfPhS0_iii
	.type	_Z33__device_stub__calculation_kernelPfPhS0_iii,@function
_Z33__device_stub__calculation_kernelPfPhS0_iii: # @_Z33__device_stub__calculation_kernelPfPhS0_iii
	.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	20(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	16(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	12(%rsp), %r8
	movl	%r9d, (%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	32(%rsp), %r12
	leaq	24(%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	$_Z18calculation_kernelPfPhS0_iii, %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_end2:
	.size	_Z33__device_stub__calculation_kernelPfPhS0_iii, .Lfunc_end2-_Z33__device_stub__calculation_kernelPfPhS0_iii
	.cfi_endproc
                                        # -- End function
	.globl	_Z11calculationPfPhS0_iii       # -- Begin function _Z11calculationPfPhS0_iii
	.type	_Z11calculationPfPhS0_iii,@function
_Z11calculationPfPhS0_iii:              # @_Z11calculationPfPhS0_iii
	.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
	movl	%r9d, %r15d
	movl	%r8d, %r12d
	movq	%rdx, 32(%rsp)                  # 8-byte Spill
	movq	%rsi, %r13
	movq	%rdi, %rbx
	movl	%ecx, 28(%rsp)                  # 4-byte Spill
	movl	%ecx, %eax
	imull	%ecx, %eax
	shll	$2, %eax
	movslq	%eax, %rbp
	leaq	16(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_1
# %bb.3:                                # %_Z11_cuda_check10hipError_tPKci.exit
	movl	%r15d, %eax
	imull	%r12d, %eax
	movslq	%eax, %r14
	leaq	8(%rsp), %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_4
# %bb.5:                                # %_Z11_cuda_check10hipError_tPKci.exit24
	movq	%rsp, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB3_6
# %bb.7:                                # %_Z11_cuda_check10hipError_tPKci.exit26
	movq	16(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%rbp, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_8
# %bb.9:                                # %_Z11_cuda_check10hipError_tPKci.exit28
	movq	8(%rsp), %rdi
	movq	%r13, %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_10
# %bb.11:                               # %_Z11_cuda_check10hipError_tPKci.exit30
	leal	31(%r12), %eax
	shrl	$5, %eax
	leal	31(%r15), %edi
	shrl	$5, %edi
	shlq	$32, %rdi
	orq	%rax, %rdi
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_13
# %bb.12:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdx
	movl	28(%rsp), %ecx                  # 4-byte Reload
	movl	%r12d, %r8d
	movl	%r15d, %r9d
	callq	_Z33__device_stub__calculation_kernelPfPhS0_iii
.LBB3_13:
	callq	hipPeekAtLastError
	testl	%eax, %eax
	jne	.LBB3_14
# %bb.15:                               # %_Z11_cuda_check10hipError_tPKci.exit32
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB3_16
# %bb.17:                               # %_Z11_cuda_check10hipError_tPKci.exit34
	movq	(%rsp), %rsi
	movq	32(%rsp), %rdi                  # 8-byte Reload
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_18
# %bb.19:                               # %_Z11_cuda_check10hipError_tPKci.exit36
	movq	16(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB3_20
# %bb.21:                               # %_Z11_cuda_check10hipError_tPKci.exit38
	movq	8(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB3_22
# %bb.23:                               # %_Z11_cuda_check10hipError_tPKci.exit40
	movq	(%rsp), %rdi
	callq	hipFree
	testl	%eax, %eax
	jne	.LBB3_24
# %bb.25:                               # %_Z11_cuda_check10hipError_tPKci.exit42
	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
.LBB3_1:
	.cfi_def_cfa_offset 96
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$81, %r8d
	jmp	.LBB3_2
.LBB3_4:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$82, %r8d
	jmp	.LBB3_2
.LBB3_6:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$83, %r8d
	jmp	.LBB3_2
.LBB3_8:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$84, %r8d
	jmp	.LBB3_2
.LBB3_10:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$85, %r8d
	jmp	.LBB3_2
.LBB3_14:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$89, %r8d
	jmp	.LBB3_2
.LBB3_16:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$90, %r8d
	jmp	.LBB3_2
.LBB3_18:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$91, %r8d
	jmp	.LBB3_2
.LBB3_20:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$92, %r8d
	jmp	.LBB3_2
.LBB3_22:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$93, %r8d
	jmp	.LBB3_2
.LBB3_24:
	movq	stderr(%rip), %rbx
	movl	%eax, %edi
	callq	hipGetErrorString
	movl	$.L.str.18, %esi
	movl	$.L.str.2, %ecx
	movq	%rbx, %rdi
	movq	%rax, %rdx
	movl	$94, %r8d
.LBB3_2:
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z11calculationPfPhS0_iii, .Lfunc_end3-_Z11calculationPfPhS0_iii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI4_0:
	.long	0x40c00000                      # float 6
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI4_1:
	.quad	0x401921fb54442d18              # double 6.2831853071795862
.LCPI4_2:
	.quad	0x3ff0000000000000              # double 1
.LCPI4_4:
	.quad	0x41cdcd6500000000              # double 1.0E+9
	.section	.rodata.cst16,"aM",@progbits,16
	.p2align	4, 0x0
.LCPI4_3:
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.long	0x80000000                      # float -0
	.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	$168, %rsp
	.cfi_def_cfa_offset 224
	.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	$4, %edi
	jne	.LBB4_25
# %bb.1:
	movq	%rsi, %r14
	movq	24(%rsi), %rdi
	callq	atof
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
	movq	8(%r14), %rdi
	movl	$.L.str.4, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB4_26
# %bb.2:
	movq	%rax, %rbx
	leaq	28(%rsp), %rdx
	movl	$.L.str.6, %esi
	movq	%rax, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB4_23
# %bb.3:
	movzwl	28(%rsp), %eax
	xorl	$13648, %eax                    # imm = 0x3550
	movzbl	30(%rsp), %ecx
	orw	%ax, %cx
	jne	.LBB4_23
# %bb.4:
	leaq	12(%rsp), %rdx
	leaq	8(%rsp), %rcx
	movl	$.L.str.9, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$2, %eax
	jne	.LBB4_23
# %bb.5:
	cmpl	$256, 12(%rsp)                  # imm = 0x100
	jb	.LBB4_24
# %bb.6:
	cmpl	$255, 8(%rsp)
	jbe	.LBB4_24
# %bb.7:
	leaq	24(%rsp), %rdx
	movl	$.L.str.11, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB4_23
# %bb.8:
	cmpl	$255, 24(%rsp)
	jne	.LBB4_23
# %bb.9:
	cvtsd2ss	32(%rsp), %xmm1                 # 8-byte Folded Reload
	xorps	%xmm0, %xmm0
	ucomiss	%xmm0, %xmm1
	jne	.LBB4_10
	jnp	.LBB4_27
.LBB4_10:
	movq	%rbx, 48(%rsp)                  # 8-byte Spill
	movss	.LCPI4_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	%xmm1, 16(%rsp)                 # 4-byte Spill
	mulss	%xmm1, %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %rbx
	movl	%ebx, %r15d
	orl	$1, %r15d
	movl	8(%rsp), %ebp
	cmpl	%ebp, %r15d
	ja	.LBB4_28
# %bb.11:
	movl	12(%rsp), %r13d
	cmpl	%r13d, %r15d
	ja	.LBB4_28
# %bb.12:
	movq	%r14, 72(%rsp)                  # 8-byte Spill
	movl	%r15d, %edi
	imull	%edi, %edi
	shlq	$2, %rdi
	callq	malloc
	movq	%rax, %r12
	imull	%ebp, %r13d
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, 80(%rsp)                  # 8-byte Spill
	movq	%r13, %rdi
	callq	malloc
	movq	%rax, %r14
	shrl	%ebx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%ebx, %xmm0
	movsd	%xmm0, 32(%rsp)                 # 8-byte Spill
	movapd	%xmm0, %xmm1
	addsd	%xmm0, %xmm1
	xorpd	%xmm0, %xmm0
	movsd	%xmm1, 56(%rsp)                 # 8-byte Spill
	ucomisd	%xmm0, %xmm1
	jb	.LBB4_20
# %bb.13:                               # %.preheader.lr.ph
	movq	%r14, 64(%rsp)                  # 8-byte Spill
	movss	16(%rsp), %xmm3                 # 4-byte Reload
                                        # xmm3 = mem[0],zero,zero,zero
	xorps	%xmm1, %xmm1
	cvtss2sd	%xmm3, %xmm1
	movsd	.LCPI4_1(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm1, %xmm2
	mulsd	%xmm1, %xmm2
	movsd	.LCPI4_2(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm2, %xmm1
	movsd	%xmm1, 88(%rsp)                 # 8-byte Spill
	movaps	%xmm3, %xmm1
	addss	%xmm3, %xmm1
	mulss	%xmm3, %xmm1
	movss	%xmm1, 16(%rsp)                 # 4-byte Spill
	movl	%r15d, 20(%rsp)                 # 4-byte Spill
	movl	%r15d, %r15d
	xorl	%r13d, %r13d
	xorl	%r14d, %r14d
.LBB4_14:                               # %.lr.ph
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_15 Depth 2
	subsd	32(%rsp), %xmm0                 # 8-byte Folded Reload
	cvttsd2si	%xmm0, %ebx
	imull	%ebx, %ebx
	xorpd	%xmm0, %xmm0
	movl	$1, %ebp
	movl	%r13d, 44(%rsp)                 # 4-byte Spill
.LBB4_15:                               #   Parent Loop BB4_14 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	subsd	32(%rsp), %xmm0                 # 8-byte Folded Reload
	cvttsd2si	%xmm0, %eax
	imull	%eax, %eax
	addl	%ebx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%eax, %xmm0
	xorps	.LCPI4_3(%rip), %xmm0
	divss	16(%rsp), %xmm0                 # 4-byte Folded Reload
	callq	expf
	cvtss2sd	%xmm0, %xmm0
	mulsd	88(%rsp), %xmm0                 # 8-byte Folded Reload
	cvtsd2ss	%xmm0, %xmm0
	movl	%r13d, %eax
	movss	%xmm0, (%r12,%rax,4)
	movl	%ebp, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	movsd	56(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	jb	.LBB4_17
# %bb.16:                               #   in Loop: Header=BB4_15 Depth=2
	incl	%r13d
	leaq	1(%rbp), %rax
	cmpq	%r15, %rbp
	movq	%rax, %rbp
	jb	.LBB4_15
.LBB4_17:                               # %._crit_edge
                                        #   in Loop: Header=BB4_14 Depth=1
	incl	%r14d
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r14, %xmm0
	movsd	56(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	ucomisd	%xmm0, %xmm1
	movl	44(%rsp), %r13d                 # 4-byte Reload
	jb	.LBB4_19
# %bb.18:                               # %._crit_edge
                                        #   in Loop: Header=BB4_14 Depth=1
	movl	20(%rsp), %eax                  # 4-byte Reload
	addl	%eax, %r13d
	cmpl	%eax, %r14d
	jb	.LBB4_14
.LBB4_19:                               # %._crit_edge77.loopexit
	movl	12(%rsp), %r13d
	imull	8(%rsp), %r13d
	movq	64(%rsp), %r14                  # 8-byte Reload
	movl	20(%rsp), %r15d                 # 4-byte Reload
.LBB4_20:                               # %._crit_edge77
	movl	$1, %esi
	movq	80(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rdi
	movq	%r13, %rdx
	movq	48(%rsp), %rcx                  # 8-byte Reload
	callq	fread
	movl	8(%rsp), %ecx
	imull	12(%rsp), %ecx
	cmpq	%rcx, %rax
	jne	.LBB4_24
# %bb.21:
	movl	$1, %r13d
	leaq	112(%rsp), %rbp
	movl	$1, %edi
	movq	%rbp, %rsi
	callq	clock_gettime
	movl	12(%rsp), %r8d
	movl	8(%rsp), %r9d
	movq	%r12, %rdi
	movq	%rbx, %rsi
	movq	%r14, %rbx
	movq	%r14, %rdx
	movl	%r15d, %ecx
	callq	_Z11calculationPfPhS0_iii
	leaq	96(%rsp), %r12
	movl	$1, %edi
	movq	%r12, %rsi
	callq	clock_gettime
	movq	(%r12), %rax
	movq	8(%r12), %rcx
	subq	(%rbp), %rax
	xorps	%xmm1, %xmm1
	cvtsi2sd	%rax, %xmm1
	subq	8(%rbp), %rcx
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rcx, %xmm0
	divsd	.LCPI4_4(%rip), %xmm0
	addsd	%xmm1, %xmm0
	movl	$.L.str.14, %edi
	movb	$1, %al
	callq	printf
	movq	72(%rsp), %rax                  # 8-byte Reload
	movq	16(%rax), %rsi
	leaq	128(%rsp), %r14
	movq	%r14, %rdi
	callq	strcpy
	movl	$.L.str.15, %esi
	movq	%r14, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB4_32
# %bb.22:
	movq	%rax, %r14
	movl	$.L.str.17, %edi
	movl	$3, %esi
	movl	$1, %edx
	movq	%rax, %rcx
	callq	fwrite
	movl	12(%rsp), %edx
	movl	8(%rsp), %ecx
	xorl	%r13d, %r13d
	movl	$.L.str.9, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	24(%rsp), %edx
	movl	$.L.str.11, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	12(%rsp), %edx
	imull	8(%rsp), %edx
	movl	$1, %esi
	movq	%rbx, %rdi
	movq	%r14, %rcx
	callq	fwrite
	movq	%r14, %rdi
	callq	fclose
	movq	48(%rsp), %rdi                  # 8-byte Reload
	callq	fclose
	jmp	.LBB4_31
.LBB4_23:
	movq	stderr(%rip), %rcx
	movl	$.L.str.7, %edi
	movl	$30, %esi
	jmp	.LBB4_29
.LBB4_24:
	movq	stderr(%rip), %rcx
	movl	$.L.str.10, %edi
	movl	$25, %esi
	jmp	.LBB4_29
.LBB4_25:
	movq	stderr(%rip), %rcx
	movl	$.L.str.3, %edi
	movl	$62, %esi
	jmp	.LBB4_29
.LBB4_26:
	movq	stderr(%rip), %rdi
	movq	8(%r14), %rdx
	movl	$.L.str.5, %esi
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB4_30
.LBB4_27:
	movq	stderr(%rip), %rcx
	movl	$.L.str.12, %edi
	movl	$26, %esi
	jmp	.LBB4_29
.LBB4_28:
	movq	stderr(%rip), %rcx
	movl	$.L.str.13, %edi
	movl	$41, %esi
.LBB4_29:
	movl	$1, %edx
	callq	fwrite
.LBB4_30:
	movl	$1, %r13d
.LBB4_31:
	movl	%r13d, %eax
	addq	$168, %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
.LBB4_32:
	.cfi_def_cfa_offset 224
	movq	stderr(%rip), %rcx
	movl	$.L.str.16, %edi
	movl	$25, %esi
	movl	$1, %edx
	callq	fwrite
	jmp	.LBB4_31
.Lfunc_end4:
	.size	main, .Lfunc_end4-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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z18calculation_kernelPfPhS0_iii, %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_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%f "
	.size	.L.str, 4

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

	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/samanthatsoi/UC-Davis/master/ECS158/ECS158_p4/gaussian_blur_cuda.hip"
	.size	.L.str.2, 131

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Usage: ./gaussian_blur_cuda <input_file> <output_file> <sigma>"
	.size	.L.str.3, 63

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"rb"
	.size	.L.str.4, 3

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Error: cannot open file %s"
	.size	.L.str.5, 27

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"%s\n"
	.size	.L.str.6, 4

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Error: invalid PGM information"
	.size	.L.str.7, 31

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"P5"
	.size	.L.str.8, 3

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"%d %d\n"
	.size	.L.str.9, 7

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Error: invalid PGM pixels"
	.size	.L.str.10, 26

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"%d\n"
	.size	.L.str.11, 4

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Error: invalid sigma value"
	.size	.L.str.12, 27

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"Error: sigma value too big for image size"
	.size	.L.str.13, 42

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Running time: %f secs\n"
	.size	.L.str.14, 23

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"wb"
	.size	.L.str.15, 3

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"cannot open file to write"
	.size	.L.str.16, 26

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"P5\n"
	.size	.L.str.17, 4

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"CudaErr: %s (%s:%d)\n"
	.size	.L.str.18, 21

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z18calculation_kernelPfPhS0_iii"
	.size	.L__unnamed_1, 33

	.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 _Z33__device_stub__calculation_kernelPfPhS0_iii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z18calculation_kernelPfPhS0_iii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z18calculation_kernelPfPhS0_iii ; -- Begin function _Z18calculation_kernelPfPhS0_iii
	.globl	_Z18calculation_kernelPfPhS0_iii
	.p2align	8
	.type	_Z18calculation_kernelPfPhS0_iii,@function
_Z18calculation_kernelPfPhS0_iii:       ; @_Z18calculation_kernelPfPhS0_iii
; %bb.0:
	s_clause 0x3
	s_load_b32 s2, s[0:1], 0x34
	s_load_b128 s[4:7], s[0:1], 0x18
	s_load_b128 s[8:11], s[0:1], 0x0
	s_load_b64 s[0:1], s[0:1], 0x10
	v_bfe_u32 v2, v0, 10, 10
	v_and_b32_e32 v3, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s3, s2, 16
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_u64_u32 v[0:1], null, s15, s3, v[2:3]
	v_mad_u64_u32 v[1:2], null, s14, s2, v[3:4]
	s_cmp_eq_u32 s4, 0
	s_mov_b32 s3, 0
	s_cbranch_scc1 .LBB0_6
; %bb.1:                                ; %.preheader.lr.ph
	v_cvt_f32_i32_e32 v2, s4
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cvt_f32_i32_e32 v5, v0
	v_cvt_f32_i32_e32 v3, v1
	s_add_i32 s7, s5, -1
	s_add_i32 s12, s6, -1
	v_mul_f32_e32 v2, 0.5, v2
	s_mov_b32 s13, 0
	s_mov_b32 s14, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_floor_f32_e32 v4, v2
	v_sub_f32_e32 v2, v3, v4
	v_dual_sub_f32 v3, v5, v4 :: v_dual_mov_b32 v4, 0
.LBB0_2:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_3 Depth 2
	v_cvt_f32_u32_e32 v5, s14
	s_mov_b32 s15, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_f32_e32 v5, v3, v5
	v_cvt_i32_f32_e32 v5, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_cmp_gt_i32_e32 vcc_lo, s6, v5
	v_cndmask_b32_e32 v6, s12, v5, vcc_lo
	v_cmp_lt_i32_e32 vcc_lo, -1, v5
	v_cndmask_b32_e32 v5, 0, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_mul_lo_u32 v5, v5, s5
.LBB0_3:                                ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	v_cvt_f32_u32_e32 v6, s15
	s_add_i32 s2, s13, s15
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_lshl_b64 s[16:17], s[2:3], 2
	s_add_u32 s16, s8, s16
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f32_e32 v6, v2, v6
	s_addc_u32 s17, s9, s17
	s_add_i32 s15, s15, 1
	s_load_b32 s2, s[16:17], 0x0
	s_cmp_eq_u32 s4, s15
	v_cvt_i32_f32_e32 v6, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_cmp_gt_i32_e32 vcc_lo, s5, v6
	v_cndmask_b32_e32 v7, s7, v6, vcc_lo
	v_cmp_lt_i32_e32 vcc_lo, -1, v6
	v_cndmask_b32_e32 v6, 0, v7, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v6, v6, v5
	v_ashrrev_i32_e32 v7, 31, v6
	v_add_co_u32 v6, vcc_lo, s10, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e32 v7, vcc_lo, s11, v7, vcc_lo
	global_load_u8 v6, v[6:7], off
	s_waitcnt vmcnt(0)
	v_cvt_f32_ubyte0_e32 v6, v6
	s_waitcnt lgkmcnt(0)
	v_fmac_f32_e32 v4, s2, v6
	s_cbranch_scc0 .LBB0_3
; %bb.4:                                ;   in Loop: Header=BB0_2 Depth=1
	s_add_i32 s14, s14, 1
	s_add_i32 s13, s13, s4
	s_cmp_eq_u32 s14, s4
	s_cbranch_scc0 .LBB0_2
; %bb.5:                                ; %._crit_edge.loopexit
	v_cvt_i32_f32_e32 v2, v4
	s_branch .LBB0_7
.LBB0_6:
	v_mov_b32_e32 v2, 0
.LBB0_7:                                ; %._crit_edge
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[3:4], null, v0, s5, v[1:2]
	v_ashrrev_i32_e32 v1, 31, v3
	v_add_co_u32 v0, vcc_lo, s0, v3
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_store_b8 v[0:1], v2, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z18calculation_kernelPfPhS0_iii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 296
		.amdhsa_user_sgpr_count 14
		.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 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 8
		.amdhsa_next_free_sgpr 18
		.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	_Z18calculation_kernelPfPhS0_iii, .Lfunc_end0-_Z18calculation_kernelPfPhS0_iii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 416
; NumSgprs: 20
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 20
; NumVGPRsForWavesPerEU: 8
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.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:           4
        .value_kind:     by_value
      - .offset:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z18calculation_kernelPfPhS0_iii
    .private_segment_fixed_size: 0
    .sgpr_count:     20
    .sgpr_spill_count: 0
    .symbol:         _Z18calculation_kernelPfPhS0_iii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     8
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
