#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include "string.h"
#include <sys/time.h>
#include <time.h>

#define DEFAULT_THRESHOLD  4000
#define DEFAULT_FILENAME "ansel3.ppm"

unsigned int* read_ppm(char* filename, int* image_width, int* image_height, int* maxval);
void write_ppm(char* filename, int image_width, int image_height, int maxval, int* pic);
__global__ void sobel(int* input_image, int* output_image, int image_width, int image_height, int thresh);

int main( int argc, char **argv )
{
	int thresh = DEFAULT_THRESHOLD;
	char *filename;
	filename = strdup( DEFAULT_FILENAME);

	if (argc > 1) 
	{
		if (argc == 3)  
		{ // filename AND threshold
			filename = strdup( argv[1]);
			thresh = atoi( argv[2] );
		}
		if (argc == 2) 
		{ // default file but specified threshhold
			thresh = atoi( argv[1] );
		}
		fprintf(stderr, "file %s    threshold %d\n", filename, thresh);
	}


	int image_width, image_height, maxval;
	unsigned int *host_input_image = read_ppm( filename, &image_width, &image_height, &maxval );

	int numbytes =  image_width * image_height * sizeof(int);
	int *host_result = (int *) malloc(numbytes);
	if (!host_result) 
	{
		fprintf(stderr, "sobel() unable to malloc %d bytes\n", numbytes);
		exit(-1); // fail
	}
	
	int* device_input_image;
	int* device_result;
	
	cudaMalloc(&device_input_image, numbytes);
	cudaMalloc(&device_result, numbytes);	
	
	cudaMemcpy(device_input_image, host_input_image, numbytes, cudaMemcpyHostToDevice);
	
	dim3 threads_per_block(32, 32);
	dim3 blocks_per_grid(ceil(image_width/32.0), ceil(image_height/32.0));
	
	float elapsed_time;
	cudaEvent_t start_event, stop_event;
	cudaEventCreate(&start_event);
	cudaEventCreate(&stop_event);
	cudaEventRecord(start_event, 0);

	sobel<<<blocks_per_grid, threads_per_block>>>(device_input_image, device_result, image_width, image_height, thresh);
	
	cudaEventRecord(stop_event, 0);
	cudaEventSynchronize(stop_event);
	cudaEventElapsedTime(&elapsed_time, start_event, stop_event);

	cudaMemcpy(host_result, device_result, numbytes, cudaMemcpyDeviceToHost);
	
	cudaFree(device_input_image);
	cudaFree(device_result);
	
	write_ppm( "result.ppm", image_width, image_height, 255, host_result);

	printf("Elapsed time: %f milliseconds\n", elapsed_time);
	
	return 0;
}

unsigned int *read_ppm( char *filename, int * image_width, int * image_height, int *maxval ){

  if ( !filename || filename[0] == '\0') {
    fprintf(stderr, "read_ppm but no file name\n");
    return NULL;  // fail
  }

  FILE *fp;

  fprintf(stderr, "read_ppm( %s )\n", filename);
  fp = fopen( filename, "rb");
  if (!fp)
    {
      fprintf(stderr, "read_ppm()    ERROR  file '%s' cannot be opened for reading\n", filename);
      return NULL; // fail

    }

  char chars[1024];
  //int num = read(fd, chars, 1000);
  int num = fread(chars, sizeof(char), 1000, fp);

  if (chars[0] != 'P' || chars[1] != '6')
    {
      fprintf(stderr, "Texture::Texture()    ERROR  file '%s' does not start with \"P6\"  I am expecting a binary PPM file\n", filename);
      return NULL;
    }

  unsigned int width, height, maxvalue;


  char *ptr = chars+3; // P 6 newline
  if (*ptr == '#') // comment line!
    {
      ptr = 1 + strstr(ptr, "\n");
    }

  num = sscanf(ptr, "%d\n%d\n%d",  &width, &height, &maxvalue);
  fprintf(stderr, "read %d things   width %d  height %d  maxval %d\n", num, width, height, maxvalue);
  *image_width = width;
  *image_height = height;
  *maxval = maxvalue;

  unsigned int *pic = (unsigned int *)malloc( width * height * sizeof(unsigned int));
  if (!pic) {
    fprintf(stderr, "read_ppm()  unable to allocate %d x %d unsigned ints for the picture\n", width, height);
    return NULL; // fail but return
  }

  // allocate buffer to read the rest of the file into
  int bufsize =  3 * width * height * sizeof(unsigned char);
  if ((*maxval) > 255) bufsize *= 2;
  unsigned char *buf = (unsigned char *)malloc( bufsize );
  if (!buf) {
    fprintf(stderr, "read_ppm()  unable to allocate %d bytes of read buffer\n", bufsize);
    return NULL; // fail but return
  }
  
  // TODO really read
  char duh[80];
  char *line = chars;

  // find the start of the pixel data.   no doubt stupid
  sprintf(duh, "%d\0", *image_width);
  line = strstr(line, duh);
  //fprintf(stderr, "%s found at offset %d\n", duh, line-chars);
  line += strlen(duh) + 1;

  sprintf(duh, "%d\0", *image_height);
  line = strstr(line, duh);
  //fprintf(stderr, "%s found at offset %d\n", duh, line-chars);
  line += strlen(duh) + 1;

  sprintf(duh, "%d\0", *maxval);
  line = strstr(line, duh);


  fprintf(stderr, "%s found at offset %d\n", duh, line - chars);
  line += strlen(duh) + 1;

  long offset = line - chars;
  //lseek(fd, offset, SEEK_SET); // move to the correct offset
  fseek(fp, offset, SEEK_SET); // move to the correct offset
  //long numread = read(fd, buf, bufsize);
  long numread = fread(buf, sizeof(char), bufsize, fp);
  fprintf(stderr, "Texture %s   read %ld of %ld bytes\n", filename, numread, bufsize);

  fclose(fp);


  int pixels = (*image_width) * (*image_height);
  int i;
  for (i=0; i<pixels; i++) pic[i] = (int) buf[3*i];  // red channel

  return pic; // success
}

void write_ppm( char *filename, int image_width, int image_height, int maxval, int *pic)
{
  FILE *fp;

  fp = fopen(filename, "w");
  if (!fp)
    {
      fprintf(stderr, "FAILED TO OPEN FILE '%s' for writing\n");
      exit(-1);
    }

  fprintf(fp, "P6\n");
  fprintf(fp,"%d %d\n%d\n", image_width, image_height, maxval);

  int numpix = image_width * image_height;
  int i;
  for (i=0; i<numpix; i++) {
    unsigned char uc = (unsigned char) pic[i];
    fprintf(fp, "%c%c%c", uc, uc, uc);
  }
  fclose(fp);

}

