#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <cuda_runtime.h>
#include <float.h>
#include <cuda.h>

//#define BOX_SIZE	23000 /* size of the data box on one dimension            */

/* Constant declaration */
#define MAX_BRIGHTNESS  255 /* Maximum gray level */
#define GRAYLEVEL       700 /* No. of gray levels */
#define MAX_FILENAME    256 /* Filename length limit */
#define MAX_BUFFERSIZE  256

/* Global constant declaration */
/* Image storage arrays */

unsigned char* image1_y;
unsigned char* image2_y;

/* struct used to hold image dimensions*/
typedef struct dim
{
  int x_size1;
  int y_size1;
  int x_size2;
  int y_size2;
} dimension;

/*prewitt cuda kernel function*/                                                                                                       
__global__ 
void prewitt(dimension size_gpu, unsigned char * image1_in_d, unsigned char* image2_out_d, double max, double min)
{
  int x_coord = threadIdx.x + blockIdx.x * blockDim.x;
  int y_coord = blockIdx.y;


  /* removes any unnecessary threads */
  if(x_coord > size_gpu.x_size1 || y_coord > size_gpu.y_size1)
  {
    return;
  } 

  int i, j;

  __syncthreads();

  extern __shared__ unsigned char block_image[];
  
  __syncthreads();
  


//initialize shared memory for each block
//each shared memory segment is size [3][1026] to access the surrounding pixels used in the block

  if(x_coord % 1024 == 0)
  {
    for(i = -1; i < 2; i++)
    {
      for(j = -1; j < 1025; j++)
      {
        if(j + blockIdx.x * blockDim.x == size_gpu.x_size1 + 1)
        {
          break;
        }
        
        if( ((int)(j + blockIdx.x * blockDim.x) < 0) || (y_coord + i < 0) || (y_coord + i >= size_gpu.y_size1) || (j + blockIdx.x * blockDim.x >= size_gpu.x_size1) )
        {
          block_image[(i+1) * 1026 + (j+1)] = NULL;
        }
        else
        {
          block_image[(i+1) * 1026 + (j+1)] = image1_in_d[ (blockIdx.x * blockDim.x) + j + ((y_coord + i) * size_gpu.x_size1) ];
        }
      }
    } 
  }

  __syncthreads();

  double grad; 
  int kernel[3][3] = 
	{	{-1, 0, 1},
		{-1, 0, 1},
		{-1, 0, 1}
 	};  


//normalizes the pixel in the grayscale image and inputs the new pixel into image2_out_d

  if(x_coord != 0 && x_coord != size_gpu.x_size1 - 1 && y_coord != 0 && y_coord != size_gpu.y_size1 - 1)
  {
    grad = 0.0;
    for(j = -1; j <= 1; j++)
    {
      for(i = -1; i <= 1; i++)
      {
        if(threadIdx.x != 0 && block_image[(j + 1) * 1026 + threadIdx.x + i] != NULL)
        {
          grad += kernel[j + 1][i + 1] * block_image[ (j + 1) * 1026 + threadIdx.x + i];
        }
        else if(block_image[(j + 1) * 1026 + threadIdx.x + 1 + i] != NULL)
	{
          grad += kernel[j + 1][i + 1] * block_image[ (j + 1) * 1026 + threadIdx.x + 1 + i];
	}
      }
    }

    grad = 255 * (grad - min) / (max - min);
    image2_out_d[y_coord * size_gpu.x_size1 + x_coord] = grad;
  }

  __syncthreads();
}





int main(void)
{
///////Image input from mypgm.h
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
  dimension size;
  char file_name[MAX_FILENAME];
  char buffer[MAX_BUFFERSIZE];
  FILE *fp; /* File pointer */
  int max_gray; /* Maximum gray level */
  int x, y; /* Loop variable */

  /* Input file open */
  printf("\n-----------------------------------------------------\n");
  printf("Monochromatic image file input routine \n");
  printf("-----------------------------------------------------\n\n");
  printf("     Only pgm binary file is acceptable\n\n");
  printf("Name of input image file? (*.pgm) : ");
  scanf("%s", file_name);
  fp = fopen(file_name, "rb");
  if (NULL == fp) {
    printf("     The file doesn't exist!\n\n");
    exit(1);
  }

  /* Check of file-type ---P5 */
  fgets(buffer, MAX_BUFFERSIZE, fp);
  if (buffer[0] != 'P' || buffer[1] != '5') {
    printf("     Mistaken file format, not P5!\n\n");
    exit(1);
  }

  /* input of x_size1, y_size1 */
  size.x_size1 = 0;
  size.y_size1 = 0;
  while (size.x_size1 == 0 || size.y_size1 == 0) {
    fgets(buffer, MAX_BUFFERSIZE, fp);
    if (buffer[0] != '#') {
      sscanf(buffer, "%d %d", &size.x_size1, &size.y_size1);
    }
  }

  image1_y = (unsigned char*)malloc(size.x_size1 * size.y_size1 * sizeof(unsigned char));
  image2_y = (unsigned char*)malloc(size.x_size1 * size.y_size1 * sizeof(unsigned char));

  /* input of max_gray */
  max_gray = 0;
  while (max_gray == 0) {
    fgets(buffer, MAX_BUFFERSIZE, fp);
    if (buffer[0] != '#') {
      sscanf(buffer, "%d", &max_gray);
    }
  }

  /* Display of parameters */
  printf("\n     Image width = %d, Image height = %d\n", size.x_size1, size.y_size1);
  printf("     Maximum gray level = %d\n\n",max_gray);
  if (max_gray != MAX_BRIGHTNESS) {
    printf("     Invalid value of maximum gray level!\n\n");
    exit(1);
  }

  /* Input of image data*/
  printf("Total Size: %d\n\n", size.y_size1 * size.x_size1);
  for (y = 0; y < size.y_size1; y++)
  {
    for (x = 0; x < size.x_size1; x++)
    {
      image1_y[y * size.x_size1 + x] = (unsigned char)fgetc(fp);
      image2_y[y * size.x_size1 + x] = 0;
    }
  }

  printf("-----Image data input OK-----\n\n");
  printf("-----------------------------------------------------\n\n");
  
  printf("\n");
  printf("\n");
  printf("\n");
 


////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
  
  unsigned char* image1_in_d;
  unsigned char* image2_out_d;
  unsigned char* result;

  const int sizeE = size.y_size1 * size.x_size1 * sizeof(unsigned char);

  //finds the darkest and lightest pixels in the grayscale image
  double max = -DBL_MAX;
  double min = DBL_MAX;
  int i, j;

  int kernel[3][3] = 
  	{	{-1, 0, -1},
		{-1, 0, -1}, 
		{-1, 0, -1}
	};
  double grad;

  for (y = 1; y < size.y_size1 - 1; y++) {
      
    for (x = 1; x < size.x_size1 - 1; x++) {
        
      grad = 0.0;
      for (j = -1; j <= 1; j++) {
          
	    for (i = -1; i <= 1; i++) {
            
	      grad += kernel[j + 1][i + 1] * image1_y[(y + j) * size.x_size1 + x + i];
            
	    }
          
      }
      if (grad < min) min = grad;
      if (grad > max) max = grad;
    }
  }
  
  //if the darkest and lightest pixel is the same then we know the image is blank
  if((int)(max-min) == 0)
  {
    printf("Nothing Exists!!!\n");
    exit(1);
  }

  result = (unsigned char *)malloc(sizeE);

  int block = 1024;
  dim3 grid ((size.x_size1 / 1024) + 1, size.y_size1);

  //allocates the memory in the cuda kernel to hold the imput image and output image
  cudaMalloc((void**)&image1_in_d, sizeE);
  cudaMalloc((void**)&image2_out_d, sizeE);
  
  //copies the image memory into the cuda kernel
  cudaMemcpy(image1_in_d, image1_y , sizeE, cudaMemcpyHostToDevice);
  cudaMemcpy(image2_out_d, image2_y , sizeE, cudaMemcpyHostToDevice);




//starts timer for cuda operation
cudaEvent_t m_start, m_stop;
float m_time;
cudaEventCreate( &m_start );
cudaEventCreate( &m_stop );
cudaEventRecord( m_start, 0 );


    //calls the cuda kernel
    prewitt<<<grid, block, 3 * 1026 * sizeof(unsigned char)>>>(size, image1_in_d, image2_out_d, max, min);


//enda timer for cuda operation and outputs the result
cudaDeviceSynchronize();
cudaEventRecord( m_stop, 0 );
cudaEventSynchronize( m_stop );
cudaEventElapsedTime( &m_time, m_start, m_stop);
printf( "******** Total Running Time of Kernal = %0.5f sec ********\n ", m_time/1000 );
cudaEventDestroy( m_start);
cudaEventDestroy( m_stop);




  //waits for all threads to exit the cuda before continuing
  cudaThreadSynchronize();


  //sets image 2 size to image 1 size
  size.x_size2 = size.x_size1;
  size.y_size2 = size.y_size1;

  //copies over the output image to the results array
  cudaMemcpy(result,image2_out_d, sizeE, cudaMemcpyDeviceToHost);

///////Image output from mypgm.h
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
  
  /* Output file open */
  printf("-----------------------------------------------------\n");
  printf("Monochromatic image file output routine\n");
  printf("-----------------------------------------------------\n\n");
  printf("Name of output image file? (*.pgm) : ");
  scanf("%s",file_name);
  fp = fopen(file_name, "wb");

  /* output of pgm file header information */
  fputs("P5\n", fp);
  fputs("# Created by Image Processing\n", fp);
  fprintf(fp, "%d %d\n", size.x_size1, size.y_size1);
  fprintf(fp, "%d\n", MAX_BRIGHTNESS);

  /* Output of image data */
  for (y = 0; y < size.y_size1; y++) {
    for (x = 0; x < size.x_size1; x++) {
      fputc(result[y * size.x_size1 + x], fp);
    }
  }
  printf("\n-----Image data output OK-----\n\n");
  printf("-----------------------------------------------------\n\n");
  fclose(fp);


////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

  //frees all allocated memory before exiting program
  cudaFree(image2_out_d);
  cudaFree(image1_in_d);
 
  free(result);
  free(image1_y);
  free(image2_y);

	return 0;
}



	.file	"tmpxft_002fea3a_00000000-6_proj3.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z35__device_stub__Z7prewitt3dimPhS0_ddR3dimPhS1_dd
	.type	_Z35__device_stub__Z7prewitt3dimPhS0_ddR3dimPhS1_dd, @function
