#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>

//----------------------------------------------------------------------------//
//----------------------------------ppmFile.c----------------------------------//
//----------------------------------------------------------------------------//

typedef struct Image
{
	  int width;
	  int height;
	  unsigned char *data;
} Image;

/************************ private functions ****************************/

/* die gracelessly */

static void
die(char const *message)
{
    fprintf(stderr, "ppm: %s\n", message);
    exit(1);
}


/* check a dimension (width or height) from the image file for reasonability */

static void
checkDimension(int dim)
{
    if (dim < 1 || dim > 6000) 
    die("file contained unreasonable width or height");
}


	/* read a header: verify format and get width and height */

static void
readPPMHeader(FILE *fp, int *width, int *height)
{
    char ch;
    int  maxval;

    if (fscanf(fp, "P%c\n", &ch) != 1 || ch != '6') 
    die("file is not in ppm raw format; cannot read");

    /* skip comments */
    ch = getc(fp);
    while (ch == '#')
    {
        do {
    ch = getc(fp);
        } while (ch != '\n');	/* read to the end of the line */
        ch = getc(fp);            
    }

    if (!isdigit(ch)) die("cannot read header information from ppm file");

    ungetc(ch, fp);		/* put that digit back */

    /* read the width, height, and maximum value for a pixel */
    fscanf(fp, "%d%d%d\n", width, height, &maxval);

    if (maxval != 255) die("image is not true-color (24 bit); read failed");
    
    checkDimension(*width);
    checkDimension(*height);
}

	/************************ exported functions ****************************/
__host__
Image *
ImageCreate(int width, int height)
{
    Image *image = (Image *) malloc(sizeof(Image));

    if (!image) die("cannot allocate memory for new image");

    image->width  = width;
    image->height = height;
    image->data   = (unsigned char *) malloc(width * height * 3);

    if (!image->data) die("cannot allocate memory for new image");

    return image;
}
	  
__host__
Image *
ImageRead(char const *filename)
{
    int width, height, num, size;
    //unsigned  *p;

    Image *image = (Image *) malloc(sizeof(Image));
    FILE  *fp    = fopen(filename, "rb");

    if (!image) die("cannot allocate memory for new image");
    if (!fp)    die("cannot open file for reading");

    readPPMHeader(fp, &width, &height);

    size          = width * height * 3;
    image->data   = (unsigned  char*) malloc(size);
    image->width  = width;
    image->height = height;

    if (!image->data) die("cannot allocate memory for new image");

    num = fread((void *) image->data, 1, (size_t) size, fp);

    if (num != size) die("cannot read image data from file");

    fclose(fp);

    return image;
}

__host__
void ImageWrite(Image *image, char const *filename)
{
    int num;
    int size = image->width * image->height * 3;

    FILE *fp = fopen(filename, "wb");

    if (!fp) die("cannot open file for writing");

    fprintf(fp, "P6\n%d %d\n%d\n", image->width, image->height, 255);

    num = fwrite((void *) image->data, 1, (size_t) size, fp);

    if (num != size) die("cannot write image data to file");

    fclose(fp);
}  

__host__
int
ImageWidth(Image *image)
{
    return image->width;
}

__host__
int
ImageHeight(Image *image)
{
    return image->height;
}

__host__
void   
ImageClear(Image *image, unsigned char red, unsigned char green, unsigned char blue)
{
    int i;
    int pix = image->width * image->height;

    unsigned char *data = image->data;

    for (i = 0; i < pix; i++)
    {
        *data++ = red;
        *data++ = green;
        *data++ = blue;
    }
}

__device__
void ImageSetPixel(unsigned char* data, int x, int y, int chan, unsigned char val,int width)    // changed for data use
{
    int offset = (y * width + x) * 3 + chan;

    data[offset] = val;
}


__device__
unsigned  char ImageGetPixel(unsigned char* data, int x, int y, int chan, int width)    //changed for data use
{
    int offset = (y * width + x) * 3 + chan;

    return data[offset];
}    


//========================================================================================//
//==============================          MY CODE          ===============================//
//========================================================================================//

typedef struct pix{
    int r,g,b;

}pix;


//--------------------------------KERNEL FUNCTION---------------------//   

__device__
pix getAvg(unsigned char* data,int w,int h,int r,int x, int y){
    pix avg = {0};

    
    int xMin, xMax, yMin, yMax;

    if((xMin = x-r) < 0) xMin = 0;
    if((yMin = y-r) < 0) yMin = 0;              //define bounds
    if((xMax = x+r) > w-1) xMax = w;
    if((yMax = y+r) > h-1) yMax = h;

    int i;
    int j;
        
    for(i=yMin; i < yMax; i++){        //i is y, j is x for row first iteration
        for(j=xMin; j < xMax; j++){    //efficient for cache
        
        avg.r += ImageGetPixel(data,j,i,0,w); 
        avg.g += ImageGetPixel(data,j,i,1,w); 
        avg.b += ImageGetPixel(data,j,i,2,w); 

        }
    }


    int num = (yMax-yMin)*(xMax-xMin);
    

    avg.r = avg.r/num;
    avg.g = avg.g/num;
    avg.b = avg.b/num;

    return avg;    
    
}

//--------------------------KERNEL---------------------------//

__global__
void myKernel(unsigned char* oldData, unsigned char* newData,int WIDTH, int HEIGHT, int r){


    int indx = blockIdx.x * blockDim.x + threadIdx.x;
    int indy = blockIdx.y * blockDim.y + threadIdx.y;

    pix avg;

    int stride_x = gridDim.x*blockDim.x;
    int stride_y = gridDim.y*blockDim.y;

    int i;
    int j;
    for(i=indy; i<HEIGHT; i+= stride_y){
        for(j=indx; j<WIDTH; j+= stride_x){

            avg = getAvg(oldData,WIDTH,HEIGHT,r,j,i);


            ImageSetPixel(newData,j,i,0,avg.r,WIDTH);
            ImageSetPixel(newData,j,i,1,avg.g,WIDTH);
            ImageSetPixel(newData,j,i,2,avg.b,WIDTH);
        }
    }
}


