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

#define DEFAULT_THRESHOLD 4000

#define DEFAULT_FILENAME "ansel3.ppm"

__global__ void sobel(unsigned int *ingoing, int *outgoing, int xsize, int ysize, int threshold) {

	 int x = threadIdx.x + (blockIdx.x * blockDim.x);
	 int y = threadIdx.y + (blockIdx.y * blockDim.y);

	 if ((x > 0) && (x < ysize - 1) &&(y > 0) &&  (y < xsize - 1)) {

		  int sum_x = ingoing[(x + 1) + ((y - 1) * ysize)]  -		ingoing[(x - 1) + ((y - 1) * ysize)]
				 + (2 * ingoing[(x + 1) + (		y * ysize)]) - (2 * ingoing[(x - 1) + (		y * ysize)])
						+ ingoing[(x + 1) + ((y + 1) * ysize)]  -		ingoing[(x - 1) + ((y + 1) * ysize)];

		  int sum_y = ingoing[(x + 1) + ((y + 1) * ysize)]  -		ingoing[(x + 1) + ((y - 1) * ysize)]
				 + (2 * ingoing[	  x  + ((y + 1) * ysize)]) - (2 * ingoing[		x + ((y - 1) * ysize)])
						+ ingoing[(x - 1) + ((y + 1) * ysize)]  -		ingoing[(x - 1) + ((y - 1) * ysize)];

		  
		  int magnitude = (sum_x * sum_x) + (sum_y * sum_y);
		  int i = x + (y * ysize);
		  if (magnitude > threshold) 
				outgoing[i] = 255;
		  

	 }
}

unsigned int *read_ppm(char *filename, int *xsize, int *ysize, int *maxval) {

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

	 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;
	 }

	 char chars[1024];
	 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);
	 *xsize = width;
	 *ysize = 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", *xsize);
	 line = strstr(line, duh);
	 //fprintf(stderr, "%s found at offset %d\n", duh, line-chars);
	 line += strlen(duh) + 1;

	 sprintf(duh, "%d\0", *ysize);
	 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 = (*xsize) * (*ysize);
	 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 xsize, int ysize, 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", xsize, ysize, maxval);

	 int numpix = xsize * ysize;
	 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);

}

int main(int argc, char **argv) {

	 char *filename;
	 filename = strdup(DEFAULT_FILENAME);

	 int threshold;
	 threshold = DEFAULT_THRESHOLD;

	 if (argc > 1) {
		  if (argc == 3) {
				filename = strdup(argv[1]);
				threshold = atoi(argv[2]);
		  }
		  if (argc == 2) {
				threshold = atoi(argv[1]);
		  }
	 }

	 int xsize, ysize, maxval;
	 unsigned int *pic = read_ppm(filename, &ysize, &xsize, &maxval);

	 int size = xsize * ysize;

	 dim3 BLOCK(32, 32);

	 dim3 GRID((int)ceil((float)ysize / 32), (int)ceil((float)xsize / 32));

	 unsigned int *h_ingoing;

	 int *h_outgoing;

	 h_ingoing = pic;

	 h_outgoing = (int *)calloc(size, sizeof *h_outgoing);

	 unsigned int *d_ingoing;

	 int *d_outgoing;

	 cudaMalloc(&d_ingoing, size * sizeof *d_ingoing);

	 cudaMalloc(&d_outgoing, size * sizeof *d_outgoing);

	 cudaMemcpy(d_ingoing, h_ingoing, size * sizeof *h_ingoing, cudaMemcpyHostToDevice);

	 cudaMemcpy(d_outgoing, h_outgoing, size * sizeof *h_outgoing, cudaMemcpyHostToDevice);

	 float time;

	 cudaEvent_t begin, end;

	 cudaEventCreate(&begin);

	 cudaEventCreate(&end);
	 cudaEventRecord(begin, 0);

	 sobel<<<GRID, BLOCK>>>(d_ingoing, d_outgoing, xsize, ysize, threshold);

	 cudaEventRecord(end, 0);


	 cudaEventSynchronize(end);


	 cudaEventElapsedTime(&time, begin, end);

	 cudaMemcpy(h_outgoing, d_outgoing, size * sizeof *h_outgoing, cudaMemcpyDeviceToHost);

	 printf("%f\n", time);

	 write_ppm("result.ppm", ysize, xsize, 255, h_outgoing);
	 
}


	.file	"tmpxft_00390f20_00000000-6_Sobel_edge_detection_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\tread %ld of %ld bytes\n"
	.text
	.globl	_Z8read_ppmPcPiS0_S0_
	.type	_Z8read_ppmPcPiS0_S0_, @function