_Z35__device_stub__Z7prewitt3dimPhS0_ddR3dimPhS1_dd:
.LFB2052:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rsi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	60(%rsp), %rsi
	movq	%rdx, 16(%rsp)
	leaq	32(%rsp), %rdx
	movsd	%xmm0, 8(%rsp)
	movsd	%xmm1, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movq	%rdi, 96(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	movq	%rsp, %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movl	$1, 56(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L2
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z7prewitt3dimPhS0_dd(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 176
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L2:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L4
	call	__stack_chk_fail@PLT
.L4:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2052:
	.size	_Z35__device_stub__Z7prewitt3dimPhS0_ddR3dimPhS1_dd, .-_Z35__device_stub__Z7prewitt3dimPhS0_ddR3dimPhS1_dd
	.globl	_Z7prewitt3dimPhS0_dd
	.type	_Z7prewitt3dimPhS0_dd, @function
_Z7prewitt3dimPhS0_dd:
.LFB2053:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	%rdi, (%rsp)
	movq	%rsp, %rdi
	movq	%rsi, 8(%rsp)
	movq	%rdx, %rsi
	movq	%rcx, %rdx
	call	_Z35__device_stub__Z7prewitt3dimPhS0_ddR3dimPhS1_dd
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2053:
	.size	_Z7prewitt3dimPhS0_dd, .-_Z7prewitt3dimPhS0_dd
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC4:
	.string	"\n-----------------------------------------------------\n"
.LC5:
	.string	"Monochromatic image file input routine \n"
.LC6:
	.string	"-----------------------------------------------------\n\n"
.LC7:
	.string	"     Only pgm binary file is acceptable\n\n"
.LC8:
	.string	"Name of input image file? (*.pgm) : "
.LC9:
	.string	"%s"
.LC10:
	.string	"rb"
.LC11:
	.string	"     The file doesn't exist!\n\n"
.LC12:
	.string	"     Mistaken file format, not P5!\n\n"
.LC13:
	.string	"%d %d"
.LC14:
	.string	"%d"
.LC15:
	.string	"\n     Image width = %d, Image height = %d\n"
.LC16:
	.string	"     Maximum gray level = %d\n\n"
.LC17:
	.string	"     Invalid value of maximum gray level!\n\n"
.LC18:
	.string	"Total Size: %d\n\n"
.LC19:
	.string	"-----Image data input OK-----\n\n"
.LC20:
	.string	"\n"
.LC21:
	.string	"Nothing Exists!!!\n"
.LC23:
	.string	"******** Total Running Time of Kernal = %0.5f sec ********\n "
.LC24:
	.string	"-----------------------------------------------------\n"
.LC25:
	.string	"Monochromatic image file output routine\n"
.LC26:
	.string	"Name of output image file? (*.pgm) : "
.LC27:
	.string	"wb"
.LC28:
	.string	"P5\n"
.LC29:
	.string	"# Created by Image Processing\n"
.LC30:
	.string	"%d %d\n"
.LC31:
	.string	"%d\n"
.LC32:
	.string	"\n-----Image data output OK-----\n\n"
	.section	.rodata
	.align 32
.LC0:
	.long	-1
	.long	0
	.long	-1
	.long	-1
	.long	0
	.long	-1
	.long	-1
	.long	0
	.long	-1
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC4(%rip), %rsi
	movl	$2, %edi
	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	$696, %rsp
	.cfi_def_cfa_offset 752
	movq	%fs:40, %rax
	movq	%rax, 680(%rsp)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC5(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	168(%rsp), %rax
	leaq	.LC9(%rip), %rdi
	movq	%rax, %rsi
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	movq	8(%rsp), %rdi
	leaq	.LC10(%rip), %rsi
	call	fopen@PLT
	leaq	.LC11(%rip), %rsi
	testq	%rax, %rax
	je	.L51
	movq	%rax, %rcx
	movl	$256, %edx
	movl	$256, %esi
	movq	%rax, %rbx
	leaq	424(%rsp), %r12
	movq	%r12, %rdi
	call	__fgets_chk@PLT
	cmpb	$80, 424(%rsp)
	jne	.L11
	cmpb	$53, 425(%rsp)
	je	.L12
.L11:
	leaq	.LC12(%rip), %rsi
.L51:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L12:
	xorl	%edx, %edx
	leaq	.LC13(%rip), %r13
	movq	%rdx, 100(%rsp)
.L14:
	movl	100(%rsp), %ebp
	testl	%ebp, %ebp
	jne	.L52
.L15:
	movq	%rbx, %rcx
	movl	$256, %edx
	movl	$256, %esi
	movq	%r12, %rdi
	call	__fgets_chk@PLT
	cmpb	$35, 424(%rsp)
	je	.L14
	leaq	100(%rsp), %rdx
	leaq	104(%rsp), %rcx
	movq	%r13, %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_sscanf@PLT
	jmp	.L14
.L52:
	movl	104(%rsp), %eax
	testl	%eax, %eax
	je	.L15
	imull	%eax, %ebp
	movslq	%ebp, %rbp
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, image1_y(%rip)
	call	malloc@PLT
	movq	%rax, image2_y(%rip)
	xorl	%eax, %eax
	movl	%eax, 36(%rsp)
.L17:
	cmpl	$0, 36(%rsp)
	jne	.L53
	movq	%rbx, %rcx
	movl	$256, %edx
	movl	$256, %esi
	movq	%r12, %rdi
	call	__fgets_chk@PLT
	cmpb	$35, 424(%rsp)
	je	.L17
	leaq	36(%rsp), %rdx
	leaq	.LC14(%rip), %rsi
	movq	%r12, %rdi
	xorl	%eax, %eax
	call	__isoc23_sscanf@PLT
	jmp	.L17
.L53:
	movl	104(%rsp), %ecx
	movl	100(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC15(%rip), %rsi
	call	__printf_chk@PLT
	movl	36(%rsp), %edx
	xorl	%eax, %eax
	movl	$2, %edi
	leaq	.LC16(%rip), %rsi
	call	__printf_chk@PLT
	cmpl	$255, 36(%rsp)
	leaq	.LC17(%rip), %rsi
	jne	.L51
	movl	104(%rsp), %edx
	imull	100(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC18(%rip), %rsi
	xorl	%ebp, %ebp
	call	__printf_chk@PLT
.L20:
	cmpl	%ebp, 104(%rsp)
	jle	.L54
	xorl	%r12d, %r12d
.L22:
	cmpl	%r12d, 100(%rsp)
	jle	.L55
	movq	%rbx, %rdi
	call	fgetc@PLT
	movq	image1_y(%rip), %rdx
	movl	%eax, %ecx
	movl	100(%rsp), %eax
	imull	%ebp, %eax
	addl	%r12d, %eax
	cltq
	movb	%cl, (%rdx,%rax)
	movl	100(%rsp), %eax
	movq	image2_y(%rip), %rdx
	imull	%ebp, %eax
	addl	%r12d, %eax
	incl	%r12d
	cltq
	movb	$0, (%rdx,%rax)
	jmp	.L22
.L55:
	incl	%ebp
	jmp	.L20
.L54:
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC20(%rip), %rbx
	leaq	.LC6(%rip), %rsi
	leaq	156(%rsp), %r15
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	104(%rsp), %ebp
	movslq	100(%rsp), %r12
	leaq	132(%rsp), %rdi
	leaq	.LC0(%rip), %rsi
	movl	$9, %ecx
	movsd	.LC2(%rip), %xmm1
	movsd	.LC3(%rip), %xmm0
	movl	%ebp, %eax
	leal	-1(%rbp), %r13d
	leal	-1(%r12), %r14d
	movq	%r12, %rbx
	rep movsl
	imull	%r12d, %eax
	movq	image1_y(%rip), %rcx
	movl	$1, %edi
	leaq	1(%rcx), %rdx
.L23:
	cmpl	%edi, %r13d
	jle	.L56
	movq	%rdx, %r9
	movl	$1, %r10d
.L30:
	cmpl	%r10d, %r14d
	jle	.L28
	leaq	120(%rsp), %rsi
	movq	%r9, %r8
	xorps	%xmm2, %xmm2
.L29:
	orq	$-1, %rcx
.L24:
	movzbl	(%r8,%rcx), %r11d
	imull	16(%rsi,%rcx,4), %r11d
	incq	%rcx
	cvtsi2sdl	%r11d, %xmm3
	addsd	%xmm3, %xmm2
	cmpq	$2, %rcx
	jne	.L24
	addq	$12, %rsi
	addq	%r12, %r8
	cmpq	%rsi, %r15
	jne	.L29
	movaps	%xmm2, %xmm4
	maxsd	%xmm0, %xmm2
	incl	%r10d
	incq	%r9
	minsd	%xmm1, %xmm4
	movaps	%xmm2, %xmm0
	movaps	%xmm4, %xmm1
	jmp	.L30
.L28:
	incl	%edi
	addq	%r12, %rdx
	jmp	.L23
.L56:
	movaps	%xmm0, %xmm2
	leaq	.LC21(%rip), %rsi
	subsd	%xmm1, %xmm2
	cvttsd2sil	%xmm2, %edx
	testl	%edx, %edx
	je	.L51
	movslq	%eax, %r12
	movsd	%xmm1, 24(%rsp)
	movq	%r12, %rdi
	movsd	%xmm0, 16(%rsp)
	call	malloc@PLT
	movl	$1024, %ecx
	movq	%r12, %rsi
	movl	%ebp, 80(%rsp)
	movq	%rax, %r13
	movl	%ebx, %eax
	leaq	40(%rsp), %rdi
	movl	$1, 84(%rsp)
	cltd
	leaq	116(%rsp), %rbx
	idivl	%ecx
	incl	%eax
	movl	%eax, 76(%rsp)
	call	cudaMalloc@PLT
	leaq	48(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	movq	image1_y(%rip), %rsi
	movq	40(%rsp), %rdi
	movq	%r12, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	image2_y(%rip), %rsi
	movq	48(%rsp), %rdi
	movq	%r12, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	64(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	76(%rsp), %rdi
	movl	84(%rsp), %esi
	xorl	%r9d, %r9d
	movl	$4194305, %edx
	movl	$3078, %r8d
	movl	$1, %ecx
	salq	$10, %rdx
	call	__cudaPushCallConfiguration@PLT
	movsd	16(%rsp), %xmm0
	movsd	24(%rsp), %xmm1
	testl	%eax, %eax
	jne	.L32
	movups	100(%rsp), %xmm5
	movq	48(%rsp), %rdx
	movq	%rbx, %rdi
	movq	40(%rsp), %rsi
	movups	%xmm5, 116(%rsp)
	call	_Z35__device_stub__Z7prewitt3dimPhS0_ddR3dimPhS1_dd
.L32:
	call	cudaDeviceSynchronize@PLT
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	xorl	%ebp, %ebp
	call	cudaEventRecord@PLT
	movq	64(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	64(%rsp), %rdx
	movq	56(%rsp), %rsi
	movq	%rbx, %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC23(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	movss	116(%rsp), %xmm0
	divss	.LC22(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	56(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	call	cudaThreadSynchronize@PLT
	movq	100(%rsp), %rax
	movq	%r12, %rdx
	movq	%r13, %rdi
	movq	48(%rsp), %rsi
	movl	$2, %ecx
	movq	%rax, 108(%rsp)
	call	cudaMemcpy@PLT
	leaq	.LC24(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC25(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC26(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	8(%rsp), %rsi
	leaq	.LC9(%rip), %rdi
	xorl	%eax, %eax
	call	__isoc23_scanf@PLT
	movq	8(%rsp), %rdi
	leaq	.LC27(%rip), %rsi
	call	fopen@PLT
	leaq	.LC28(%rip), %rdi
	movq	%rax, %rbx
	movq	%rax, %rsi
	call	fputs@PLT
	movq	%rbx, %rsi
	leaq	.LC29(%rip), %rdi
	call	fputs@PLT
	movl	104(%rsp), %r8d
	movl	100(%rsp), %ecx
	xorl	%eax, %eax
	leaq	.LC30(%rip), %rdx
	movl	$2, %esi
	movq	%rbx, %rdi
	call	__fprintf_chk@PLT
	movl	$255, %ecx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	leaq	.LC31(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
.L33:
	cmpl	%ebp, 104(%rsp)
	jle	.L57
	xorl	%r12d, %r12d
.L35:
	movl	100(%rsp), %eax
	cmpl	%r12d, %eax
	jle	.L58
	imull	%ebp, %eax
	movq	%rbx, %rsi
	addl	%r12d, %eax
	incl	%r12d
	cltq
	movzbl	0(%r13,%rax), %edi
	call	fputc@PLT
	jmp	.L35
.L58:
	incl	%ebp
	jmp	.L33
.L57:
	leaq	.LC32(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	%r13, %rdi
	call	free@PLT
	movq	image1_y(%rip), %rdi
	call	free@PLT
	movq	image2_y(%rip), %rdi
	call	free@PLT
	movq	680(%rsp), %rax
	subq	%fs:40, %rax
	je	.L36
	call	__stack_chk_fail@PLT
.L36:
	addq	$696, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %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
.LFE2027:
	.size	main, .-main
	.section	.rodata.str1.1
.LC34:
	.string	"_Z7prewitt3dimPhS0_dd"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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	.LC34(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z7prewitt3dimPhS0_dd(%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
.LFE2055:
	.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
	.globl	image2_y
	.bss
	.align 8
	.type	image2_y, @object
	.size	image2_y, 8
image2_y:
	.zero	8
	.globl	image1_y
	.align 8
	.type	image1_y, @object
	.size	image1_y, 8
image1_y:
	.zero	8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC2:
	.long	-1
	.long	2146435071
	.align 8
.LC3:
	.long	-1
	.long	-1048577
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC22:
	.long	1148846080
	.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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <hip/hip_runtime.h>
#include <float.h>


//#define BOX_SIZE	23000 /* size of the data box on one dimension            */

/* Constant declaration */
#define MAX_BRIGHTNESS  255 /* Maximum gray level */
#define GRAYLEVEL       700 /* No. of gray levels */
#define MAX_FILENAME    256 /* Filename length limit */
#define MAX_BUFFERSIZE  256

/* Global constant declaration */
/* Image storage arrays */

unsigned char* image1_y;
unsigned char* image2_y;

/* struct used to hold image dimensions*/
typedef struct dim
{
  int x_size1;
  int y_size1;
  int x_size2;
  int y_size2;
} dimension;

/*prewitt cuda kernel function*/                                                                                                       
__global__ 
void prewitt(dimension size_gpu, unsigned char * image1_in_d, unsigned char* image2_out_d, double max, double min)
{
  int x_coord = threadIdx.x + blockIdx.x * blockDim.x;
  int y_coord = blockIdx.y;


  /* removes any unnecessary threads */
  if(x_coord > size_gpu.x_size1 || y_coord > size_gpu.y_size1)
  {
    return;
  } 

  int i, j;

  __syncthreads();

  extern __shared__ unsigned char block_image[];
  
  __syncthreads();
  


//initialize shared memory for each block
//each shared memory segment is size [3][1026] to access the surrounding pixels used in the block

  if(x_coord % 1024 == 0)
  {
    for(i = -1; i < 2; i++)
    {
      for(j = -1; j < 1025; j++)
      {
        if(j + blockIdx.x * blockDim.x == size_gpu.x_size1 + 1)
        {
          break;
        }
        
        if( ((int)(j + blockIdx.x * blockDim.x) < 0) || (y_coord + i < 0) || (y_coord + i >= size_gpu.y_size1) || (j + blockIdx.x * blockDim.x >= size_gpu.x_size1) )
        {
          block_image[(i+1) * 1026 + (j+1)] = NULL;
        }
        else
        {
          block_image[(i+1) * 1026 + (j+1)] = image1_in_d[ (blockIdx.x * blockDim.x) + j + ((y_coord + i) * size_gpu.x_size1) ];
        }
      }
    } 
  }

  __syncthreads();

  double grad; 
  int kernel[3][3] = 
	{	{-1, 0, 1},
		{-1, 0, 1},
		{-1, 0, 1}
 	};  


//normalizes the pixel in the grayscale image and inputs the new pixel into image2_out_d

  if(x_coord != 0 && x_coord != size_gpu.x_size1 - 1 && y_coord != 0 && y_coord != size_gpu.y_size1 - 1)
  {
    grad = 0.0;
    for(j = -1; j <= 1; j++)
    {
      for(i = -1; i <= 1; i++)
      {
        if(threadIdx.x != 0 && block_image[(j + 1) * 1026 + threadIdx.x + i] != NULL)
        {
          grad += kernel[j + 1][i + 1] * block_image[ (j + 1) * 1026 + threadIdx.x + i];
        }
        else if(block_image[(j + 1) * 1026 + threadIdx.x + 1 + i] != NULL)
	{
          grad += kernel[j + 1][i + 1] * block_image[ (j + 1) * 1026 + threadIdx.x + 1 + i];
	}
      }
    }

    grad = 255 * (grad - min) / (max - min);
    image2_out_d[y_coord * size_gpu.x_size1 + x_coord] = grad;
  }

  __syncthreads();
}





int main(void)
{
///////Image input from mypgm.h
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
  dimension size;
  char file_name[MAX_FILENAME];
  char buffer[MAX_BUFFERSIZE];
  FILE *fp; /* File pointer */
  int max_gray; /* Maximum gray level */
  int x, y; /* Loop variable */

  /* Input file open */
  printf("\n-----------------------------------------------------\n");
  printf("Monochromatic image file input routine \n");
  printf("-----------------------------------------------------\n\n");
  printf("     Only pgm binary file is acceptable\n\n");
  printf("Name of input image file? (*.pgm) : ");
  scanf("%s", file_name);
  fp = fopen(file_name, "rb");
  if (NULL == fp) {
    printf("     The file doesn't exist!\n\n");
    exit(1);
  }

  /* Check of file-type ---P5 */
  fgets(buffer, MAX_BUFFERSIZE, fp);
  if (buffer[0] != 'P' || buffer[1] != '5') {
    printf("     Mistaken file format, not P5!\n\n");
    exit(1);
  }

  /* input of x_size1, y_size1 */
  size.x_size1 = 0;
  size.y_size1 = 0;
  while (size.x_size1 == 0 || size.y_size1 == 0) {
    fgets(buffer, MAX_BUFFERSIZE, fp);
    if (buffer[0] != '#') {
      sscanf(buffer, "%d %d", &size.x_size1, &size.y_size1);
    }
  }

  image1_y = (unsigned char*)malloc(size.x_size1 * size.y_size1 * sizeof(unsigned char));
  image2_y = (unsigned char*)malloc(size.x_size1 * size.y_size1 * sizeof(unsigned char));

  /* input of max_gray */
  max_gray = 0;
  while (max_gray == 0) {
    fgets(buffer, MAX_BUFFERSIZE, fp);
    if (buffer[0] != '#') {
      sscanf(buffer, "%d", &max_gray);
    }
  }

  /* Display of parameters */
  printf("\n     Image width = %d, Image height = %d\n", size.x_size1, size.y_size1);
  printf("     Maximum gray level = %d\n\n",max_gray);
  if (max_gray != MAX_BRIGHTNESS) {
    printf("     Invalid value of maximum gray level!\n\n");
    exit(1);
  }

  /* Input of image data*/
  printf("Total Size: %d\n\n", size.y_size1 * size.x_size1);
  for (y = 0; y < size.y_size1; y++)
  {
    for (x = 0; x < size.x_size1; x++)
    {
      image1_y[y * size.x_size1 + x] = (unsigned char)fgetc(fp);
      image2_y[y * size.x_size1 + x] = 0;
    }
  }

  printf("-----Image data input OK-----\n\n");
  printf("-----------------------------------------------------\n\n");
  
  printf("\n");
  printf("\n");
  printf("\n");
 


////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
  
  unsigned char* image1_in_d;
  unsigned char* image2_out_d;
  unsigned char* result;

  const int sizeE = size.y_size1 * size.x_size1 * sizeof(unsigned char);

  //finds the darkest and lightest pixels in the grayscale image
  double max = -DBL_MAX;
  double min = DBL_MAX;
  int i, j;

  int kernel[3][3] = 
  	{	{-1, 0, -1},
		{-1, 0, -1}, 
		{-1, 0, -1}
	};
  double grad;

  for (y = 1; y < size.y_size1 - 1; y++) {
      
    for (x = 1; x < size.x_size1 - 1; x++) {
        
      grad = 0.0;
      for (j = -1; j <= 1; j++) {
          
	    for (i = -1; i <= 1; i++) {
            
	      grad += kernel[j + 1][i + 1] * image1_y[(y + j) * size.x_size1 + x + i];
            
	    }
          
      }
      if (grad < min) min = grad;
      if (grad > max) max = grad;
    }
  }
  
  //if the darkest and lightest pixel is the same then we know the image is blank
  if((int)(max-min) == 0)
  {
    printf("Nothing Exists!!!\n");
    exit(1);
  }

  result = (unsigned char *)malloc(sizeE);

  int block = 1024;
  dim3 grid ((size.x_size1 / 1024) + 1, size.y_size1);

  //allocates the memory in the cuda kernel to hold the imput image and output image
  hipMalloc((void**)&image1_in_d, sizeE);
  hipMalloc((void**)&image2_out_d, sizeE);
  
  //copies the image memory into the cuda kernel
  hipMemcpy(image1_in_d, image1_y , sizeE, hipMemcpyHostToDevice);
  hipMemcpy(image2_out_d, image2_y , sizeE, hipMemcpyHostToDevice);




//starts timer for cuda operation
hipEvent_t m_start, m_stop;
float m_time;
hipEventCreate( &m_start );
hipEventCreate( &m_stop );
hipEventRecord( m_start, 0 );


    //calls the cuda kernel
    prewitt<<<grid, block, 3 * 1026 * sizeof(unsigned char)>>>(size, image1_in_d, image2_out_d, max, min);


//enda timer for cuda operation and outputs the result
hipDeviceSynchronize();
hipEventRecord( m_stop, 0 );
hipEventSynchronize( m_stop );
hipEventElapsedTime( &m_time, m_start, m_stop);
printf( "******** Total Running Time of Kernal = %0.5f sec ********\n ", m_time/1000 );
hipEventDestroy( m_start);
hipEventDestroy( m_stop);




  //waits for all threads to exit the cuda before continuing
  hipDeviceSynchronize();


  //sets image 2 size to image 1 size
  size.x_size2 = size.x_size1;
  size.y_size2 = size.y_size1;

  //copies over the output image to the results array
  hipMemcpy(result,image2_out_d, sizeE, hipMemcpyDeviceToHost);

///////Image output from mypgm.h
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
  
  /* Output file open */
  printf("-----------------------------------------------------\n");
  printf("Monochromatic image file output routine\n");
  printf("-----------------------------------------------------\n\n");
  printf("Name of output image file? (*.pgm) : ");
  scanf("%s",file_name);
  fp = fopen(file_name, "wb");

  /* output of pgm file header information */
  fputs("P5\n", fp);
  fputs("# Created by Image Processing\n", fp);
  fprintf(fp, "%d %d\n", size.x_size1, size.y_size1);
  fprintf(fp, "%d\n", MAX_BRIGHTNESS);

  /* Output of image data */
  for (y = 0; y < size.y_size1; y++) {
    for (x = 0; x < size.x_size1; x++) {
      fputc(result[y * size.x_size1 + x], fp);
    }
  }
  printf("\n-----Image data output OK-----\n\n");
  printf("-----------------------------------------------------\n\n");
  fclose(fp);


////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

  //frees all allocated memory before exiting program
  hipFree(image2_out_d);
  hipFree(image1_in_d);
 
  free(result);
  free(image1_y);
  free(image2_y);

	return 0;
}



	.text
	.file	"proj3.hip"
	.globl	_Z22__device_stub__prewitt3dimPhS0_dd # -- Begin function _Z22__device_stub__prewitt3dimPhS0_dd
	.type	_Z22__device_stub__prewitt3dimPhS0_dd,@function
_Z22__device_stub__prewitt3dimPhS0_dd:  # @_Z22__device_stub__prewitt3dimPhS0_dd
	.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	80(%rsp), %rax
	movq	%rdi, (%rax)
	movq	%rsi, 8(%rax)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	24(%rsp), %rcx
	movsd	%xmm0, (%rcx)
	leaq	16(%rsp), %rdi
	movsd	%xmm1, (%rdi)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rsi, 8(%rbx)
	movq	%rdx, 16(%rbx)
	movq	%rcx, 24(%rbx)
	movq	%rdi, 32(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%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	$_Z7prewitt3dimPhS0_dd, %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_end0:
	.size	_Z22__device_stub__prewitt3dimPhS0_dd, .Lfunc_end0-_Z22__device_stub__prewitt3dimPhS0_dd
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI1_0:
	.quad	0xffefffffffffffff              # double -1.7976931348623157E+308
.LCPI1_1:
	.quad	0x7fefffffffffffff              # double 1.7976931348623157E+308
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0
.LCPI1_2:
	.long	0x447a0000                      # float 1000
	.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	$648, %rsp                      # imm = 0x288
	.cfi_def_cfa_offset 704
	.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	$.Lstr, %edi
	callq	puts@PLT
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$.Lstr.10, %edi
	callq	puts@PLT
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	leaq	384(%rsp), %rbx
	movl	$.L.str.5, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	$.L.str.6, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB1_1
# %bb.3:
	movq	%rax, %rbx
	leaq	128(%rsp), %r14
	movq	%r14, %rdi
	movl	$256, %esi                      # imm = 0x100
	movq	%rax, %rdx
	callq	fgets
	cmpb	$80, (%r14)
	jne	.LBB1_5
# %bb.4:
	cmpb	$53, 129(%rsp)
	jne	.LBB1_5
# %bb.6:                                # %.lr.ph.preheader
	leaq	4(%rsp), %r14
	movq	$0, -4(%r14)
	leaq	128(%rsp), %r15
	movq	%rsp, %r12
.LBB1_7:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	%r15, %rdi
	movl	$256, %esi                      # imm = 0x100
	movq	%rbx, %rdx
	callq	fgets
	cmpb	$35, 128(%rsp)
	je	.LBB1_9
# %bb.8:                                #   in Loop: Header=BB1_7 Depth=1
	movl	$.L.str.9, %esi
	movq	%r15, %rdi
	movq	%r12, %rdx
	movq	%r14, %rcx
	xorl	%eax, %eax
	callq	__isoc23_sscanf
.LBB1_9:                                #   in Loop: Header=BB1_7 Depth=1
	movl	(%rsp), %eax
	testl	%eax, %eax
	je	.LBB1_7
# %bb.10:                               #   in Loop: Header=BB1_7 Depth=1
	movl	4(%rsp), %ecx
	testl	%ecx, %ecx
	je	.LBB1_7
# %bb.11:                               # %._crit_edge
	imull	%eax, %ecx
	movslq	%ecx, %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, image1_y(%rip)
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, image2_y(%rip)
	leaq	20(%rsp), %r14
	movl	$0, (%r14)
	leaq	128(%rsp), %r15
.LBB1_12:                               # =>This Inner Loop Header: Depth=1
	movq	%r15, %rdi
	movl	$256, %esi                      # imm = 0x100
	movq	%rbx, %rdx
	callq	fgets
	cmpb	$35, 128(%rsp)
	je	.LBB1_14
# %bb.13:                               #   in Loop: Header=BB1_12 Depth=1
	movl	$.L.str.10, %esi
	movq	%r15, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_sscanf
.LBB1_14:                               #   in Loop: Header=BB1_12 Depth=1
	cmpl	$0, 20(%rsp)
	je	.LBB1_12
# %bb.15:
	movl	(%rsp), %esi
	movl	4(%rsp), %edx
	movl	$.L.str.11, %edi
	xorl	%eax, %eax
	callq	printf
	movl	20(%rsp), %esi
	movl	$.L.str.12, %edi
	xorl	%eax, %eax
	callq	printf
	cmpl	$255, 20(%rsp)
	jne	.LBB1_16
# %bb.17:
	movl	(%rsp), %esi
	imull	4(%rsp), %esi
	xorl	%r14d, %r14d
	movl	$.L.str.14, %edi
	xorl	%eax, %eax
	callq	printf
	movl	4(%rsp), %ecx
	testl	%ecx, %ecx
	jle	.LBB1_24
# %bb.18:                               # %.preheader108.preheader
	movl	(%rsp), %eax
.LBB1_19:                               # %.preheader108
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_21 Depth 2
	testl	%eax, %eax
	jle	.LBB1_23
# %bb.20:                               # %.lr.ph112.preheader
                                        #   in Loop: Header=BB1_19 Depth=1
	movslq	%r14d, %r15
	xorl	%r12d, %r12d
.LBB1_21:                               # %.lr.ph112
                                        #   Parent Loop BB1_19 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%rbx, %rdi
	callq	fgetc
	movslq	(%rsp), %rcx
	imulq	%r15, %rcx
	addq	image1_y(%rip), %rcx
	movb	%al, (%r12,%rcx)
	movslq	(%rsp), %rax
	imulq	%r15, %rax
	addq	image2_y(%rip), %rax
	movb	$0, (%r12,%rax)
	movl	(%rsp), %eax
	incq	%r12
	cmpl	%eax, %r12d
	jl	.LBB1_21
# %bb.22:                               # %._crit_edge113.loopexit
                                        #   in Loop: Header=BB1_19 Depth=1
	movl	4(%rsp), %ecx
.LBB1_23:                               # %._crit_edge113
                                        #   in Loop: Header=BB1_19 Depth=1
	incl	%r14d
	cmpl	%ecx, %r14d
	jl	.LBB1_19
.LBB1_24:                               # %._crit_edge115
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	movl	$.Lstr.10, %edi
	callq	puts@PLT
	movl	$10, %edi
	callq	putchar@PLT
	movl	$10, %edi
	callq	putchar@PLT
	movl	$10, %edi
	callq	putchar@PLT
	movslq	4(%rsp), %rbx
	movslq	(%rsp), %r12
	movl	%ebx, %r15d
	xorpd	%xmm0, %xmm0
	movapd	%xmm0, 80(%rsp)
	movapd	%xmm0, 96(%rsp)
	movl	$-1, %eax
	movl	%eax, 80(%rsp)
	movq	$-1, %rcx
	movq	%rcx, 88(%rsp)
	movq	%rcx, 100(%rsp)
	movl	%eax, 112(%rsp)
	cmpq	$3, %rbx
	jl	.LBB1_25
# %bb.26:                               # %.preheader107.lr.ph
	leal	-1(%r15), %eax
	leal	-1(%r12), %ecx
	movq	image1_y(%rip), %rdx
	movsd	.LCPI1_1(%rip), %xmm1           # xmm1 = mem[0],zero
	movsd	.LCPI1_0(%rip), %xmm0           # xmm0 = mem[0],zero
	movl	$1, %esi
	leaq	80(%rsp), %rdi
.LBB1_27:                               # %.preheader107
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_30 Depth 2
                                        #       Child Loop BB1_31 Depth 3
                                        #         Child Loop BB1_32 Depth 4
	cmpl	$3, %r12d
	jl	.LBB1_28
# %bb.29:                               # %.preheader106.preheader
                                        #   in Loop: Header=BB1_27 Depth=1
	movl	$1, %r8d
	movq	%rdx, %r9
.LBB1_30:                               # %.preheader106
                                        #   Parent Loop BB1_27 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB1_31 Depth 3
                                        #         Child Loop BB1_32 Depth 4
	xorpd	%xmm3, %xmm3
	movq	$-1, %r10
	movq	%rdi, %r11
	movq	%r9, %r14
.LBB1_31:                               # %.preheader105
                                        #   Parent Loop BB1_27 Depth=1
                                        #     Parent Loop BB1_30 Depth=2
                                        # =>    This Loop Header: Depth=3
                                        #         Child Loop BB1_32 Depth 4
	incq	%r10
	xorl	%r13d, %r13d
.LBB1_32:                               #   Parent Loop BB1_27 Depth=1
                                        #     Parent Loop BB1_30 Depth=2
                                        #       Parent Loop BB1_31 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	movzbl	(%r14,%r13), %ebp
	imull	(%r11,%r13,4), %ebp
	xorps	%xmm2, %xmm2
	cvtsi2sd	%ebp, %xmm2
	addsd	%xmm2, %xmm3
	incq	%r13
	cmpq	$3, %r13
	jne	.LBB1_32
# %bb.33:                               #   in Loop: Header=BB1_31 Depth=3
	addq	%r12, %r14
	addq	$12, %r11
	cmpq	$2, %r10
	jne	.LBB1_31
# %bb.34:                               #   in Loop: Header=BB1_30 Depth=2
	movapd	%xmm3, %xmm2
	minsd	%xmm1, %xmm2
	maxsd	%xmm0, %xmm3
	incq	%r8
	incq	%r9
	movapd	%xmm3, %xmm0
	movapd	%xmm2, %xmm1
	cmpq	%rcx, %r8
	jne	.LBB1_30
	jmp	.LBB1_35
.LBB1_28:                               #   in Loop: Header=BB1_27 Depth=1
	movapd	%xmm1, %xmm2
	movapd	%xmm0, %xmm3
.LBB1_35:                               # %._crit_edge123
                                        #   in Loop: Header=BB1_27 Depth=1
	incq	%rsi
	addq	%r12, %rdx
	movapd	%xmm3, %xmm0
	movapd	%xmm2, %xmm1
	cmpq	%rax, %rsi
	jne	.LBB1_27
	jmp	.LBB1_36
.LBB1_25:
	movsd	.LCPI1_0(%rip), %xmm3           # xmm3 = mem[0],zero
	movsd	.LCPI1_1(%rip), %xmm2           # xmm2 = mem[0],zero
.LBB1_36:                               # %._crit_edge129
	movsd	%xmm3, 64(%rsp)                 # 8-byte Spill
	movapd	%xmm3, %xmm0
	movsd	%xmm2, 56(%rsp)                 # 8-byte Spill
	subsd	%xmm2, %xmm0
	cvttsd2si	%xmm0, %eax
	testl	%eax, %eax
	je	.LBB1_37
# %bb.38:
	imulq	%r12, %rbx
	movq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r14
	leal	1023(%r12), %eax
	testl	%r12d, %r12d
	cmovnsl	%r12d, %eax
	sarl	$10, %eax
	incl	%eax
	shlq	$32, %r15
	orq	%rax, %r15
	leaq	48(%rsp), %r12
	movq	%r12, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	32(%rsp), %r13
	movq	%r13, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	movq	image1_y(%rip), %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	movq	image2_y(%rip), %rsi
	movq	%rbx, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r12), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movabsq	$4294968320, %rdx               # imm = 0x100000400
	movl	$3078, %r8d                     # imm = 0xC06
	movq	%r15, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_40
# %bb.39:
	movq	(%rsp), %rdi
	movq	8(%rsp), %rsi
	movq	48(%rsp), %rdx
	movq	32(%rsp), %rcx
	movsd	64(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movsd	56(%rsp), %xmm1                 # 8-byte Reload
                                        # xmm1 = mem[0],zero
	callq	_Z22__device_stub__prewitt3dimPhS0_dd
.LBB1_40:
	callq	hipDeviceSynchronize
	movq	24(%rsp), %rdi
	xorl	%r12d, %r12d
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	24(%rsp), %rdi
	callq	hipEventSynchronize
	movq	40(%rsp), %rsi
	movq	24(%rsp), %rdx
	leaq	76(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventElapsedTime
	movss	(%r15), %xmm0                   # xmm0 = mem[0],zero,zero,zero
	divss	.LCPI1_2(%rip), %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.18, %edi
	movb	$1, %al
	callq	printf
	movq	40(%rsp), %rdi
	callq	hipEventDestroy
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	callq	hipDeviceSynchronize
	movq	(%rsp), %rax
	movq	%rax, 8(%rsp)
	movq	32(%rsp), %rsi
	movq	%r14, %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movl	$.Lstr.6, %edi
	callq	puts@PLT
	movl	$.Lstr.7, %edi
	callq	puts@PLT
	movl	$.Lstr.10, %edi
	callq	puts@PLT
	movl	$.L.str.21, %edi
	xorl	%eax, %eax
	callq	printf
	leaq	384(%rsp), %rbx
	movl	$.L.str.5, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	__isoc23_scanf
	movl	$.L.str.22, %esi
	movq	%rbx, %rdi
	callq	fopen
	movq	%rax, %rbx
	movl	$.L.str.23, %edi
	movq	%rax, %rsi
	callq	fputs
	movl	$.L.str.24, %edi
	movq	%rbx, %rsi
	callq	fputs
	movl	(%rsp), %edx
	movl	4(%rsp), %ecx
	movl	$.L.str.25, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.26, %esi
	movq	%rbx, %rdi
	movl	$255, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	4(%rsp), %eax
	testl	%eax, %eax
	jle	.LBB1_46
.LBB1_41:                               # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_43 Depth 2
	movl	(%rsp), %ecx
	testl	%ecx, %ecx
	jle	.LBB1_45
# %bb.42:                               # %.lr.ph133.preheader
                                        #   in Loop: Header=BB1_41 Depth=1
	xorl	%r15d, %r15d
.LBB1_43:                               # %.lr.ph133
                                        #   Parent Loop BB1_41 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	imull	%r12d, %ecx
	movslq	%ecx, %rax
	addq	%r14, %rax
	movzbl	(%r15,%rax), %edi
	movq	%rbx, %rsi
	callq	fputc
	movl	(%rsp), %ecx
	incq	%r15
	cmpl	%ecx, %r15d
	jl	.LBB1_43
# %bb.44:                               # %._crit_edge134.loopexit
                                        #   in Loop: Header=BB1_41 Depth=1
	movl	4(%rsp), %eax
.LBB1_45:                               # %._crit_edge134
                                        #   in Loop: Header=BB1_41 Depth=1
	incl	%r12d
	cmpl	%eax, %r12d
	jl	.LBB1_41
.LBB1_46:                               # %._crit_edge136
	movl	$.Lstr.9, %edi
	callq	puts@PLT
	movl	$.Lstr.10, %edi
	callq	puts@PLT
	movq	%rbx, %rdi
	callq	fclose
	movq	32(%rsp), %rdi
	callq	hipFree
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	%r14, %rdi
	callq	free
	movq	image1_y(%rip), %rdi
	callq	free
	movq	image2_y(%rip), %rdi
	callq	free
	xorl	%eax, %eax
	addq	$648, %rsp                      # imm = 0x288
	.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
.LBB1_1:
	.cfi_def_cfa_offset 704
	movl	$.Lstr.14, %edi
	jmp	.LBB1_2
.LBB1_5:
	movl	$.Lstr.13, %edi
	jmp	.LBB1_2
.LBB1_16:
	movl	$.Lstr.12, %edi
	jmp	.LBB1_2
.LBB1_37:
	movl	$.Lstr.11, %edi
.LBB1_2:
	callq	puts@PLT
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	main, .Lfunc_end1-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	.LBB2_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB2_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z7prewitt3dimPhS0_dd, %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_end2:
	.size	__hip_module_ctor, .Lfunc_end2-__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	.LBB3_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
.LBB3_2:
	retq
.Lfunc_end3:
	.size	__hip_module_dtor, .Lfunc_end3-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	image1_y,@object                # @image1_y
	.bss
	.globl	image1_y
	.p2align	3, 0x0
image1_y:
	.quad	0
	.size	image1_y, 8

	.type	image2_y,@object                # @image2_y
	.globl	image2_y
	.p2align	3, 0x0
image2_y:
	.quad	0
	.size	image2_y, 8

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

	.type	.L.str.4,@object                # @.str.4
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.4:
	.asciz	"Name of input image file? (*.pgm) : "
	.size	.L.str.4, 37

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"%s"
	.size	.L.str.5, 3

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

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"%d %d"
	.size	.L.str.9, 6

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"%d"
	.size	.L.str.10, 3

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"\n     Image width = %d, Image height = %d\n"
	.size	.L.str.11, 43

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"     Maximum gray level = %d\n\n"
	.size	.L.str.12, 31

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Total Size: %d\n\n"
	.size	.L.str.14, 17

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"******** Total Running Time of Kernal = %0.5f sec ********\n "
	.size	.L.str.18, 61

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"Name of output image file? (*.pgm) : "
	.size	.L.str.21, 38

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

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

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"# Created by Image Processing\n"
	.size	.L.str.24, 31

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

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

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z7prewitt3dimPhS0_dd"
	.size	.L__unnamed_1, 22

	.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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"\n-----------------------------------------------------"
	.size	.Lstr, 55

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"Monochromatic image file input routine "
	.size	.Lstr.1, 40

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"     Only pgm binary file is acceptable\n"
	.size	.Lstr.3, 41

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"-----Image data input OK-----\n"
	.size	.Lstr.4, 31

	.type	.Lstr.6,@object                 # @str.6
.Lstr.6:
	.asciz	"-----------------------------------------------------"
	.size	.Lstr.6, 54

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"Monochromatic image file output routine"
	.size	.Lstr.7, 40

	.type	.Lstr.9,@object                 # @str.9
.Lstr.9:
	.asciz	"\n-----Image data output OK-----\n"
	.size	.Lstr.9, 33

	.type	.Lstr.10,@object                # @str.10
.Lstr.10:
	.asciz	"-----------------------------------------------------\n"
	.size	.Lstr.10, 55

	.type	.Lstr.11,@object                # @str.11
.Lstr.11:
	.asciz	"Nothing Exists!!!"
	.size	.Lstr.11, 18

	.type	.Lstr.12,@object                # @str.12
.Lstr.12:
	.asciz	"     Invalid value of maximum gray level!\n"
	.size	.Lstr.12, 43

	.type	.Lstr.13,@object                # @str.13
.Lstr.13:
	.asciz	"     Mistaken file format, not P5!\n"
	.size	.Lstr.13, 36

	.type	.Lstr.14,@object                # @str.14
.Lstr.14:
	.asciz	"     The file doesn't exist!\n"
	.size	.Lstr.14, 30

	.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 _Z22__device_stub__prewitt3dimPhS0_dd
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z7prewitt3dimPhS0_dd
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z7prewitt3dimPhS0_dd   ; -- Begin function _Z7prewitt3dimPhS0_dd
	.globl	_Z7prewitt3dimPhS0_dd
	.p2align	8
	.type	_Z7prewitt3dimPhS0_dd,@function
_Z7prewitt3dimPhS0_dd:                  ; @_Z7prewitt3dimPhS0_dd
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x3c
	s_load_b64 s[8:9], s[0:1], 0x0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_cmp_le_i32 s15, s9
	s_mul_i32 s10, s14, s2
	s_cselect_b32 s2, -1, 0
	v_add_nc_u32_e32 v1, s10, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_cmp_ge_i32_e32 vcc_lo, s8, v1
	s_and_b32 s2, vcc_lo, s2
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_29
; %bb.1:
	s_load_b256 s[0:7], s[0:1], 0x10
	v_and_b32_e32 v2, 0x3ff, v1
	s_mov_b32 s11, 0
	s_mov_b32 s12, exec_lo
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_barrier
	buffer_gl0_inv
	v_cmpx_eq_u32_e32 0, v2
	s_cbranch_execz .LBB0_11
; %bb.2:                                ; %.preheader83
	s_add_i32 s14, s15, -1
	s_sub_i32 s16, s10, s8
	s_mul_i32 s14, s8, s14
	s_mov_b32 s13, -1
	s_add_i32 s14, s14, -1
	s_add_i32 s16, s16, -2
.LBB0_3:                                ; %.preheader82
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	s_add_i32 s17, s13, s15
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_gt_i32 s17, -1
	s_cselect_b32 s18, -1, 0
	s_cmp_lt_i32 s17, s9
	s_cselect_b32 s19, -1, 0
	s_add_i32 s17, s10, s14
	s_and_b32 s18, s18, s19
	s_mov_b32 s19, 0
.LBB0_4:                                ;   Parent Loop BB0_3 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s20, s16, s19
	s_cmp_eq_u32 s20, 0
	s_cbranch_scc1 .LBB0_8
; %bb.5:                                ;   in Loop: Header=BB0_4 Depth=2
	s_add_i32 s20, s10, s19
	v_mov_b32_e32 v2, 0
	s_add_i32 s20, s20, -1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_3) | instid1(SALU_CYCLE_1)
	s_cmp_gt_i32 s20, -1
	s_cselect_b32 s21, -1, 0
	s_cmp_lt_u32 s20, s8
	s_cselect_b32 s20, -1, 0
	s_and_b32 s20, s18, s20
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s20, s21, s20
	s_and_not1_b32 vcc_lo, exec_lo, s20
	s_cbranch_vccnz .LBB0_7
; %bb.6:                                ;   in Loop: Header=BB0_4 Depth=2
	s_add_i32 s20, s17, s19
	s_delay_alu instid0(SALU_CYCLE_1)
	v_mov_b32_e32 v2, s20
	global_load_u8 v2, v2, s[0:1]
.LBB0_7:                                ;   in Loop: Header=BB0_4 Depth=2
	s_add_i32 s20, s11, s19
	s_add_i32 s19, s19, 1
	v_mov_b32_e32 v3, s20
	s_cmpk_eq_i32 s19, 0x402
	s_cselect_b32 s20, -1, 0
	s_waitcnt vmcnt(0)
	ds_store_b8 v3, v2
	s_branch .LBB0_9
.LBB0_8:                                ;   in Loop: Header=BB0_4 Depth=2
	s_mov_b32 s20, -1
                                        ; implicit-def: $sgpr19
.LBB0_9:                                ; %Flow133
                                        ;   in Loop: Header=BB0_4 Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 vcc_lo, exec_lo, s20
	s_cbranch_vccz .LBB0_4
; %bb.10:                               ;   in Loop: Header=BB0_3 Depth=1
	s_add_i32 s13, s13, 1
	s_addk_i32 s11, 0x402
	s_add_i32 s14, s14, s8
	s_cmp_lg_u32 s13, 2
	s_cbranch_scc1 .LBB0_3
.LBB0_11:                               ; %Flow135
	s_or_b32 exec_lo, exec_lo, s12
	s_mov_b32 s16, 0
	s_mov_b32 s0, 1
	s_mov_b32 s17, s16
	s_mov_b32 s18, s16
	s_mov_b32 s19, s16
	v_dual_mov_b32 v2, s16 :: v_dual_mov_b32 v3, s17
	v_dual_mov_b32 v4, s18 :: v_dual_mov_b32 v5, s19
	s_mov_b32 s1, -1
	s_delay_alu instid0(SALU_CYCLE_1)
	v_dual_mov_b32 v8, -1 :: v_dual_mov_b32 v7, s1
	v_cmp_ne_u32_e32 vcc_lo, 0, v1
	v_mov_b32_e32 v6, s0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_clause 0x1
	scratch_store_b128 off, v[2:5], off offset:16
	scratch_store_b128 off, v[2:5], off offset:32
	v_mov_b32_e32 v2, 1
	s_clause 0x3
	scratch_store_b32 off, v8, off offset:16
	scratch_store_b64 off, v[6:7], off offset:24
	scratch_store_b64 off, v[6:7], off offset:36
	scratch_store_b32 off, v2, off offset:48
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_28
; %bb.12:
	s_add_i32 s0, s8, -1
	s_cmp_lg_u32 s15, 0
	v_cmp_ne_u32_e32 vcc_lo, s0, v1
	s_cselect_b32 s0, -1, 0
	s_add_i32 s1, s9, -1
	s_and_b32 s0, s0, vcc_lo
	s_cmp_lg_u32 s15, s1
	s_cselect_b32 s1, -1, 0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s0, s0, s1
	s_and_b32 exec_lo, exec_lo, s0
	s_cbranch_execz .LBB0_28
; %bb.13:                               ; %.preheader81
	v_mov_b32_e32 v4, 0
	v_cmp_eq_u32_e64 s1, 0, v0
	v_cmp_ne_u32_e32 vcc_lo, 0, v0
	v_mov_b32_e32 v5, 0
	v_add_nc_u32_e64 v8, 16, 16
	s_mov_b32 s9, -1
.LBB0_14:                               ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_15 Depth 2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mov_b32 s0, s9
	s_add_i32 s9, s9, 1
	v_mov_b32_e32 v7, v5
	v_mad_u64_u32 v[2:3], null, 0x402, s9, v[0:1]
	v_dual_mov_b32 v3, -1 :: v_dual_mov_b32 v6, v4
	s_mul_i32 s11, s0, 12
	s_mov_b32 s10, 0
.LBB0_15:                               ;   Parent Loop BB0_14 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(VALU_DEP_1)
	v_add3_u32 v10, v2, v3, 0
	s_mov_b32 s0, s1
                                        ; implicit-def: $vgpr4_vgpr5
                                        ; implicit-def: $vgpr9
	s_and_saveexec_b32 s12, vcc_lo
	s_cbranch_execz .LBB0_19
; %bb.16:                               ;   in Loop: Header=BB0_15 Depth=2
	ds_load_u8 v11, v10
	s_mov_b32 s14, -1
                                        ; implicit-def: $vgpr4_vgpr5
                                        ; implicit-def: $vgpr9
	s_waitcnt lgkmcnt(0)
	v_cmp_ne_u16_e64 s0, 0, v11
	s_delay_alu instid0(VALU_DEP_1)
	s_and_saveexec_b32 s13, s0
	s_cbranch_execz .LBB0_18
; %bb.17:                               ;   in Loop: Header=BB0_15 Depth=2
	v_lshlrev_b32_e32 v4, 2, v3
	v_and_b32_e32 v5, 0xffff, v11
	v_add_nc_u32_e32 v9, 1, v3
	s_xor_b32 s14, exec_lo, -1
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_add3_u32 v4, v8, s11, v4
	scratch_load_b32 v4, v4, off
	s_waitcnt vmcnt(0)
	v_mul_lo_u32 v4, v4, v5
	v_cvt_f64_i32_e32 v[4:5], v4
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f64 v[4:5], v[6:7], v[4:5]
.LBB0_18:                               ; %Flow131
                                        ;   in Loop: Header=BB0_15 Depth=2
	s_or_b32 exec_lo, exec_lo, s13
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	s_and_not1_b32 s0, s1, exec_lo
	s_and_b32 s13, s14, exec_lo
	s_or_b32 s0, s0, s13
.LBB0_19:                               ; %Flow130
                                        ;   in Loop: Header=BB0_15 Depth=2
	s_or_b32 exec_lo, exec_lo, s12
	s_and_saveexec_b32 s12, s0
	s_cbranch_execz .LBB0_25
; %bb.20:                               ;   in Loop: Header=BB0_15 Depth=2
	ds_load_u8 v10, v10 offset:1
	v_add_nc_u32_e32 v9, 1, v3
                                        ; implicit-def: $vgpr4_vgpr5
	s_waitcnt lgkmcnt(0)
	v_cmp_ne_u16_e64 s0, 0, v10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s13, s0
	s_xor_b32 s0, exec_lo, s13
	s_cbranch_execz .LBB0_22
; %bb.21:                               ;   in Loop: Header=BB0_15 Depth=2
	v_lshlrev_b32_e32 v4, 2, v3
	v_and_b32_e32 v5, 0xffff, v10
	v_add_nc_u32_e32 v9, 1, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_add3_u32 v4, v8, s11, v4
	scratch_load_b32 v4, v4, off
	s_waitcnt vmcnt(0)
	v_mul_lo_u32 v4, v4, v5
	v_cvt_f64_i32_e32 v[4:5], v4
	s_delay_alu instid0(VALU_DEP_1)
	v_add_f64 v[4:5], v[6:7], v[4:5]
                                        ; implicit-def: $vgpr6_vgpr7
.LBB0_22:                               ; %Flow
                                        ;   in Loop: Header=BB0_15 Depth=2
	s_and_not1_saveexec_b32 s0, s0
; %bb.23:                               ; %._crit_edge
                                        ;   in Loop: Header=BB0_15 Depth=2
	v_dual_mov_b32 v4, v6 :: v_dual_mov_b32 v5, v7
; %bb.24:                               ; %Flow129
                                        ;   in Loop: Header=BB0_15 Depth=2
	s_or_b32 exec_lo, exec_lo, s0
.LBB0_25:                               ;   in Loop: Header=BB0_15 Depth=2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	s_or_b32 exec_lo, exec_lo, s12
	v_cmp_eq_u32_e64 s0, 2, v9
	v_dual_mov_b32 v7, v5 :: v_dual_mov_b32 v6, v4
	v_mov_b32_e32 v3, v9
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s10, s0, s10
	s_and_not1_b32 exec_lo, exec_lo, s10
	s_cbranch_execnz .LBB0_15
; %bb.26:                               ;   in Loop: Header=BB0_14 Depth=1
	s_or_b32 exec_lo, exec_lo, s10
	s_cmp_lg_u32 s9, 2
	s_cbranch_scc1 .LBB0_14
; %bb.27:
	v_add_f64 v[2:3], v[4:5], -s[6:7]
	v_add_f64 v[4:5], s[4:5], -s[6:7]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[2:3], 0x406fe000, v[2:3]
	v_div_scale_f64 v[6:7], null, v[4:5], v[4:5], v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_f64_e32 v[8:9], v[6:7]
	s_waitcnt_depctr 0xfff
	v_fma_f64 v[10:11], -v[6:7], v[8:9], 1.0
	v_fma_f64 v[8:9], v[8:9], v[10:11], v[8:9]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fma_f64 v[10:11], -v[6:7], v[8:9], 1.0
	v_fma_f64 v[8:9], v[8:9], v[10:11], v[8:9]
	v_div_scale_f64 v[10:11], vcc_lo, v[2:3], v[4:5], v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f64 v[12:13], v[10:11], v[8:9]
	v_fma_f64 v[6:7], -v[6:7], v[12:13], v[10:11]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fmas_f64 v[6:7], v[6:7], v[8:9], v[12:13]
	v_div_fixup_f64 v[2:3], v[6:7], v[4:5], v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cvt_i32_f64_e32 v4, v[2:3]
	v_mad_u64_u32 v[2:3], null, s8, s15, v[1:2]
	v_ashrrev_i32_e32 v1, 31, v2
	v_add_co_u32 v0, vcc_lo, s2, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	global_store_b8 v[0:1], v4, off
.LBB0_28:
	s_waitcnt_vscnt null, 0x0
	s_barrier
	buffer_gl0_inv
.LBB0_29:
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z7prewitt3dimPhS0_dd
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 64
		.amdhsa_kernarg_size 304
		.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 1
		.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 0
		.amdhsa_next_free_vgpr 14
		.amdhsa_next_free_sgpr 22
		.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	_Z7prewitt3dimPhS0_dd, .Lfunc_end0-_Z7prewitt3dimPhS0_dd
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1156
; NumSgprs: 24
; NumVgprs: 14
; ScratchSize: 64
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 24
; NumVGPRsForWavesPerEU: 14
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 1
; 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: 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:
      - .offset:         0
        .size:           16
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         24
        .size:           8
        .value_kind:     global_buffer
      - .offset:         32
        .size:           8
        .value_kind:     by_value
      - .offset:         40
        .size:           8
        .value_kind:     by_value
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         52
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         56
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         60
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         62
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         64
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         66
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         68
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         70
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         104
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         112
        .size:           2
        .value_kind:     hidden_grid_dims
      - .offset:         168
        .size:           4
        .value_kind:     hidden_dynamic_lds_size
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 304
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z7prewitt3dimPhS0_dd
    .private_segment_fixed_size: 64
    .sgpr_count:     24
    .sgpr_spill_count: 0
    .symbol:         _Z7prewitt3dimPhS0_dd.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     14
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