//------------------------------------MAIN----------------------------------//

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

    //--------------Handle Input Arguments
    int r = atoi(argv[1]);
    char const * inFile = argv[2];
    char const * outFile = argv[3];

    //--------------Create
    Image* oldPic;
    Image* newPic;
    
    oldPic = ImageRead(inFile);                  //read old

    int WIDTH = ImageWidth(oldPic);
    int HEIGHT = ImageHeight(oldPic);

    newPic = ImageCreate(WIDTH,HEIGHT);        //make new same size as old

    printf("Processing...\n");

    //------------------cuda init----------------//

    dim3 blockDim(32,32);   //1024
    dim3 gridDim(20,20);

    int dsize = WIDTH*HEIGHT*3;         //size of data

    unsigned char* oldDataDevice;             //device data 
    unsigned char* newDataDevice;

    cudaMalloc(&oldDataDevice,dsize);
    cudaMalloc(&newDataDevice,dsize);

    cudaMemcpy(oldDataDevice,oldPic->data,dsize,cudaMemcpyHostToDevice);    //copy to device data

    //----------------------KERNEL--------------------//
    
    myKernel<<<gridDim,blockDim>>>(oldDataDevice,newDataDevice,WIDTH,HEIGHT,r);
    cudaDeviceSynchronize();

    //----------------------post proccess------------------------//

    cudaMemcpy(newPic->data,newDataDevice,dsize, cudaMemcpyDeviceToHost);           //copy back
    ImageWrite(newPic,outFile);

    cudaFree(oldDataDevice);
    cudaFree(newDataDevice);
    printf("New picture written to: %s\n",outFile);

    return 0;
}    


	.file	"tmpxft_0039a660_00000000-6_hw5.cudafe1.cpp"
	.text
#APP

	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"ppm: %s\n"
#NO_APP
	.text
	.type	_ZL3diePKc, @function
_ZL3diePKc:
.LFB2040:
	.cfi_startproc
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	leaq	.LC0(%rip), %rdx
	movl	$2, %esi
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rdi, %rcx
	movq	stderr(%rip), %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
	.cfi_endproc
.LFE2040:
	.size	_ZL3diePKc, .-_ZL3diePKc
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2055:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2055:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1
.LC1:
	.string	"cannot allocate memory for new image"
	.text
	.globl	_Z11ImageCreateii
	.type	_Z11ImageCreateii, @function
_Z11ImageCreateii:
.LFB2043:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	%esi, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movl	%edi, %ebx
	movl	$16, %edi
	call	malloc@PLT
	testq	%rax, %rax
	jne	.L5
.L6:
	leaq	.LC1(%rip), %rdi
	call	_ZL3diePKc
.L5:
	movl	%ebx, (%rax)
	imull	%r12d, %ebx
	movq	%rax, %rbp
	movl	%r12d, 4(%rax)
	leal	(%rbx,%rbx,2), %edi
	movslq	%edi, %rdi
	call	malloc@PLT
	movq	%rax, 8(%rbp)
	testq	%rax, %rax
	je	.L6
	movq	%rbp, %rax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2043:
	.size	_Z11ImageCreateii, .-_Z11ImageCreateii
	.section	.rodata.str1.1
.LC2:
	.string	"rb"
.LC3:
	.string	"cannot open file for reading"
.LC4:
	.string	"P%c\n"
.LC5:
	.string	"file is not in ppm raw format; cannot read"
.LC6:
	.string	"cannot read header information from ppm file"
.LC7:
	.string	"%d%d%d\n"
.LC8:
	.string	"image is not true-color (24 bit); read failed"
.LC9:
	.string	"file contained unreasonable width or height"
.LC10:
	.string	"cannot read image data from file"
	.text
	.globl	_Z9ImageReadPKc
	.type	_Z9ImageReadPKc, @function
_Z9ImageReadPKc:
.LFB2044:
	.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
	movq	%rdi, %rbx
	movl	$16, %edi
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	leaq	.LC2(%rip), %rsi
	movq	%rbx, %rdi
	movq	%rax, %rbp
	call	fopen@PLT
	testq	%rbp, %rbp
	jne	.L13
.L28:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC1(%rip), %rdi
	je	.L41
	jmp	.L42
.L13:
	movq	%rax, %rbx
	testq	%rax, %rax
	jne	.L15
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC3(%rip), %rdi
	je	.L41
	jmp	.L42
.L15:
	movq	%rax, %rdi
	leaq	11(%rsp), %rdx
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	decl	%eax
	je	.L43
.L17:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC5(%rip), %rdi
	jne	.L42
.L41:
	call	_ZL3diePKc
.L43:
	cmpb	$54, 11(%rsp)
	je	.L39
	jmp	.L17
.L20:
	movq	%rbx, %rdi
	call	getc@PLT
	movb	%al, 11(%rsp)
	cmpb	$10, %al
	jne	.L20
.L39:
	movq	%rbx, %rdi
	call	getc@PLT
	movb	%al, 11(%rsp)
	cmpb	$35, %al
	je	.L20
	movsbl	%al, %edi
	leal	-48(%rdi), %eax
	cmpl	$9, %eax
	jbe	.L21
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC6(%rip), %rdi
	je	.L41
	jmp	.L42
.L21:
	movq	%rbx, %rsi
	call	ungetc@PLT
	xorl	%eax, %eax
	leaq	16(%rsp), %rcx
	movq	%rbx, %rdi
	leaq	12(%rsp), %rdx
	leaq	20(%rsp), %r8
	leaq	.LC7(%rip), %rsi
	call	__isoc23_fscanf@PLT
	cmpl	$255, 20(%rsp)
	je	.L23
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC8(%rip), %rdi
	je	.L41
	jmp	.L42
.L23:
	movl	12(%rsp), %r15d
	leal	-1(%r15), %eax
	cmpl	$5999, %eax
	jbe	.L25
.L27:
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC9(%rip), %rdi
	je	.L41
	jmp	.L42
.L25:
	movl	16(%rsp), %r14d
	leal	-1(%r14), %eax
	cmpl	$5999, %eax
	ja	.L27
	movl	%r15d, %r12d
	imull	%r14d, %r12d
	leal	(%r12,%r12,2), %r12d
	movslq	%r12d, %r13
	movq	%r13, %rdi
	call	malloc@PLT
	movl	%r15d, 0(%rbp)
	movq	%rax, 8(%rbp)
	movq	%rax, %rdi
	movl	%r14d, 4(%rbp)
	testq	%rax, %rax
	je	.L28
	movq	%rbx, %r8
	movq	%r13, %rcx
	movl	$1, %edx
	movq	%r13, %rsi
	call	__fread_chk@PLT
	cmpl	%eax, %r12d
	je	.L29
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	leaq	.LC10(%rip), %rdi
	je	.L41
	jmp	.L42
.L29:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L31
.L42:
	call	__stack_chk_fail@PLT
.L31:
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	movq	%rbp, %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
.LFE2044:
	.size	_Z9ImageReadPKc, .-_Z9ImageReadPKc
	.section	.rodata.str1.1
.LC11:
	.string	"wb"
.LC12:
	.string	"cannot open file for writing"