__global__ void sobel(int* input_image, int* output_image, int image_width, int image_height, int thresh)
{	
	int j = blockIdx.x * blockDim.x + threadIdx.x + 1;
	int i = blockIdx.y * blockDim.y + threadIdx.y + 1;

	if( (i < (image_height-1)) && (j < (image_width-1)) )
	{
		int sum1, sum2, magnitude;
	
		int offset = i*image_width + j;

		sum1 =  input_image[ image_width * (i-1) + j+1 ] -     input_image[ image_width*(i-1) + j-1 ]
		+ 2 * input_image[ image_width * (i)   + j+1 ] - 2 * input_image[ image_width*(i)   + j-1 ]
		+     input_image[ image_width * (i+1) + j+1 ] -     input_image[ image_width*(i+1) + j-1 ];

		sum2 = input_image[ image_width * (i-1) + j-1 ] + 2 * input_image[ image_width * (i-1) + j ]  + input_image[ image_width * (i-1) + j+1 ]
		- input_image[image_width * (i+1) + j-1 ] - 2 * input_image[ image_width * (i+1) + j ] - input_image[ image_width * (i+1) + j+1 ];

		magnitude =  sum1*sum1 + sum2*sum2;

		if (magnitude > thresh)
			output_image[offset] = 255;
		else
			output_image[offset] = 0;
	}
}

	.file	"tmpxft_002dd8a7_00000000-6_sobel_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2036:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2036:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"read_ppm but no file name\n"
.LC1:
	.string	"read_ppm( %s )\n"
.LC2:
	.string	"rb"
.LC3:
	.string	"read_ppm()    ERROR  file '%s' cannot be opened for reading\n"
.LC4:
	.string	"Texture::Texture()    ERROR  file '%s' does not start with \"P6\"  I am expecting a binary PPM file\n"
.LC5:
	.string	"%d\n%d\n%d"
.LC6:
	.string	"read %d things   width %d  height %d  maxval %d\n"
.LC7:
	.string	"read_ppm()  unable to allocate %d x %d unsigned ints for the picture\n"
.LC8:
	.string	"read_ppm()  unable to allocate %d bytes of read buffer\n"
	.section	.rodata
.LC9:
	.string	"%d"
	.string	""
	.section	.rodata.str1.1
.LC10:
	.string	"%s found at offset %d\n"
.LC11:
	.string	"Texture %s   read %ld of %ld bytes\n"
	.text
	.globl	_Z8read_ppmPcPiS0_S0_
	.type	_Z8read_ppmPcPiS0_S0_, @function
_Z8read_ppmPcPiS0_S0_:
.LFB2032:
	.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	$1208, %rsp
	.cfi_def_cfa_offset 1264
	movq	%rdi, 16(%rsp)
	movq	stderr(%rip), %rdi
	movq	%fs:40, %rax
	movq	%rax, 1192(%rsp)
	xorl	%eax, %eax
	cmpq	$0, 16(%rsp)
	je	.L3
	movq	16(%rsp), %rax
	cmpb	$0, (%rax)
	jne	.L4
.L3:
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L5
.L4:
	movq	%rcx, %r14
	movq	16(%rsp), %rcx
	movq	%rsi, %r13
	movq	%rdx, %r12
	movl	$2, %esi
	leaq	.LC1(%rip), %rdx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	16(%rsp), %rdi
	leaq	.LC2(%rip), %rsi
	call	fopen@PLT
	movq	%rax, 24(%rsp)
	testq	%rax, %rax
	jne	.L6
	movq	16(%rsp), %rcx
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC3(%rip), %rdx
.L22:
	call	__fprintf_chk@PLT
.L5:
	xorl	%ebx, %ebx
	jmp	.L2
.L6:
	movq	24(%rsp), %r8
	movl	$1000, %ecx
	movl	$1, %edx
	leaq	168(%rsp), %rdi
	movl	$1024, %esi
	call	__fread_chk@PLT
	cmpb	$80, 168(%rsp)
	jne	.L8
	cmpb	$54, 169(%rsp)
	je	.L9
