#include <cuda.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <chrono>

#define RGB_COMPONENT_COLOR 255

#define BLUR_SIZE 5

using namespace std;

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

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

unsigned char *readPPM(const char *filename, int &x, int &y)
{
	char buff[16];
	unsigned char* imgchar;
	FILE *fp;
	int c, rgb_comp_color;
	//open PPM file for reading
	fp = fopen(filename, "rb");
	if (!fp) {
		fprintf(stderr, "Unable to open file '%s'\n", filename);
		exit(1);
	}

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

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

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

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

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

	while (fgetc(fp) != '\n');
	//memory allocation for pixel data
	imgchar = (unsigned char*)malloc(3 * x*y * sizeof(char));

	//read pixel data from file
	fread(imgchar, 3 * x, y, fp);


	fclose(fp);
	return imgchar;
}

void writePPM(unsigned char * img, int x, int y)
{
	FILE *fp;
	//open file for output
	fp = fopen("C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\img_blur.ppm", "wb");
	if (!fp) {
		fprintf(stderr, "Unable to open file '%s'\n", "out");
		exit(1);
	}

	//write the header file
	//image format
	fprintf(fp, "P6\n");

	//image size
	fprintf(fp, "%d %d\n", x, y);

	// rgb component depth
	fprintf(fp, "%d\n", RGB_COMPONENT_COLOR);

	// pixel data
	fwrite(img, 3 * x, y, fp);
	fclose(fp);
}

unsigned char* readBMP(char* file_name, int &width, int &height) {
	FILE* img = fopen(file_name, "rb");
	unsigned char header[54];
	fread(header, sizeof(unsigned char), 54, img);
	width = *(int*)&header[18];
	height = *(int*)&header[22];
	int size = width * height * 3;
	unsigned char* r_img = (unsigned char*)malloc(size * sizeof(unsigned char));
	fread(r_img, sizeof(unsigned char), size, img);
	fclose(img);
	return r_img;
}