.LC13:
	.string	"P6\n%d %d\n%d\n"
.LC14:
	.string	"cannot write image data to file"
	.text
	.globl	_Z10ImageWriteP5ImagePKc
	.type	_Z10ImageWriteP5ImagePKc, @function
_Z10ImageWriteP5ImagePKc:
.LFB2045:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rdi, %rbx
	movq	%rsi, %rdi
	leaq	.LC11(%rip), %rsi
	movl	(%rbx), %r12d
	imull	4(%rbx), %r12d
	call	fopen@PLT
	leaq	.LC12(%rip), %rdi
	leal	(%r12,%r12,2), %r12d
	testq	%rax, %rax
	je	.L48
	movl	(%rbx), %ecx
	movl	4(%rbx), %r8d
	movq	%rax, %rdi
	movq	%rax, %rbp
	leaq	.LC13(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	movl	$255, %r9d
	call	__fprintf_chk@PLT
	movq	8(%rbx), %rdi
	movslq	%r12d, %rdx
	movq	%rbp, %rcx
	movl	$1, %esi
	call	fwrite@PLT
	cmpl	%eax, %r12d
	je	.L46
	leaq	.LC14(%rip), %rdi
.L48:
	call	_ZL3diePKc
.L46:
	popq	%rbx
	.cfi_def_cfa_offset 24
	movq	%rbp, %rdi
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE2045:
	.size	_Z10ImageWriteP5ImagePKc, .-_Z10ImageWriteP5ImagePKc
	.globl	_Z10ImageWidthP5Image
	.type	_Z10ImageWidthP5Image, @function
_Z10ImageWidthP5Image:
.LFB2046:
	.cfi_startproc
	endbr64
	movl	(%rdi), %eax
	ret
	.cfi_endproc
.LFE2046:
	.size	_Z10ImageWidthP5Image, .-_Z10ImageWidthP5Image
	.globl	_Z11ImageHeightP5Image
	.type	_Z11ImageHeightP5Image, @function
_Z11ImageHeightP5Image:
.LFB2047:
	.cfi_startproc
	endbr64
	movl	4(%rdi), %eax
	ret
	.cfi_endproc
.LFE2047:
	.size	_Z11ImageHeightP5Image, .-_Z11ImageHeightP5Image
	.globl	_Z10ImageClearP5Imagehhh
	.type	_Z10ImageClearP5Imagehhh, @function
_Z10ImageClearP5Imagehhh:
.LFB2048:
	.cfi_startproc
	endbr64
	movl	(%rdi), %r8d
	movq	8(%rdi), %rax
	imull	4(%rdi), %r8d
	xorl	%edi, %edi
.L52:
	cmpl	%r8d, %edi
	jge	.L54
	movb	%sil, (%rax)
	addq	$3, %rax
	incl	%edi
	movb	%dl, -2(%rax)
	movb	%cl, -1(%rax)
	jmp	.L52
.L54:
	ret
	.cfi_endproc
.LFE2048:
	.size	_Z10ImageClearP5Imagehhh, .-_Z10ImageClearP5Imagehhh
	.globl	_Z13ImageSetPixelPhiiihi
	.type	_Z13ImageSetPixelPhiiihi, @function
_Z13ImageSetPixelPhiiihi:
.LFB2049:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2049:
	.size	_Z13ImageSetPixelPhiiihi, .-_Z13ImageSetPixelPhiiihi
	.globl	_Z13ImageGetPixelPhiiii
	.type	_Z13ImageGetPixelPhiiii, @function
_Z13ImageGetPixelPhiiii:
.LFB2050:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2050:
	.size	_Z13ImageGetPixelPhiiii, .-_Z13ImageGetPixelPhiiii
	.globl	_Z6getAvgPhiiiii
	.type	_Z6getAvgPhiiiii, @function
_Z6getAvgPhiiiii:
.LFB2051:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	popq	%rax
	.cfi_def_cfa_offset 8
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movl	$1, 12(%rsp)
	movl	12(%rsp), %edi
	call	exit@PLT
	.cfi_endproc
.LFE2051:
	.size	_Z6getAvgPhiiiii, .-_Z6getAvgPhiiiii
	.globl	_Z32__device_stub__Z8myKernelPhS_iiiPhS_iii
	.type	_Z32__device_stub__Z8myKernelPhS_iiiPhS_iii, @function
_Z32__device_stub__Z8myKernelPhS_iiiPhS_iii:
.LFB2077:
	.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	.L61
	pushq	40(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z8myKernelPhS_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
.L61:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L63
	call	__stack_chk_fail@PLT
.L63:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2077:
	.size	_Z32__device_stub__Z8myKernelPhS_iiiPhS_iii, .-_Z32__device_stub__Z8myKernelPhS_iiiPhS_iii
	.globl	_Z8myKernelPhS_iii
	.type	_Z8myKernelPhS_iii, @function
_Z8myKernelPhS_iii:
.LFB2078:
	.cfi_startproc
	endbr64
	jmp	_Z32__device_stub__Z8myKernelPhS_iiiPhS_iii
	.cfi_endproc
.LFE2078:
	.size	_Z8myKernelPhS_iii, .-_Z8myKernelPhS_iii
	.section	.rodata.str1.1
.LC15:
	.string	"Processing...\n"
.LC16:
	.string	"New picture written to: %s\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2052:
	.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
	movq	%rsi, %rbx
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	8(%rsi), %rdi
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	atoi@PLT
	movq	16(%rbx), %rdi
	movq	24(%rbx), %r15
	movl	%eax, 12(%rsp)
	call	_Z9ImageReadPKc
	movl	4(%rax), %r13d
	movl	(%rax), %r12d
	movq	%rax, %r14
	movl	%r13d, %ebx
	movl	%r13d, %esi
	movl	%r12d, %edi
	call	_Z11ImageCreateii
	imull	%r12d, %ebx
	movl	$2, %edi
	leaq	.LC15(%rip), %rsi
	movq	%rax, %rbp
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	16(%rsp), %rdi
	movabsq	$137438953504, %rax
	movl	$1, 40(%rsp)
	leal	(%rbx,%rbx,2), %ebx
	movq	%rax, 32(%rsp)
	movabsq	$85899345940, %rax
	movslq	%ebx, %rbx
	movq	%rax, 44(%rsp)
	movq	%rbx, %rsi
	movl	$1, 52(%rsp)
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	8(%r14), %rsi
	movq	16(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movl	40(%rsp), %ecx
	movl	52(%rsp), %esi
	xorl	%r9d, %r9d
	movq	32(%rsp), %rdx
	movq	44(%rsp), %rdi
	xorl	%r8d, %r8d
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L67
	movl	12(%rsp), %r8d
	movq	24(%rsp), %rsi
	movl	%r13d, %ecx
	movl	%r12d, %edx
	movq	16(%rsp), %rdi
	call	_Z32__device_stub__Z8myKernelPhS_iiiPhS_iii
.L67:
	call	cudaDeviceSynchronize@PLT
	movq	8(%rbp), %rdi
	movq	24(%rsp), %rsi
	movq	%rbx, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	%r15, %rsi
	movq	%rbp, %rdi
	call	_Z10ImageWriteP5ImagePKc
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
	movq	%r15, %rdx
	movl	$2, %edi
	leaq	.LC16(%rip), %rsi
	call	__printf_chk@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L68
	call	__stack_chk_fail@PLT
.L68:
	addq	$72, %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
.LFE2052:
	.size	main, .-main
	.section	.rodata.str1.1
.LC17:
	.string	"_Z8myKernelPhS_iii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2080:
	.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	.LC17(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z8myKernelPhS_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
.LFE2080:
	.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
	.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 <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>

//----------------------------------------------------------------------------//
//----------------------------------ppmFile.c----------------------------------//
//----------------------------------------------------------------------------//

typedef struct Image
{
	  int width;
	  int height;
	  unsigned char *data;
} Image;

/************************ private functions ****************************/

/* die gracelessly */

static void
die(char const *message)
{
    fprintf(stderr, "ppm: %s\n", message);
    exit(1);
}


/* check a dimension (width or height) from the image file for reasonability */

static void
checkDimension(int dim)
{
    if (dim < 1 || dim > 6000) 
    die("file contained unreasonable width or height");
}


	/* read a header: verify format and get width and height */

static void
readPPMHeader(FILE *fp, int *width, int *height)
{
    char ch;
    int  maxval;

    if (fscanf(fp, "P%c\n", &ch) != 1 || ch != '6') 
    die("file is not in ppm raw format; cannot read");

    /* skip comments */
    ch = getc(fp);
    while (ch == '#')
    {
        do {
    ch = getc(fp);
        } while (ch != '\n');	/* read to the end of the line */
        ch = getc(fp);            
    }

    if (!isdigit(ch)) die("cannot read header information from ppm file");

    ungetc(ch, fp);		/* put that digit back */

    /* read the width, height, and maximum value for a pixel */
    fscanf(fp, "%d%d%d\n", width, height, &maxval);

    if (maxval != 255) die("image is not true-color (24 bit); read failed");
    
    checkDimension(*width);
    checkDimension(*height);
}

	/************************ exported functions ****************************/
__host__
Image *
ImageCreate(int width, int height)
{
    Image *image = (Image *) malloc(sizeof(Image));

    if (!image) die("cannot allocate memory for new image");

    image->width  = width;
    image->height = height;
    image->data   = (unsigned char *) malloc(width * height * 3);

    if (!image->data) die("cannot allocate memory for new image");

    return image;
}
	  
__host__
Image *
ImageRead(char const *filename)
{
    int width, height, num, size;
    //unsigned  *p;

    Image *image = (Image *) malloc(sizeof(Image));
    FILE  *fp    = fopen(filename, "rb");

    if (!image) die("cannot allocate memory for new image");
    if (!fp)    die("cannot open file for reading");

    readPPMHeader(fp, &width, &height);

    size          = width * height * 3;
    image->data   = (unsigned  char*) malloc(size);
    image->width  = width;
    image->height = height;

    if (!image->data) die("cannot allocate memory for new image");

    num = fread((void *) image->data, 1, (size_t) size, fp);

    if (num != size) die("cannot read image data from file");

    fclose(fp);

    return image;
}

__host__
void ImageWrite(Image *image, char const *filename)
{
    int num;
    int size = image->width * image->height * 3;

    FILE *fp = fopen(filename, "wb");

    if (!fp) die("cannot open file for writing");

    fprintf(fp, "P6\n%d %d\n%d\n", image->width, image->height, 255);

    num = fwrite((void *) image->data, 1, (size_t) size, fp);

    if (num != size) die("cannot write image data to file");

    fclose(fp);
}  

__host__
int
ImageWidth(Image *image)
{
    return image->width;
}

__host__
int
ImageHeight(Image *image)
{
    return image->height;
}

__host__
void   
ImageClear(Image *image, unsigned char red, unsigned char green, unsigned char blue)
{
    int i;
    int pix = image->width * image->height;

    unsigned char *data = image->data;

    for (i = 0; i < pix; i++)
    {
        *data++ = red;
        *data++ = green;
        *data++ = blue;
    }
}

__device__
void ImageSetPixel(unsigned char* data, int x, int y, int chan, unsigned char val,int width)    // changed for data use
{
    int offset = (y * width + x) * 3 + chan;

    data[offset] = val;
}


__device__
unsigned  char ImageGetPixel(unsigned char* data, int x, int y, int chan, int width)    //changed for data use
{
    int offset = (y * width + x) * 3 + chan;

    return data[offset];
}    


//========================================================================================//
//==============================          MY CODE          ===============================//
//========================================================================================//

typedef struct pix{
    int r,g,b;

}pix;


//--------------------------------KERNEL FUNCTION---------------------//   

__device__
pix getAvg(unsigned char* data,int w,int h,int r,int x, int y){
    pix avg = {0};

    
    int xMin, xMax, yMin, yMax;

    if((xMin = x-r) < 0) xMin = 0;
    if((yMin = y-r) < 0) yMin = 0;              //define bounds
    if((xMax = x+r) > w-1) xMax = w;
    if((yMax = y+r) > h-1) yMax = h;

    int i;
    int j;
        
    for(i=yMin; i < yMax; i++){        //i is y, j is x for row first iteration
        for(j=xMin; j < xMax; j++){    //efficient for cache
        
        avg.r += ImageGetPixel(data,j,i,0,w); 
        avg.g += ImageGetPixel(data,j,i,1,w); 
        avg.b += ImageGetPixel(data,j,i,2,w); 

        }
    }


    int num = (yMax-yMin)*(xMax-xMin);
    

    avg.r = avg.r/num;
    avg.g = avg.g/num;
    avg.b = avg.b/num;

    return avg;    
    
}

//--------------------------KERNEL---------------------------//

__global__
void myKernel(unsigned char* oldData, unsigned char* newData,int WIDTH, int HEIGHT, int r){


    int indx = blockIdx.x * blockDim.x + threadIdx.x;
    int indy = blockIdx.y * blockDim.y + threadIdx.y;

    pix avg;

    int stride_x = gridDim.x*blockDim.x;
    int stride_y = gridDim.y*blockDim.y;

    int i;
    int j;
    for(i=indy; i<HEIGHT; i+= stride_y){
        for(j=indx; j<WIDTH; j+= stride_x){

            avg = getAvg(oldData,WIDTH,HEIGHT,r,j,i);


            ImageSetPixel(newData,j,i,0,avg.r,WIDTH);
            ImageSetPixel(newData,j,i,1,avg.g,WIDTH);
            ImageSetPixel(newData,j,i,2,avg.b,WIDTH);
        }
    }
}


//------------------------------------MAIN----------------------------------//

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

    //--------------Handle Input Arguments
    int r = atoi(argv[1]);
    char const * inFile = argv[2];
    char const * outFile = argv[3];

    //--------------Create
    Image* oldPic;
    Image* newPic;
    
    oldPic = ImageRead(inFile);                  //read old

    int WIDTH = ImageWidth(oldPic);
    int HEIGHT = ImageHeight(oldPic);

    newPic = ImageCreate(WIDTH,HEIGHT);        //make new same size as old

    printf("Processing...\n");

    //------------------cuda init----------------//

    dim3 blockDim(32,32);   //1024
    dim3 gridDim(20,20);

    int dsize = WIDTH*HEIGHT*3;         //size of data

    unsigned char* oldDataDevice;             //device data 
    unsigned char* newDataDevice;

    hipMalloc(&oldDataDevice,dsize);
    hipMalloc(&newDataDevice,dsize);

    hipMemcpy(oldDataDevice,oldPic->data,dsize,hipMemcpyHostToDevice);    //copy to device data

    //----------------------KERNEL--------------------//
    
    myKernel<<<gridDim,blockDim>>>(oldDataDevice,newDataDevice,WIDTH,HEIGHT,r);
    hipDeviceSynchronize();

    //----------------------post proccess------------------------//

    hipMemcpy(newPic->data,newDataDevice,dsize, hipMemcpyDeviceToHost);           //copy back
    ImageWrite(newPic,outFile);

    hipFree(oldDataDevice);
    hipFree(newDataDevice);
    printf("New picture written to: %s\n",outFile);

    return 0;
}    


	.text
	.file	"hw5.hip"
	.globl	_Z11ImageCreateii               # -- Begin function _Z11ImageCreateii
	.type	_Z11ImageCreateii,@function
_Z11ImageCreateii:                      # @_Z11ImageCreateii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movl	%edi, %r14d
	movl	$16, %edi
	callq	malloc
	testq	%rax, %rax
	je	.LBB0_3
# %bb.1:
	movq	%rax, %rbx
	movl	%r14d, (%rax)
	movl	%ebp, 4(%rax)
	imull	%ebp, %r14d
	leal	(%r14,%r14,2), %eax
	movslq	%eax, %rdi
	callq	malloc
	movq	%rax, 8(%rbx)
	testq	%rax, %rax
	je	.LBB0_3
# %bb.2:
	movq	%rbx, %rax
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.LBB0_3:
	.cfi_def_cfa_offset 32
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z11ImageCreateii, .Lfunc_end0-_Z11ImageCreateii
	.cfi_endproc
                                        # -- End function
	.globl	_Z9ImageReadPKc                 # -- Begin function _Z9ImageReadPKc
	.type	_Z9ImageReadPKc,@function
_Z9ImageReadPKc:                        # @_Z9ImageReadPKc
	.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
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %r14
	movl	$16, %edi
	callq	malloc
	movq	%rax, %rbx
	movl	$.L.str.1, %esi
	movq	%r14, %rdi
	callq	fopen
	testq	%rbx, %rbx
	je	.LBB1_1
# %bb.3:
	movq	%rax, %r14
	testq	%rax, %rax
	je	.LBB1_4
# %bb.5:
	leaq	3(%rsp), %rdx
	movl	$.L.str.11, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB1_7
# %bb.6:
	cmpb	$54, 3(%rsp)
	je	.LBB1_8
.LBB1_7:
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str.12, %edx
	jmp	.LBB1_2
.LBB1_9:                                # %.preheader.i
                                        #   Parent Loop BB1_8 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%r14, %rdi
	callq	getc
	movb	%al, 3(%rsp)
	cmpb	$10, %al
	jne	.LBB1_9
.LBB1_8:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_9 Depth 2
	movq	%r14, %rdi
	callq	getc
	movb	%al, 3(%rsp)
	movl	%eax, %ecx
	shll	$24, %ecx
	cmpl	$587202560, %ecx                # imm = 0x23000000
	je	.LBB1_9
# %bb.10:                               # %._crit_edge.i
	sarl	$24, %ecx
	addl	$-48, %ecx
	cmpl	$10, %ecx
	jae	.LBB1_11
# %bb.12:
	movsbl	%al, %edi
	movq	%r14, %rsi
	callq	ungetc
	leaq	8(%rsp), %rdx
	leaq	4(%rsp), %rcx
	leaq	12(%rsp), %r15
	movl	$.L.str.14, %esi
	movq	%r14, %rdi
	movq	%r15, %r8
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$255, (%r15)
	jne	.LBB1_13
# %bb.14:                               # %_ZL13readPPMHeaderP8_IO_FILEPiS1_.exit
	movl	8(%rsp), %edi
	callq	_ZL14checkDimensioni
	movl	4(%rsp), %edi
	callq	_ZL14checkDimensioni
	movl	8(%rsp), %ebp
	movl	4(%rsp), %r12d
	movl	%ebp, %eax
	imull	%r12d, %eax
	leal	(%rax,%rax,2), %eax
	movslq	%eax, %r15
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, 8(%rbx)
	movl	%ebp, (%rbx)
	movl	%r12d, 4(%rbx)
	testq	%rax, %rax
	je	.LBB1_1
# %bb.15:
	movl	$1, %esi
	movq	%rax, %rdi
	movq	%r15, %rdx
	movq	%r14, %rcx
	callq	fread
	cmpl	%eax, %r15d
	jne	.LBB1_16
# %bb.17:
	movq	%r14, %rdi
	callq	fclose
	movq	%rbx, %rax
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	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
	retq
.LBB1_1:
	.cfi_def_cfa_offset 64
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str, %edx
	jmp	.LBB1_2
.LBB1_4:
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str.2, %edx
	jmp	.LBB1_2
.LBB1_11:
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str.13, %edx
	jmp	.LBB1_2
.LBB1_13:
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str.15, %edx
	jmp	.LBB1_2
.LBB1_16:
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str.3, %edx
.LBB1_2:
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z9ImageReadPKc, .Lfunc_end1-_Z9ImageReadPKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z10ImageWriteP5ImagePKc        # -- Begin function _Z10ImageWriteP5ImagePKc
	.type	_Z10ImageWriteP5ImagePKc,@function
_Z10ImageWriteP5ImagePKc:               # @_Z10ImageWriteP5ImagePKc
	.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	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rax
	movq	%rdi, %r14
	movl	(%rdi), %r15d
	movl	4(%rdi), %ebp
	movl	$.L.str.4, %esi
	movq	%rax, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB2_1
# %bb.3:
	movq	%rax, %rbx
	imull	%ebp, %r15d
	leal	(%r15,%r15,2), %ebp
	movl	(%r14), %edx
	movl	4(%r14), %ecx
	movl	$.L.str.6, %esi
	movq	%rax, %rdi
	movl	$255, %r8d
	xorl	%eax, %eax
	callq	fprintf
	movq	8(%r14), %rdi
	movslq	%ebp, %r14
	movl	$1, %esi
	movq	%r14, %rdx
	movq	%rbx, %rcx
	callq	fwrite
	cmpl	%eax, %r14d
	jne	.LBB2_4
# %bb.5:
	movq	%rbx, %rdi
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.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_1:
	.cfi_def_cfa_offset 48
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str.5, %edx
	jmp	.LBB2_2
.LBB2_4:
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str.7, %edx
.LBB2_2:
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end2:
	.size	_Z10ImageWriteP5ImagePKc, .Lfunc_end2-_Z10ImageWriteP5ImagePKc
	.cfi_endproc
                                        # -- End function
	.globl	_Z10ImageWidthP5Image           # -- Begin function _Z10ImageWidthP5Image
	.type	_Z10ImageWidthP5Image,@function
_Z10ImageWidthP5Image:                  # @_Z10ImageWidthP5Image
	.cfi_startproc
# %bb.0:
	movl	(%rdi), %eax
	retq
.Lfunc_end3:
	.size	_Z10ImageWidthP5Image, .Lfunc_end3-_Z10ImageWidthP5Image
	.cfi_endproc
                                        # -- End function
	.globl	_Z11ImageHeightP5Image          # -- Begin function _Z11ImageHeightP5Image
	.type	_Z11ImageHeightP5Image,@function
_Z11ImageHeightP5Image:                 # @_Z11ImageHeightP5Image
	.cfi_startproc
# %bb.0:
	movl	4(%rdi), %eax
	retq
.Lfunc_end4:
	.size	_Z11ImageHeightP5Image, .Lfunc_end4-_Z11ImageHeightP5Image
	.cfi_endproc
                                        # -- End function
	.globl	_Z10ImageClearP5Imagehhh        # -- Begin function _Z10ImageClearP5Imagehhh
	.type	_Z10ImageClearP5Imagehhh,@function
_Z10ImageClearP5Imagehhh:               # @_Z10ImageClearP5Imagehhh
	.cfi_startproc
# %bb.0:
	movl	4(%rdi), %eax
	imull	(%rdi), %eax
	testl	%eax, %eax
	jle	.LBB5_3
# %bb.1:                                # %.lr.ph.preheader
	movq	8(%rdi), %rdi
.LBB5_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movb	%sil, (%rdi)
	movb	%dl, 1(%rdi)
	movb	%cl, 2(%rdi)
	addq	$3, %rdi
	decl	%eax
	jne	.LBB5_2
.LBB5_3:                                # %._crit_edge
	retq
.Lfunc_end5:
	.size	_Z10ImageClearP5Imagehhh, .Lfunc_end5-_Z10ImageClearP5Imagehhh
	.cfi_endproc
                                        # -- End function
	.globl	_Z23__device_stub__myKernelPhS_iii # -- Begin function _Z23__device_stub__myKernelPhS_iii
	.type	_Z23__device_stub__myKernelPhS_iii,@function
_Z23__device_stub__myKernelPhS_iii:     # @_Z23__device_stub__myKernelPhS_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	$_Z8myKernelPhS_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_end6:
	.size	_Z23__device_stub__myKernelPhS_iii, .Lfunc_end6-_Z23__device_stub__myKernelPhS_iii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, 20(%rsp)                  # 4-byte Spill
	movq	16(%rbx), %rdi
	movq	24(%rbx), %rax
	movq	%rax, 32(%rsp)                  # 8-byte Spill
	callq	_Z9ImageReadPKc
	movq	%rax, %rbp
	movl	(%rax), %r15d
	movl	4(%rax), %r12d
	movl	%r15d, %edi
	movl	%r12d, %esi
	callq	_Z11ImageCreateii
	movq	%rax, %r14
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	%r15d, %eax
	imull	%r12d, %eax
	leal	(%rax,%rax,2), %eax
	movslq	%eax, %r13
	leaq	24(%rsp), %rbx
	movq	%rbx, %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
	movq	(%rbx), %rdi
	movq	8(%rbp), %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movabsq	$85899345940, %rdi              # imm = 0x1400000014
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB7_2
# %bb.1:
	movq	24(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	%r15d, %edx
	movl	%r12d, %ecx
	movl	20(%rsp), %r8d                  # 4-byte Reload
	callq	_Z23__device_stub__myKernelPhS_iii
.LBB7_2:
	callq	hipDeviceSynchronize
	movq	8(%r14), %rdi
	movq	8(%rsp), %rsi
	movq	%r13, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%r14, %rdi
	movq	32(%rsp), %rbx                  # 8-byte Reload
	movq	%rbx, %rsi
	callq	_Z10ImageWriteP5ImagePKc
	movq	24(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movl	$.L.str.9, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	xorl	%eax, %eax
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end7:
	.size	main, .Lfunc_end7-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL14checkDimensioni,@function  # -- Begin function _ZL14checkDimensioni
_ZL14checkDimensioni:                   # @_ZL14checkDimensioni
	.cfi_startproc
# %bb.0:
	movl	$-6001, %eax                    # imm = 0xE88F
	addl	%eax, %edi
	cmpl	%eax, %edi
	jbe	.LBB8_2
# %bb.1:
	retq
.LBB8_2:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	stderr(%rip), %rdi
	movl	$.L.str.10, %esi
	movl	$.L.str.16, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end8:
	.size	_ZL14checkDimensioni, .Lfunc_end8-_ZL14checkDimensioni
	.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	.LBB9_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB9_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z8myKernelPhS_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_end9:
	.size	__hip_module_ctor, .Lfunc_end9-__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	.LBB10_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
.LBB10_2:
	retq
.Lfunc_end10:
	.size	__hip_module_dtor, .Lfunc_end10-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"cannot allocate memory for new image"
	.size	.L.str, 37

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

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"cannot open file for reading"
	.size	.L.str.2, 29

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"cannot read image data from file"
	.size	.L.str.3, 33

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

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"cannot open file for writing"
	.size	.L.str.5, 29

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

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"cannot write image data to file"
	.size	.L.str.7, 32

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

	.type	.L.str.9,@object                # @.str.9
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.9:
	.asciz	"New picture written to: %s\n"
	.size	.L.str.9, 28

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"ppm: %s\n"
	.size	.L.str.10, 9

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"P%c\n"
	.size	.L.str.11, 5

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"file is not in ppm raw format; cannot read"
	.size	.L.str.12, 43

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"cannot read header information from ppm file"
	.size	.L.str.13, 45

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

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"image is not true-color (24 bit); read failed"
	.size	.L.str.15, 46

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"file contained unreasonable width or height"
	.size	.L.str.16, 44

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z8myKernelPhS_iii"
	.size	.L__unnamed_1, 19

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

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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"Processing..."
	.size	.Lstr, 14

	.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 _Z23__device_stub__myKernelPhS_iii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z8myKernelPhS_iii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z8myKernelPhS_iii      ; -- Begin function _Z8myKernelPhS_iii
	.globl	_Z8myKernelPhS_iii
	.p2align	8
	.type	_Z8myKernelPhS_iii,@function
_Z8myKernelPhS_iii:                     ; @_Z8myKernelPhS_iii
; %bb.0:
	s_clause 0x1
	s_load_b32 s12, s[0:1], 0x2c
	s_load_b128 s[4:7], s[0:1], 0x10
	v_bfe_u32 v3, v0, 10, 10
	s_add_u32 s2, s0, 32
	s_addc_u32 s3, s1, 0
	s_waitcnt lgkmcnt(0)
	s_mov_b32 s7, exec_lo
	s_lshr_b32 s13, s12, 16
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s13, v[3:4]
	v_cmpx_gt_i32_e64 s5, v1
	s_cbranch_execz .LBB0_14
; %bb.1:                                ; %.preheader.lr.ph
	s_load_b64 s[2:3], s[2:3], 0x0
	s_load_b128 s[8:11], s[0:1], 0x0
	v_and_b32_e32 v0, 0x3ff, v0
	s_and_b32 s0, s12, 0xffff
	s_mul_i32 s7, s4, 3
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[2:3], null, s14, s0, v[0:1]
	v_subrev_nc_u32_e32 v0, s6, v1
	s_mov_b32 s14, 0
	v_cmp_gt_i32_e32 vcc_lo, s4, v2
	v_subrev_nc_u32_e32 v3, s6, v2
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s12, s2, s0
	s_mul_i32 s13, s3, s13
.LBB0_2:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
                                        ;       Child Loop BB0_6 Depth 3
                                        ;         Child Loop BB0_8 Depth 4
	s_and_saveexec_b32 s15, vcc_lo
	s_cbranch_execz .LBB0_13
; %bb.3:                                ; %.lr.ph
                                        ;   in Loop: Header=BB0_2 Depth=1
	v_subrev_nc_u32_e32 v4, s6, v1
	v_dual_mov_b32 v10, v2 :: v_dual_add_nc_u32 v5, s6, v1
	v_max_i32_e32 v8, 0, v0
	v_mul_lo_u32 v6, v1, s4
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_max_i32_e32 v4, 0, v4
	v_min_i32_e32 v5, s5, v5
	v_mov_b32_e32 v7, v3
	v_mul_lo_u32 v8, s4, v8
	s_mov_b32 s16, 0
	s_delay_alu instid0(VALU_DEP_3)
	v_cmp_gt_i32_e64 s0, v5, v4
	v_sub_nc_u32_e32 v9, v5, v4
.LBB0_4:                                ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Loop Header: Depth=2
                                        ;       Child Loop BB0_6 Depth 3
                                        ;         Child Loop BB0_8 Depth 4
	v_subrev_nc_u32_e32 v13, s6, v10
	v_dual_mov_b32 v12, 0 :: v_dual_add_nc_u32 v15, s6, v10
	v_mov_b32_e32 v11, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_max_i32_e32 v14, 0, v13
	v_min_i32_e32 v15, s4, v15
	v_mov_b32_e32 v13, 0
	s_and_saveexec_b32 s3, s0
	s_cbranch_execz .LBB0_12
; %bb.5:                                ; %.preheader.lr.ph.i
                                        ;   in Loop: Header=BB0_4 Depth=2
	v_max_i32_e32 v11, 0, v7
	v_cmp_gt_i32_e64 s1, v15, v14
	s_mov_b32 s17, 0
	v_mov_b32_e32 v16, v4
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_dual_mov_b32 v12, 0 :: v_dual_add_nc_u32 v13, v8, v11
	v_mov_b32_e32 v11, 0
	v_lshl_add_u32 v17, v13, 1, v13
	v_mov_b32_e32 v13, 0
.LBB0_6:                                ; %.preheader.i
                                        ;   Parent Loop BB0_2 Depth=1
                                        ;     Parent Loop BB0_4 Depth=2
                                        ; =>    This Loop Header: Depth=3
                                        ;         Child Loop BB0_8 Depth 4
	s_and_saveexec_b32 s18, s1
	s_cbranch_execz .LBB0_10
; %bb.7:                                ; %.lr.ph.i
                                        ;   in Loop: Header=BB0_6 Depth=3
	s_delay_alu instid0(VALU_DEP_2)
	v_dual_mov_b32 v18, v17 :: v_dual_mov_b32 v19, v14
	s_mov_b32 s19, 0
.LBB0_8:                                ;   Parent Loop BB0_2 Depth=1
                                        ;     Parent Loop BB0_4 Depth=2
                                        ;       Parent Loop BB0_6 Depth=3
                                        ; =>      This Inner Loop Header: Depth=4
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_ashrrev_i32_e32 v21, 31, v18
	v_add_co_u32 v20, s2, s8, v18
	v_add_nc_u32_e32 v19, 1, v19
	v_add_nc_u32_e32 v18, 3, v18
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e64 v21, s2, s9, v21, s2
	s_clause 0x1
	global_load_u16 v22, v[20:21], off
	global_load_u8 v20, v[20:21], off offset:2
	v_cmp_ge_i32_e64 s2, v19, v15
	s_or_b32 s19, s2, s19
	s_waitcnt vmcnt(1)
	v_and_b32_e32 v21, 0xff, v22
	v_lshrrev_b32_e32 v22, 8, v22
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v13, v13, v20
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v11, v11, v21
	v_add_nc_u32_e32 v12, v12, v22
	s_and_not1_b32 exec_lo, exec_lo, s19
	s_cbranch_execnz .LBB0_8
; %bb.9:                                ; %Flow
                                        ;   in Loop: Header=BB0_6 Depth=3
	s_or_b32 exec_lo, exec_lo, s19
.LBB0_10:                               ; %Flow81
                                        ;   in Loop: Header=BB0_6 Depth=3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	s_or_b32 exec_lo, exec_lo, s18
	v_add_nc_u32_e32 v16, 1, v16
	v_add_nc_u32_e32 v17, s7, v17
	v_cmp_ge_i32_e64 s2, v16, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s17, s2, s17
	s_and_not1_b32 exec_lo, exec_lo, s17
	s_cbranch_execnz .LBB0_6
; %bb.11:                               ; %Flow82
                                        ;   in Loop: Header=BB0_4 Depth=2
	s_or_b32 exec_lo, exec_lo, s17
.LBB0_12:                               ; %Flow83
                                        ;   in Loop: Header=BB0_4 Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 exec_lo, exec_lo, s3
	v_sub_nc_u32_e32 v14, v15, v14
	v_ashrrev_i32_e32 v18, 31, v11
	v_ashrrev_i32_e32 v19, 31, v12
	v_ashrrev_i32_e32 v20, 31, v13
	v_add_nc_u32_e32 v23, v10, v6
	v_mul_lo_u32 v14, v14, v9
	v_add_nc_u32_e32 v10, s12, v10
	v_add_nc_u32_e32 v12, v12, v19
	v_add_nc_u32_e32 v13, v13, v20
	v_add_nc_u32_e32 v7, s12, v7
	v_add_nc_u32_e32 v11, v11, v18
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_2) | instid1(VALU_DEP_4)
	v_xor_b32_e32 v12, v12, v19
	v_ashrrev_i32_e32 v15, 31, v14
	v_xor_b32_e32 v13, v13, v20
	v_xor_b32_e32 v11, v11, v18
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v14, v14, v15
	v_xor_b32_e32 v18, v18, v15
	v_xor_b32_e32 v19, v19, v15
	v_xor_b32_e32 v14, v14, v15
	v_xor_b32_e32 v15, v20, v15
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cvt_f32_u32_e32 v16, v14
	v_sub_nc_u32_e32 v17, 0, v14
	v_rcp_iflag_f32_e32 v16, v16
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v16, 0x4f7ffffe, v16
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_u32_f32_e32 v16, v16
	v_mul_lo_u32 v17, v17, v16
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_hi_u32 v17, v16, v17
	v_add_nc_u32_e32 v16, v16, v17
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_mul_hi_u32 v21, v12, v16
	v_mul_hi_u32 v17, v11, v16
	v_mul_hi_u32 v16, v13, v16
	v_mul_lo_u32 v24, v21, v14
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mul_lo_u32 v22, v17, v14
	v_mul_lo_u32 v25, v16, v14
	v_add_nc_u32_e32 v26, 1, v17
	v_add_nc_u32_e32 v27, 1, v16
	v_sub_nc_u32_e32 v12, v12, v24
	v_sub_nc_u32_e32 v11, v11, v22
	v_add_nc_u32_e32 v22, 1, v21
	v_sub_nc_u32_e32 v13, v13, v25
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cmp_ge_u32_e64 s2, v12, v14
	v_cmp_ge_u32_e64 s1, v11, v14
	v_sub_nc_u32_e32 v24, v11, v14
	s_delay_alu instid0(VALU_DEP_4)
	v_cmp_ge_u32_e64 s3, v13, v14
	v_sub_nc_u32_e32 v25, v13, v14
	v_cndmask_b32_e64 v21, v21, v22, s2
	v_sub_nc_u32_e32 v22, v12, v14
	v_cndmask_b32_e64 v17, v17, v26, s1
	v_cndmask_b32_e64 v11, v11, v24, s1
	v_cndmask_b32_e64 v16, v16, v27, s3
	v_cndmask_b32_e64 v13, v13, v25, s3
	v_cndmask_b32_e64 v12, v12, v22, s2
	v_add_nc_u32_e32 v22, 1, v21
	v_add_nc_u32_e32 v24, 1, v17
	v_add_nc_u32_e32 v25, 1, v16
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_ge_u32_e64 s1, v12, v14
	v_cndmask_b32_e64 v12, v21, v22, s1
	v_cmp_ge_u32_e64 s1, v11, v14
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_xor_b32_e32 v12, v12, v19
	v_cndmask_b32_e64 v11, v17, v24, s1
	v_cmp_ge_u32_e64 s1, v13, v14
	v_lshl_add_u32 v14, v23, 1, v23
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_sub_nc_u32_e32 v12, v12, v19
	v_xor_b32_e32 v11, v11, v18
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cndmask_b32_e64 v13, v16, v25, s1
	v_ashrrev_i32_e32 v16, 31, v14
	v_cmp_le_i32_e64 s1, s4, v10
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_sub_nc_u32_e32 v11, v11, v18
	v_xor_b32_e32 v13, v13, v15
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	s_or_b32 s16, s1, s16
	v_and_b32_e32 v17, 0xff, v11
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v13, v13, v15
	v_lshlrev_b16 v15, 8, v12
	v_add_co_u32 v11, s2, s10, v14
	v_add_co_ci_u32_e64 v12, s2, s11, v16, s2
	s_delay_alu instid0(VALU_DEP_3)
	v_or_b32_e32 v14, v17, v15
	s_clause 0x1
	global_store_b8 v[11:12], v13, off offset:2
	global_store_b16 v[11:12], v14, off
	s_and_not1_b32 exec_lo, exec_lo, s16
	s_cbranch_execnz .LBB0_4
.LBB0_13:                               ; %Flow85
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s15
	v_add_nc_u32_e32 v1, s13, v1
	v_add_nc_u32_e32 v0, s13, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_le_i32_e64 s0, s5, v1
	s_or_b32 s14, s0, s14
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s14
	s_cbranch_execnz .LBB0_2
.LBB0_14:                               ; %._crit_edge45
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z8myKernelPhS_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 28
		.amdhsa_next_free_sgpr 20
		.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	_Z8myKernelPhS_iii, .Lfunc_end0-_Z8myKernelPhS_iii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1084
; NumSgprs: 22
; NumVgprs: 28
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 3
; NumSGPRsForWavesPerEU: 22
; NumVGPRsForWavesPerEU: 28
; 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:           _Z8myKernelPhS_iii
    .private_segment_fixed_size: 0
    .sgpr_count:     22
    .sgpr_spill_count: 0
    .symbol:         _Z8myKernelPhS_iii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     28
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