.L8:
	movq	16(%rsp), %rcx
	movq	stderr(%rip), %rdi
	leaq	.LC4(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	jmp	.L22
.L9:
	cmpb	$35, 171(%rsp)
	leaq	171(%rsp), %rdi
	jne	.L10
	movl	$10, %esi
	call	strchr@PLT
	leaq	1(%rax), %rdi
.L10:
	leaq	80(%rsp), %rcx
	leaq	76(%rsp), %rdx
	xorl	%eax, %eax
	leaq	84(%rsp), %r8
	leaq	.LC5(%rip), %rsi
	call	__isoc23_sscanf@PLT
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	leaq	.LC6(%rip), %rdx
	movl	%eax, %ecx
	pushq	%rax
	.cfi_def_cfa_offset 1272
	movl	92(%rsp), %eax
	pushq	%rax
	.cfi_def_cfa_offset 1280
	movl	96(%rsp), %r9d
	xorl	%eax, %eax
	movl	92(%rsp), %r8d
	call	__fprintf_chk@PLT
	movl	92(%rsp), %ebp
	movl	96(%rsp), %r8d
	movl	100(%rsp), %eax
	movl	%ebp, %r15d
	movl	%ebp, 0(%r13)
	imull	%r8d, %r15d
	movl	%r8d, (%r12)
	movl	%eax, (%r14)
	movl	%r8d, 24(%rsp)
	movl	%r15d, %edi
	salq	$2, %rdi
	call	malloc@PLT
	popq	%rdx
	.cfi_def_cfa_offset 1272
	popq	%rcx
	.cfi_def_cfa_offset 1264
	testq	%rax, %rax
	movl	8(%rsp), %r8d
	movq	%rax, %rbx
	jne	.L11
	movq	stderr(%rip), %rdi
	movl	%ebp, %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC7(%rip), %rdx
	call	__fprintf_chk@PLT
	jmp	.L5
.L11:
	cmpl	$255, (%r14)
	leal	(%r15,%r15,2), %r9d
	jle	.L12
	addl	%r9d, %r9d
.L12:
	movslq	%r9d, %rax
	movl	%r9d, 8(%rsp)
	movq	%rax, %rdi
	movq	%rax, 32(%rsp)
	call	malloc@PLT
	movl	8(%rsp), %r9d
	testq	%rax, %rax
	movq	%rax, %rbp
	jne	.L13
	movq	stderr(%rip), %rdi
	movl	%r9d, %ecx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC8(%rip), %rdx
	call	__fprintf_chk@PLT
	jmp	.L5
.L13:
	movl	0(%r13), %r8d
	leaq	88(%rsp), %rax
	movl	$80, %edx
	leaq	.LC9(%rip), %rcx
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movl	$2, %esi
	movl	%r9d, 60(%rsp)
	leaq	168(%rsp), %r15
	call	__sprintf_chk@PLT
	movq	8(%rsp), %rsi
	movq	%r15, %rdi
	call	strstr@PLT
	movq	8(%rsp), %rdi
	movq	%rax, 48(%rsp)
	call	strlen@PLT
	movl	(%r12), %r8d
	movq	8(%rsp), %rdi
	leaq	.LC9(%rip), %rcx
	movl	$80, %edx
	movq	%rax, 40(%rsp)
	movl	$2, %esi
	xorl	%eax, %eax
	call	__sprintf_chk@PLT
	movq	40(%rsp), %r10
	movq	48(%rsp), %r11
	movq	8(%rsp), %rsi
	leaq	1(%r11,%r10), %rdi
	call	strstr@PLT
	movq	8(%rsp), %rdi
	movq	%rax, 48(%rsp)
	call	strlen@PLT
	movl	(%r14), %r8d
	movq	8(%rsp), %rdi
	leaq	.LC9(%rip), %rcx
	movl	$80, %edx
	movq	%rax, 40(%rsp)
	movl	$2, %esi
	xorl	%eax, %eax
	call	__sprintf_chk@PLT
	movq	40(%rsp), %r10
	movq	48(%rsp), %r11
	movq	8(%rsp), %rsi
	leaq	1(%r11,%r10), %rdi
	call	strstr@PLT
	movq	8(%rsp), %rcx
	movl	$2, %esi
	movq	stderr(%rip), %rdi
	movq	%rax, %r8
	leaq	.LC10(%rip), %rdx
	movq	%rax, %r14
	xorl	%eax, %eax
	subq	%r15, %r8
	call	__fprintf_chk@PLT
	movq	8(%rsp), %rdi
	call	strlen@PLT
	movq	24(%rsp), %rdi
	xorl	%edx, %edx
	leaq	1(%r14,%rax), %rsi
	subq	%r15, %rsi
	call	fseek@PLT
	movq	32(%rsp), %rcx
	movq	24(%rsp), %r8
	movq	%rbp, %rdi
	movl	$1, %edx
	movq	%rcx, %rsi
	call	__fread_chk@PLT
	movl	60(%rsp), %r9d
	movq	16(%rsp), %rcx
	leaq	.LC11(%rip), %rdx
	movq	stderr(%rip), %rdi
	movq	%rax, %r8
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	24(%rsp), %rdi
	call	fclose@PLT
	movl	0(%r13), %edx
	imull	(%r12), %edx
	xorl	%eax, %eax
.L14:
	cmpl	%eax, %edx
	jle	.L2
	leaq	(%rax,%rax,2), %rcx
	movzbl	0(%rbp,%rcx), %ecx
	movl	%ecx, (%rbx,%rax,4)
	incq	%rax
	jmp	.L14
.L2:
	movq	1192(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	addq	$1208, %rsp
	.cfi_def_cfa_offset 56
	movq	%rbx, %rax
	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
.LFE2032:
	.size	_Z8read_ppmPcPiS0_S0_, .-_Z8read_ppmPcPiS0_S0_
	.section	.rodata.str1.1
.LC12:
	.string	"w"
.LC13:
	.string	"FAILED TO OPEN FILE '%s' for writing\n"
.LC14:
	.string	"P6\n"
.LC15:
	.string	"%d %d\n%d\n"
.LC16:
	.string	"%c%c%c"
	.text
	.globl	_Z9write_ppmPciiiPi
	.type	_Z9write_ppmPciiiPi, @function
_Z9write_ppmPciiiPi:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movl	%ecx, %r14d
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%r8, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movl	%esi, %ebp
	leaq	.LC12(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L25
	movq	stderr(%rip), %rdi
	leaq	.LC13(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L25:
	movq	%rax, %rdi
	movq	%rax, %rbx
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC14(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	%r14d, %r9d
	movl	%r12d, %r8d
	movl	%ebp, %ecx
	leaq	.LC15(%rip), %rdx
	movl	$2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	imull	%r12d, %ebp
	leaq	.LC16(%rip), %r14
	xorl	%r12d, %r12d
.L26:
	cmpl	%r12d, %ebp
	jle	.L29
	movzbl	0(%r13,%r12,4), %ecx
	movq	%r14, %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	movq	%rbx, %rdi
	incq	%r12
	movl	%ecx, %r9d
	movl	%ecx, %r8d
	call	__fprintf_chk@PLT
	jmp	.L26
.L29:
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE2033:
	.size	_Z9write_ppmPciiiPi, .-_Z9write_ppmPciiiPi
	.globl	_Z29__device_stub__Z5sobelPiS_iiiPiS_iii
	.type	_Z29__device_stub__Z5sobelPiS_iiiPiS_iii, @function
_Z29__device_stub__Z5sobelPiS_iiiPiS_iii:
.LFB2058:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, 4(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L30
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z5sobelPiS_iii(%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
.L30:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
	call	__stack_chk_fail@PLT
.L32:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2058:
	.size	_Z29__device_stub__Z5sobelPiS_iiiPiS_iii, .-_Z29__device_stub__Z5sobelPiS_iiiPiS_iii
	.globl	_Z5sobelPiS_iii
	.type	_Z5sobelPiS_iii, @function
_Z5sobelPiS_iii:
.LFB2059:
	.cfi_startproc
	endbr64
	jmp	_Z29__device_stub__Z5sobelPiS_iiiPiS_iii
	.cfi_endproc
.LFE2059:
	.size	_Z5sobelPiS_iii, .-_Z5sobelPiS_iii
	.section	.rodata.str1.1
.LC17:
	.string	"ansel3.ppm"
.LC18:
	.string	"file %s    threshold %d\n"
.LC19:
	.string	"sobel() unable to malloc %d bytes\n"
.LC21:
	.string	"result.ppm"
.LC22:
	.string	"Elapsed time: %f milliseconds\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
	movl	%edi, %r13d
	leaq	.LC17(%rip), %rdi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rsi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	$4000, %ebx
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	call	strdup@PLT
	movq	%rax, %rbp
	cmpl	$1, %r13d
	jle	.L36
	cmpl	$3, %r13d
	jne	.L37
	movq	8(%r12), %rdi
	call	strdup@PLT
	movq	16(%r12), %rdi
	movq	%rax, %rbp
	jmp	.L45
.L37:
	cmpl	$2, %r13d
	jne	.L38
	movq	8(%r12), %rdi
.L45:
	call	atoi@PLT
	movl	%eax, %ebx
.L38:
	movq	stderr(%rip), %rdi
	movl	%ebx, %r8d
	movq	%rbp, %rcx
	xorl	%eax, %eax
	leaq	.LC18(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
.L36:
	movq	%rbp, %rdi
	leaq	24(%rsp), %rcx
	leaq	20(%rsp), %rdx
	leaq	16(%rsp), %rsi
	call	_Z8read_ppmPcPiS0_S0_
	movl	16(%rsp), %r13d
	movl	20(%rsp), %r14d
	movq	%rax, %r15
	movl	%r13d, %ecx
	imull	%r14d, %ecx
	sall	$2, %ecx
	movslq	%ecx, %r12
	movl	%ecx, 12(%rsp)
	movq	%r12, %rdi
	call	malloc@PLT
	movl	12(%rsp), %ecx
	testq	%rax, %rax
	movq	%rax, %rbp
	jne	.L39
	movq	stderr(%rip), %rdi
	leaq	.LC19(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	orl	$-1, %edi
	call	exit@PLT
.L39:
	leaq	32(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leaq	40(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	movq	32(%rsp), %rdi
	movq	%r12, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	cvtsi2sdl	%r14d, %xmm0
	mulsd	.LC20(%rip), %xmm0
	movabsq	$137438953504, %rax
	movq	%rax, 64(%rsp)
	movl	$1, 72(%rsp)
	call	ceil@PLT
	movq	%xmm0, %r15
	cvtsi2sdl	%r13d, %xmm0
	mulsd	.LC20(%rip), %xmm0
	call	ceil@PLT
	movq	%r15, %xmm1
	leaq	48(%rsp), %rdi
	movl	$1, 84(%rsp)
	cvttsd2siq	%xmm0, %rax
	movl	%eax, 76(%rsp)
	cvttsd2siq	%xmm1, %rax
	movl	%eax, 80(%rsp)
	call	cudaEventCreate@PLT
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	72(%rsp), %ecx
	movl	84(%rsp), %esi
	xorl	%r9d, %r9d
	movq	64(%rsp), %rdx
	movq	76(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L40
	movq	40(%rsp), %rsi
	movq	32(%rsp), %rdi
	movl	%ebx, %r8d
	movl	%r14d, %ecx
	movl	%r13d, %edx
	call	_Z29__device_stub__Z5sobelPiS_iiiPiS_iii
.L40:
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	28(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	40(%rsp), %rsi
	movq	%r12, %rdx
	movq	%rbp, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movl	%r13d, %esi
	movq	%rbp, %r8
	movl	$255, %ecx
	leaq	.LC21(%rip), %rdi
	movl	%r14d, %edx
	call	_Z9write_ppmPciiiPi
	leaq	.LC22(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	28(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L41
	call	__stack_chk_fail@PLT
.L41:
	addq	$104, %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
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC23:
	.string	"_Z5sobelPiS_iii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2061:
	.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	.LC23(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z5sobelPiS_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
.LFE2061:
	.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.cst8,"aM",@progbits,8
	.align 8
.LC20:
	.long	0
	.long	1067450368
	.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 <fcntl.h>
#include "string.h"
#include <sys/time.h>
#include <time.h>

#define DEFAULT_THRESHOLD  4000
#define DEFAULT_FILENAME "ansel3.ppm"

unsigned int* read_ppm(char* filename, int* image_width, int* image_height, int* maxval);
void write_ppm(char* filename, int image_width, int image_height, int maxval, int* pic);
__global__ void sobel(int* input_image, int* output_image, int image_width, int image_height, int thresh);

int main( int argc, char **argv )
{
	int thresh = DEFAULT_THRESHOLD;
	char *filename;
	filename = strdup( DEFAULT_FILENAME);

	if (argc > 1) 
	{
		if (argc == 3)  
		{ // filename AND threshold
			filename = strdup( argv[1]);
			thresh = atoi( argv[2] );
		}
		if (argc == 2) 
		{ // default file but specified threshhold
			thresh = atoi( argv[1] );
		}
		fprintf(stderr, "file %s    threshold %d\n", filename, thresh);
	}


	int image_width, image_height, maxval;
	unsigned int *host_input_image = read_ppm( filename, &image_width, &image_height, &maxval );

	int numbytes =  image_width * image_height * sizeof(int);
	int *host_result = (int *) malloc(numbytes);
	if (!host_result) 
	{
		fprintf(stderr, "sobel() unable to malloc %d bytes\n", numbytes);
		exit(-1); // fail
	}
	
	int* device_input_image;
	int* device_result;
	
	hipMalloc(&device_input_image, numbytes);
	hipMalloc(&device_result, numbytes);	
	
	hipMemcpy(device_input_image, host_input_image, numbytes, hipMemcpyHostToDevice);
	
	dim3 threads_per_block(32, 32);
	dim3 blocks_per_grid(ceil(image_width/32.0), ceil(image_height/32.0));
	
	float elapsed_time;
	hipEvent_t start_event, stop_event;
	hipEventCreate(&start_event);
	hipEventCreate(&stop_event);
	hipEventRecord(start_event, 0);

	sobel<<<blocks_per_grid, threads_per_block>>>(device_input_image, device_result, image_width, image_height, thresh);
	
	hipEventRecord(stop_event, 0);
	hipEventSynchronize(stop_event);
	hipEventElapsedTime(&elapsed_time, start_event, stop_event);

	hipMemcpy(host_result, device_result, numbytes, hipMemcpyDeviceToHost);
	
	hipFree(device_input_image);
	hipFree(device_result);
	
	write_ppm( "result.ppm", image_width, image_height, 255, host_result);

	printf("Elapsed time: %f milliseconds\n", elapsed_time);
	
	return 0;
}

unsigned int *read_ppm( char *filename, int * image_width, int * image_height, int *maxval ){

  if ( !filename || filename[0] == '\0') {
    fprintf(stderr, "read_ppm but no file name\n");
    return NULL;  // fail
  }

  FILE *fp;

  fprintf(stderr, "read_ppm( %s )\n", filename);
  fp = fopen( filename, "rb");
  if (!fp)
    {
      fprintf(stderr, "read_ppm()    ERROR  file '%s' cannot be opened for reading\n", filename);
      return NULL; // fail

    }

  char chars[1024];
  //int num = read(fd, chars, 1000);
  int num = fread(chars, sizeof(char), 1000, fp);

  if (chars[0] != 'P' || chars[1] != '6')
    {
      fprintf(stderr, "Texture::Texture()    ERROR  file '%s' does not start with \"P6\"  I am expecting a binary PPM file\n", filename);
      return NULL;
    }

  unsigned int width, height, maxvalue;


  char *ptr = chars+3; // P 6 newline
  if (*ptr == '#') // comment line!
    {
      ptr = 1 + strstr(ptr, "\n");
    }

  num = sscanf(ptr, "%d\n%d\n%d",  &width, &height, &maxvalue);
  fprintf(stderr, "read %d things   width %d  height %d  maxval %d\n", num, width, height, maxvalue);
  *image_width = width;
  *image_height = height;
  *maxval = maxvalue;

  unsigned int *pic = (unsigned int *)malloc( width * height * sizeof(unsigned int));
  if (!pic) {
    fprintf(stderr, "read_ppm()  unable to allocate %d x %d unsigned ints for the picture\n", width, height);
    return NULL; // fail but return
  }

  // allocate buffer to read the rest of the file into
  int bufsize =  3 * width * height * sizeof(unsigned char);
  if ((*maxval) > 255) bufsize *= 2;
  unsigned char *buf = (unsigned char *)malloc( bufsize );
  if (!buf) {
    fprintf(stderr, "read_ppm()  unable to allocate %d bytes of read buffer\n", bufsize);
    return NULL; // fail but return
  }
  
  // TODO really read
  char duh[80];
  char *line = chars;

  // find the start of the pixel data.   no doubt stupid
  sprintf(duh, "%d\0", *image_width);
  line = strstr(line, duh);
  //fprintf(stderr, "%s found at offset %d\n", duh, line-chars);
  line += strlen(duh) + 1;

  sprintf(duh, "%d\0", *image_height);
  line = strstr(line, duh);
  //fprintf(stderr, "%s found at offset %d\n", duh, line-chars);
  line += strlen(duh) + 1;

  sprintf(duh, "%d\0", *maxval);
  line = strstr(line, duh);


  fprintf(stderr, "%s found at offset %d\n", duh, line - chars);
  line += strlen(duh) + 1;

  long offset = line - chars;
  //lseek(fd, offset, SEEK_SET); // move to the correct offset
  fseek(fp, offset, SEEK_SET); // move to the correct offset
  //long numread = read(fd, buf, bufsize);
  long numread = fread(buf, sizeof(char), bufsize, fp);
  fprintf(stderr, "Texture %s   read %ld of %ld bytes\n", filename, numread, bufsize);

  fclose(fp);


  int pixels = (*image_width) * (*image_height);
  int i;
  for (i=0; i<pixels; i++) pic[i] = (int) buf[3*i];  // red channel

  return pic; // success
}

void write_ppm( char *filename, int image_width, int image_height, int maxval, int *pic)
{
  FILE *fp;

  fp = fopen(filename, "w");
  if (!fp)
    {
      fprintf(stderr, "FAILED TO OPEN FILE '%s' for writing\n");
      exit(-1);
    }

  fprintf(fp, "P6\n");
  fprintf(fp,"%d %d\n%d\n", image_width, image_height, maxval);

  int numpix = image_width * image_height;
  int i;
  for (i=0; i<numpix; i++) {
    unsigned char uc = (unsigned char) pic[i];
    fprintf(fp, "%c%c%c", uc, uc, uc);
  }
  fclose(fp);

}

__global__ void sobel(int* input_image, int* output_image, int image_width, int image_height, int thresh)
{	
	int j = blockIdx.x * blockDim.x + threadIdx.x + 1;
	int i = blockIdx.y * blockDim.y + threadIdx.y + 1;

	if( (i < (image_height-1)) && (j < (image_width-1)) )
	{
		int sum1, sum2, magnitude;
	
		int offset = i*image_width + j;

		sum1 =  input_image[ image_width * (i-1) + j+1 ] -     input_image[ image_width*(i-1) + j-1 ]
		+ 2 * input_image[ image_width * (i)   + j+1 ] - 2 * input_image[ image_width*(i)   + j-1 ]
		+     input_image[ image_width * (i+1) + j+1 ] -     input_image[ image_width*(i+1) + j-1 ];

		sum2 = input_image[ image_width * (i-1) + j-1 ] + 2 * input_image[ image_width * (i-1) + j ]  + input_image[ image_width * (i-1) + j+1 ]
		- input_image[image_width * (i+1) + j-1 ] - 2 * input_image[ image_width * (i+1) + j ] - input_image[ image_width * (i+1) + j+1 ];

		magnitude =  sum1*sum1 + sum2*sum2;

		if (magnitude > thresh)
			output_image[offset] = 255;
		else
			output_image[offset] = 0;
	}
}

	.text
	.file	"sobel_cuda.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x3fa0000000000000              # double 0.03125
	.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	$56, %rsp
	.cfi_def_cfa_offset 112
	.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	%rsi, %r15
	movl	%edi, %ebp
	movl	$.L.str, %edi
	callq	strdup
	movq	%rax, %r14
	movl	$4000, %ebx                     # imm = 0xFA0
	cmpl	$2, %ebp
	jge	.LBB0_1
.LBB0_7:
	leaq	52(%rsp), %r15
	leaq	48(%rsp), %r12
	leaq	44(%rsp), %rcx
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	callq	_Z8read_ppmPcPiS0_S0_
	movq	%rax, %r13
	movl	(%r15), %r15d
	movl	(%r12), %r14d
	movl	%r15d, %ebp
	imull	%r14d, %ebp
	shll	$2, %ebp
	movslq	%ebp, %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	testq	%rax, %rax
	je	.LBB0_11
# %bb.8:
	leaq	16(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movq	(%rbp), %rdi
	movq	%r13, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	cvtsi2sd	%r15d, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	callq	ceil@PLT
	cvttsd2si	%xmm0, %rbp
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r14d, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	callq	ceil@PLT
	cvttsd2si	%xmm0, %r13
	movl	%ebp, %eax
	shlq	$32, %r13
	orq	%rax, %r13
	leaq	32(%rsp), %rbp
	movq	%rbp, %rdi
	callq	hipEventCreate
	movq	%rsp, %rdi
	callq	hipEventCreate
	movq	(%rbp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movq	%r13, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB0_10
# %bb.9:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	%r15d, %edx
	movl	%r14d, %ecx
	movl	%ebx, %r8d
	callq	_Z20__device_stub__sobelPiS_iii
.LBB0_10:
	movq	(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	(%rsp), %rdi
	callq	hipEventSynchronize
	movq	32(%rsp), %rsi
	movq	(%rsp), %rdx
	leaq	40(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	movq	8(%rsp), %rsi
	movq	24(%rsp), %r13                  # 8-byte Reload
	movq	%r13, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movl	$.L.str.3, %edi
	movl	%r15d, %esi
	movl	%r14d, %edx
	movl	$255, %ecx
	movq	%r13, %r8
	callq	_Z9write_ppmPciiiPi
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx), %xmm0
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	xorl	%eax, %eax
	addq	$56, %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
.LBB0_1:
	.cfi_def_cfa_offset 112
	je	.LBB0_2
# %bb.3:
	cmpl	$3, %ebp
	jne	.LBB0_6
# %bb.4:                                # %.thread
	movq	8(%r15), %rdi
	callq	strdup
	movq	%rax, %r14
	movl	$2, %eax
	jmp	.LBB0_5
.LBB0_2:
	movl	$1, %eax
.LBB0_5:                                # %.sink.split
	movq	(%r15,%rax,8), %rdi
	callq	atoi
	movl	%eax, %ebx
.LBB0_6:
	movq	stderr(%rip), %rdi
	movl	$.L.str.1, %esi
	movq	%r14, %rdx
	movl	%ebx, %ecx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB0_7
.LBB0_11:
	movq	stderr(%rip), %rdi
	movl	$.L.str.2, %esi
	movl	%ebp, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$-1, %edi
	callq	exit
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z8read_ppmPcPiS0_S0_           # -- Begin function _Z8read_ppmPcPiS0_S0_
	.type	_Z8read_ppmPcPiS0_S0_,@function
_Z8read_ppmPcPiS0_S0_:                  # @_Z8read_ppmPcPiS0_S0_
	.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	$1160, %rsp                     # imm = 0x488
	.cfi_def_cfa_offset 1216
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	testq	%rdi, %rdi
	je	.LBB1_2
# %bb.1:
	movq	%rdi, %rbx
	cmpb	$0, (%rdi)
	jne	.LBB1_3
.LBB1_2:
	movq	stderr(%rip), %rcx
	movl	$.L.str.5, %edi
	movl	$26, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	xorl	%r12d, %r12d
.LBB1_18:
	movq	%r12, %rax
	addq	$1160, %rsp                     # imm = 0x488
	.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_3:
	.cfi_def_cfa_offset 1216
	movq	%rcx, %rbp
	movq	%rdx, %r14
	movq	%rsi, %r15
	movq	stderr(%rip), %rdi
	movl	$.L.str.6, %esi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.7, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	jne	.LBB1_6
# %bb.4:
	movq	stderr(%rip), %rdi
	xorl	%r12d, %r12d
	movl	$.L.str.8, %esi
	jmp	.LBB1_5
.LBB1_6:
	movq	%rax, %r12
	leaq	128(%rsp), %r13
	movl	$1, %esi
	movl	$1000, %edx                     # imm = 0x3E8
	movq	%r13, %rdi
	movq	%rax, %rcx
	callq	fread
	cmpb	$80, (%r13)
	jne	.LBB1_8
# %bb.7:
	cmpb	$54, 129(%rsp)
	jne	.LBB1_8
# %bb.9:
	leaq	131(%rsp), %rdi
	cmpb	$35, (%rdi)
	movq	%r14, 8(%rsp)                   # 8-byte Spill
	movq	%r12, 32(%rsp)                  # 8-byte Spill
	jne	.LBB1_11
# %bb.10:
	movl	$10, %esi
	callq	strchr@PLT
	movq	%rax, %rdi
	incq	%rdi
.LBB1_11:
	leaq	44(%rsp), %r14
	leaq	40(%rsp), %r12
	leaq	20(%rsp), %r13
	movl	$.L.str.11, %esi
	movq	%r14, %rdx
	movq	%r12, %rcx
	movq	%r13, %r8
	xorl	%eax, %eax
	callq	__isoc23_sscanf
	movq	stderr(%rip), %rdi
	movl	(%r14), %ecx
	movl	(%r12), %r8d
	movl	(%r13), %r9d
	movl	$.L.str.12, %esi
	movl	%eax, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	(%r14), %r13d
	movq	%r15, 24(%rsp)                  # 8-byte Spill
	movl	%r13d, (%r15)
	movl	(%r12), %r14d
	movq	8(%rsp), %rax                   # 8-byte Reload
	movl	%r14d, (%rax)
	leaq	20(%rsp), %rax
	movl	(%rax), %eax
	movl	%eax, (%rsp)                    # 4-byte Spill
	movl	%eax, (%rbp)
	movl	%r14d, %r15d
	imull	%r13d, %r15d
	leaq	(,%r15,4), %rdi
	callq	malloc
	testq	%rax, %rax
	jne	.LBB1_13
# %bb.12:
	movq	stderr(%rip), %rdi
	xorl	%r12d, %r12d
	movl	$.L.str.13, %esi
	movl	%r13d, %edx
	movl	%r14d, %ecx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB1_18
.LBB1_8:
	movq	stderr(%rip), %rdi
	xorl	%r12d, %r12d
	movl	$.L.str.9, %esi
.LBB1_5:
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB1_18
.LBB1_13:
	movq	%rax, %r12
	leal	(%r15,%r15,2), %r14d
	cmpl	$256, (%rsp)                    # 4-byte Folded Reload
                                        # imm = 0x100
	setge	%cl
	shll	%cl, %r14d
	movslq	%r14d, %r15
	movq	%r15, %rdi
	callq	malloc
	testq	%rax, %rax
	jne	.LBB1_15
# %bb.14:
	movq	stderr(%rip), %rdi
	xorl	%r12d, %r12d
	movl	$.L.str.14, %esi
	movl	%r14d, %edx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB1_18
.LBB1_15:
	movq	%rax, %r13
	movq	24(%rsp), %rax                  # 8-byte Reload
	movl	(%rax), %edx
	leaq	48(%rsp), %r14
	movl	$.L.str.15, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	sprintf
	movq	%r15, (%rsp)                    # 8-byte Spill
	leaq	128(%rsp), %rdi
	movq	%r14, %rsi
	movq	%r14, %r15
	callq	strstr
	movq	%rax, %r14
	movq	%r15, %rdi
	callq	strlen
	addq	%rax, %r14
	incq	%r14
	movq	8(%rsp), %rax                   # 8-byte Reload
	movl	(%rax), %edx
	movl	$.L.str.15, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	sprintf
	movq	%r14, %rdi
	movq	%r15, %rsi
	callq	strstr
	movq	%rax, %r14
	movq	%r15, %rdi
	callq	strlen
	addq	%rax, %r14
	incq	%r14
	movl	(%rbp), %edx
	movl	$.L.str.15, %esi
	movq	%r15, %rdi
	xorl	%eax, %eax
	callq	sprintf
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r15, %rbp
	callq	strstr
	movq	%rax, %r14
	movq	stderr(%rip), %rdi
	movq	%rax, %rcx
	leaq	128(%rsp), %r15
	subq	%r15, %rcx
	movl	$.L.str.16, %esi
	movq	%rbp, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movq	%rbp, %rdi
	callq	strlen
	addq	%rax, %r14
	subq	%r15, %r14
	incq	%r14
	movq	32(%rsp), %rbp                  # 8-byte Reload
	movq	%rbp, %rdi
	movq	%r14, %rsi
	xorl	%edx, %edx
	callq	fseek
	movl	$1, %esi
	movq	%r13, %rdi
	movq	(%rsp), %r14                    # 8-byte Reload
	movq	%r14, %rdx
	movq	%rbp, %rcx
	callq	fread
	movq	stderr(%rip), %rdi
	movl	$.L.str.17, %esi
	movq	%rbx, %rdx
	movq	%rax, %rcx
	movl	%r14d, %r8d
	xorl	%eax, %eax
	callq	fprintf
	movq	%rbp, %rdi
	callq	fclose
	movq	8(%rsp), %rax                   # 8-byte Reload
	movl	(%rax), %eax
	movq	24(%rsp), %rcx                  # 8-byte Reload
	imull	(%rcx), %eax
	testl	%eax, %eax
	jle	.LBB1_18
# %bb.16:                               # %.lr.ph.preheader
	movl	%eax, %eax
	xorl	%ecx, %ecx
.LBB1_17:                               # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%r13), %edx
	movl	%edx, (%r12,%rcx,4)
	incq	%rcx
	addq	$3, %r13
	cmpq	%rcx, %rax
	jne	.LBB1_17
	jmp	.LBB1_18
.Lfunc_end1:
	.size	_Z8read_ppmPcPiS0_S0_, .Lfunc_end1-_Z8read_ppmPcPiS0_S0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z20__device_stub__sobelPiS_iii # -- Begin function _Z20__device_stub__sobelPiS_iii
	.type	_Z20__device_stub__sobelPiS_iii,@function
_Z20__device_stub__sobelPiS_iii:        # @_Z20__device_stub__sobelPiS_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	$128, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	8(%rsp), %rdx
	movl	%ecx, (%rdx)
	leaq	4(%rsp), %rcx
	movl	%r8d, (%rcx)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%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	$_Z5sobelPiS_iii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$144, %rsp
	.cfi_adjust_cfa_offset -144
	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	_Z20__device_stub__sobelPiS_iii, .Lfunc_end2-_Z20__device_stub__sobelPiS_iii
	.cfi_endproc
                                        # -- End function
	.globl	_Z9write_ppmPciiiPi             # -- Begin function _Z9write_ppmPciiiPi
	.type	_Z9write_ppmPciiiPi,@function
_Z9write_ppmPciiiPi:                    # @_Z9write_ppmPciiiPi
	.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	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r8, %rbx
	movl	%ecx, %r12d
	movl	%edx, %ebp
	movl	%esi, %r15d
	movl	$.L.str.18, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB3_5
# %bb.1:
	movq	%rax, %r14
	movl	$.L.str.20, %edi
	movl	$3, %esi
	movl	$1, %edx
	movq	%rax, %rcx
	callq	fwrite@PLT
	movl	$.L.str.21, %esi
	movq	%r14, %rdi
	movl	%r15d, %edx
	movl	%ebp, %ecx
	movl	%r12d, %r8d
	xorl	%eax, %eax
	callq	fprintf
	imull	%r15d, %ebp
	testl	%ebp, %ebp
	jle	.LBB3_4
# %bb.2:                                # %.lr.ph.preheader
	movl	%ebp, %r15d
	xorl	%r12d, %r12d
.LBB3_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%rbx,%r12,4), %ecx
	movl	$.L.str.22, %esi
	movq	%r14, %rdi
	movl	%ecx, %edx
	movl	%ecx, %r8d
	xorl	%eax, %eax
	callq	fprintf
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB3_3
.LBB3_4:                                # %._crit_edge
	movq	%r14, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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
	jmp	fclose                          # TAILCALL
.LBB3_5:
	.cfi_def_cfa_offset 48
	movq	stderr(%rip), %rdi
	movl	$.L.str.19, %esi
	xorl	%eax, %eax
	callq	fprintf
	movl	$-1, %edi
	callq	exit
.Lfunc_end3:
	.size	_Z9write_ppmPciiiPi, .Lfunc_end3-_Z9write_ppmPciiiPi
	.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	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z5sobelPiS_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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__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	.LBB5_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
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"ansel3.ppm"
	.size	.L.str, 11

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"file %s    threshold %d\n"
	.size	.L.str.1, 25

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"sobel() unable to malloc %d bytes\n"
	.size	.L.str.2, 35

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

	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"result.ppm"
	.size	.L.str.3, 11

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Elapsed time: %f milliseconds\n"
	.size	.L.str.4, 31

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"read_ppm but no file name\n"
	.size	.L.str.5, 27

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"read_ppm( %s )\n"
	.size	.L.str.6, 16

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"read_ppm()    ERROR  file '%s' cannot be opened for reading\n"
	.size	.L.str.8, 61

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Texture::Texture()    ERROR  file '%s' does not start with \"P6\"  I am expecting a binary PPM file\n"
	.size	.L.str.9, 99

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

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"read %d things   width %d  height %d  maxval %d\n"
	.size	.L.str.12, 49

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"read_ppm()  unable to allocate %d x %d unsigned ints for the picture\n"
	.size	.L.str.13, 70

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"read_ppm()  unable to allocate %d bytes of read buffer\n"
	.size	.L.str.14, 56

	.type	.L.str.15,@object               # @.str.15
	.section	.rodata.cst4,"aM",@progbits,4
.L.str.15:
	.asciz	"%d\000"
	.size	.L.str.15, 4

	.type	.L.str.16,@object               # @.str.16
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.16:
	.asciz	"%s found at offset %d\n"
	.size	.L.str.16, 23

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Texture %s   read %ld of %ld bytes\n"
	.size	.L.str.17, 36

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

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"FAILED TO OPEN FILE '%s' for writing\n"
	.size	.L.str.19, 38

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"P6\n"
	.size	.L.str.20, 4

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

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"%c%c%c"
	.size	.L.str.22, 7

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z5sobelPiS_iii"
	.size	.L__unnamed_1, 16

	.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 _Z20__device_stub__sobelPiS_iii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z5sobelPiS_iii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z5sobelPiS_iii         ; -- Begin function _Z5sobelPiS_iii
	.globl	_Z5sobelPiS_iii
	.p2align	8
	.type	_Z5sobelPiS_iii,@function
_Z5sobelPiS_iii:                        ; @_Z5sobelPiS_iii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x2c
	s_load_b128 s[4:7], s[0:1], 0x10
	v_bfe_u32 v1, v0, 10, 10
	v_and_b32_e32 v4, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s3, s2, 16
	s_and_b32 s2, s2, 0xffff
	v_mad_u64_u32 v[2:3], null, s15, s3, v[1:2]
	v_mad_u64_u32 v[0:1], null, s14, s2, v[4:5]
	s_add_i32 s2, s5, -1
	s_add_i32 s3, s4, -1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v3, 1, v2
	v_add_nc_u32_e32 v1, 1, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_gt_i32_e32 vcc_lo, s2, v3
	v_cmp_gt_i32_e64 s2, s3, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, vcc_lo, s2
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_2
; %bb.1:                                ; %.sink.split
	v_mul_lo_u32 v5, v2, s4
	s_load_b128 s[0:3], s[0:1], 0x0
	v_mul_lo_u32 v7, v3, s4
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_4)
	v_add_nc_u32_e32 v2, v5, v1
	v_lshl_add_u32 v11, s4, 1, v5
	v_add_nc_u32_e32 v4, v5, v0
	v_add_nc_u32_e32 v6, v7, v1
	v_add_nc_u32_e32 v8, v7, v0
	v_ashrrev_i32_e32 v3, 31, v2
	v_add_nc_u32_e32 v1, v11, v1
	v_ashrrev_i32_e32 v5, 31, v4
	v_ashrrev_i32_e32 v7, 31, v6
	v_add_nc_u32_e32 v0, v11, v0
	v_lshlrev_b64 v[9:10], 2, v[2:3]
	v_ashrrev_i32_e32 v2, 31, v1
	v_lshlrev_b64 v[3:4], 2, v[4:5]
	v_lshlrev_b64 v[5:6], 2, v[6:7]
	s_delay_alu instid0(VALU_DEP_3)
	v_lshlrev_b64 v[13:14], 2, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v11, vcc_lo, s0, v9
	v_ashrrev_i32_e32 v1, 31, v0
	v_add_co_ci_u32_e32 v12, vcc_lo, s1, v10, vcc_lo
	v_add_co_u32 v3, vcc_lo, s0, v3
	v_ashrrev_i32_e32 v9, 31, v8
	v_add_co_ci_u32_e32 v4, vcc_lo, s1, v4, vcc_lo
	v_add_co_u32 v15, vcc_lo, s0, v5
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	v_add_co_ci_u32_e32 v16, vcc_lo, s1, v6, vcc_lo
	v_lshlrev_b64 v[7:8], 2, v[8:9]
	v_add_co_u32 v9, vcc_lo, s0, v13
	v_add_co_ci_u32_e32 v10, vcc_lo, s1, v14, vcc_lo
	v_add_co_u32 v0, vcc_lo, s0, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	v_add_co_u32 v7, vcc_lo, s0, v7
	v_add_co_ci_u32_e32 v8, vcc_lo, s1, v8, vcc_lo
	s_clause 0x5
	global_load_b64 v[9:10], v[9:10], off
	global_load_b32 v2, v[0:1], off
	global_load_b64 v[0:1], v[11:12], off
	global_load_b32 v3, v[3:4], off
	global_load_b32 v4, v[15:16], off offset:4
	global_load_b32 v7, v[7:8], off
	s_waitcnt vmcnt(3)
	v_add_nc_u32_e32 v8, v1, v10
	s_waitcnt vmcnt(2)
	v_add_nc_u32_e32 v11, v3, v2
	v_add_nc_u32_e32 v1, v3, v1
	s_waitcnt vmcnt(0)
	v_sub_nc_u32_e32 v4, v4, v7
	v_add_nc_u32_e32 v2, v10, v2
	v_sub_nc_u32_e32 v7, v8, v11
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_nc_u32_e32 v1, v1, v2
	v_lshl_add_u32 v3, v4, 1, v7
	v_sub_nc_u32_e32 v4, v0, v9
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v0, v3, v3
	v_lshl_add_u32 v3, v4, 1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_mad_u64_u32 v[1:2], null, v3, v3, v[0:1]
	v_add_co_u32 v2, vcc_lo, s2, v5
	v_add_co_ci_u32_e32 v3, vcc_lo, s3, v6, vcc_lo
	v_cmp_lt_i32_e32 vcc_lo, s6, v1
	v_cndmask_b32_e64 v0, 0, 0xff, vcc_lo
	global_store_b32 v[2:3], v0, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z5sobelPiS_iii
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 288
		.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 17
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z5sobelPiS_iii, .Lfunc_end0-_Z5sobelPiS_iii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 520
; NumSgprs: 18
; NumVgprs: 17
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 17
; 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
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         36
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         44
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         46
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         48
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         50
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         52
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         54
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         96
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 288
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z5sobelPiS_iii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z5sobelPiS_iii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     17
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