_Z8read_ppmPcPiS0_S0_:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$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
.LFE2031:
	.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:
.LFB2032:
	.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
.LFE2032:
	.size	_Z9write_ppmPciiiPi, .-_Z9write_ppmPciiiPi
	.globl	_Z29__device_stub__Z5sobelPjPiiiiPjPiiii
	.type	_Z29__device_stub__Z5sobelPjPiiiiPjPiiii, @function
_Z29__device_stub__Z5sobelPjPiiiiPjPiiii:
.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	_Z5sobelPjPiiii(%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__Z5sobelPjPiiiiPjPiiii, .-_Z29__device_stub__Z5sobelPjPiiiiPjPiiii
	.globl	_Z5sobelPjPiiii
	.type	_Z5sobelPjPiiii, @function
_Z5sobelPjPiiii:
.LFB2059:
	.cfi_startproc
	endbr64
	jmp	_Z29__device_stub__Z5sobelPjPiiiiPjPiiii
	.cfi_endproc
.LFE2059:
	.size	_Z5sobelPjPiiii, .-_Z5sobelPjPiiii
	.section	.rodata.str1.1
.LC17:
	.string	"ansel3.ppm"
.LC19:
	.string	"%f\n"
.LC20:
	.string	"result.ppm"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2033:
	.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
	movl	$4000, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	call	strdup@PLT
	movq	%rax, %rbx
	cmpl	$1, %r13d
	jle	.L36
	cmpl	$3, %r13d
	jne	.L37
	movq	8(%r12), %rdi
	call	strdup@PLT
	movq	16(%r12), %rdi
	movq	%rax, %rbx
	jmp	.L43
.L37:
	cmpl	$2, %r13d
	jne	.L36
	movq	8(%r12), %rdi
.L43:
	call	atoi@PLT
	movl	%eax, %ebp
.L36:
	leaq	8(%rsp), %rcx
	movq	%rsp, %rdx
	leaq	4(%rsp), %rsi
	movq	%rbx, %rdi
	call	_Z8read_ppmPcPiS0_S0_
	movl	(%rsp), %r14d
	movl	4(%rsp), %r13d
	movl	$1, 56(%rsp)
	movq	%rax, %r15
	movabsq	$137438953504, %rax
	cvtsi2ssl	%r14d, %xmm0
	mulss	.LC18(%rip), %xmm0
	movq	%rax, 48(%rsp)
	call	ceilf@PLT
	movd	%xmm0, %ebx
	cvtsi2ssl	%r13d, %xmm0
	mulss	.LC18(%rip), %xmm0
	call	ceilf@PLT
	movd	%ebx, %xmm1
	movl	%r14d, %ebx
	movl	$4, %esi
	cvttss2sil	%xmm0, %eax
	movl	$1, 68(%rsp)
	imull	%r13d, %ebx
	movl	%eax, 60(%rsp)
	cvttss2sil	%xmm1, %eax
	movslq	%ebx, %rbx
	movq	%rbx, %rdi
	salq	$2, %rbx
	movl	%eax, 64(%rsp)
	call	calloc@PLT
	leaq	16(%rsp), %rdi
	movq	%rbx, %rsi
	movq	%rax, %r12
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r15, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	24(%rsp), %rdi
	movq	%rbx, %rdx
	movq	%r12, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	32(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	32(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	56(%rsp), %ecx
	movl	68(%rsp), %esi
	xorl	%r9d, %r9d
	movq	48(%rsp), %rdx
	movq	60(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L38
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	%ebp, %r8d
	movl	%r13d, %ecx
	movl	%r14d, %edx
	call	_Z29__device_stub__Z5sobelPjPiiiiPjPiiii
.L38:
	movq	40(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	40(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	leaq	12(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	24(%rsp), %rsi
	movq	%rbx, %rdx
	movq	%r12, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	12(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	%r12, %r8
	movl	$255, %ecx
	movl	%r14d, %edx
	movl	%r13d, %esi
	leaq	.LC20(%rip), %rdi
	call	_Z9write_ppmPciiiPi
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L39
	call	__stack_chk_fail@PLT
.L39:
	addq	$88, %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
.LFE2033:
	.size	main, .-main
	.section	.rodata.str1.1
.LC21:
	.string	"_Z5sobelPjPiiii"
	.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	.LC21(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z5sobelPjPiiii(%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.cst4,"aM",@progbits,4
	.align 4
.LC18:
	.long	1023410176
	.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 <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <fcntl.h>
#include <hip/hip_runtime.h>
#include "string.h"

#define DEFAULT_THRESHOLD 4000

#define DEFAULT_FILENAME "ansel3.ppm"

__global__ void sobel(unsigned int *ingoing, int *outgoing, int xsize, int ysize, int threshold) {

	 int x = threadIdx.x + (blockIdx.x * blockDim.x);
	 int y = threadIdx.y + (blockIdx.y * blockDim.y);

	 if ((x > 0) && (x < ysize - 1) &&(y > 0) &&  (y < xsize - 1)) {

		  int sum_x = ingoing[(x + 1) + ((y - 1) * ysize)]  -		ingoing[(x - 1) + ((y - 1) * ysize)]
				 + (2 * ingoing[(x + 1) + (		y * ysize)]) - (2 * ingoing[(x - 1) + (		y * ysize)])
						+ ingoing[(x + 1) + ((y + 1) * ysize)]  -		ingoing[(x - 1) + ((y + 1) * ysize)];

		  int sum_y = ingoing[(x + 1) + ((y + 1) * ysize)]  -		ingoing[(x + 1) + ((y - 1) * ysize)]
				 + (2 * ingoing[	  x  + ((y + 1) * ysize)]) - (2 * ingoing[		x + ((y - 1) * ysize)])
						+ ingoing[(x - 1) + ((y + 1) * ysize)]  -		ingoing[(x - 1) + ((y - 1) * ysize)];

		  
		  int magnitude = (sum_x * sum_x) + (sum_y * sum_y);
		  int i = x + (y * ysize);
		  if (magnitude > threshold) 
				outgoing[i] = 255;
		  

	 }
}

unsigned int *read_ppm(char *filename, int *xsize, int *ysize, int *maxval) {

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

	 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;
	 }

	 char chars[1024];
	 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);
	 *xsize = width;
	 *ysize = 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", *xsize);
	 line = strstr(line, duh);
	 //fprintf(stderr, "%s found at offset %d\n", duh, line-chars);
	 line += strlen(duh) + 1;

	 sprintf(duh, "%d\0", *ysize);
	 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 = (*xsize) * (*ysize);
	 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 xsize, int ysize, 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", xsize, ysize, maxval);

	 int numpix = xsize * ysize;
	 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);

}

int main(int argc, char **argv) {

	 char *filename;
	 filename = strdup(DEFAULT_FILENAME);

	 int threshold;
	 threshold = DEFAULT_THRESHOLD;

	 if (argc > 1) {
		  if (argc == 3) {
				filename = strdup(argv[1]);
				threshold = atoi(argv[2]);
		  }
		  if (argc == 2) {
				threshold = atoi(argv[1]);
		  }
	 }

	 int xsize, ysize, maxval;
	 unsigned int *pic = read_ppm(filename, &ysize, &xsize, &maxval);

	 int size = xsize * ysize;

	 dim3 BLOCK(32, 32);

	 dim3 GRID((int)ceil((float)ysize / 32), (int)ceil((float)xsize / 32));

	 unsigned int *h_ingoing;

	 int *h_outgoing;

	 h_ingoing = pic;

	 h_outgoing = (int *)calloc(size, sizeof *h_outgoing);

	 unsigned int *d_ingoing;

	 int *d_outgoing;

	 hipMalloc(&d_ingoing, size * sizeof *d_ingoing);

	 hipMalloc(&d_outgoing, size * sizeof *d_outgoing);

	 hipMemcpy(d_ingoing, h_ingoing, size * sizeof *h_ingoing, hipMemcpyHostToDevice);

	 hipMemcpy(d_outgoing, h_outgoing, size * sizeof *h_outgoing, hipMemcpyHostToDevice);

	 float time;

	 hipEvent_t begin, end;

	 hipEventCreate(&begin);

	 hipEventCreate(&end);
	 hipEventRecord(begin, 0);

	 sobel<<<GRID, BLOCK>>>(d_ingoing, d_outgoing, xsize, ysize, threshold);

	 hipEventRecord(end, 0);


	 hipEventSynchronize(end);


	 hipEventElapsedTime(&time, begin, end);

	 hipMemcpy(h_outgoing, d_outgoing, size * sizeof *h_outgoing, hipMemcpyDeviceToHost);

	 printf("%f\n", time);

	 write_ppm("result.ppm", ysize, xsize, 255, h_outgoing);
	 
}


	.text
	.file	"Sobel_edge_detection_CUDA.hip"
	.globl	_Z20__device_stub__sobelPjPiiii # -- Begin function _Z20__device_stub__sobelPjPiiii
	.type	_Z20__device_stub__sobelPjPiiii,@function
_Z20__device_stub__sobelPjPiiii:        # @_Z20__device_stub__sobelPjPiiii
	.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	$_Z5sobelPjPiiii, %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_end0:
	.size	_Z20__device_stub__sobelPjPiiii, .Lfunc_end0-_Z20__device_stub__sobelPjPiiii
	.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, %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.1, %esi
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.2, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	jne	.LBB1_6
# %bb.4:
	movq	stderr(%rip), %rdi
	xorl	%r12d, %r12d
	movl	$.L.str.3, %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.6, %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.7, %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.8, %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.4, %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.9, %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.10, %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.10, %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.10, %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.11, %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.12, %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	_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.13, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB2_5
# %bb.1:
	movq	%rax, %r14
	movl	$.L.str.15, %edi
	movl	$3, %esi
	movl	$1, %edx
	movq	%rax, %rcx
	callq	fwrite@PLT
	movl	$.L.str.16, %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	.LBB2_4
# %bb.2:                                # %.lr.ph.preheader
	movl	%ebp, %r15d
	xorl	%r12d, %r12d
.LBB2_3:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%rbx,%r12,4), %ecx
	movl	$.L.str.17, %esi
	movq	%r14, %rdi
	movl	%ecx, %edx
	movl	%ecx, %r8d
	xorl	%eax, %eax
	callq	fprintf
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB2_3
.LBB2_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
.LBB2_5:
	.cfi_def_cfa_offset 48
	movq	stderr(%rip), %rdi
	movl	$.L.str.14, %esi
	xorl	%eax, %eax
	callq	fprintf
	movl	$-1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z9write_ppmPciiiPi, .Lfunc_end2-_Z9write_ppmPciiiPi
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI3_0:
	.long	0x3d000000                      # float 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	$72, %rsp
	.cfi_def_cfa_offset 128
	.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, %r14
	movl	%edi, %ebp
	movl	$.L.str.18, %edi
	callq	strdup
	movq	%rax, %rbx
	movl	$4000, %eax                     # imm = 0xFA0
	cmpl	$2, %ebp
	jl	.LBB3_6
# %bb.1:
	je	.LBB3_2
# %bb.3:
	cmpl	$3, %ebp
	jne	.LBB3_6
# %bb.4:                                # %.thread
	movq	8(%r14), %rdi
	callq	strdup
	movq	%rax, %rbx
	movl	$2, %eax
	jmp	.LBB3_5
.LBB3_2:
	movl	$1, %eax
.LBB3_5:                                # %.sink.split
	movq	(%r14,%rax,8), %rdi
	callq	atoi
.LBB3_6:
	movl	%eax, 28(%rsp)                  # 4-byte Spill
	leaq	64(%rsp), %r15
	leaq	68(%rsp), %r12
	leaq	60(%rsp), %rcx
	movq	%rbx, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	callq	_Z8read_ppmPcPiS0_S0_
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	movl	(%r12), %r14d
	movl	(%r15), %ebp
	cvtsi2ss	%ebp, %xmm0
	movl	%ebp, %ebx
	imull	%r14d, %ebx
	mulss	.LCPI3_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %r15d
	xorps	%xmm0, %xmm0
	cvtsi2ss	%r14d, %xmm0
	mulss	.LCPI3_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %r13d
	shlq	$32, %r13
	orq	%r15, %r13
	movslq	%ebx, %r12
	movl	$4, %esi
	movq	%r12, %rdi
	callq	calloc
	movq	%rax, %r15
	shlq	$2, %r12
	leaq	48(%rsp), %rbx
	movq	%rbx, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movq	(%rbx), %rdi
	movq	32(%rsp), %rsi                  # 8-byte Reload
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	16(%rsp), %rax
	movq	(%rax), %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	40(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	(%rbx), %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	.LBB3_8
# %bb.7:
	movq	48(%rsp), %rdi
	movq	16(%rsp), %rsi
	movl	%r14d, %edx
	movl	%ebp, %ecx
	movl	28(%rsp), %r8d                  # 4-byte Reload
	callq	_Z20__device_stub__sobelPjPiiii
.LBB3_8:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	40(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	56(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	movq	16(%rsp), %rsi
	movq	%r15, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx), %xmm0
	movl	$.L.str.19, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.20, %edi
	movl	%ebp, %esi
	movl	%r14d, %edx
	movl	$255, %ecx
	movq	%r15, %r8
	callq	_Z9write_ppmPciiiPi
	xorl	%eax, %eax
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-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	.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	$_Z5sobelPjPiiii, %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	_Z5sobelPjPiiii,@object         # @_Z5sobelPjPiiii
	.section	.rodata,"a",@progbits
	.globl	_Z5sobelPjPiiii
	.p2align	3, 0x0
_Z5sobelPjPiiii:
	.quad	_Z20__device_stub__sobelPjPiiii
	.size	_Z5sobelPjPiiii, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"read_ppm but no file name\n"
	.size	.L.str, 27

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

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

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

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

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

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

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

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

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

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

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Texture %s\tread %ld of %ld bytes\n"
	.size	.L.str.12, 34

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

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

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

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

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

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"ansel3.ppm"
	.size	.L.str.18, 11

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

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"result.ppm"
	.size	.L.str.20, 11

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z5sobelPjPiiii"
	.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__sobelPjPiiii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z5sobelPjPiiii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z5sobelPjPiiii         ; -- Begin function _Z5sobelPjPiiii
	.globl	_Z5sobelPjPiiii
	.p2align	8
	.type	_Z5sobelPjPiiii,@function
_Z5sobelPjPiiii:                        ; @_Z5sobelPjPiiii
; %bb.0:
	s_load_b32 s2, s[0:1], 0x2c
	v_and_b32_e32 v3, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, s2, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[1:2], null, s14, s3, v[3:4]
	s_mov_b32 s3, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_lt_i32_e32 0, v1
	s_cbranch_execz .LBB0_4
; %bb.1:
	s_load_b128 s[4:7], s[0:1], 0x10
	v_bfe_u32 v0, v0, 10, 10
	s_lshr_b32 s2, s2, 16
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[2:3], null, s15, s2, v[0:1]
	s_waitcnt lgkmcnt(0)
	s_add_i32 s2, s5, -1
	s_add_i32 s3, s4, -1
	v_cmp_gt_i32_e32 vcc_lo, s2, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cmp_gt_i32_e64 s2, s3, v2
	v_cmp_lt_i32_e64 s3, 0, v2
	s_and_b32 s2, vcc_lo, s2
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, s2, s3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_b32 exec_lo, exec_lo, s2
	s_cbranch_execz .LBB0_4
; %bb.2:
	v_add_nc_u32_e32 v3, -1, v2
	v_mul_lo_u32 v0, v2, s5
	s_load_b128 s[0:3], s[0:1], 0x0
	v_ashrrev_i32_e32 v10, 31, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mul_lo_u32 v11, v3, s5
	v_add_nc_u32_e32 v5, s5, v0
	v_ashrrev_i32_e32 v12, 31, v0
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_ashrrev_i32_e32 v4, 31, v11
	v_add_co_u32 v3, vcc_lo, v11, v1
	v_ashrrev_i32_e32 v6, 31, v5
	v_add_nc_u32_e32 v2, v5, v1
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_add_co_ci_u32_e32 v4, vcc_lo, v4, v10, vcc_lo
	v_add_co_u32 v5, vcc_lo, v5, v1
	v_add_co_ci_u32_e32 v6, vcc_lo, v6, v10, vcc_lo
	s_delay_alu instid0(VALU_DEP_3)
	v_lshlrev_b64 v[7:8], 2, v[3:4]
	v_add_nc_u32_e32 v4, v11, v1
	v_add_co_u32 v9, vcc_lo, v0, v1
	v_ashrrev_i32_e32 v3, 31, v2
	v_add_co_ci_u32_e32 v10, vcc_lo, v12, v10, vcc_lo
	v_lshlrev_b64 v[11:12], 2, v[5:6]
	v_ashrrev_i32_e32 v5, 31, v4
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v6, vcc_lo, s0, v7
	v_lshlrev_b64 v[2:3], 2, v[2:3]
	v_add_co_ci_u32_e32 v7, vcc_lo, s1, v8, vcc_lo
	v_lshlrev_b64 v[8:9], 2, v[9:10]
	v_add_co_u32 v10, vcc_lo, s0, v11
	v_lshlrev_b64 v[4:5], 2, v[4:5]
	v_add_co_ci_u32_e32 v11, vcc_lo, s1, v12, vcc_lo
	v_add_co_u32 v2, vcc_lo, s0, v2
	v_add_co_ci_u32_e32 v3, vcc_lo, s1, v3, vcc_lo
	s_delay_alu instid0(VALU_DEP_4)
	v_add_co_u32 v4, vcc_lo, s0, v4
	v_add_co_ci_u32_e32 v5, vcc_lo, s1, v5, vcc_lo
	v_add_co_u32 v8, vcc_lo, s0, v8
	v_add_co_ci_u32_e32 v9, vcc_lo, s1, v9, vcc_lo
	s_clause 0x7
	global_load_b32 v2, v[2:3], off
	global_load_b32 v3, v[4:5], off
	global_load_b32 v4, v[10:11], off offset:4
	global_load_b32 v5, v[10:11], off offset:-4
	global_load_b32 v10, v[8:9], off offset:-4
	global_load_b32 v11, v[6:7], off offset:4
	global_load_b32 v6, v[6:7], off offset:-4
	global_load_b32 v7, v[8:9], off offset:4
	s_waitcnt vmcnt(6)
	v_sub_nc_u32_e32 v2, v2, v3
	s_waitcnt vmcnt(2)
	v_add_nc_u32_e32 v8, v11, v4
	s_waitcnt vmcnt(1)
	v_add_nc_u32_e32 v9, v6, v5
	s_waitcnt vmcnt(0)
	v_sub_nc_u32_e32 v7, v7, v10
	v_add_nc_u32_e32 v3, v6, v11
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_sub_nc_u32_e32 v8, v8, v9
	v_sub_nc_u32_e32 v3, v4, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_lshl_add_u32 v6, v7, 1, v8
	v_lshlrev_b32_e32 v7, 1, v2
	v_mul_lo_u32 v2, v6, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add3_u32 v5, v3, v5, v7
	v_mad_u64_u32 v[3:4], null, v5, v5, v[2:3]
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_lt_i32_e32 vcc_lo, s6, v3
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_4
; %bb.3:
	v_add_nc_u32_e32 v0, v0, v1
	v_mov_b32_e32 v2, 0xff
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_ashrrev_i32_e32 v1, 31, v0
	v_lshlrev_b64 v[0:1], 2, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	global_store_b32 v[0:1], v2, off
.LBB0_4:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z5sobelPjPiiii
		.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 13
		.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	_Z5sobelPjPiiii, .Lfunc_end0-_Z5sobelPjPiiii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 612
; NumSgprs: 18
; NumVgprs: 13
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 13
; 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:           _Z5sobelPjPiiii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z5sobelPjPiiii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     13
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
