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


// MASK SIZE
#define MASK_WIDTH 5
// MASK RADIO
#define MASK_R (MASK_WIDTH-1)/2

#define COMMENT "Histogram_GPU"
#define RGB_COMPONENT_COLOR 255

// SIZE OF TILE
#define TILE_WIDTH 32
// SIZE OF SHARE MATRIX
#define SHARED_SIZE (MASK_WIDTH-1 + TILE_WIDTH)


typedef struct {
    unsigned char red, green, blue;
} PPMPixel;

typedef struct {
    int x, y;
    PPMPixel *data;
} PPMImage;

double rtclock()
{
    struct timezone Tzp;
    struct timeval Tp;
    int stat;
    stat = gettimeofday (&Tp, &Tzp);
    if (stat != 0) printf("Error return from gettimeofday: %d",stat);
    return(Tp.tv_sec + Tp.tv_usec*1.0e-6);
}


static PPMImage *readPPM(const char *filename) {
    char buff[16];
    PPMImage *img;
    FILE *fp;
    int c, rgb_comp_color;
    fp = fopen(filename, "rb");
    if (!fp) {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        exit(1);
    }

    if (!fgets(buff, sizeof(buff), fp)) {
        perror(filename);
        exit(1);
    }

    if (buff[0] != 'P' || buff[1] != '6') {
        fprintf(stderr, "Invalid image format (must be 'P6')\n");
        exit(1);
    }

    img = (PPMImage *) malloc(sizeof(PPMImage));
    if (!img) {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }

    c = getc(fp);
    while (c == '#') {
        while (getc(fp) != '\n')
            ;
        c = getc(fp);
    }

    ungetc(c, fp);
    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
        fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
        exit(1);
    }

    if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
        fprintf(stderr, "Invalid rgb component (error loading '%s')\n",
                filename);
        exit(1);
    }

    if (rgb_comp_color != RGB_COMPONENT_COLOR) {
        fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
        exit(1);
    }

    while (fgetc(fp) != '\n')
        ;
    img->data = (PPMPixel*) malloc(img->x * img->y * sizeof(PPMPixel));

    if (!img) {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }

    if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
        fprintf(stderr, "Error loading image '%s'\n", filename);
        exit(1);
    }

    fclose(fp);
    return img;
}

void writePPM(PPMImage *img) {

    fprintf(stdout, "P6\n");
    fprintf(stdout, "# %s\n", COMMENT);
    fprintf(stdout, "%d %d\n", img->x, img->y);
    fprintf(stdout, "%d\n", RGB_COMPONENT_COLOR);

    fwrite(img->data, 3 * img->x, img->y, stdout);
    fclose(stdout);
}

__global__ void smoothing_kernel(PPMImage *d_image, PPMImage *d_image_copy) {
    // Creating variables
    int i, j, row, col;
    int total_red = 0, total_blue = 0, total_green = 0;
    int index_dst_y, index_dst_x, index_src_y, index_src_x;

    // Get Row and COl
    row = blockIdx.y * TILE_WIDTH + threadIdx.y;
    col = blockIdx.x * TILE_WIDTH + threadIdx.x;

    // Create Shared block of data
    __shared__ PPMPixel shared_image_data[SHARED_SIZE*SHARED_SIZE];

    // Filling the shared variable with a two-step for.
    // first step fill the data with index inside the blocks dim
    // -----------------
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // | | | | | | | | |
    // -----------------
    // Second step fill the data with index outside the blocks dim
    // -----------------
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // |x|x|x|x|x|x|x|x|
    // -----------------

    for (i = 0; i <= TILE_WIDTH * TILE_WIDTH; i = i + TILE_WIDTH * TILE_WIDTH)
    {
        // Get indexs of dst matrix
        index_dst_y = (threadIdx.y * TILE_WIDTH + threadIdx.x + i) / SHARED_SIZE;
        index_dst_x = (threadIdx.y * TILE_WIDTH + threadIdx.x + i) % SHARED_SIZE;

        // Get indexs of destination matrix
        index_src_y = (blockIdx.y * TILE_WIDTH) + index_dst_y - MASK_R;
        index_src_x = (blockIdx.x * TILE_WIDTH) + index_dst_x - MASK_R;

        //Work only if dst geral index stay into shared matrix size
        if (index_dst_y * SHARED_SIZE + index_dst_x < (SHARED_SIZE*SHARED_SIZE)) {
            // if src index stay into image save images values else save 0
            if (index_src_y >= 0 && index_src_y < d_image->y && index_src_x >= 0 && index_src_x < d_image->x){
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].red = d_image_copy->data[(index_src_y * d_image->x) + index_src_x].red;
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].blue = d_image_copy->data[(index_src_y * d_image->x) + index_src_x].blue;
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].green = d_image_copy->data[(index_src_y * d_image->x) + index_src_x].green;
            }
            else{
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].red = 0;
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].blue = 0;
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].green = 0;
            }
        }
    }

    // sync threads
    __syncthreads();

    // if row and col stay into image proceed with convolution
    if (row < d_image->y && col < d_image->x){
        for (i = 0; i < MASK_WIDTH; i++){
             for (j = 0; j < MASK_WIDTH; j++) {
                total_red += shared_image_data[((threadIdx.y + j) * SHARED_SIZE) + (threadIdx.x + i)].red;
                total_blue += shared_image_data[((threadIdx.y + j) * SHARED_SIZE) + (threadIdx.x + i)].blue;
                total_green += shared_image_data[((threadIdx.y + j) * SHARED_SIZE) + (threadIdx.x + i)].green;
             }
        }
        // Save data of convolution into devise image
        d_image->data[(row * d_image->x) + col].red = total_red / (MASK_WIDTH*MASK_WIDTH);
        d_image->data[(row * d_image->x) + col].blue = total_blue / (MASK_WIDTH*MASK_WIDTH);
        d_image->data[(row * d_image->x) + col].green = total_green / (MASK_WIDTH*MASK_WIDTH);
    }
}


