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

#define MASK_WIDTH 5
#define BLOCK_SIZE 8

#define COMMENT "Histogram_GPU"
#define RGB_COMPONENT_COLOR 255

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(PPMPixel *image, PPMPixel *image_copy, int size_x, int size_y) {
    __shared__ PPMPixel image_ds[BLOCK_SIZE + MASK_WIDTH - 1][BLOCK_SIZE + MASK_WIDTH - 1];

    int total_red; 
    int total_blue; 
    int total_green;

    int n = (MASK_WIDTH - 1)/2;

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

    // NOTES(ciroceissler): calcula apenas o elementos internos
    image_ds[n + threadIdx.x][n + threadIdx.y]  = image_copy[(y * size_x) + x] ;

    int halo_index_left   = (blockIdx.x - 1)*blockDim.x + threadIdx.x;
    int halo_index_right  = (blockIdx.x + 1)*blockDim.x + threadIdx.x;
    int halo_index_top    = (blockIdx.y - 1)*blockDim.y + threadIdx.y;
    int halo_index_bottom = (blockIdx.y + 1)*blockDim.y + threadIdx.y;

    // NOTES(ciroceissler): calcula os elementos do halo left
    if (threadIdx.x >= blockDim.x - n) {
        if (halo_index_left >= 0) {
            image_ds[threadIdx.x - (blockDim.x - n)][threadIdx.y + n]
                = image_copy[(y * size_x) + halo_index_left];
        } else {
            image_ds[threadIdx.x - (blockDim.x - n)][threadIdx.y + n] = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo right
    if (threadIdx.x < n) {
        if (halo_index_right < size_x) {
            image_ds[n + blockDim.x + threadIdx.x][threadIdx.y + n]
                = image_copy[(y * size_x) + halo_index_right];
        } else {
            image_ds[n + blockDim.x + threadIdx.x][threadIdx.y + n] = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo top
    if (threadIdx.y >= blockDim.y - n) {
        if (halo_index_top >= 0) {
            image_ds[threadIdx.x + n][threadIdx.y - (blockDim.y - n)]
                = image_copy[(halo_index_top * size_x) + x];
        } else {
            image_ds[threadIdx.x + n][threadIdx.y - (blockDim.y - n)] = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo bottom
    if (threadIdx.y < n) {
        if (halo_index_bottom < size_y) {
            image_ds[threadIdx.x + n][n + blockDim.y + threadIdx.y]
                = image_copy[(halo_index_bottom * size_x) + x];
        } else {
            image_ds[threadIdx.x + n][n + blockDim.y + threadIdx.y] = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo top-left
    if (threadIdx.x >= blockDim.x - n && threadIdx.y >= blockDim.y - n) {
        if (halo_index_left >= 0 && halo_index_top >= 0) {
            image_ds[threadIdx.x - (blockDim.x - n)][threadIdx.y - (blockDim.y - n)]
                = image_copy[(halo_index_top * size_x) + halo_index_left];
        } else {
            image_ds[threadIdx.x - (blockDim.x - n)][threadIdx.y - (blockDim.y - n)] 
                = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo top-right
    if (threadIdx.x < n && threadIdx.y >= blockDim.y - n) {
        if (halo_index_right < size_x && halo_index_top >= 0) {
            image_ds[n + blockDim.x + threadIdx.x][threadIdx.y - (blockDim.y - n)]
                = image_copy[(halo_index_top * size_x) + halo_index_right];
        } else {
            image_ds[n + blockDim.x + threadIdx.x][threadIdx.y - (blockDim.y - n)]
                = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo bottom-left
    if (threadIdx.x >= blockDim.x - n && threadIdx.y < n) {
        if (halo_index_left >= 0 && halo_index_bottom < size_y) {
            image_ds[threadIdx.x - (blockDim.x - n)][n + blockDim.y + threadIdx.y]
                = image_copy[(halo_index_bottom * size_x) + halo_index_left];
        } else {
            image_ds[threadIdx.x - (blockDim.x - n)][n + blockDim.y + threadIdx.y]
                = (PPMPixel) {0,0,0};
        }
    }
    
    // NOTES(ciroceissler): calcula os elementos do halo bottom-right
    if (threadIdx.x < n && threadIdx.y < n) {
        if (halo_index_right < size_x && halo_index_bottom < size_y) {
            image_ds[n + blockDim.x + threadIdx.x][n + blockDim.y + threadIdx.y]
                = image_copy[(halo_index_bottom * size_x) + halo_index_right];
        } else {
            image_ds[n + blockDim.x + threadIdx.x][n + blockDim.y + threadIdx.y]
                = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): sincronizar todas as threads
    __syncthreads();

    total_red   = 0;
    total_blue  = 0;
    total_green = 0;

    // NOTES(ciroceissler): calculo do smooth
    for (int j = threadIdx.y; j < threadIdx.y + MASK_WIDTH; j++) {
        for (int i = threadIdx.x; i < threadIdx.x + MASK_WIDTH; i++) {
            total_red   += image_ds[i][j].red   ;
            total_blue  += image_ds[i][j].blue  ;
            total_green += image_ds[i][j].green ;
        } 
    } 

    image[(y * size_x) + x].red   = total_red   / (MASK_WIDTH*MASK_WIDTH);
    image[(y * size_x) + x].blue  = total_blue  / (MASK_WIDTH*MASK_WIDTH);
    image[(y * size_x) + x].green = total_green / (MASK_WIDTH*MASK_WIDTH);
}

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

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

#ifdef __DEBUG__
    double t_start, t_end;
#endif // __DEBUG__

    char *filename = argv[1]; //Recebendo o arquivo!;

    std::size_t size_image;

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

    size_image = sizeof(PPMPixel)*image->x*image->y;

    // NOTES(ciroceissler): variaveis do device
    PPMPixel *d_image;
    PPMPixel *d_image_output;

    // NOTES(ciroceissler): dimensoes
    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE, 1);
    dim3 dimGrid(ceil((float)image->x / BLOCK_SIZE), ceil((float)image->y / BLOCK_SIZE), 1);

#ifdef __DEBUG__
    t_start = rtclock();
#endif // __DEBUG__

    // NOTES(ciroceissler): alocacao dos buffers do device
    cudaMalloc((void **) &d_image       , size_image);
    cudaMalloc((void **) &d_image_output, size_image);

    // NOTES(ciroceissler): copiar os vetores para o device
    cudaMemcpy(d_image, image->data, size_image, cudaMemcpyHostToDevice); 

    // NOTES(ciroceissler): execucaco do kernel
    smoothing_kernel<<<dimGrid, dimBlock>>>(d_image_output, d_image, image->x, image->y);
    cudaDeviceSynchronize();

    // NOTES(ciroceissler): copiar o valor da GPU
    cudaMemcpy(image_output->data, d_image_output, size_image, cudaMemcpyDeviceToHost);


#ifdef __DEBUG__
    t_end = rtclock();

    fprintf(stdout, "\n%0.6lfs\n", t_end - t_start);  
#endif // __DEBUG__

    writePPM(image_output);

    free(image);
    free(image_output);
}

// ------------------------------------------------------------------------------------
// input    | cpu_serial  | gpu_nosharedmemory | gpu_sharedmemory | speedup (cpu/gpusm)
// ------------------------------------------------------------------------------------
// arq1.ppm | 0.302824    | 0.667255           | 0.633555         |  0.4779758663
// arq2.ppm | 0.678099    | 0.755541           | 0.655261         |  1.034853287
// arq3.ppm | 2.710807    | 1.076326           | 0.751093         |  3.609149599
// ------------------------------------------------------------------------------------
// 
// - numero de acesso a memoria global substituido por shared memory:
//
// TILE_ACCESS = (BLOCK_SIZE*BLOCK_SIZE) * (MASK_WIDTH*MASK_WIDTH)
//
// - elementos carregados para cada TILE:
//
// INPUT_ACCESS = (BLOCK_SIZE + MASK_WIDTH - 1)^2
//
// - reduction:
//
// REDUCTION = TILE_ACCESS/INPUT_ACCESS
//
// ---------------------------------------------------------------
// MASK_WIDHT\BLOCK_SIZE |  8X8  | 14X14 | 15X15 | 16X16 | 32X32
// ---------------------------------------------------------------
//           5           | 11.11 | 15.12 | 15.58 | 16.00 | 19.75
//           7           | 16.00 | 24.01 | 25.00 | 25.92 | 34.75
//           9           | 20.25 | 32.80 | 34.45 | 36.00 | 51.84
//           11          | 23.90 | 41.17 | 43.56 | 45.82 | 70.24
//           13          | 27.04 | 49.00 | 52.16 | 55.18 | 89.39
// ---------------------------------------------------------------
//
// taf!

	.file	"tmpxft_0034f059_00000000-6_smooth.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:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.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	_Z49__device_stub__Z16smoothing_kernelP8PPMPixelS0_iiP8PPMPixelS0_ii
	.type	_Z49__device_stub__Z16smoothing_kernelP8PPMPixelS0_iiP8PPMPixelS0_ii, @function
_Z49__device_stub__Z16smoothing_kernelP8PPMPixelS0_iiP8PPMPixelS0_ii:
.LFB2055:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	40(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	48(%rsp), %rcx
	movq	%fs:40, %rax
	movq	%rax, 136(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 64(%rsp)
	movq	%rax, 104(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 128(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 56(%rsp)
	movq	%rax, 68(%rsp)
	movl	$1, 76(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L35
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z16smoothing_kernelP8PPMPixelS0_ii(%rip), %rdi
	pushq	48(%rsp)
	.cfi_def_cfa_offset 176
	movq	84(%rsp), %rcx
	movl	92(%rsp), %r8d
	movq	72(%rsp), %rsi
	movl	80(%rsp), %edx
	leaq	120(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 168
	popq	%rdx
	.cfi_def_cfa_offset 160
.L35:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L37
	call	__stack_chk_fail@PLT
.L37:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2055:
	.size	_Z49__device_stub__Z16smoothing_kernelP8PPMPixelS0_iiP8PPMPixelS0_ii, .-_Z49__device_stub__Z16smoothing_kernelP8PPMPixelS0_iiP8PPMPixelS0_ii
	.globl	_Z16smoothing_kernelP8PPMPixelS0_ii
	.type	_Z16smoothing_kernelP8PPMPixelS0_ii, @function
_Z16smoothing_kernelP8PPMPixelS0_ii:
.LFB2056:
	.cfi_startproc
	endbr64
	jmp	_Z49__device_stub__Z16smoothing_kernelP8PPMPixelS0_iiP8PPMPixelS0_ii
	.cfi_endproc
.LFE2056:
	.size	_Z16smoothing_kernelP8PPMPixelS0_ii, .-_Z16smoothing_kernelP8PPMPixelS0_ii
	.section	.rodata.str1.1
.LC17:
	.string	"Too many or no one arguments supplied.\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rsi, %rbx
	subq	$72, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	cmpl	$2, %edi
	je	.L41
	leaq	.LC17(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
.L41:
	movq	8(%rbx), %rbp
	movq	%rbp, %rdi
	call	_ZL7readPPMPKc
	movq	%rbp, %rdi
	movq	%rax, %rbx
	call	_ZL7readPPMPKc
	movslq	4(%rbx), %rdx
	movslq	(%rbx), %rbp
	movl	$1, 40(%rsp)
	movq	%rax, %r12
	movq	%rdx, %rax
	movq	%rbp, %r13
	cvtsi2ssl	%eax, %xmm0
	mulss	.LC18(%rip), %xmm0
	imulq	%rdx, %rbp
	movabsq	$34359738376, %rdx
	movq	%rdx, 32(%rsp)
	leaq	0(%rbp,%rbp,2), %rbp
	call	ceilf@PLT
	movss	%xmm0, 12(%rsp)
	cvtsi2ssl	%r13d, %xmm0
	mulss	.LC18(%rip), %xmm0
	call	ceilf@PLT
	movss	12(%rsp), %xmm1
	movq	%rbp, %rsi
	leaq	16(%rsp), %rdi
	cvttss2siq	%xmm0, %rax
	movl	$1, 52(%rsp)
	movl	%eax, 44(%rsp)
	cvttss2siq	%xmm1, %rax
	movl	%eax, 48(%rsp)
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movq	%rbp, %rsi
	call	cudaMalloc@PLT
	movq	8(%rbx), %rsi
	movq	16(%rsp), %rdi
	movq	%rbp, %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	.L42
	movl	4(%rbx), %ecx
	movl	(%rbx), %edx
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdi
	call	_Z49__device_stub__Z16smoothing_kernelP8PPMPixelS0_iiP8PPMPixelS0_ii
.L42:
	call	cudaDeviceSynchronize@PLT
	movq	24(%rsp), %rsi
	movq	8(%r12), %rdi
	movq	%rbp, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	%r12, %rdi
	call	_Z8writePPMP8PPMImage
	movq	%rbx, %rdi
	call	free@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L43
	call	__stack_chk_fail@PLT
.L43:
	addq	$72, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.section	.rodata.str1.1
.LC19:
	.string	"_Z16smoothing_kernelP8PPMPixelS0_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2058:
	.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_kernelP8PPMPixelS0_ii(%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
.LFE2058:
	.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
.LC18:
	.long	1040187392
	.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>

#define MASK_WIDTH 5
#define BLOCK_SIZE 8

#define COMMENT "Histogram_GPU"
#define RGB_COMPONENT_COLOR 255

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(PPMPixel *image, PPMPixel *image_copy, int size_x, int size_y) {
    __shared__ PPMPixel image_ds[BLOCK_SIZE + MASK_WIDTH - 1][BLOCK_SIZE + MASK_WIDTH - 1];

    int total_red; 
    int total_blue; 
    int total_green;

    int n = (MASK_WIDTH - 1)/2;

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

    // NOTES(ciroceissler): calcula apenas o elementos internos
    image_ds[n + threadIdx.x][n + threadIdx.y]  = image_copy[(y * size_x) + x] ;

    int halo_index_left   = (blockIdx.x - 1)*blockDim.x + threadIdx.x;
    int halo_index_right  = (blockIdx.x + 1)*blockDim.x + threadIdx.x;
    int halo_index_top    = (blockIdx.y - 1)*blockDim.y + threadIdx.y;
    int halo_index_bottom = (blockIdx.y + 1)*blockDim.y + threadIdx.y;

    // NOTES(ciroceissler): calcula os elementos do halo left
    if (threadIdx.x >= blockDim.x - n) {
        if (halo_index_left >= 0) {
            image_ds[threadIdx.x - (blockDim.x - n)][threadIdx.y + n]
                = image_copy[(y * size_x) + halo_index_left];
        } else {
            image_ds[threadIdx.x - (blockDim.x - n)][threadIdx.y + n] = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo right
    if (threadIdx.x < n) {
        if (halo_index_right < size_x) {
            image_ds[n + blockDim.x + threadIdx.x][threadIdx.y + n]
                = image_copy[(y * size_x) + halo_index_right];
        } else {
            image_ds[n + blockDim.x + threadIdx.x][threadIdx.y + n] = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo top
    if (threadIdx.y >= blockDim.y - n) {
        if (halo_index_top >= 0) {
            image_ds[threadIdx.x + n][threadIdx.y - (blockDim.y - n)]
                = image_copy[(halo_index_top * size_x) + x];
        } else {
            image_ds[threadIdx.x + n][threadIdx.y - (blockDim.y - n)] = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo bottom
    if (threadIdx.y < n) {
        if (halo_index_bottom < size_y) {
            image_ds[threadIdx.x + n][n + blockDim.y + threadIdx.y]
                = image_copy[(halo_index_bottom * size_x) + x];
        } else {
            image_ds[threadIdx.x + n][n + blockDim.y + threadIdx.y] = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo top-left
    if (threadIdx.x >= blockDim.x - n && threadIdx.y >= blockDim.y - n) {
        if (halo_index_left >= 0 && halo_index_top >= 0) {
            image_ds[threadIdx.x - (blockDim.x - n)][threadIdx.y - (blockDim.y - n)]
                = image_copy[(halo_index_top * size_x) + halo_index_left];
        } else {
            image_ds[threadIdx.x - (blockDim.x - n)][threadIdx.y - (blockDim.y - n)] 
                = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo top-right
    if (threadIdx.x < n && threadIdx.y >= blockDim.y - n) {
        if (halo_index_right < size_x && halo_index_top >= 0) {
            image_ds[n + blockDim.x + threadIdx.x][threadIdx.y - (blockDim.y - n)]
                = image_copy[(halo_index_top * size_x) + halo_index_right];
        } else {
            image_ds[n + blockDim.x + threadIdx.x][threadIdx.y - (blockDim.y - n)]
                = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): calcula os elementos do halo bottom-left
    if (threadIdx.x >= blockDim.x - n && threadIdx.y < n) {
        if (halo_index_left >= 0 && halo_index_bottom < size_y) {
            image_ds[threadIdx.x - (blockDim.x - n)][n + blockDim.y + threadIdx.y]
                = image_copy[(halo_index_bottom * size_x) + halo_index_left];
        } else {
            image_ds[threadIdx.x - (blockDim.x - n)][n + blockDim.y + threadIdx.y]
                = (PPMPixel) {0,0,0};
        }
    }
    
    // NOTES(ciroceissler): calcula os elementos do halo bottom-right
    if (threadIdx.x < n && threadIdx.y < n) {
        if (halo_index_right < size_x && halo_index_bottom < size_y) {
            image_ds[n + blockDim.x + threadIdx.x][n + blockDim.y + threadIdx.y]
                = image_copy[(halo_index_bottom * size_x) + halo_index_right];
        } else {
            image_ds[n + blockDim.x + threadIdx.x][n + blockDim.y + threadIdx.y]
                = (PPMPixel) {0,0,0};
        }
    }

    // NOTES(ciroceissler): sincronizar todas as threads
    __syncthreads();

    total_red   = 0;
    total_blue  = 0;
    total_green = 0;

    // NOTES(ciroceissler): calculo do smooth
    for (int j = threadIdx.y; j < threadIdx.y + MASK_WIDTH; j++) {
        for (int i = threadIdx.x; i < threadIdx.x + MASK_WIDTH; i++) {
            total_red   += image_ds[i][j].red   ;
            total_blue  += image_ds[i][j].blue  ;
            total_green += image_ds[i][j].green ;
        } 
    } 

    image[(y * size_x) + x].red   = total_red   / (MASK_WIDTH*MASK_WIDTH);
    image[(y * size_x) + x].blue  = total_blue  / (MASK_WIDTH*MASK_WIDTH);
    image[(y * size_x) + x].green = total_green / (MASK_WIDTH*MASK_WIDTH);
}

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

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

#ifdef __DEBUG__
    double t_start, t_end;
#endif // __DEBUG__

    char *filename = argv[1]; //Recebendo o arquivo!;

    std::size_t size_image;

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

    size_image = sizeof(PPMPixel)*image->x*image->y;

    // NOTES(ciroceissler): variaveis do device
    PPMPixel *d_image;
    PPMPixel *d_image_output;

    // NOTES(ciroceissler): dimensoes
    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE, 1);
    dim3 dimGrid(ceil((float)image->x / BLOCK_SIZE), ceil((float)image->y / BLOCK_SIZE), 1);

#ifdef __DEBUG__
    t_start = rtclock();
#endif // __DEBUG__

    // NOTES(ciroceissler): alocacao dos buffers do device
    hipMalloc((void **) &d_image       , size_image);
    hipMalloc((void **) &d_image_output, size_image);

    // NOTES(ciroceissler): copiar os vetores para o device
    hipMemcpy(d_image, image->data, size_image, hipMemcpyHostToDevice); 

    // NOTES(ciroceissler): execucaco do kernel
    smoothing_kernel<<<dimGrid, dimBlock>>>(d_image_output, d_image, image->x, image->y);
    hipDeviceSynchronize();

    // NOTES(ciroceissler): copiar o valor da GPU
    hipMemcpy(image_output->data, d_image_output, size_image, hipMemcpyDeviceToHost);


#ifdef __DEBUG__
    t_end = rtclock();

    fprintf(stdout, "\n%0.6lfs\n", t_end - t_start);  
#endif // __DEBUG__

    writePPM(image_output);

    free(image);
    free(image_output);
}

// ------------------------------------------------------------------------------------
// input    | cpu_serial  | gpu_nosharedmemory | gpu_sharedmemory | speedup (cpu/gpusm)
// ------------------------------------------------------------------------------------
// arq1.ppm | 0.302824    | 0.667255           | 0.633555         |  0.4779758663
// arq2.ppm | 0.678099    | 0.755541           | 0.655261         |  1.034853287
// arq3.ppm | 2.710807    | 1.076326           | 0.751093         |  3.609149599
// ------------------------------------------------------------------------------------
// 
// - numero de acesso a memoria global substituido por shared memory:
//
// TILE_ACCESS = (BLOCK_SIZE*BLOCK_SIZE) * (MASK_WIDTH*MASK_WIDTH)
//
// - elementos carregados para cada TILE:
//
// INPUT_ACCESS = (BLOCK_SIZE + MASK_WIDTH - 1)^2
//
// - reduction:
//
// REDUCTION = TILE_ACCESS/INPUT_ACCESS
//
// ---------------------------------------------------------------
// MASK_WIDHT\BLOCK_SIZE |  8X8  | 14X14 | 15X15 | 16X16 | 32X32
// ---------------------------------------------------------------
//           5           | 11.11 | 15.12 | 15.58 | 16.00 | 19.75
//           7           | 16.00 | 24.01 | 25.00 | 25.92 | 34.75
//           9           | 20.25 | 32.80 | 34.45 | 36.00 | 51.84
//           11          | 23.90 | 41.17 | 43.56 | 45.82 | 70.24
//           13          | 27.04 | 49.00 | 52.16 | 55.18 | 89.39
// ---------------------------------------------------------------
//
// taf!

	.text
	.file	"smooth.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_kernelP8PPMPixelS0_ii # -- Begin function _Z31__device_stub__smoothing_kernelP8PPMPixelS0_ii
	.type	_Z31__device_stub__smoothing_kernelP8PPMPixelS0_ii,@function
_Z31__device_stub__smoothing_kernelP8PPMPixelS0_ii: # @_Z31__device_stub__smoothing_kernelP8PPMPixelS0_ii
	.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	$112, %rsp
	.cfi_def_cfa_offset 160
	.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	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%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	$_Z16smoothing_kernelP8PPMPixelS0_ii, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	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_kernelP8PPMPixelS0_ii, .Lfunc_end2-_Z31__device_stub__smoothing_kernelP8PPMPixelS0_ii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI3_0:
	.long	0x3e000000                      # float 0.125
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.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	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	cmpl	$2, %edi
	je	.LBB3_2
# %bb.1:
	movl	$.Lstr, %edi
	callq	puts@PLT
.LBB3_2:
	movq	8(%rbx), %r14
	movq	%r14, %rdi
	callq	_ZL7readPPMPKc
	movq	%rax, %rbx
	movq	%r14, %rdi
	callq	_ZL7readPPMPKc
	movq	%rax, %r14
	movslq	(%rbx), %rax
	movslq	4(%rbx), %r12
	cvtsi2ss	%eax, %xmm0
	imulq	%r12, %rax
	leaq	(%rax,%rax,2), %r15
	mulss	.LCPI3_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %r13
	xorps	%xmm0, %xmm0
	cvtsi2ss	%r12d, %xmm0
	mulss	.LCPI3_0(%rip), %xmm0
	callq	ceilf@PLT
	cvttss2si	%xmm0, %r12
	movl	%r13d, %eax
	shlq	$32, %r12
	orq	%rax, %r12
	leaq	8(%rsp), %r13
	movq	%r13, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	%rsp, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movq	8(%rbx), %rsi
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	movabsq	$34359738376, %rdx              # imm = 0x800000008
	movq	%r12, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_4
# %bb.3:
	movq	(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	(%rbx), %edx
	movl	4(%rbx), %ecx
	callq	_Z31__device_stub__smoothing_kernelP8PPMPixelS0_ii
.LBB3_4:
	callq	hipDeviceSynchronize
	movq	8(%r14), %rdi
	movq	(%rsp), %rsi
	movq	%r15, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%r14, %rdi
	callq	_Z8writePPMP8PPMImage
	movq	%rbx, %rdi
	callq	free
	movq	%r14, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$16, %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
.Lfunc_end3:
	.size	main, .Lfunc_end3-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	.LBB4_14
# %bb.1:
	movq	%rax, %r14
	leaq	16(%rsp), %rdi
	movl	$16, %esi
	movq	%rax, %rdx
	callq	fgets
	testq	%rax, %rax
	je	.LBB4_15
# %bb.2:
	cmpb	$80, 16(%rsp)
	jne	.LBB4_16
# %bb.3:
	cmpb	$54, 17(%rsp)
	jne	.LBB4_16
# %bb.4:
	movl	$16, %edi
	callq	malloc
	testq	%rax, %rax
	je	.LBB4_17
# %bb.5:
	movq	%rax, %r15
.LBB4_6:                                # =>This Loop Header: Depth=1
                                        #     Child Loop BB4_7 Depth 2
	movq	%r14, %rdi
	callq	getc
	cmpl	$35, %eax
	jne	.LBB4_8
.LBB4_7:                                # %.preheader44
                                        #   Parent Loop BB4_6 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movq	%r14, %rdi
	callq	getc
	cmpl	$10, %eax
	jne	.LBB4_7
	jmp	.LBB4_6
.LBB4_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	.LBB4_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	.LBB4_21
# %bb.10:
	cmpl	$255, 12(%rsp)
	jne	.LBB4_22
.LBB4_11:                               # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	fgetc
	cmpl	$10, %eax
	jne	.LBB4_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	.LBB4_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
.LBB4_14:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rdi
	movl	$.L.str.8, %esi
	jmp	.LBB4_24
.LBB4_15:
	movq	%rbx, %rdi
	callq	perror
	jmp	.LBB4_19
.LBB4_16:
	movq	stderr(%rip), %rcx
	movl	$.L.str.9, %edi
	movl	$36, %esi
	jmp	.LBB4_18
.LBB4_17:
	movq	stderr(%rip), %rcx
	movl	$.L.str.10, %edi
	movl	$26, %esi
.LBB4_18:
	movl	$1, %edx
	callq	fwrite
	jmp	.LBB4_19
.LBB4_20:
	movq	stderr(%rip), %rdi
	movl	$.L.str.12, %esi
	jmp	.LBB4_24
.LBB4_21:
	movq	stderr(%rip), %rdi
	movl	$.L.str.14, %esi
	jmp	.LBB4_24
.LBB4_22:
	movq	stderr(%rip), %rdi
	movl	$.L.str.15, %esi
	jmp	.LBB4_24
.LBB4_23:
	movq	stderr(%rip), %rdi
	movl	$.L.str.16, %esi
.LBB4_24:
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
.LBB4_19:
	movl	$1, %edi
	callq	exit
.Lfunc_end4:
	.size	_ZL7readPPMPKc, .Lfunc_end4-_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	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z16smoothing_kernelP8PPMPixelS0_ii, %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_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__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	.LBB6_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
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__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_kernelP8PPMPixelS0_ii,@object # @_Z16smoothing_kernelP8PPMPixelS0_ii
	.section	.rodata,"a",@progbits
	.globl	_Z16smoothing_kernelP8PPMPixelS0_ii
	.p2align	3, 0x0
_Z16smoothing_kernelP8PPMPixelS0_ii:
	.quad	_Z31__device_stub__smoothing_kernelP8PPMPixelS0_ii
	.size	_Z16smoothing_kernelP8PPMPixelS0_ii, 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_kernelP8PPMPixelS0_ii"
	.size	.L__unnamed_1, 36

	.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_kernelP8PPMPixelS0_ii
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z16smoothing_kernelP8PPMPixelS0_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z16smoothing_kernelP8PPMPixelS0_ii ; -- Begin function _Z16smoothing_kernelP8PPMPixelS0_ii
	.globl	_Z16smoothing_kernelP8PPMPixelS0_ii
	.p2align	8
	.type	_Z16smoothing_kernelP8PPMPixelS0_ii,@function
_Z16smoothing_kernelP8PPMPixelS0_ii:    ; @_Z16smoothing_kernelP8PPMPixelS0_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b64 s[8:9], s[0:1], 0x10
	v_bfe_u32 v4, v0, 10, 10
	s_load_b128 s[4:7], s[0:1], 0x0
	v_and_b32_e32 v6, 0x3ff, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_u32_u24_e32 v8, 3, v4
	v_mad_u32_u24 v5, v6, 36, v8
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s10, s2, 16
	s_and_b32 s11, s2, 0xffff
	s_mul_i32 s15, s15, s10
	s_mul_i32 s14, s14, s11
	v_add_nc_u32_e32 v1, s15, v4
	v_add_nc_u32_e32 v2, s14, v6
	s_add_i32 s0, s11, -2
	s_sub_i32 s1, s14, s11
	v_cmp_gt_u32_e32 vcc_lo, s0, v6
	v_mul_lo_u32 v7, v1, s8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add_nc_u32_e32 v3, v7, v2
	v_mad_i64_i32 v[0:1], null, v3, 3, s[6:7]
	s_clause 0x1
	global_load_u16 v9, v[0:1], off
	global_load_u8 v0, v[0:1], off offset:2
	v_add_nc_u32_e32 v1, s1, v6
	s_mov_b32 s1, exec_lo
	s_waitcnt vmcnt(1)
	ds_store_b16 v5, v9 offset:78
	s_waitcnt vmcnt(0)
	ds_store_b8 v5, v0 offset:80
	v_cmpx_le_u32_e64 s0, v6
	s_cbranch_execz .LBB0_5
; %bb.1:
	v_cmp_gt_i32_e64 s0, 0, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s2, s0
	s_xor_b32 s0, exec_lo, s2
	s_cbranch_execz .LBB0_3
; %bb.2:
	v_subrev_nc_u32_e32 v0, s11, v6
	v_mov_b32_e32 v9, 0
	s_delay_alu instid0(VALU_DEP_2)
	v_mad_i32_i24 v0, v0, 36, v8
	ds_store_b8 v0, v9 offset:78
	ds_store_b8 v0, v9 offset:79
	ds_store_b8 v0, v9 offset:80
.LBB0_3:                                ; %Flow278
	s_and_not1_saveexec_b32 s0, s0
	s_cbranch_execz .LBB0_5
; %bb.4:
	v_add_nc_u32_e32 v0, v7, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_mad_i64_i32 v[9:10], null, v0, 3, s[6:7]
	s_clause 0x1
	global_load_u8 v0, v[9:10], off offset:2
	global_load_u16 v9, v[9:10], off
	v_subrev_nc_u32_e32 v10, s11, v6
	v_mad_i32_i24 v10, v10, 36, v8
	s_waitcnt vmcnt(1)
	ds_store_b8 v10, v0 offset:80
	s_waitcnt vmcnt(0)
	ds_store_b16 v10, v9 offset:78
.LBB0_5:
	s_or_b32 exec_lo, exec_lo, s1
	v_lshl_add_u32 v0, s11, 1, v1
	v_cmp_lt_u32_e64 s0, 1, v6
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_u32_e32 2, v6
	s_cbranch_execz .LBB0_10
; %bb.6:
	v_cmp_le_i32_e64 s1, s8, v0
	v_add3_u32 v9, s11, v6, 2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s3, s1
	s_xor_b32 s1, exec_lo, s3
	s_cbranch_execz .LBB0_8
; %bb.7:
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_u32_u24 v7, v9, 36, v8
	v_mov_b32_e32 v8, 0
                                        ; implicit-def: $vgpr9
	ds_store_b8 v7, v8 offset:6
	ds_store_b8 v7, v8 offset:7
	ds_store_b8 v7, v8 offset:8
                                        ; implicit-def: $vgpr7
                                        ; implicit-def: $vgpr8
.LBB0_8:                                ; %Flow274
	s_and_not1_saveexec_b32 s1, s1
	s_cbranch_execz .LBB0_10
; %bb.9:
	v_add_nc_u32_e32 v7, v7, v0
	v_mad_u32_u24 v8, v9, 36, v8
	s_delay_alu instid0(VALU_DEP_2)
	v_mad_i64_i32 v[10:11], null, v7, 3, s[6:7]
	s_clause 0x1
	global_load_u8 v7, v[10:11], off offset:2
	global_load_u16 v10, v[10:11], off
	s_waitcnt vmcnt(1)
	ds_store_b8 v8, v7 offset:8
	s_waitcnt vmcnt(0)
	ds_store_b16 v8, v10 offset:6
.LBB0_10:
	s_or_b32 exec_lo, exec_lo, s2
	s_sub_i32 s1, s15, s10
	s_add_i32 s3, s10, -2
	v_add_nc_u32_e32 v8, s1, v4
	v_cmp_gt_u32_e64 s2, s3, v4
	v_cmp_le_u32_e64 s1, s3, v4
	s_delay_alu instid0(VALU_DEP_1)
	s_and_saveexec_b32 s3, s1
	s_cbranch_execz .LBB0_15
; %bb.11:
	v_cmp_gt_i32_e64 s1, 0, v8
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s12, s1
	s_xor_b32 s1, exec_lo, s12
	s_cbranch_execz .LBB0_13
; %bb.12:
	v_subrev_nc_u32_e32 v7, s10, v4
	v_mov_b32_e32 v9, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_i32_i24_e32 v7, 3, v7
	v_mad_u32_u24 v7, v6, 36, v7
	ds_store_b8 v7, v9 offset:78
	ds_store_b8 v7, v9 offset:79
	ds_store_b8 v7, v9 offset:80
.LBB0_13:                               ; %Flow271
	s_and_not1_saveexec_b32 s1, s1
	s_cbranch_execz .LBB0_15
; %bb.14:
	v_mad_u64_u32 v[9:10], null, v8, s8, v[2:3]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_mad_i64_i32 v[10:11], null, v9, 3, s[6:7]
	s_clause 0x1
	global_load_u8 v7, v[10:11], off offset:2
	global_load_u16 v9, v[10:11], off
	v_subrev_nc_u32_e32 v10, s10, v4
	v_mul_i32_i24_e32 v10, 3, v10
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_u32_u24 v10, v6, 36, v10
	s_waitcnt vmcnt(1)
	ds_store_b8 v10, v7 offset:80
	s_waitcnt vmcnt(0)
	ds_store_b16 v10, v9 offset:78
.LBB0_15:
	s_or_b32 exec_lo, exec_lo, s3
	v_lshl_add_u32 v7, s10, 1, v8
	v_cmp_lt_u32_e64 s1, 1, v4
	s_mov_b32 s12, exec_lo
	v_cmpx_gt_u32_e32 2, v4
	s_cbranch_execz .LBB0_20
; %bb.16:
	v_cmp_le_i32_e64 s3, s9, v7
	v_add3_u32 v9, s10, v4, 2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s13, s3
	s_xor_b32 s3, exec_lo, s13
	s_cbranch_execz .LBB0_18
; %bb.17:
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_u32_u24_e32 v2, 3, v9
	v_mov_b32_e32 v9, 0
	v_mad_u32_u24 v2, v6, 36, v2
	ds_store_b8 v2, v9 offset:72
	ds_store_b8 v2, v9 offset:73
	ds_store_b8 v2, v9 offset:74
                                        ; implicit-def: $vgpr2
                                        ; implicit-def: $vgpr9
.LBB0_18:                               ; %Flow268
	s_and_not1_saveexec_b32 s3, s3
	s_cbranch_execz .LBB0_20
; %bb.19:
	v_mad_u64_u32 v[10:11], null, v7, s8, v[2:3]
	v_mul_u32_u24_e32 v9, 3, v9
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mad_u32_u24 v9, v6, 36, v9
	v_mad_i64_i32 v[11:12], null, v10, 3, s[6:7]
	s_clause 0x1
	global_load_u8 v2, v[11:12], off offset:2
	global_load_u16 v10, v[11:12], off
	s_waitcnt vmcnt(1)
	ds_store_b8 v9, v2 offset:74
	s_waitcnt vmcnt(0)
	ds_store_b16 v9, v10 offset:72
.LBB0_20:
	s_or_b32 exec_lo, exec_lo, s12
	s_or_b32 s3, vcc_lo, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_xor_b32 s3, s3, -1
	s_and_saveexec_b32 s12, s3
	s_cbranch_execz .LBB0_25
; %bb.21:
	v_or_b32_e32 v2, v8, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_gt_i32_e64 s3, 0, v2
	s_and_saveexec_b32 s13, s3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_xor_b32 s3, exec_lo, s13
	s_cbranch_execz .LBB0_23
; %bb.22:
	v_subrev_nc_u32_e32 v2, s10, v4
	v_subrev_nc_u32_e32 v9, s11, v6
	v_mov_b32_e32 v10, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_i32_i24_e32 v2, 3, v2
	v_mad_i32_i24 v2, v9, 36, v2
	ds_store_b8 v2, v10 offset:78
	ds_store_b8 v2, v10 offset:79
	ds_store_b8 v2, v10 offset:80
.LBB0_23:                               ; %Flow266
	s_and_not1_saveexec_b32 s3, s3
	s_cbranch_execz .LBB0_25
; %bb.24:
	v_mad_u64_u32 v[9:10], null, v8, s8, v[1:2]
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_i64_i32 v[10:11], null, v9, 3, s[6:7]
	s_clause 0x1
	global_load_u8 v2, v[10:11], off offset:2
	global_load_u16 v9, v[10:11], off
	v_subrev_nc_u32_e32 v10, s10, v4
	v_subrev_nc_u32_e32 v11, s11, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_i32_i24_e32 v10, 3, v10
	v_mad_i32_i24 v10, v11, 36, v10
	s_waitcnt vmcnt(1)
	ds_store_b8 v10, v2 offset:80
	s_waitcnt vmcnt(0)
	ds_store_b16 v10, v9 offset:78
.LBB0_25:
	s_or_b32 exec_lo, exec_lo, s12
	s_or_b32 s0, s0, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_xor_b32 s0, s0, -1
	s_and_saveexec_b32 s3, s0
	s_cbranch_execz .LBB0_30
; %bb.26:
	v_subrev_nc_u32_e32 v2, s10, v4
	v_cmp_le_i32_e64 s0, s8, v0
	v_cmp_gt_i32_e64 s2, 0, v8
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_i32_i24_e32 v2, 3, v2
	s_or_b32 s0, s0, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s2, s0
	s_xor_b32 s0, exec_lo, s2
	s_cbranch_execz .LBB0_28
; %bb.27:
	v_dual_mov_b32 v9, 0 :: v_dual_add_nc_u32 v8, s11, v6
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_u32_u24 v2, v8, 36, v2
                                        ; implicit-def: $vgpr8
	ds_store_b8 v2, v9 offset:78
	ds_store_b8 v2, v9 offset:79
	ds_store_b8 v2, v9 offset:80
                                        ; implicit-def: $vgpr2
.LBB0_28:                               ; %Flow264
	s_and_not1_saveexec_b32 s0, s0
	s_cbranch_execz .LBB0_30
; %bb.29:
	v_mad_u64_u32 v[9:10], null, v8, s8, v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_mad_i64_i32 v[10:11], null, v9, 3, s[6:7]
	s_clause 0x1
	global_load_u8 v8, v[10:11], off offset:2
	global_load_u16 v9, v[10:11], off
	v_add_nc_u32_e32 v10, s11, v6
	v_mad_u32_u24 v2, v10, 36, v2
	s_waitcnt vmcnt(1)
	ds_store_b8 v2, v8 offset:80
	s_waitcnt vmcnt(0)
	ds_store_b16 v2, v9 offset:78
.LBB0_30:
	s_or_b32 exec_lo, exec_lo, s3
	s_or_b32 s0, s1, vcc_lo
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_xor_b32 s0, s0, -1
	s_and_saveexec_b32 s1, s0
	s_cbranch_execz .LBB0_35
; %bb.31:
	v_cmp_gt_i32_e32 vcc_lo, 0, v1
	v_cmp_le_i32_e64 s0, s9, v7
	v_subrev_nc_u32_e32 v2, s11, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_or_b32 s0, vcc_lo, s0
	s_and_saveexec_b32 s2, s0
	s_delay_alu instid0(SALU_CYCLE_1)
	s_xor_b32 s0, exec_lo, s2
	s_cbranch_execz .LBB0_33
; %bb.32:
	v_dual_mov_b32 v8, 0 :: v_dual_add_nc_u32 v1, s10, v4
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_u32_u24_e32 v1, 3, v1
	v_mad_i32_i24 v1, v2, 36, v1
                                        ; implicit-def: $vgpr2
	ds_store_b8 v1, v8 offset:78
	ds_store_b8 v1, v8 offset:79
	ds_store_b8 v1, v8 offset:80
                                        ; implicit-def: $vgpr1
.LBB0_33:                               ; %Flow262
	s_and_not1_saveexec_b32 s0, s0
	s_cbranch_execz .LBB0_35
; %bb.34:
	v_mad_u64_u32 v[8:9], null, v7, s8, v[1:2]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_4) | instid1(VALU_DEP_1)
	v_mad_i64_i32 v[9:10], null, v8, 3, s[6:7]
	s_clause 0x1
	global_load_u8 v1, v[9:10], off offset:2
	global_load_u16 v8, v[9:10], off
	v_add_nc_u32_e32 v9, s10, v4
	v_mul_u32_u24_e32 v9, 3, v9
	s_delay_alu instid0(VALU_DEP_1)
	v_mad_i32_i24 v2, v2, 36, v9
	s_waitcnt vmcnt(1)
	ds_store_b8 v2, v1 offset:80
	s_waitcnt vmcnt(0)
	ds_store_b16 v2, v8 offset:78
.LBB0_35:
	s_or_b32 exec_lo, exec_lo, s1
	v_or_b32_e32 v1, v6, v4
	s_mov_b32 s1, exec_lo
	s_delay_alu instid0(VALU_DEP_1)
	v_cmpx_gt_u32_e32 2, v1
	s_cbranch_execz .LBB0_40
; %bb.36:
	v_add_nc_u32_e32 v2, s10, v4
	v_cmp_le_i32_e32 vcc_lo, s8, v0
	v_cmp_le_i32_e64 s0, s9, v7
	v_add_nc_u32_e32 v1, s11, v6
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mul_u32_u24_e32 v2, 3, v2
	s_or_b32 s0, vcc_lo, s0
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_saveexec_b32 s2, s0
	s_xor_b32 s0, exec_lo, s2
	s_cbranch_execz .LBB0_38
; %bb.37:
	v_mad_u32_u24 v0, v1, 36, v2
	v_mov_b32_e32 v1, 0
                                        ; implicit-def: $vgpr7
                                        ; implicit-def: $vgpr2
	ds_store_b8 v0, v1 offset:78
	ds_store_b8 v0, v1 offset:79
	ds_store_b8 v0, v1 offset:80
                                        ; implicit-def: $vgpr0
                                        ; implicit-def: $vgpr1
.LBB0_38:                               ; %Flow
	s_and_not1_saveexec_b32 s0, s0
	s_cbranch_execz .LBB0_40
; %bb.39:
	v_mad_u64_u32 v[8:9], null, v7, s8, v[0:1]
	v_mad_u32_u24 v1, v1, 36, v2
	s_delay_alu instid0(VALU_DEP_2)
	v_mad_i64_i32 v[6:7], null, v8, 3, s[6:7]
	s_clause 0x1
	global_load_u8 v0, v[6:7], off offset:2
	global_load_u16 v6, v[6:7], off
	s_waitcnt vmcnt(1)
	ds_store_b8 v1, v0 offset:80
	s_waitcnt vmcnt(0)
	ds_store_b16 v1, v6 offset:78
.LBB0_40:
	s_or_b32 exec_lo, exec_lo, s1
	v_dual_mov_b32 v1, 0 :: v_dual_add_nc_u32 v6, 4, v4
	v_mov_b32_e32 v2, 0
	v_mov_b32_e32 v0, 0
	s_mov_b32 s0, 0
	s_waitcnt lgkmcnt(0)
	s_barrier
	buffer_gl0_inv
.LBB0_41:                               ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_42 Depth 2
	s_mov_b32 s1, 0
.LBB0_42:                               ;   Parent Loop BB0_41 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(SALU_CYCLE_1)
	v_add_nc_u32_e32 v7, s1, v5
	s_add_i32 s1, s1, 36
	ds_load_u8 v8, v7
	ds_load_u8 v9, v7 offset:2
	ds_load_u8 v7, v7 offset:1
	s_cmpk_eq_i32 s1, 0xb4
	s_waitcnt lgkmcnt(2)
	v_add_nc_u32_e32 v2, v2, v8
	s_waitcnt lgkmcnt(1)
	v_add_nc_u32_e32 v0, v0, v9
	s_waitcnt lgkmcnt(0)
	v_add_nc_u32_e32 v1, v1, v7
	s_cbranch_scc0 .LBB0_42
; %bb.43:                               ;   in Loop: Header=BB0_41 Depth=1
	v_add_nc_u32_e32 v7, 1, v4
	v_cmp_eq_u32_e32 vcc_lo, v4, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(SALU_CYCLE_1)
	v_dual_mov_b32 v4, v7 :: v_dual_add_nc_u32 v5, 3, v5
	s_or_b32 s0, vcc_lo, s0
	s_and_not1_b32 exec_lo, exec_lo, s0
	s_cbranch_execnz .LBB0_41
; %bb.44:
	s_or_b32 exec_lo, exec_lo, s0
	v_mul_hi_i32 v2, 0x51eb851f, v2
	v_mul_hi_i32 v1, 0x51eb851f, v1
	v_mul_hi_i32 v0, 0x51eb851f, v0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_lshrrev_b32_e32 v4, 31, v2
	v_lshrrev_b32_e32 v5, 31, v1
	v_lshrrev_b32_e32 v1, 3, v1
	v_lshrrev_b32_e32 v2, 3, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v1, v1, v5
	v_add_nc_u32_e32 v2, v2, v4
	v_lshrrev_b32_e32 v4, 31, v0
	v_lshrrev_b32_e32 v5, 3, v0
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_lshlrev_b16 v6, 8, v1
	v_and_b32_e32 v2, 0xff, v2
	v_mad_i64_i32 v[0:1], null, v3, 3, s[4:5]
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v3, v5, v4
	v_or_b32_e32 v2, v2, v6
	s_clause 0x1
	global_store_b8 v[0:1], v3, off offset:2
	global_store_b16 v[0:1], v2, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z16smoothing_kernelP8PPMPixelS0_ii
		.amdhsa_group_segment_fixed_size 432
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 14
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 1
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 1
		.amdhsa_next_free_vgpr 13
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z16smoothing_kernelP8PPMPixelS0_ii, .Lfunc_end0-_Z16smoothing_kernelP8PPMPixelS0_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 2012
; NumSgprs: 18
; NumVgprs: 13
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 432 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 1
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 13
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 432
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z16smoothing_kernelP8PPMPixelS0_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z16smoothing_kernelP8PPMPixelS0_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     13
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