void writeBMP(unsigned char* img, int width, int height) {
	FILE* f_img;
	int f_size = 54 + 3 * width* height;
	unsigned char file_header[14] = { 'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 };
	unsigned char info_header[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0 };
	unsigned char pad[3] = { 0,0,0 };
	file_header[2] = (unsigned char)(f_size);
	file_header[3] = (unsigned char)(f_size >> 8);
	file_header[4] = (unsigned char)(f_size >> 16);
	file_header[5] = (unsigned char)(f_size >> 24);
	info_header[4] = (unsigned char)(width);
	info_header[5] = (unsigned char)(width >> 8);
	info_header[6] = (unsigned char)(width >> 16);
	info_header[7] = (unsigned char)(width >> 24);
	info_header[8] = (unsigned char)(height);
	info_header[9] = (unsigned char)(height >> 8);
	info_header[10] = (unsigned char)(height >> 16);
	info_header[11] = (unsigned char)(height >> 24);
	f_img = fopen("C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\img_blur.bmp", "wb");
	fwrite(file_header, 1, 14, f_img);
	fwrite(info_header, 1, 40, f_img);
	for (int i = height - 1; i >= 0; i--) {
		fwrite(img + (width * (height - i - 1) * 3), 3, width, f_img);
		fwrite(pad, 1, (4 - (width * 3) % 4) % 4, f_img);
	}
	free(img);
	fclose(f_img);
}

// Blur GPU
__global__
void blurKernel(unsigned char* out, unsigned char* in, int w, int h) {
	int Col = threadIdx.x + blockIdx.x * blockDim.x;
	int Row = threadIdx.y + blockIdx.y * blockDim.y;
	int Offset = Row * w + Col;
	if ((Col < w) && (Row < h)) {
		int pixValR = 0;
		int pixValG = 0;
		int pixValB = 0;
		int pixels = 0;
		// Get the average of the surrounding BLUR_SIZE x BLUR_SIZE box
		for (int blurRow = -BLUR_SIZE; blurRow < BLUR_SIZE; blurRow++) {
			for (int blurCol = -BLUR_SIZE; blurCol < BLUR_SIZE; blurCol++) {
				int curRow = Row + blurRow;
				int curCol = Col + blurCol;
				// Verify we have a valid image pixel
				if ((curRow > -1) && (curRow < h) && (curCol > -1) && (curCol < w)) {
					int curOffset = curRow * w + curCol;
					pixValR += in[curOffset * 3];
					pixValG += in[curOffset * 3 + 1];
					pixValB += in[curOffset * 3 + 2];
					pixels++; // Keep track of number of pixels in the avg
				}
			}
		}
		// Write our new pixel value out
		out[Offset * 3] = (unsigned char)(pixValR / pixels);
		out[Offset * 3 + 1] = (unsigned char)(pixValG / pixels);
		out[Offset * 3 + 2] = (unsigned char)(pixValB / pixels);
	}
}

int main() {
	unsigned char* h_img_in;
	unsigned char* h_img_out;
	unsigned char* d_img_in;
	unsigned char* d_img_out;

	int width = 0;
	int height = 0;

	char* img_name = "D:\\Documentos\\Semestre 2020-2\\Computación Paralela y Distribuida\\Tareas\\CUDA\\AP-master\\CUDA\\Blur";
	//char* img_name = "C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\lenna.ppm";

	h_img_in = readBMP(img_name, width, height);
	//h_img_in = readPPM(img_name, width, height);
	cout << "Ready img_in" << endl;
	int size_grey = (width * height * sizeof(unsigned char)) * 3;
	int size_rgb = (width * height * sizeof(unsigned char)) * 3;
	h_img_out = (unsigned char*)malloc(size_grey * sizeof(unsigned char));
	cout << "Ready img_out" << endl;

	cudaMalloc(&d_img_in, size_rgb);
	cudaMemcpy(d_img_in, h_img_in, size_rgb, cudaMemcpyHostToDevice);
	cudaMalloc(&d_img_out, size_grey);
	cudaMemcpy(d_img_out, h_img_out, size_grey, cudaMemcpyHostToDevice);

	dim3 dimGrid(ceil(width / 32.0), ceil(height / 32.0), 1);
	dim3 dimBlock(32, 32, 1);

	chrono::time_point<chrono::system_clock> GPU_Start, GPU_End;

	GPU_Start = chrono::system_clock::now();
	blurKernel <<< dimGrid, dimBlock >>> (d_img_out, d_img_in, width, height);
	GPU_End = chrono::system_clock::now();

	cout << "GPU: " << chrono::duration_cast<chrono::nanoseconds>(GPU_End - GPU_Start).count() << "ns." << endl;

	cudaMemcpy(h_img_out, d_img_out, size_grey, cudaMemcpyDeviceToHost);

	writeBMP(h_img_out, width, height);
	//writePPM(h_img_out, width, height);

	cudaFree(d_img_in);
	cudaFree(d_img_out);

	free(h_img_in);
	free(h_img_out);

	return 0;
}

	.file	"tmpxft_00372b89_00000000-6_blur.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3747:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3747:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.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	"%d %d"
.LC4:
	.string	"Invalid image size (error loading '%s')\n"
.LC5:
	.string	"%d"
.LC6:
	.string	"Invalid rgb component (error loading '%s')\n"
.LC7:
	.string	"'%s' does not have 8-bits components\n"
	.text
	.globl	_Z7readPPMPKcRiS1_
	.type	_Z7readPPMPKcRiS1_, @function
_Z7readPPMPKcRiS1_:
.LFB3734:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	movq	%rsi, %r12
	leaq	.LC0(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$32, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movq	%rbp, %rcx
	leaq	.LC1(%rip), %rdx
	testq	%rax, %rax
	je	.L15
	leaq	8(%rsp), %rdi
	movq	%rax, %rcx
	movl	$16, %edx
	movq	%rax, %rbx
	movl	$16, %esi
	call	__fgets_chk@PLT
	testq	%rax, %rax
	jne	.L4
	movq	%rbp, %rdi
	call	perror@PLT
.L14:
	movl	$1, %edi
	call	exit@PLT
.L4:
	cmpb	$80, 8(%rsp)
	jne	.L5
	cmpb	$54, 9(%rsp)
	je	.L6
.L5:
	movq	stderr(%rip), %rdi
	leaq	.LC2(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L14
.L6:
	movq	%r13, %rcx
	movq	%r12, %rdx
	leaq	.LC3(%rip), %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%rbp, %rcx
	leaq	.LC4(%rip), %rdx
	cmpl	$2, %eax
	jne	.L15
	leaq	4(%rsp), %rdx
	xorl	%eax, %eax
	leaq	.LC5(%rip), %rsi
	movq	%rbx, %rdi
	call	__isoc23_fscanf@PLT
	movq	%rbp, %rcx
	leaq	.LC6(%rip), %rdx
	decl	%eax
	jne	.L15
	cmpl	$255, 4(%rsp)
	je	.L9
	leaq	.LC7(%rip), %rdx
.L15:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L14
.L9:
	movq	%rbx, %rdi
	call	fgetc@PLT
	cmpl	$10, %eax
	jne	.L9
	movl	(%r12), %ebp
	imull	0(%r13), %ebp
	leal	0(%rbp,%rbp,2), %ebp
	movslq	%ebp, %rbp
	movq	%rbp, %rdi
	call	malloc@PLT
	imull	$3, (%r12), %edx
	movq	%rbx, %r8
	movq	%rbp, %rsi
	movslq	0(%r13), %rcx
	movq	%rax, %rdi
	movq	%rax, %r14
	movslq	%edx, %rdx
	call	__fread_chk@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$32, %rsp
	.cfi_def_cfa_offset 48
	movq	%r14, %rax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3734:
	.size	_Z7readPPMPKcRiS1_, .-_Z7readPPMPKcRiS1_
	.section	.rodata.str1.1
.LC8:
	.string	"wb"
.LC9:
	.string	"C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\img_blur.ppm"
.LC10:
	.string	"out"
.LC11:
	.string	"P6\n"
.LC12:
	.string	"%d %d\n"
.LC13:
	.string	"%d\n"
	.text
	.globl	_Z8writePPMPhii
	.type	_Z8writePPMPhii, @function
_Z8writePPMPhii:
.LFB3735:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rdi, %r13
	leaq	.LC9(%rip), %rdi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movl	%esi, %ebp
	leaq	.LC8(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L18
	movq	stderr(%rip), %rdi
	leaq	.LC10(%rip), %rcx
	leaq	.LC1(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L18:
	movq	%rax, %rbx
	movq	%rax, %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	leaq	.LC11(%rip), %rdx
	call	__fprintf_chk@PLT
	movl	%r12d, %r8d
	movl	%ebp, %ecx
	movq	%rbx, %rdi
	leaq	.LC12(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	%rbx, %rdi
	movl	$255, %ecx
	xorl	%eax, %eax
	leaq	.LC13(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	leal	0(%rbp,%rbp,2), %esi
	movq	%r13, %rdi
	movslq	%r12d, %rdx
	movq	%rbx, %rcx
	movslq	%esi, %rsi
	call	fwrite@PLT
	popq	%rax
	.cfi_def_cfa_offset 40
	movq	%rbx, %rdi
	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
	jmp	fclose@PLT
	.cfi_endproc
.LFE3735:
	.size	_Z8writePPMPhii, .-_Z8writePPMPhii
	.globl	_Z7readBMPPcRiS0_
	.type	_Z7readBMPPcRiS0_, @function
_Z7readBMPPcRiS0_:
.LFB3736:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rsi, %r12
	leaq	.LC0(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$72, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	movl	$54, %ecx
	movl	$1, %edx
	movl	$54, %esi
	movq	%rax, %r8
	leaq	2(%rsp), %rdi
	movq	%rax, %rbp
	call	__fread_chk@PLT
	movl	24(%rsp), %ebx
	movl	20(%rsp), %eax
	movl	%eax, (%r12)
	movl	%ebx, 0(%r13)
	imull	(%r12), %ebx
	leal	(%rbx,%rbx,2), %ebx
	movslq	%ebx, %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	movq	%rbp, %r8
	movq	%rbx, %rcx
	movl	$1, %edx
	movq	%rax, %rdi
	movq	%rbx, %rsi
	movq	%rax, %r12
	call	__fread_chk@PLT
	movq	%rbp, %rdi
	call	fclose@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$72, %rsp
	.cfi_def_cfa_offset 40
	movq	%r12, %rax
	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
.LFE3736:
	.size	_Z7readBMPPcRiS0_, .-_Z7readBMPPcRiS0_
	.section	.rodata.str1.1
.LC14:
	.string	"C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\img_blur.bmp"
	.text
	.globl	_Z8writeBMPPhii
	.type	_Z8writeBMPPhii, @function
_Z8writeBMPPhii:
.LFB3737:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	$6, %ecx
	movslq	%esi, %r15
	leaq	.LC8(%rip), %rsi
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%r13, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	leal	(%r15,%r15,2), %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	movl	$1572865, %edx
	salq	$32, %rdx
	subq	$104, %rsp
	.cfi_def_cfa_offset 160
	movq	%fs:40, %rax
	movq	%rax, 88(%rsp)
	xorl	%eax, %eax
	movl	$27, %eax
	leaq	64(%rsp), %rdi
	movq	$19778, 34(%rsp)
	salq	$33, %rax
	movq	%rdx, 56(%rsp)
	movq	%rax, 40(%rsp)
	movl	$40, %eax
	movq	%rax, 48(%rsp)
	xorl	%eax, %eax
	rep stosl
	movl	%r15d, %eax
	movl	%ebx, 56(%rsp)
	leaq	.LC14(%rip), %rdi
	imull	%ebx, %eax
	movw	$0, 31(%rsp)
	decl	%ebx
	movb	$0, 33(%rsp)
	movl	%r15d, 52(%rsp)
	leal	(%rax,%rax,2), %eax
	addl	$54, %eax
	movl	%eax, 36(%rsp)
	call	fopen@PLT
	leaq	34(%rsp), %rdi
	movl	$14, %edx
	movl	$1, %esi
	movq	%rax, %rcx
	movq	%rax, 8(%rsp)
	call	fwrite@PLT
	movq	8(%rsp), %rcx
	movl	$40, %edx
	movl	$1, %esi
	leaq	48(%rsp), %rdi
	call	fwrite@PLT
	movl	%ebp, %eax
	movl	$4, %esi
	movslq	%ebp, %rbp
	cltd
	idivl	%esi
	negl	%edx
	movl	%edx, %r14d
	andl	$3, %r14d
.L24:
	testl	%ebx, %ebx
	js	.L28
	movq	8(%rsp), %rcx
	movq	%r12, %rdi
	movq	%r15, %rdx
	movl	$3, %esi
	decl	%ebx
	addq	%rbp, %r12
	call	fwrite@PLT
	movq	8(%rsp), %rcx
	leaq	31(%rsp), %rdi
	movq	%r14, %rdx
	movl	$1, %esi
	call	fwrite@PLT
	jmp	.L24
.L28:
	movq	%r13, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	fclose@PLT
	movq	88(%rsp), %rax
	subq	%fs:40, %rax
	je	.L26
	call	__stack_chk_fail@PLT
.L26:
	addq	$104, %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
.LFE3737:
	.size	_Z8writeBMPPhii, .-_Z8writeBMPPhii
	.globl	_Z34__device_stub__Z10blurKernelPhS_iiPhS_ii
	.type	_Z34__device_stub__Z10blurKernelPhS_iiPhS_ii, @function
_Z34__device_stub__Z10blurKernelPhS_iiPhS_ii:
.LFB3769:
	.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	.L29
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z10blurKernelPhS_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
.L29:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L31
	call	__stack_chk_fail@PLT
.L31:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3769:
	.size	_Z34__device_stub__Z10blurKernelPhS_iiPhS_ii, .-_Z34__device_stub__Z10blurKernelPhS_iiPhS_ii
	.globl	_Z10blurKernelPhS_ii
	.type	_Z10blurKernelPhS_ii, @function
_Z10blurKernelPhS_ii:
.LFB3770:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z10blurKernelPhS_iiPhS_ii
	.cfi_endproc
.LFE3770:
	.size	_Z10blurKernelPhS_ii, .-_Z10blurKernelPhS_ii
	.section	.rodata.str1.1
.LC15:
	.string	"D:\\Documentos\\Semestre 2020-2\\Computaci\303\263n Paralela y Distribuida\\Tareas\\CUDA\\AP-master\\CUDA\\Blur"
.LC16:
	.string	"Ready img_in"
.LC17:
	.string	"Ready img_out"
.LC19:
	.string	"GPU: "
.LC20:
	.string	"ns."
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3738:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	leaq	.LC15(%rip), %rdi
	leaq	_ZSt4cout(%rip), %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$88, %rsp
	.cfi_def_cfa_offset 144
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rdx
	xorl	%eax, %eax
	leaq	24(%rsp), %rsi
	movl	%eax, 24(%rsp)
	movl	%eax, 28(%rsp)
	call	_Z7readBMPPcRiS0_
	leaq	.LC16(%rip), %rsi
	movq	%r15, %rdi
	movq	%rax, 8(%rsp)
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movl	24(%rsp), %r12d
	movl	28(%rsp), %r13d
	movl	%r12d, %ebx
	imull	%r13d, %ebx
	leal	(%rbx,%rbx,2), %ebx
	movslq	%ebx, %rbx
	movq	%rbx, %rdi
	call	malloc@PLT
	leaq	.LC17(%rip), %rsi
	movq	%r15, %rdi
	movq	%rax, (%rsp)
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	leaq	32(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rsi
	movq	32(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	leaq	40(%rsp), %rdi
	movq	%rbx, %rsi
	call	cudaMalloc@PLT
	movq	(%rsp), %rsi
	movq	40(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	cvtsi2sdl	%r13d, %xmm0
	mulsd	.LC18(%rip), %xmm0
	call	ceil@PLT
	movq	%xmm0, %rbp
	cvtsi2sdl	%r12d, %xmm0
	mulsd	.LC18(%rip), %xmm0
	call	ceil@PLT
	movq	%rbp, %xmm1
	movl	$1, 56(%rsp)
	cvttsd2siq	%xmm0, %rax
	movl	$1, 68(%rsp)
	movl	%eax, 48(%rsp)
	cvttsd2siq	%xmm1, %rax
	movl	%eax, 52(%rsp)
	movabsq	$137438953504, %rax
	movq	%rax, 60(%rsp)
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movl	68(%rsp), %ecx
	movl	56(%rsp), %esi
	xorl	%r9d, %r9d
	movq	60(%rsp), %rdx
	movq	48(%rsp), %rdi
	xorl	%r8d, %r8d
	movq	%rax, %r14
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L35
	movq	32(%rsp), %rsi
	movq	40(%rsp), %rdi
	movl	%r13d, %ecx
	movl	%r12d, %edx
	call	_Z34__device_stub__Z10blurKernelPhS_iiPhS_ii
.L35:
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	leaq	.LC19(%rip), %rsi
	movq	%r15, %rdi
	movq	%rax, %rbp
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rbp, %rsi
	subq	%r14, %rsi
	movq	%rax, %rdi
	call	_ZNSo9_M_insertIlEERSoT_@PLT
	leaq	.LC20(%rip), %rsi
	movq	%rax, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
	movq	40(%rsp), %rsi
	movq	(%rsp), %rdi
	movq	%rbx, %rdx
	movl	$2, %ecx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rdi
	movl	%r13d, %edx
	movl	%r12d, %esi
	call	_Z8writeBMPPhii
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	movq	(%rsp), %rdi
	call	free@PLT
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L36
	call	__stack_chk_fail@PLT
.L36:
	addq	$88, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3738:
	.size	main, .-main
	.section	.rodata.str1.1
.LC21:
	.string	"_Z10blurKernelPhS_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3772:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC21(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z10blurKernelPhS_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
.LFE3772:
	.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
.LC18:
	.long	0
	.long	1067450368
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <hip/hip_runtime.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <chrono>

#define RGB_COMPONENT_COLOR 255

#define BLUR_SIZE 5

using namespace std;

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

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

unsigned char *readPPM(const char *filename, int &x, int &y)
{
	char buff[16];
	unsigned char* imgchar;
	FILE *fp;
	int c, rgb_comp_color;
	//open PPM file for reading
	fp = fopen(filename, "rb");
	if (!fp) {
		fprintf(stderr, "Unable to open file '%s'\n", filename);
		exit(1);
	}

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

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

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

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

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

	while (fgetc(fp) != '\n');
	//memory allocation for pixel data
	imgchar = (unsigned char*)malloc(3 * x*y * sizeof(char));

	//read pixel data from file
	fread(imgchar, 3 * x, y, fp);


	fclose(fp);
	return imgchar;
}

void writePPM(unsigned char * img, int x, int y)
{
	FILE *fp;
	//open file for output
	fp = fopen("C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\img_blur.ppm", "wb");
	if (!fp) {
		fprintf(stderr, "Unable to open file '%s'\n", "out");
		exit(1);
	}

	//write the header file
	//image format
	fprintf(fp, "P6\n");

	//image size
	fprintf(fp, "%d %d\n", x, y);

	// rgb component depth
	fprintf(fp, "%d\n", RGB_COMPONENT_COLOR);

	// pixel data
	fwrite(img, 3 * x, y, fp);
	fclose(fp);
}

unsigned char* readBMP(char* file_name, int &width, int &height) {
	FILE* img = fopen(file_name, "rb");
	unsigned char header[54];
	fread(header, sizeof(unsigned char), 54, img);
	width = *(int*)&header[18];
	height = *(int*)&header[22];
	int size = width * height * 3;
	unsigned char* r_img = (unsigned char*)malloc(size * sizeof(unsigned char));
	fread(r_img, sizeof(unsigned char), size, img);
	fclose(img);
	return r_img;
}

void writeBMP(unsigned char* img, int width, int height) {
	FILE* f_img;
	int f_size = 54 + 3 * width* height;
	unsigned char file_header[14] = { 'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0 };
	unsigned char info_header[40] = { 40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0 };
	unsigned char pad[3] = { 0,0,0 };
	file_header[2] = (unsigned char)(f_size);
	file_header[3] = (unsigned char)(f_size >> 8);
	file_header[4] = (unsigned char)(f_size >> 16);
	file_header[5] = (unsigned char)(f_size >> 24);
	info_header[4] = (unsigned char)(width);
	info_header[5] = (unsigned char)(width >> 8);
	info_header[6] = (unsigned char)(width >> 16);
	info_header[7] = (unsigned char)(width >> 24);
	info_header[8] = (unsigned char)(height);
	info_header[9] = (unsigned char)(height >> 8);
	info_header[10] = (unsigned char)(height >> 16);
	info_header[11] = (unsigned char)(height >> 24);
	f_img = fopen("C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\img_blur.bmp", "wb");
	fwrite(file_header, 1, 14, f_img);
	fwrite(info_header, 1, 40, f_img);
	for (int i = height - 1; i >= 0; i--) {
		fwrite(img + (width * (height - i - 1) * 3), 3, width, f_img);
		fwrite(pad, 1, (4 - (width * 3) % 4) % 4, f_img);
	}
	free(img);
	fclose(f_img);
}

// Blur GPU
__global__
void blurKernel(unsigned char* out, unsigned char* in, int w, int h) {
	int Col = threadIdx.x + blockIdx.x * blockDim.x;
	int Row = threadIdx.y + blockIdx.y * blockDim.y;
	int Offset = Row * w + Col;
	if ((Col < w) && (Row < h)) {
		int pixValR = 0;
		int pixValG = 0;
		int pixValB = 0;
		int pixels = 0;
		// Get the average of the surrounding BLUR_SIZE x BLUR_SIZE box
		for (int blurRow = -BLUR_SIZE; blurRow < BLUR_SIZE; blurRow++) {
			for (int blurCol = -BLUR_SIZE; blurCol < BLUR_SIZE; blurCol++) {
				int curRow = Row + blurRow;
				int curCol = Col + blurCol;
				// Verify we have a valid image pixel
				if ((curRow > -1) && (curRow < h) && (curCol > -1) && (curCol < w)) {
					int curOffset = curRow * w + curCol;
					pixValR += in[curOffset * 3];
					pixValG += in[curOffset * 3 + 1];
					pixValB += in[curOffset * 3 + 2];
					pixels++; // Keep track of number of pixels in the avg
				}
			}
		}
		// Write our new pixel value out
		out[Offset * 3] = (unsigned char)(pixValR / pixels);
		out[Offset * 3 + 1] = (unsigned char)(pixValG / pixels);
		out[Offset * 3 + 2] = (unsigned char)(pixValB / pixels);
	}
}

int main() {
	unsigned char* h_img_in;
	unsigned char* h_img_out;
	unsigned char* d_img_in;
	unsigned char* d_img_out;

	int width = 0;
	int height = 0;

	char* img_name = "D:\\Documentos\\Semestre 2020-2\\Computación Paralela y Distribuida\\Tareas\\CUDA\\AP-master\\CUDA\\Blur";
	//char* img_name = "C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\lenna.ppm";

	h_img_in = readBMP(img_name, width, height);
	//h_img_in = readPPM(img_name, width, height);
	cout << "Ready img_in" << endl;
	int size_grey = (width * height * sizeof(unsigned char)) * 3;
	int size_rgb = (width * height * sizeof(unsigned char)) * 3;
	h_img_out = (unsigned char*)malloc(size_grey * sizeof(unsigned char));
	cout << "Ready img_out" << endl;

	hipMalloc(&d_img_in, size_rgb);
	hipMemcpy(d_img_in, h_img_in, size_rgb, hipMemcpyHostToDevice);
	hipMalloc(&d_img_out, size_grey);
	hipMemcpy(d_img_out, h_img_out, size_grey, hipMemcpyHostToDevice);

	dim3 dimGrid(ceil(width / 32.0), ceil(height / 32.0), 1);
	dim3 dimBlock(32, 32, 1);

	chrono::time_point<chrono::system_clock> GPU_Start, GPU_End;

	GPU_Start = chrono::system_clock::now();
	blurKernel <<< dimGrid, dimBlock >>> (d_img_out, d_img_in, width, height);
	GPU_End = chrono::system_clock::now();

	cout << "GPU: " << chrono::duration_cast<chrono::nanoseconds>(GPU_End - GPU_Start).count() << "ns." << endl;

	hipMemcpy(h_img_out, d_img_out, size_grey, hipMemcpyDeviceToHost);

	writeBMP(h_img_out, width, height);
	//writePPM(h_img_out, width, height);

	hipFree(d_img_in);
	hipFree(d_img_out);

	free(h_img_in);
	free(h_img_out);

	return 0;
}

	.text
	.file	"blur.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	_Z7readPPMPKcRiS1_              # -- Begin function _Z7readPPMPKcRiS1_
	.type	_Z7readPPMPKcRiS1_,@function
_Z7readPPMPKcRiS1_:                     # @_Z7readPPMPKcRiS1_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$40, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdx, %r15
	movq	%rsi, %r12
	movq	%rdi, %rbx
	movl	$.L.str, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_9
# %bb.1:
	movq	%rax, %r14
	leaq	16(%rsp), %rdi
	movl	$16, %esi
	movq	%rax, %rdx
	callq	fgets
	testq	%rax, %rax
	je	.LBB0_10
# %bb.2:
	cmpb	$80, 16(%rsp)
	jne	.LBB0_12
# %bb.3:
	cmpb	$54, 17(%rsp)
	jne	.LBB0_12
# %bb.4:
	movl	$.L.str.3, %esi
	movq	%r14, %rdi
	movq	%r12, %rdx
	movq	%r15, %rcx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$2, %eax
	jne	.LBB0_13
# %bb.5:
	leaq	12(%rsp), %rdx
	movl	$.L.str.5, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$1, %eax
	jne	.LBB0_14
# %bb.6:
	cmpl	$255, 12(%rsp)
	jne	.LBB0_15
.LBB0_7:                                # %.preheader
                                        # =>This Inner Loop Header: Depth=1
	movq	%r14, %rdi
	callq	fgetc
	cmpl	$10, %eax
	jne	.LBB0_7
# %bb.8:
	movslq	(%r12), %rax
	leaq	(%rax,%rax,2), %r12
	movslq	(%r15), %rbx
	movq	%r12, %rdi
	imulq	%rbx, %rdi
	callq	malloc
	movq	%rax, %r15
	movslq	%r12d, %rsi
	movq	%rax, %rdi
	movq	%rbx, %rdx
	movq	%r14, %rcx
	callq	fread
	movq	%r14, %rdi
	callq	fclose
	movq	%r15, %rax
	addq	$40, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB0_9:
	.cfi_def_cfa_offset 80
	movq	stderr(%rip), %rdi
	movl	$.L.str.1, %esi
	jmp	.LBB0_16
.LBB0_10:
	movq	%rbx, %rdi
	callq	perror
	jmp	.LBB0_11
.LBB0_12:
	movq	stderr(%rip), %rcx
	movl	$.L.str.2, %edi
	movl	$36, %esi
	movl	$1, %edx
	callq	fwrite
	jmp	.LBB0_11
.LBB0_13:
	movq	stderr(%rip), %rdi
	movl	$.L.str.4, %esi
	jmp	.LBB0_16
.LBB0_14:
	movq	stderr(%rip), %rdi
	movl	$.L.str.6, %esi
	jmp	.LBB0_16
.LBB0_15:
	movq	stderr(%rip), %rdi
	movl	$.L.str.7, %esi
.LBB0_16:
	movq	%rbx, %rdx
	xorl	%eax, %eax
	callq	fprintf
.LBB0_11:
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z7readPPMPKcRiS1_, .Lfunc_end0-_Z7readPPMPKcRiS1_
	.cfi_endproc
                                        # -- End function
	.globl	_Z8writePPMPhii                 # -- Begin function _Z8writePPMPhii
	.type	_Z8writePPMPhii,@function
_Z8writePPMPhii:                        # @_Z8writePPMPhii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	pushq	%rax
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%edx, %ebp
	movl	%esi, %r15d
	movq	%rdi, %rbx
	movl	$.L.str.8, %edi
	movl	$.L.str.9, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB1_1
# %bb.2:
	movq	%rax, %r14
	movl	$.L.str.11, %edi
	movl	$3, %esi
	movl	$1, %edx
	movq	%rax, %rcx
	callq	fwrite
	movl	$.L.str.12, %esi
	movq	%r14, %rdi
	movl	%r15d, %edx
	movl	%ebp, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movl	$.L.str.13, %esi
	movq	%r14, %rdi
	movl	$255, %edx
	xorl	%eax, %eax
	callq	fprintf
	leal	(%r15,%r15,2), %eax
	movslq	%eax, %rsi
	movslq	%ebp, %rdx
	movq	%rbx, %rdi
	movq	%r14, %rcx
	callq	fwrite
	movq	%r14, %rdi
	addq	$8, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.LBB1_1:
	.cfi_def_cfa_offset 48
	movq	stderr(%rip), %rdi
	movl	$.L.str.1, %esi
	movl	$.L.str.10, %edx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z8writePPMPhii, .Lfunc_end1-_Z8writePPMPhii
	.cfi_endproc
                                        # -- End function
	.globl	_Z7readBMPPcRiS0_               # -- Begin function _Z7readBMPPcRiS0_
	.type	_Z7readBMPPcRiS0_,@function
_Z7readBMPPcRiS0_:                      # @_Z7readBMPPcRiS0_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r12
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$56, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -40
	.cfi_offset %r12, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdx, %r14
	movq	%rsi, %r15
	movl	$.L.str, %esi
	callq	fopen
	movq	%rax, %rbx
	movq	%rsp, %r12
	movl	$1, %esi
	movl	$54, %edx
	movq	%r12, %rdi
	movq	%rax, %rcx
	callq	fread
	movl	18(%r12), %eax
	movl	%eax, (%r15)
	movl	22(%r12), %eax
	movl	%eax, (%r14)
	imull	(%r15), %eax
	leal	(%rax,%rax,2), %eax
	movslq	%eax, %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, %r15
	movl	$1, %esi
	movq	%rax, %rdi
	movq	%r14, %rdx
	movq	%rbx, %rcx
	callq	fread
	movq	%rbx, %rdi
	callq	fclose
	movq	%r15, %rax
	addq	$56, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z7readBMPPcRiS0_, .Lfunc_end2-_Z7readBMPPcRiS0_
	.cfi_endproc
                                        # -- End function
	.globl	_Z8writeBMPPhii                 # -- Begin function _Z8writeBMPPhii
	.type	_Z8writeBMPPhii,@function
_Z8writeBMPPhii:                        # @_Z8writeBMPPhii
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%edx, %ebx
	movl	%esi, %edx
	movq	%rdi, 8(%rsp)                   # 8-byte Spill
	leal	(%rdx,%rdx,2), %eax
	movq	%rax, %rbp
                                        # kill: def $eax killed $eax killed $rax
	imull	%ebx, %eax
	addl	$54, %eax
	leaq	58(%rsp), %r13
	movw	$19778, (%r13)                  # imm = 0x4D42
	movabsq	$231928233984, %rcx             # imm = 0x3600000000
	movq	%rcx, 6(%r13)
	xorps	%xmm0, %xmm0
	leaq	16(%rsp), %r12
	movaps	%xmm0, (%r12)
	movaps	%xmm0, 16(%r12)
	movq	$0, 32(%r12)
	movb	$40, (%r12)
	movb	$1, 12(%r12)
	movb	$24, 14(%r12)
	movb	$0, 6(%rsp)
	movw	$0, 4(%rsp)
	movl	%eax, 2(%r13)
	movb	%dl, 4(%r12)
	movb	%dh, 21(%rsp)
	movl	%esi, %eax
	shrl	$16, %eax
	movb	%al, 6(%r12)
	movq	%rdx, %r14
	movl	%edx, %eax
	shrl	$24, %eax
	movb	%al, 7(%r12)
	movb	%bl, 8(%r12)
	movb	%bh, 25(%rsp)
	movl	%ebx, %eax
	shrl	$16, %eax
	movb	%al, 10(%r12)
	movl	%ebx, %eax
	shrl	$24, %eax
	movb	%al, 11(%r12)
	movl	$.L.str.14, %edi
	movl	$.L.str.9, %esi
	callq	fopen
	movq	%rax, %r15
	movl	$1, %esi
	movl	$14, %edx
	movq	%r13, %rdi
	movq	%rax, %rcx
	callq	fwrite
	movl	$1, %esi
	movl	$40, %edx
	movq	%r12, %rdi
	movq	%r15, %rcx
	callq	fwrite
	testl	%ebx, %ebx
	jle	.LBB3_3
# %bb.1:                                # %.lr.ph
	movslq	%r14d, %r12
	movq	%rbp, %r13
	negl	%r13d
	andl	$3, %r13d
	movl	%ebx, %ebp
	leal	(%r14,%r14,2), %ebx
	incq	%rbp
	xorl	%r14d, %r14d
.LBB3_2:                                # =>This Inner Loop Header: Depth=1
	movslq	%r14d, %r14
	movq	8(%rsp), %rax                   # 8-byte Reload
	leaq	(%rax,%r14), %rdi
	movl	$3, %esi
	movq	%r12, %rdx
	movq	%r15, %rcx
	callq	fwrite
	movl	$1, %esi
	leaq	4(%rsp), %rdi
	movq	%r13, %rdx
	movq	%r15, %rcx
	callq	fwrite
	addl	%ebx, %r14d
	decq	%rbp
	cmpq	$1, %rbp
	ja	.LBB3_2
.LBB3_3:                                # %._crit_edge
	movq	8(%rsp), %rdi                   # 8-byte Reload
	callq	free
	movq	%r15, %rdi
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.Lfunc_end3:
	.size	_Z8writeBMPPhii, .Lfunc_end3-_Z8writeBMPPhii
	.cfi_endproc
                                        # -- End function
	.globl	_Z25__device_stub__blurKernelPhS_ii # -- Begin function _Z25__device_stub__blurKernelPhS_ii
	.type	_Z25__device_stub__blurKernelPhS_ii,@function
_Z25__device_stub__blurKernelPhS_ii:    # @_Z25__device_stub__blurKernelPhS_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	$_Z10blurKernelPhS_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_end4:
	.size	_Z25__device_stub__blurKernelPhS_ii, .Lfunc_end4-_Z25__device_stub__blurKernelPhS_ii
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI5_0:
	.quad	0x3fa0000000000000              # double 0.03125
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$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
	xorl	%eax, %eax
	leaq	36(%rsp), %r14
	movl	%eax, (%r14)
	leaq	32(%rsp), %r15
	movl	%eax, (%r15)
	movl	$.L.str.15, %edi
	movq	%r14, %rsi
	movq	%r15, %rdx
	callq	_Z7readBMPPcRiS0_
	movq	%rax, %rbx
	movl	$_ZSt4cout, %r13d
	movl	$_ZSt4cout, %edi
	movl	$.L.str.16, %esi
	movl	$12, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r13, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movl	(%r14), %ebp
	movl	(%r15), %r15d
	movl	%ebp, %eax
	imull	%r15d, %eax
	leal	(%rax,%rax,2), %eax
	movslq	%eax, %r12
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r14
	movl	$_ZSt4cout, %edi
	movl	$.L.str.17, %esi
	movl	$13, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%r13, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	leaq	16(%rsp), %r13
	movq	%r13, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movq	%rbx, 24(%rsp)                  # 8-byte Spill
	movq	%rbx, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	8(%rsp), %r13
	movq	%r13, %rdi
	movq	%r12, %rsi
	callq	hipMalloc
	movq	(%r13), %rdi
	movq	%r14, %rsi
	movq	%r12, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	cvtsi2sd	%ebp, %xmm0
	mulsd	.LCPI5_0(%rip), %xmm0
	callq	ceil@PLT
	cvttsd2si	%xmm0, %r13
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15d, %xmm0
	mulsd	.LCPI5_0(%rip), %xmm0
	callq	ceil@PLT
	cvttsd2si	%xmm0, %rbx
	movl	%r13d, %eax
	shlq	$32, %rbx
	orq	%rax, %rbx
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, %r13
	movabsq	$137438953504, %rdx             # imm = 0x2000000020
	movq	%rbx, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB5_2
# %bb.1:
	movq	8(%rsp), %rdi
	movq	16(%rsp), %rsi
	movl	%ebp, %edx
	movl	%r15d, %ecx
	callq	_Z25__device_stub__blurKernelPhS_ii
.LBB5_2:
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, %rbx
	movl	$_ZSt4cout, %edi
	movl	$.L.str.18, %esi
	movl	$5, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	subq	%r13, %rbx
	movl	$_ZSt4cout, %edi
	movq	%rbx, %rsi
	callq	_ZNSo9_M_insertIlEERSoT_
	movq	%rax, %rbx
	movl	$.L.str.19, %esi
	movl	$3, %edx
	movq	%rax, %rdi
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	(%rbx), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movq	%rbx, %rdi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	movq	8(%rsp), %rsi
	movq	%r14, %rdi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%r14, %rdi
	movl	%ebp, %esi
	movl	%r15d, %edx
	callq	_Z8writeBMPPhii
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	movq	24(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%r14, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$40, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	main, .Lfunc_end5-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.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	$_Z10blurKernelPhS_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_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	"rb"
	.size	.L.str, 3

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

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

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

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

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

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

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\img_blur.ppm"
	.size	.L.str.8, 50

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

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"out"
	.size	.L.str.10, 4

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

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

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

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"C:\\UCSP\\2019-I\\AP\\Practice\\blur\\blur\\img_blur.bmp"
	.size	.L.str.14, 50

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

	.type	.L.str.15,@object               # @.str.15
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.15:
	.asciz	"D:\\Documentos\\Semestre 2020-2\\Computaci\303\263n Paralela y Distribuida\\Tareas\\CUDA\\AP-master\\CUDA\\Blur"
	.size	.L.str.15, 98

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"Ready img_in"
	.size	.L.str.16, 13

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"Ready img_out"
	.size	.L.str.17, 14

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"GPU: "
	.size	.L.str.18, 6

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"ns."
	.size	.L.str.19, 4

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z10blurKernelPhS_ii"
	.size	.L__unnamed_1, 21

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

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

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z25__device_stub__blurKernelPhS_ii
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z10blurKernelPhS_ii
	.addrsig_sym _ZSt4cout
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z10blurKernelPhS_ii    ; -- Begin function _Z10blurKernelPhS_ii
	.globl	_Z10blurKernelPhS_ii
	.p2align	8
	.type	_Z10blurKernelPhS_ii,@function
_Z10blurKernelPhS_ii:                   ; @_Z10blurKernelPhS_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 v3, v0, 10, 10
	v_and_b32_e32 v2, 0x3ff, v0
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, s2, 0xffff
	s_lshr_b32 s2, s2, 16
	s_mul_i32 s14, s14, s3
	v_mad_u64_u32 v[0:1], null, s15, s2, v[3:4]
	v_add_nc_u32_e32 v1, s14, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cmp_gt_i32_e32 vcc_lo, s8, v1
	v_cmp_gt_i32_e64 s2, s9, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_and_b32 s2, vcc_lo, s2
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_10
; %bb.1:                                ; %.preheader.preheader
	v_add_nc_u32_e32 v3, -5, v0
	s_load_b128 s[4:7], s[0:1], 0x0
	v_mov_b32_e32 v5, 0
	s_mov_b32 s3, -5
	s_mul_i32 s10, s8, 3
	v_mul_lo_u32 v3, s8, v3
	v_mov_b32_e32 v6, 0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add3_u32 v4, v2, v3, s14
	v_mad_u64_u32 v[2:3], null, v4, 3, -15
	v_dual_mov_b32 v4, 0 :: v_dual_mov_b32 v3, 0
.LBB0_2:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_3 Depth 2
	v_add_nc_u32_e32 v7, s3, v0
	s_mov_b32 s11, -5
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_cmp_lt_i32_e32 vcc_lo, -1, v7
	v_cmp_gt_i32_e64 s0, s9, v7
	v_mov_b32_e32 v7, v2
.LBB0_3:                                ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_and_saveexec_b32 s12, vcc_lo
	s_cbranch_execz .LBB0_7
; %bb.4:                                ;   in Loop: Header=BB0_3 Depth=2
	v_add_nc_u32_e32 v8, s11, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cmp_lt_i32_e64 s1, -1, v8
	v_cmp_gt_i32_e64 s2, s8, v8
	s_and_b32 s1, s0, s1
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	s_and_b32 s1, s1, s2
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_saveexec_b32 s2, s1
	s_cbranch_execz .LBB0_6
; %bb.5:                                ;   in Loop: Header=BB0_3 Depth=2
	v_ashrrev_i32_e32 v9, 31, v7
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v8, s1, s6, v7
	v_add_nc_u32_e32 v6, 1, v6
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e64 v9, s1, s7, v9, s1
	s_clause 0x1
	global_load_u16 v10, v[8:9], off
	global_load_u8 v8, v[8:9], off offset:2
	s_waitcnt vmcnt(1)
	v_and_b32_e32 v9, 0xff, v10
	v_lshrrev_b32_e32 v10, 8, v10
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v4, v4, v8
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v3, v3, v9
	v_add_nc_u32_e32 v5, v5, v10
.LBB0_6:                                ; %Flow
                                        ;   in Loop: Header=BB0_3 Depth=2
	s_or_b32 exec_lo, exec_lo, s2
.LBB0_7:                                ;   in Loop: Header=BB0_3 Depth=2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	s_or_b32 exec_lo, exec_lo, s12
	v_add_nc_u32_e32 v7, 3, v7
	s_add_i32 s11, s11, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s11, 5
	s_cbranch_scc0 .LBB0_3
; %bb.8:                                ;   in Loop: Header=BB0_2 Depth=1
	v_add_nc_u32_e32 v2, s10, v2
	s_add_i32 s3, s3, 1
	s_delay_alu instid0(SALU_CYCLE_1)
	s_cmp_eq_u32 s3, 5
	s_cbranch_scc0 .LBB0_2
; %bb.9:
	v_ashrrev_i32_e32 v7, 31, v6
	v_ashrrev_i32_e32 v9, 31, v3
	v_ashrrev_i32_e32 v10, 31, v5
	v_ashrrev_i32_e32 v11, 31, v4
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_nc_u32_e32 v2, v6, v7
	v_add_nc_u32_e32 v3, v3, v9
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v4, v4, v11
	v_xor_b32_e32 v6, v2, v7
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_xor_b32_e32 v12, v3, v9
	v_xor_b32_e32 v4, v4, v11
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_cvt_f32_u32_e32 v2, v6
	v_sub_nc_u32_e32 v8, 0, v6
	v_rcp_iflag_f32_e32 v2, v2
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v2, 0x4f7ffffe, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_u32_f32_e32 v2, v2
	v_mul_lo_u32 v8, v8, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_hi_u32 v8, v2, v8
	v_add_nc_u32_e32 v2, v2, v8
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_mul_hi_u32 v8, v12, v2
	v_add_nc_u32_e32 v5, v5, v10
	v_mul_hi_u32 v14, v4, v2
	v_xor_b32_e32 v5, v5, v10
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v15, v8, v6
	v_mul_hi_u32 v13, v5, v2
	v_mad_u64_u32 v[2:3], null, v0, s8, v[1:2]
	v_xor_b32_e32 v3, v10, v7
	v_mul_lo_u32 v0, v14, v6
	v_sub_nc_u32_e32 v10, v12, v15
	v_xor_b32_e32 v1, v9, v7
	v_add_nc_u32_e32 v9, 1, v8
	v_mul_lo_u32 v16, v13, v6
	v_add_nc_u32_e32 v12, 1, v13
	v_cmp_ge_u32_e32 vcc_lo, v10, v6
	v_xor_b32_e32 v7, v11, v7
	v_sub_nc_u32_e32 v0, v4, v0
	v_lshl_add_u32 v2, v2, 1, v2
	v_cndmask_b32_e32 v4, v8, v9, vcc_lo
	v_sub_nc_u32_e32 v5, v5, v16
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_ge_u32_e64 s1, v0, v6
	v_sub_nc_u32_e32 v8, v5, v6
	v_cmp_ge_u32_e64 s0, v5, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_cndmask_b32_e64 v9, v13, v12, s0
	v_sub_nc_u32_e32 v12, v10, v6
	v_cndmask_b32_e64 v5, v5, v8, s0
	v_add_nc_u32_e32 v15, 1, v14
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_nc_u32_e32 v8, 1, v9
	v_cndmask_b32_e32 v10, v10, v12, vcc_lo
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cmp_ge_u32_e32 vcc_lo, v5, v6
	v_cndmask_b32_e64 v13, v14, v15, s1
	v_sub_nc_u32_e32 v14, v0, v6
	v_add_nc_u32_e32 v12, 1, v4
	v_cndmask_b32_e32 v5, v9, v8, vcc_lo
	v_cmp_ge_u32_e32 vcc_lo, v10, v6
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_cndmask_b32_e64 v0, v0, v14, s1
	v_add_nc_u32_e32 v8, 1, v13
	v_xor_b32_e32 v5, v5, v3
	v_cndmask_b32_e32 v4, v4, v12, vcc_lo
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cmp_ge_u32_e32 vcc_lo, v0, v6
	v_sub_nc_u32_e32 v3, v5, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_xor_b32_e32 v4, v4, v1
	v_cndmask_b32_e32 v0, v13, v8, vcc_lo
	v_lshlrev_b16 v3, 8, v3
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_4)
	v_sub_nc_u32_e32 v1, v4, v1
	v_ashrrev_i32_e32 v4, 31, v2
	v_xor_b32_e32 v5, v0, v7
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s4, v2
	v_and_b32_e32 v6, 0xff, v1
	v_add_co_ci_u32_e32 v1, vcc_lo, s5, v4, vcc_lo
	v_sub_nc_u32_e32 v2, v5, v7
	s_delay_alu instid0(VALU_DEP_3)
	v_or_b32_e32 v3, v6, v3
	s_clause 0x1
	global_store_b8 v[0:1], v2, off offset:2
	global_store_b16 v[0:1], v3, off
.LBB0_10:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z10blurKernelPhS_ii
		.amdhsa_group_segment_fixed_size 0
		.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 17
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z10blurKernelPhS_ii, .Lfunc_end0-_Z10blurKernelPhS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 856
; NumSgprs: 18
; NumVgprs: 17
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 17
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 14
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 1
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         20
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     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: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z10blurKernelPhS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z10blurKernelPhS_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     17
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