void smoothing_GPU(PPMImage *image, PPMImage *image_copy) {

    unsigned int rows, cols, img_size;
    PPMImage *d_image, *d_image_copy;
    PPMPixel *d_pixels, *d_pixels_copy, *new_pixels;

    // Get data
    cols = image->x;
    rows = image->y;
    img_size = cols * rows;

    // Alloc structure to devise
    cudaMalloc((void **)&d_image, sizeof(PPMImage));
    cudaMalloc((void **)&d_image_copy, sizeof(PPMImage));

    // Alloc image to devise
    cudaMalloc((void **)&d_pixels, sizeof(PPMPixel) * img_size);
    cudaMalloc((void **)&d_pixels_copy, sizeof(PPMPixel) * img_size);

    // cpy stucture to devise
    cudaMemcpy(d_image, image, sizeof(PPMImage), cudaMemcpyHostToDevice);
    cudaMemcpy(d_pixels, image->data, sizeof(PPMPixel) * img_size, cudaMemcpyHostToDevice);
    cudaMemcpy(&(d_image->data), &d_pixels, sizeof(PPMPixel *), cudaMemcpyHostToDevice);

    cudaMemcpy(d_image_copy, image, sizeof(PPMImage), cudaMemcpyHostToDevice);
    cudaMemcpy(d_pixels_copy, image->data, sizeof(PPMPixel) * img_size, cudaMemcpyHostToDevice);
    cudaMemcpy(&(d_image_copy->data), &d_pixels_copy, sizeof(PPMPixel *), cudaMemcpyHostToDevice);

    dim3 dimGrid(ceil((float)cols / TILE_WIDTH), ceil((float)rows / TILE_WIDTH), 1);
    dim3 dimBlock(TILE_WIDTH, TILE_WIDTH, 1);

    // Call function
    smoothing_kernel<<<dimGrid, dimBlock>>>(d_image, d_image_copy);


    new_pixels = (PPMPixel *) malloc(img_size * sizeof(PPMPixel));
    // Copy result to local array
    cudaMemcpy(image, d_image, sizeof(PPMImage), cudaMemcpyDeviceToHost);
    cudaMemcpy(new_pixels, d_pixels, sizeof(PPMPixel) * img_size, cudaMemcpyDeviceToHost);
    image->data = new_pixels;

    //Free memory
    cudaFree(d_image);
    cudaFree(d_image_copy);
    cudaFree(d_pixels);
    cudaFree(d_pixels_copy);
}

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

    if( argc != 2 ) {
        printf("Too many or no one arguments supplied.\n");
    }

    //double t_start, t_end;
    //int i;
    char *filename = argv[1]; //Recebendo o arquivo!;

    PPMImage *image = readPPM(filename);
    PPMImage *image_output = readPPM(filename);

    //t_start = rtclock();
    smoothing_GPU(image_output, image);
    //t_end = rtclock();

    writePPM(image_output);

    //fprintf(stdout, "\n%0.6lfs\n", t_end - t_start);
    free(image);
    free(image_output);
}

	.file	"tmpxft_0027ca06_00000000-6_smooth_gpu.cudafe1.cpp"
	.text
#APP

	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rb"
.LC1:
	.string	"Unable to open file '%s'\n"
.LC2:
	.string	"Invalid image format (must be 'P6')\n"
.LC3:
	.string	"Unable to allocate memory\n"
.LC4:
	.string	"%d %d"
.LC5:
	.string	"Invalid image size (error loading '%s')\n"
.LC6:
	.string	"%d"
.LC7:
	.string	"Invalid rgb component (error loading '%s')\n"
.LC8:
	.string	"'%s' does not have 8-bits components\n"
.LC9:
	.string	"Error loading image '%s'\n"
#NO_APP
	.text
	.type	_ZL7readPPMPKc, @function
_ZL7readPPMPKc:
.LFB2028:
	.cfi_startproc
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC0(%rip), %rsi
	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
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%r12, %rcx
	leaq	.LC1(%rip), %rdx
	testq	%rax, %rax
	je	.L22
	leaq	8(%rsp), %rdi
	movq	%rax, %rcx
	movl	$16, %edx
	movq	%rax, %rbx
	movl	$16, %esi
	call	__fgets_chk@PLT
	testq	%rax, %rax
	jne	.L3
	movq	%r12, %rdi
	call	perror@PLT
.L21:
	movl	$1, %edi
	call	exit@PLT
.L3:
	cmpb	$80, 8(%rsp)
	jne	.L4
	cmpb	$54, 9(%rsp)
	je	.L5
.L4:
	leaq	.LC2(%rip), %rdx
	jmp	.L23
.L5:
	movl	$16, %edi
	call	malloc@PLT
	movq	%rax, %rbp
	testq	%rax, %rax
	jne	.L20
	leaq	.LC3(%rip), %rdx
.L23:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L21
.L8:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$10, %eax
	jne	.L8
.L20:
	movq	%rbx, %rdi
	call	getc@PLT
	cmpl	$35, %eax
	je	.L8
	movq	%rbx, %rsi
	movl	%eax, %edi
	call	ungetc@PLT
	leaq	4(%rbp), %rcx
	movq	%rbp, %rdx
	movq	%rbx, %rdi
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%r12, %rcx
	leaq	.LC5(%rip), %rdx
	cmpl	$2, %eax
	jne	.L22
	leaq	4(%rsp), %rdx
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	movq	%rbx, %rdi
	call	__isoc23_fscanf@PLT
	movq	%r12, %rcx
	leaq	.LC7(%rip), %rdx
	decl	%eax
	jne	.L22
	cmpl	$255, 4(%rsp)
	leaq	.LC8(%rip), %rdx
	jne	.L22
.L11:
	movq	%rbx, %rdi
	call	fgetc@PLT
	cmpl	$10, %eax
	jne	.L11
	movl	0(%rbp), %r14d
	movl	4(%rbp), %r15d
	movl	%r14d, %r13d
	imull	%r15d, %r13d
	movslq	%r13d, %r13
	leaq	0(%r13,%r13,2), %r13
	movq	%r13, %rdi
	call	malloc@PLT
	leal	(%r14,%r14,2), %edx
	movslq	%r15d, %rcx
	movq	%rbx, %r8
	movq	%rax, 8(%rbp)
	movslq	%edx, %rdx
	movq	%rax, %rdi
	movq	%r13, %rsi
	call	__fread_chk@PLT
	movslq	4(%rbp), %rdx
	cmpq	%rax, %rdx
	je	.L12
	movq	%r12, %rcx
	leaq	.LC9(%rip), %rdx
.L22:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L21
.L12:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L13
	call	__stack_chk_fail@PLT
.L13:
	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
.LFE2028:
	.size	_ZL7readPPMPKc, .-_ZL7readPPMPKc
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2034:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2034:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1
.LC10:
	.string	"Error return from gettimeofday: %d"
	.text
	.globl	_Z7rtclockv
	.type	_Z7rtclockv, @function
_Z7rtclockv:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rsi
	leaq	8(%rsp), %rdi
	call	gettimeofday@PLT
	testl	%eax, %eax
	je	.L27
	movl	%eax, %edx
	leaq	.LC10(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L27:
	cvtsi2sdq	16(%rsp), %xmm0
	mulsd	.LC11(%rip), %xmm0
	cvtsi2sdq	8(%rsp), %xmm1
	addsd	%xmm1, %xmm0
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L28
	call	__stack_chk_fail@PLT
.L28:
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z7rtclockv, .-_Z7rtclockv
	.section	.rodata.str1.1
.LC12:
	.string	"P6\n"
.LC13:
	.string	"Histogram_GPU"
.LC14:
	.string	"# %s\n"
.LC15:
	.string	"%d %d\n"
.LC16:
	.string	"%d\n"
	.text
	.globl	_Z8writePPMP8PPMImage
	.type	_Z8writePPMP8PPMImage, @function
_Z8writePPMP8PPMImage:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	stdout(%rip), %rdi
	movl	$2, %esi
	leaq	.LC12(%rip), %rdx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	stdout(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC13(%rip), %rcx
	leaq	.LC14(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	4(%rbx), %r8d
	movl	(%rbx), %ecx
	movl	$2, %esi
	movq	stdout(%rip), %rdi
	leaq	.LC15(%rip), %rdx
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	stdout(%rip), %rdi
	movl	$255, %ecx
	xorl	%eax, %eax
	leaq	.LC16(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	imull	$3, (%rbx), %esi
	movq	8(%rbx), %rdi
	movslq	4(%rbx), %rdx
	movq	stdout(%rip), %rcx
	movslq	%esi, %rsi
	call	fwrite@PLT
	movq	stdout(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE2029:
	.size	_Z8writePPMP8PPMImage, .-_Z8writePPMP8PPMImage
	.globl	_Z47__device_stub__Z16smoothing_kernelP8PPMImageS0_P8PPMImageS0_
	.type	_Z47__device_stub__Z16smoothing_kernelP8PPMImageS0_P8PPMImageS0_, @function
_Z47__device_stub__Z16smoothing_kernelP8PPMImageS0_P8PPMImageS0_:
.LFB2056:
	.cfi_startproc
	endbr64
	subq	$120, %rsp
	.cfi_def_cfa_offset 128
	movq	%rdi, 8(%rsp)
	leaq	32(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movq	%rsi, (%rsp)
	leaq	40(%rsp), %rdi
	leaq	52(%rsp), %rsi
	movq	%fs:40, %rax
	movq	%rax, 104(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rax
	movl	$1, 48(%rsp)
	movq	%rax, 88(%rsp)
	movq	%rsp, %rax
	movq	%rax, 96(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 40(%rsp)
	movq	%rax, 52(%rsp)
	movl	$1, 60(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L35
	pushq	32(%rsp)
	.cfi_def_cfa_offset 136
	leaq	_Z16smoothing_kernelP8PPMImageS0_(%rip), %rdi
	pushq	32(%rsp)
	.cfi_def_cfa_offset 144
	movq	68(%rsp), %rcx
	movl	76(%rsp), %r8d
	movq	56(%rsp), %rsi
	movl	64(%rsp), %edx
	leaq	104(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 136
	popq	%rdx
	.cfi_def_cfa_offset 128
.L35:
	movq	104(%rsp), %rax
	subq	%fs:40, %rax
	je	.L37
	call	__stack_chk_fail@PLT
.L37:
	addq	$120, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2056:
	.size	_Z47__device_stub__Z16smoothing_kernelP8PPMImageS0_P8PPMImageS0_, .-_Z47__device_stub__Z16smoothing_kernelP8PPMImageS0_P8PPMImageS0_
	.globl	_Z16smoothing_kernelP8PPMImageS0_
	.type	_Z16smoothing_kernelP8PPMImageS0_, @function
_Z16smoothing_kernelP8PPMImageS0_:
.LFB2057:
	.cfi_startproc
	endbr64
	jmp	_Z47__device_stub__Z16smoothing_kernelP8PPMImageS0_P8PPMImageS0_
	.cfi_endproc
.LFE2057:
	.size	_Z16smoothing_kernelP8PPMImageS0_, .-_Z16smoothing_kernelP8PPMImageS0_
	.globl	_Z13smoothing_GPUP8PPMImageS0_
	.type	_Z13smoothing_GPUP8PPMImageS0_, @function
_Z13smoothing_GPUP8PPMImageS0_:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$16, %esi
	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
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movl	(%rdi), %r12d
	movl	4(%rdi), %r13d
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	leaq	16(%rsp), %r15
	leaq	24(%rsp), %r14
	call	cudaMalloc@PLT
	movl	%r12d, %ebp
	leaq	8(%rsp), %rdi
	movl	$16, %esi
	call	cudaMalloc@PLT
	imull	%r13d, %ebp
	movq	%r15, %rdi
	leaq	0(%rbp,%rbp,2), %rbp
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	movq	%rbp, %rsi
	movq	%r14, %rdi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$16, %edx
	call	cudaMemcpy@PLT
	movq	8(%rbx), %rsi
	movq	16(%rsp), %rdi
	movq	%rbp, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rax
	movl	$1, %ecx
	movq	%r15, %rsi
	movl	$8, %edx
	leaq	8(%rax), %rdi
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$16, %edx
	call	cudaMemcpy@PLT
	movq	8(%rbx), %rsi
	movq	24(%rsp), %rdi
	movq	%rbp, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rax
	movl	$1, %ecx
	movq	%r14, %rsi
	movl	$8, %edx
	leaq	8(%rax), %rdi
	call	cudaMemcpy@PLT
	cvtsi2ssq	%r13, %xmm0
	mulss	.LC17(%rip), %xmm0
	call	ceilf@PLT
	movd	%xmm0, %r13d
	cvtsi2ssq	%r12, %xmm0
	mulss	.LC17(%rip), %xmm0
	call	ceilf@PLT
	movd	%r13d, %xmm1
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	cvttss2siq	%xmm0, %rax
	movl	$1, %ecx
	movl	$1, %esi
	movabsq	$137438953504, %rdx
	movl	%eax, 32(%rsp)
	cvttss2siq	%xmm1, %rax
	movl	%eax, 36(%rsp)
	movq	32(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L45
	movq	8(%rsp), %rsi
	movq	(%rsp), %rdi
	call	_Z47__device_stub__Z16smoothing_kernelP8PPMImageS0_P8PPMImageS0_
.L45:
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	(%rsp), %rsi
	movl	$2, %ecx
	movq	%rbx, %rdi
	movl	$16, %edx
	movq	%rax, %r12
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rsi
	movq	%rbp, %rdx
	movq	%r12, %rdi
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	%r12, 8(%rbx)
	movq	(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L46
	call	__stack_chk_fail@PLT
.L46:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	_Z13smoothing_GPUP8PPMImageS0_, .-_Z13smoothing_GPUP8PPMImageS0_
	.section	.rodata.str1.1
.LC18:
	.string	"Too many or no one arguments supplied.\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2031:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rsi, %rbx
	pushq	%rcx
	.cfi_def_cfa_offset 32
	cmpl	$2, %edi
	je	.L49
	leaq	.LC18(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L49:
	movq	8(%rbx), %rbx
	movq	%rbx, %rdi
	call	_ZL7readPPMPKc
	movq	%rbx, %rdi
	movq	%rax, %rbp
	call	_ZL7readPPMPKc
	movq	%rbp, %rsi
	movq	%rax, %rbx
	movq	%rax, %rdi
	call	_Z13smoothing_GPUP8PPMImageS0_
	movq	%rbx, %rdi
	call	_Z8writePPMP8PPMImage
	movq	%rbp, %rdi
	call	free@PLT
	movq	%rbx, %rdi
	call	free@PLT
	popq	%rdx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	main, .-main
	.section	.rodata.str1.1
.LC19:
	.string	"_Z16smoothing_kernelP8PPMImageS0_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2059:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC19(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z16smoothing_kernelP8PPMImageS0_(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2059:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC11:
	.long	-1598689907
	.long	1051772663
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC17:
	.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 <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>


// MASK SIZE
#define MASK_WIDTH 5
// MASK RADIO
#define MASK_R (MASK_WIDTH-1)/2

#define COMMENT "Histogram_GPU"
#define RGB_COMPONENT_COLOR 255

// SIZE OF TILE
#define TILE_WIDTH 32
// SIZE OF SHARE MATRIX
#define SHARED_SIZE (MASK_WIDTH-1 + TILE_WIDTH)


typedef struct {
    unsigned char red, green, blue;
} PPMPixel;

typedef struct {
    int x, y;
    PPMPixel *data;
} PPMImage;

double rtclock()
{
    struct timezone Tzp;
    struct timeval Tp;
    int stat;
    stat = gettimeofday (&Tp, &Tzp);
    if (stat != 0) printf("Error return from gettimeofday: %d",stat);
    return(Tp.tv_sec + Tp.tv_usec*1.0e-6);
}


static PPMImage *readPPM(const char *filename) {
    char buff[16];
    PPMImage *img;
    FILE *fp;
    int c, rgb_comp_color;
    fp = fopen(filename, "rb");
    if (!fp) {
        fprintf(stderr, "Unable to open file '%s'\n", filename);
        exit(1);
    }

    if (!fgets(buff, sizeof(buff), fp)) {
        perror(filename);
        exit(1);
    }

    if (buff[0] != 'P' || buff[1] != '6') {
        fprintf(stderr, "Invalid image format (must be 'P6')\n");
        exit(1);
    }

    img = (PPMImage *) malloc(sizeof(PPMImage));
    if (!img) {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }

    c = getc(fp);
    while (c == '#') {
        while (getc(fp) != '\n')
            ;
        c = getc(fp);
    }

    ungetc(c, fp);
    if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
        fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
        exit(1);
    }

    if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
        fprintf(stderr, "Invalid rgb component (error loading '%s')\n",
                filename);
        exit(1);
    }

    if (rgb_comp_color != RGB_COMPONENT_COLOR) {
        fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
        exit(1);
    }

    while (fgetc(fp) != '\n')
        ;
    img->data = (PPMPixel*) malloc(img->x * img->y * sizeof(PPMPixel));

    if (!img) {
        fprintf(stderr, "Unable to allocate memory\n");
        exit(1);
    }

    if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
        fprintf(stderr, "Error loading image '%s'\n", filename);
        exit(1);
    }

    fclose(fp);
    return img;
}

void writePPM(PPMImage *img) {

    fprintf(stdout, "P6\n");
    fprintf(stdout, "# %s\n", COMMENT);
    fprintf(stdout, "%d %d\n", img->x, img->y);
    fprintf(stdout, "%d\n", RGB_COMPONENT_COLOR);

    fwrite(img->data, 3 * img->x, img->y, stdout);
    fclose(stdout);
}

__global__ void smoothing_kernel(PPMImage *d_image, PPMImage *d_image_copy) {
    // Creating variables
    int i, j, row, col;
    int total_red = 0, total_blue = 0, total_green = 0;
    int index_dst_y, index_dst_x, index_src_y, index_src_x;

    // Get Row and COl
    row = blockIdx.y * TILE_WIDTH + threadIdx.y;
    col = blockIdx.x * TILE_WIDTH + threadIdx.x;

    // Create Shared block of data
    __shared__ PPMPixel shared_image_data[SHARED_SIZE*SHARED_SIZE];

    // Filling the shared variable with a two-step for.
    // first step fill the data with index inside the blocks dim
    // -----------------
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // |x|x|x|x|x|x|x| |
    // | | | | | | | | |
    // -----------------
    // Second step fill the data with index outside the blocks dim
    // -----------------
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // | | | | | | | |x|
    // |x|x|x|x|x|x|x|x|
    // -----------------

    for (i = 0; i <= TILE_WIDTH * TILE_WIDTH; i = i + TILE_WIDTH * TILE_WIDTH)
    {
        // Get indexs of dst matrix
        index_dst_y = (threadIdx.y * TILE_WIDTH + threadIdx.x + i) / SHARED_SIZE;
        index_dst_x = (threadIdx.y * TILE_WIDTH + threadIdx.x + i) % SHARED_SIZE;

        // Get indexs of destination matrix
        index_src_y = (blockIdx.y * TILE_WIDTH) + index_dst_y - MASK_R;
        index_src_x = (blockIdx.x * TILE_WIDTH) + index_dst_x - MASK_R;

        //Work only if dst geral index stay into shared matrix size
        if (index_dst_y * SHARED_SIZE + index_dst_x < (SHARED_SIZE*SHARED_SIZE)) {
            // if src index stay into image save images values else save 0
            if (index_src_y >= 0 && index_src_y < d_image->y && index_src_x >= 0 && index_src_x < d_image->x){
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].red = d_image_copy->data[(index_src_y * d_image->x) + index_src_x].red;
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].blue = d_image_copy->data[(index_src_y * d_image->x) + index_src_x].blue;
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].green = d_image_copy->data[(index_src_y * d_image->x) + index_src_x].green;
            }
            else{
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].red = 0;
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].blue = 0;
                shared_image_data[index_dst_y * SHARED_SIZE + index_dst_x].green = 0;
            }
        }
    }

    // sync threads
    __syncthreads();

    // if row and col stay into image proceed with convolution
    if (row < d_image->y && col < d_image->x){
        for (i = 0; i < MASK_WIDTH; i++){
             for (j = 0; j < MASK_WIDTH; j++) {
                total_red += shared_image_data[((threadIdx.y + j) * SHARED_SIZE) + (threadIdx.x + i)].red;
                total_blue += shared_image_data[((threadIdx.y + j) * SHARED_SIZE) + (threadIdx.x + i)].blue;
                total_green += shared_image_data[((threadIdx.y + j) * SHARED_SIZE) + (threadIdx.x + i)].green;
             }
        }
        // Save data of convolution into devise image
        d_image->data[(row * d_image->x) + col].red = total_red / (MASK_WIDTH*MASK_WIDTH);
        d_image->data[(row * d_image->x) + col].blue = total_blue / (MASK_WIDTH*MASK_WIDTH);
        d_image->data[(row * d_image->x) + col].green = total_green / (MASK_WIDTH*MASK_WIDTH);
    }
}


void smoothing_GPU(PPMImage *image, PPMImage *image_copy) {

    unsigned int rows, cols, img_size;
    PPMImage *d_image, *d_image_copy;
    PPMPixel *d_pixels, *d_pixels_copy, *new_pixels;

    // Get data
    cols = image->x;
    rows = image->y;
    img_size = cols * rows;

    // Alloc structure to devise
    hipMalloc((void **)&d_image, sizeof(PPMImage));
    hipMalloc((void **)&d_image_copy, sizeof(PPMImage));

    // Alloc image to devise
    hipMalloc((void **)&d_pixels, sizeof(PPMPixel) * img_size);
    hipMalloc((void **)&d_pixels_copy, sizeof(PPMPixel) * img_size);

    // cpy stucture to devise
    hipMemcpy(d_image, image, sizeof(PPMImage), hipMemcpyHostToDevice);
    hipMemcpy(d_pixels, image->data, sizeof(PPMPixel) * img_size, hipMemcpyHostToDevice);
    hipMemcpy(&(d_image->data), &d_pixels, sizeof(PPMPixel *), hipMemcpyHostToDevice);

    hipMemcpy(d_image_copy, image, sizeof(PPMImage), hipMemcpyHostToDevice);
    hipMemcpy(d_pixels_copy, image->data, sizeof(PPMPixel) * img_size, hipMemcpyHostToDevice);
    hipMemcpy(&(d_image_copy->data), &d_pixels_copy, sizeof(PPMPixel *), hipMemcpyHostToDevice);

    dim3 dimGrid(ceil((float)cols / TILE_WIDTH), ceil((float)rows / TILE_WIDTH), 1);
    dim3 dimBlock(TILE_WIDTH, TILE_WIDTH, 1);

    // Call function
    smoothing_kernel<<<dimGrid, dimBlock>>>(d_image, d_image_copy);


    new_pixels = (PPMPixel *) malloc(img_size * sizeof(PPMPixel));
    // Copy result to local array
    hipMemcpy(image, d_image, sizeof(PPMImage), hipMemcpyDeviceToHost);
    hipMemcpy(new_pixels, d_pixels, sizeof(PPMPixel) * img_size, hipMemcpyDeviceToHost);
    image->data = new_pixels;

    //Free memory
    hipFree(d_image);
    hipFree(d_image_copy);
    hipFree(d_pixels);
    hipFree(d_pixels_copy);
}

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

    if( argc != 2 ) {
        printf("Too many or no one arguments supplied.\n");
    }

    //double t_start, t_end;
    //int i;
    char *filename = argv[1]; //Recebendo o arquivo!;

    PPMImage *image = readPPM(filename);
    PPMImage *image_output = readPPM(filename);

    //t_start = rtclock();
    smoothing_GPU(image_output, image);
    //t_end = rtclock();

    writePPM(image_output);

    //fprintf(stdout, "\n%0.6lfs\n", t_end - t_start);
    free(image);
    free(image_output);
}

	.text
	.file	"smooth_gpu.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z7rtclockv
.LCPI0_0:
	.quad	0x3eb0c6f7a0b5ed8d              # double 9.9999999999999995E-7
	.text
	.globl	_Z7rtclockv
	.type	_Z7rtclockv,@function
_Z7rtclockv:                            # @_Z7rtclockv
	.cfi_startproc
# %bb.0:
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	%rsp, %rdi
	leaq	16(%rsp), %rsi
	callq	gettimeofday
	testl	%eax, %eax
	je	.LBB0_2
# %bb.1:
	movl	$.L.str, %edi
	movl	%eax, %esi
	xorl	%eax, %eax
	callq	printf
.LBB0_2:
	cvtsi2sdq	(%rsp), %xmm1
	cvtsi2sdq	8(%rsp), %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	addsd	%xmm1, %xmm0
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z7rtclockv, .Lfunc_end0-_Z7rtclockv
	.cfi_endproc
                                        # -- End function
	.globl	_Z8writePPMP8PPMImage           # -- Begin function _Z8writePPMP8PPMImage
	.type	_Z8writePPMP8PPMImage,@function
_Z8writePPMP8PPMImage:                  # @_Z8writePPMP8PPMImage
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	stdout(%rip), %rcx
	movl	$.L.str.1, %edi
	movl	$3, %esi
	movl	$1, %edx
	callq	fwrite
	movq	stdout(%rip), %rdi
	movl	$.L.str.2, %esi
	movl	$.L.str.3, %edx
	xorl	%eax, %eax
	callq	fprintf
	movq	stdout(%rip), %rdi
	movl	(%rbx), %edx
	movl	4(%rbx), %ecx
	movl	$.L.str.4, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	stdout(%rip), %rdi
	movl	$.L.str.5, %esi
	movl	$255, %edx
	xorl	%eax, %eax
	callq	fprintf
	movq	8(%rbx), %rdi
	movslq	(%rbx), %rax
	leaq	(%rax,%rax,2), %rsi
	movslq	4(%rbx), %rdx
	movq	stdout(%rip), %rcx
	callq	fwrite
	movq	stdout(%rip), %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.Lfunc_end1:
	.size	_Z8writePPMP8PPMImage, .Lfunc_end1-_Z8writePPMP8PPMImage
	.cfi_endproc
                                        # -- End function
	.globl	_Z31__device_stub__smoothing_kernelP8PPMImageS0_ # -- Begin function _Z31__device_stub__smoothing_kernelP8PPMImageS0_
	.type	_Z31__device_stub__smoothing_kernelP8PPMImageS0_,@function
_Z31__device_stub__smoothing_kernelP8PPMImageS0_: # @_Z31__device_stub__smoothing_kernelP8PPMImageS0_
	.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	$80, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	24(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	16(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	64(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	leaq	48(%rsp), %r14
	leaq	32(%rsp), %r15
	leaq	8(%rsp), %r12
	movq	%rsp, %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z16smoothing_kernelP8PPMImageS0_, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$96, %rsp
	.cfi_adjust_cfa_offset -96
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z31__device_stub__smoothing_kernelP8PPMImageS0_, .Lfunc_end2-_Z31__device_stub__smoothing_kernelP8PPMImageS0_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z13smoothing_GPUP8PPMImageS0_
.LCPI3_0:
	.long	0x3d000000                      # float 0.03125
	.text
	.globl	_Z13smoothing_GPUP8PPMImageS0_
	.type	_Z13smoothing_GPUP8PPMImageS0_,@function
_Z13smoothing_GPUP8PPMImageS0_:         # @_Z13smoothing_GPUP8PPMImageS0_
	.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	%rdi, %rbx
	movl	(%rdi), %r15d
	movl	4(%rdi), %r14d
	movq	%r14, 24(%rsp)                  # 8-byte Spill
                                        # kill: def $r14d killed $r14d killed $r14 def $r14
	imull	%r15d, %r14d
	leaq	8(%rsp), %r13
	movl	$16, %esi
	movq	%r13, %rdi
	callq	hipMalloc
	movq	%rsp, %rdi
	movl	$16, %esi
	callq	hipMalloc
	leaq	(%r14,%r14,2), %r14
	leaq	16(%rsp), %rbp
	movq	%rbp, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	leaq	32(%rsp), %r12
	movq	%r12, %rdi
	movq	%r14, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movl	$16, %edx
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%rbp), %rdi
	movq	8(%rbx), %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	addq	$8, %rdi
	movl	$8, %edx
	movq	%rbp, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	%rsp, %r13
	movq	(%r13), %rdi
	movl	$16, %edx
	movq	%rbx, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r12), %rdi
	movq	8(%rbx), %rsi
	movq	%r14, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r13), %rdi
	addq	$8, %rdi
	movl	$8, %edx
	movq	%r12, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	cvtsi2ss	%r15, %xmm0
	mulss	.LCPI3_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %r15
	xorps	%xmm0, %xmm0
	cvtsi2ssq	24(%rsp), %xmm0         # 8-byte Folded Reload
	mulss	.LCPI3_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %rdi
	movl	%r15d, %eax
	shlq	$32, %rdi
	orq	%rax, %rdi
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_2
# %bb.1:
	movq	8(%rsp), %rdi
	movq	(%rsp), %rsi
	callq	_Z31__device_stub__smoothing_kernelP8PPMImageS0_
.LBB3_2:
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	8(%rsp), %rsi
	movl	$16, %edx
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	16(%rsp), %rsi
	movq	%r15, %rdi
	movq	%r14, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%r15, 8(%rbx)
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipFree
	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_end3:
	.size	_Z13smoothing_GPUP8PPMImageS0_, .Lfunc_end3-_Z13smoothing_GPUP8PPMImageS0_
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movq	%rsi, %rbx
	cmpl	$2, %edi
	je	.LBB4_2
# %bb.1:
	movl	$.Lstr, %edi
	callq	puts@PLT
.LBB4_2:
	movq	8(%rbx), %rbx
	movq	%rbx, %rdi
	callq	_ZL7readPPMPKc
	movq	%rax, %r14
	movq	%rbx, %rdi
	callq	_ZL7readPPMPKc
	movq	%rax, %rbx
	movq	%rax, %rdi
	callq	_Z13smoothing_GPUP8PPMImageS0_
	movq	%rbx, %rdi
	callq	_Z8writePPMP8PPMImage
	movq	%r14, %rdi
	callq	free
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
                                        # -- End function
	.type	_ZL7readPPMPKc,@function        # -- Begin function _ZL7readPPMPKc
_ZL7readPPMPKc:                         # @_ZL7readPPMPKc
	.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	$32, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %rbx
	movl	$.L.str.7, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB5_14
# %bb.1:
	movq	%rax, %r14
	leaq	16(%rsp), %rdi
	movl	$16, %esi
	movq	%rax, %rdx
	callq	fgets
	testq	%rax, %rax
	je	.LBB5_15
# %bb.2:
	cmpb	$80, 16(%rsp)
	jne	.LBB5_16
# %bb.3:
	cmpb	$54, 17(%rsp)
	jne	.LBB5_16
# %bb.4:
	movl	$16, %edi
	callq	malloc
	testq	%rax, %rax
	je	.LBB5_17
# %bb.5:
	movq	%rax, %r15
.LBB5_6:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB5_7 Depth 2
	movq	%r14, %rdi
	callq	getc
	cmpl	$35, %eax
	jne	.LBB5_8
.LBB5_7:                                # %.preheader44
                                        #   Parent Loop BB5_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%r14, %rdi
	callq	getc
	cmpl	$10, %eax
	jne	.LBB5_7
	jmp	.LBB5_6
.LBB5_8:                                # %._crit_edge
	movl	%eax, %edi
	movq	%r14, %rsi
	callq	ungetc
	movq	%r15, %rcx
	addq	$4, %rcx
	movl	$.L.str.11, %esi
	movq	%r14, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$2, %eax
	jne	.LBB5_20
# %bb.9:
	leaq	12(%rsp), %rdx
	movl	$.L.str.13, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB5_21
# %bb.10:
	cmpl	$255, 12(%rsp)
	jne	.LBB5_22
.LBB5_11:                               # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	fgetc
	cmpl	$10, %eax
	jne	.LBB5_11
# %bb.12:
	movslq	(%r15), %rax
	movslq	4(%r15), %r12
	leaq	(%rax,%rax,2), %r13
	movq	%r13, %rdi
	imulq	%r12, %rdi
	callq	malloc
	movq	%rax, 8(%r15)
	movq	%rax, %rdi
	movq	%r13, %rsi
	movq	%r12, %rdx
	movq	%r14, %rcx
	callq	fread
	movslq	4(%r15), %rcx
	cmpq	%rcx, %rax
	jne	.LBB5_23
# %bb.13:
	movq	%r14, %rdi
	callq	fclose
	movq	%r15, %rax
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	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
.LBB5_14:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rdi
	movl	$.L.str.8, %esi
	jmp	.LBB5_24
.LBB5_15:
	movq	%rbx, %rdi
	callq	perror
	jmp	.LBB5_19
.LBB5_16:
	movq	stderr(%rip), %rcx
	movl	$.L.str.9, %edi
	movl	$36, %esi
	jmp	.LBB5_18
.LBB5_17:
	movq	stderr(%rip), %rcx
	movl	$.L.str.10, %edi
	movl	$26, %esi
.LBB5_18:
	movl	$1, %edx
	callq	fwrite
	jmp	.LBB5_19
.LBB5_20:
	movq	stderr(%rip), %rdi
	movl	$.L.str.12, %esi
	jmp	.LBB5_24
.LBB5_21:
	movq	stderr(%rip), %rdi
	movl	$.L.str.14, %esi
	jmp	.LBB5_24
.LBB5_22:
	movq	stderr(%rip), %rdi
	movl	$.L.str.15, %esi
	jmp	.LBB5_24
.LBB5_23:
	movq	stderr(%rip), %rdi
	movl	$.L.str.16, %esi
.LBB5_24:
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
.LBB5_19:
	movl	$1, %edi
	callq	exit
.Lfunc_end5:
	.size	_ZL7readPPMPKc, .Lfunc_end5-_ZL7readPPMPKc
	.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	.LBB6_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB6_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16smoothing_kernelP8PPMImageS0_, %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_end6:
	.size	__hip_module_ctor, .Lfunc_end6-__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	.LBB7_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
.LBB7_2:
	retq
.Lfunc_end7:
	.size	__hip_module_dtor, .Lfunc_end7-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Error return from gettimeofday: %d"
	.size	.L.str, 35

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

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

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"Histogram_GPU"
	.size	.L.str.3, 14

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

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

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

	.type	.L.str.7,@object                # @.str.7
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.7:
	.asciz	"rb"
	.size	.L.str.7, 3

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Unable to open file '%s'\n"
	.size	.L.str.8, 26

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"Invalid image format (must be 'P6')\n"
	.size	.L.str.9, 37

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"Unable to allocate memory\n"
	.size	.L.str.10, 27

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

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"Invalid image size (error loading '%s')\n"
	.size	.L.str.12, 41

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

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"Invalid rgb component (error loading '%s')\n"
	.size	.L.str.14, 44

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"'%s' does not have 8-bits components\n"
	.size	.L.str.15, 38

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Error loading image '%s'\n"
	.size	.L.str.16, 26

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z16smoothing_kernelP8PPMImageS0_"
	.size	.L__unnamed_1, 34

	.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	"Too many or no one arguments supplied."
	.size	.Lstr, 39

	.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 _Z31__device_stub__smoothing_kernelP8PPMImageS0_
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z16smoothing_kernelP8PPMImageS0_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z16smoothing_kernelP8PPMImageS0_ ; -- Begin function _Z16smoothing_kernelP8PPMImageS0_
	.globl	_Z16smoothing_kernelP8PPMImageS0_
	.p2align	8
	.type	_Z16smoothing_kernelP8PPMImageS0_,@function
_Z16smoothing_kernelP8PPMImageS0_:      ; @_Z16smoothing_kernelP8PPMImageS0_
; %bb.0:
	s_load_b128 s[4:7], s[0:1], 0x0
	v_bfe_u32 v1, v0, 10, 10
	v_and_b32_e32 v2, 0x3ff, v0
	v_mov_b32_e32 v4, 0
	s_lshl_b32 s2, s15, 5
	s_lshl_b32 s1, s14, 5
	s_add_i32 s3, s2, -2
	v_lshl_add_u32 v3, v1, 5, v2
	s_add_i32 s8, s1, -2
	s_mov_b32 s0, 0
	s_mov_b32 s9, -1
                                        ; implicit-def: $vgpr5
                                        ; implicit-def: $vgpr0
.LBB0_1:                                ; =>This Inner Loop Header: Depth=1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v7, s0, v3
	s_mov_b32 s10, exec_lo
	v_cmpx_gt_u32_e32 0x510, v7
	s_cbranch_execz .LBB0_11
; %bb.2:                                ;   in Loop: Header=BB0_1 Depth=1
	v_mul_hi_u32 v6, 0x38e38e39, v7
	s_mov_b32 s0, 0
	s_mov_b32 s12, exec_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshrrev_b32_e32 v8, 3, v6
	v_add_nc_u32_e32 v6, s3, v8
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_i32_e64 s11, 0, v6
	v_cmpx_lt_i32_e32 -1, v6
	s_cbranch_execz .LBB0_6
; %bb.3:                                ;   in Loop: Header=BB0_1 Depth=1
	s_waitcnt lgkmcnt(0)
	s_load_b32 s0, s[4:5], 0x4
	v_mul_lo_u32 v0, v8, 36
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_sub_nc_u32_e32 v0, v7, v0
	v_add_nc_u32_e32 v0, s8, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_cmp_lt_i32_e32 vcc_lo, -1, v0
	s_waitcnt lgkmcnt(0)
	v_cmp_gt_i32_e64 s0, s0, v6
	s_and_b32 s15, vcc_lo, s0
	s_mov_b32 s0, 0
	s_xor_b32 s13, s15, -1
	s_and_saveexec_b32 s14, s15
	s_cbranch_execz .LBB0_5
; %bb.4:                                ;   in Loop: Header=BB0_1 Depth=1
	s_load_b32 s15, s[4:5], 0x0
	s_and_not1_b32 s13, s13, exec_lo
	s_mov_b32 s0, exec_lo
	s_waitcnt lgkmcnt(0)
	v_cmp_le_i32_e32 vcc_lo, s15, v0
	v_mov_b32_e32 v5, s15
	s_and_b32 s15, vcc_lo, exec_lo
	s_delay_alu instid0(SALU_CYCLE_1)
	s_or_b32 s13, s13, s15
.LBB0_5:                                ; %Flow135
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s14
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 s11, s11, exec_lo
	s_and_b32 s13, s13, exec_lo
	s_and_b32 s0, s0, exec_lo
	s_or_b32 s11, s11, s13
.LBB0_6:                                ; %Flow134
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s12
	v_lshl_add_u32 v7, v7, 1, v7
                                        ; implicit-def: $sgpr12
	s_and_saveexec_b32 s13, s11
	s_cbranch_execz .LBB0_8
; %bb.7:                                ;   in Loop: Header=BB0_1 Depth=1
	s_mov_b32 s12, 0
	s_and_not1_b32 s0, s0, exec_lo
	ds_store_b8 v7, v4
	ds_store_b8 v7, v4 offset:2
.LBB0_8:                                ; %Flow136
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s13
	v_mov_b32_e32 v8, s12
	s_and_saveexec_b32 s11, s0
	s_cbranch_execz .LBB0_10
; %bb.9:                                ;   in Loop: Header=BB0_1 Depth=1
	s_waitcnt lgkmcnt(0)
	s_load_b64 s[12:13], s[6:7], 0x8
	v_mad_u64_u32 v[8:9], null, v5, v6, v[0:1]
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_i64_i32 v[9:10], null, v8, 3, s[12:13]
	s_clause 0x1
	global_load_u16 v6, v[9:10], off
	global_load_u8 v9, v[9:10], off offset:2
	s_waitcnt vmcnt(1)
	ds_store_b8 v7, v6
	s_waitcnt vmcnt(0)
	ds_store_b8 v7, v9 offset:2
	v_lshrrev_b32_e32 v8, 8, v6
.LBB0_10:                               ; %.sink.split
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s11
	ds_store_b8 v7, v8 offset:1
.LBB0_11:                               ; %Flow137
                                        ;   in Loop: Header=BB0_1 Depth=1
	s_or_b32 exec_lo, exec_lo, s10
	s_xor_b32 s9, s9, -1
	s_movk_i32 s0, 0x400
	s_and_not1_b32 vcc_lo, exec_lo, s9
	s_mov_b32 s9, 0
	s_cbranch_vccnz .LBB0_1
; %bb.12:
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
	s_load_b32 s0, s[4:5], 0x4
	v_add_nc_u32_e32 v3, s2, v1
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_i32_e32 vcc_lo, s0, v3
	s_and_saveexec_b32 s0, vcc_lo
	s_cbranch_execz .LBB0_19
; %bb.13:
	s_load_b32 s0, s[4:5], 0x0
	v_add_nc_u32_e32 v0, s1, v2
	s_waitcnt lgkmcnt(0)
	s_delay_alu instid0(VALU_DEP_1)
	v_cmp_gt_i32_e32 vcc_lo, s0, v0
	s_and_b32 exec_lo, exec_lo, vcc_lo
	s_cbranch_execz .LBB0_19
; %bb.14:                               ; %.preheader.preheader
	v_mul_u32_u24_e32 v5, 3, v2
	v_mov_b32_e32 v4, 0
	v_mov_b32_e32 v2, 0
	s_mov_b32 s1, 0
	s_delay_alu instid0(VALU_DEP_3)
	v_mad_u32_u24 v5, 0x6c, v1, v5
	v_mov_b32_e32 v1, 0
.LBB0_15:                               ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_16 Depth 2
	s_mov_b32 s2, 0
.LBB0_16:                               ;   Parent Loop BB0_15 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_add_nc_u32_e32 v6, s2, v5
	s_addk_i32 s2, 0x6c
	ds_load_u8 v7, v6
	ds_load_u8 v8, v6 offset:2
	ds_load_u8 v6, v6 offset:1
	s_cmpk_lg_i32 s2, 0x21c
	s_waitcnt lgkmcnt(2)
	v_add_nc_u32_e32 v4, v4, v7
	s_waitcnt lgkmcnt(1)
	v_add_nc_u32_e32 v2, v2, v8
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v1, v1, v6
	s_cbranch_scc1 .LBB0_16
; %bb.17:                               ;   in Loop: Header=BB0_15 Depth=1
	v_add_nc_u32_e32 v5, 3, v5
	s_add_i32 s1, s1, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_lg_u32 s1, 5
	s_cbranch_scc1 .LBB0_15
; %bb.18:
	s_load_b64 s[2:3], s[4:5], 0x8
	v_mul_hi_i32 v4, 0x51eb851f, v4
	v_mul_hi_i32 v1, 0x51eb851f, v1
	v_mul_hi_i32 v5, 0x51eb851f, v2
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_lshrrev_b32_e32 v6, 31, v4
	v_lshrrev_b32_e32 v7, 31, v1
	v_lshrrev_b32_e32 v8, 3, v1
	v_lshrrev_b32_e32 v4, 3, v4
	v_mad_u64_u32 v[1:2], null, s0, v3, v[0:1]
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v0, v8, v7
	v_add_nc_u32_e32 v2, v4, v6
	v_lshrrev_b32_e32 v4, 31, v5
	v_lshrrev_b32_e32 v5, 3, v5
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_lshlrev_b16 v0, 8, v0
	v_and_b32_e32 v6, 0xff, v2
	s_waitcnt lgkmcnt(0)
	v_mad_i64_i32 v[2:3], null, v1, 3, s[2:3]
	v_add_nc_u32_e32 v1, v5, v4
	s_delay_alu instid0(VALU_DEP_3)
	v_or_b32_e32 v0, v6, v0
	s_clause 0x1
	global_store_b8 v[2:3], v1, off offset:2
	global_store_b16 v[2:3], v0, off
.LBB0_19:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16smoothing_kernelP8PPMImageS0_
		.amdhsa_group_segment_fixed_size 3888
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 16
		.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 11
		.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	_Z16smoothing_kernelP8PPMImageS0_, .Lfunc_end0-_Z16smoothing_kernelP8PPMImageS0_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 828
; NumSgprs: 18
; NumVgprs: 11
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 3888 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 11
; Occupancy: 16
; WaveLimiterHint : 1
; 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
    .group_segment_fixed_size: 3888
    .kernarg_segment_align: 8
    .kernarg_segment_size: 16
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z16smoothing_kernelP8PPMImageS0_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z16smoothing_kernelP8PPMImageS0_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     11
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
