#include <stdio.h>
#include <stdint.h>

#define CHECK(call)\
{\
	const cudaError_t error = call;\
	if (error != cudaSuccess)\
	{\
		fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__);\
		fprintf(stderr, "code: %d, reason: %s\n", error,\
				cudaGetErrorString(error));\
		exit(EXIT_FAILURE);\
	}\
}

struct GpuTimer
{
	cudaEvent_t start;
	cudaEvent_t stop;

	GpuTimer()
	{
		cudaEventCreate(&start);
		cudaEventCreate(&stop);
	}

	~GpuTimer()
	{
		cudaEventDestroy(start);
		cudaEventDestroy(stop);
	}

	void Start()
	{
		cudaEventRecord(start, 0);                                                                 
		cudaEventSynchronize(start);
	}

	void Stop()
	{
		cudaEventRecord(stop, 0);
	}

	float Elapsed()
	{
		float elapsed;
		cudaEventSynchronize(stop);
		cudaEventElapsedTime(&elapsed, start, stop);
		return elapsed;
	}
};

void readPnm(char * fileName, 
		int &numChannels, int &width, int &height, uint8_t * &pixels)
{
	FILE * f = fopen(fileName, "r");
	if (f == NULL)
	{
		printf("Cannot read %s\n", fileName);
		exit(EXIT_FAILURE);
	}

	char type[3];
	fscanf(f, "%s", type);
	if (strcmp(type, "P2") == 0)
		numChannels = 1;
	else if (strcmp(type, "P3") == 0)
		numChannels = 3;
	else // In this exercise, we don't touch other types
	{
		fclose(f);
		printf("Cannot read %s\n", fileName); 
		exit(EXIT_FAILURE); 
	}

	fscanf(f, "%i", &width);
	fscanf(f, "%i", &height);

	int max_val;
	fscanf(f, "%i", &max_val);
	if (max_val > 255) // In this exercise, we assume 1 byte per value
	{
		fclose(f);
		printf("Cannot read %s\n", fileName); 
		exit(EXIT_FAILURE); 
	}

	pixels = (uint8_t *)malloc(width * height * numChannels);
	for (int i = 0; i < width * height * numChannels; i++)
		fscanf(f, "%hhu", &pixels[i]);

	fclose(f);
}

void writePnm(uint8_t * pixels, int numChannels, int width, int height, 
		char * fileName)
{
	FILE * f = fopen(fileName, "w");
	if (f == NULL)
	{
		printf("Cannot write %s\n", fileName);
		exit(EXIT_FAILURE);
	}	

	if (numChannels == 1)
		fprintf(f, "P2\n");
	else if (numChannels == 3)
		fprintf(f, "P3\n");
	else
	{
		fclose(f);
		printf("Cannot write %s\n", fileName);
		exit(EXIT_FAILURE);
	}

	fprintf(f, "%i\n%i\n255\n", width, height); 

	for (int i = 0; i < width * height * numChannels; i++)
		fprintf(f, "%hhu\n", pixels[i]);

	fclose(f);
}

__global__ void convertRgb2GrayKernel(uint8_t * inPixels, int width, int height, 
		uint8_t * outPixels)
{
	// TODO
	int ix = blockIdx.x * blockDim.x + threadIdx.x;
	int iy = blockIdx.y * blockDim.y + threadIdx.y;
	if (ix < width && iy < height)
	{
		int i = iy * width + ix;
		uint8_t r = inPixels[i * 3];
		uint8_t g = inPixels[i * 3 + 1];
		uint8_t b = inPixels[i * 3 + 2];
		outPixels[i] = 0.299f * r + 0.587f * g + 0.114f * b;
	}
}

void convertRgb2Gray(uint8_t * inPixels, int width, int height,
		uint8_t * outPixels, 
		bool useDevice=false, dim3 blockSize=dim3(1))
{
	GpuTimer timer;
	timer.Start();
	if (useDevice == false)
	{
		for (int r = 0; r < height; r++)
		{
			for (int c = 0; c < width; c++)
			{
				int i = r * width + c;
				uint8_t r = inPixels[i * 3];
				uint8_t g = inPixels[i * 3 + 1];
				uint8_t b = inPixels[i * 3 + 2];
				outPixels[i] = 0.299f * r + 0.587f * g + 0.114f * b;
			}
		}
	}
	else // use device
	{
		int numChannels = 3;
		cudaDeviceProp devProp;
		cudaGetDeviceProperties(&devProp, 0);
		printf("GPU name: %s\n", devProp.name);
		printf("GPU compute capability: %d.%d\n", devProp.major, devProp.minor);

		// TODO: Allocate device memories
		uint8_t *d_inPixels, * d_outPixels;
		CHECK(cudaMalloc(&d_inPixels, height * width * numChannels * sizeof(uint8_t)));
		CHECK(cudaMalloc(&d_outPixels, height * width * sizeof(uint8_t)));

		// TODO: Copy data to device memories
		CHECK(cudaMemcpy(d_inPixels, inPixels, height * width * numChannels * sizeof(uint8_t), cudaMemcpyHostToDevice));

		// TODO: Set grid size and call kernel (remember to check kernel error)
		dim3 gridSize((width - 1)/blockSize.x + 1, (height - 1)/blockSize.y + 1);
		convertRgb2GrayKernel<<<gridSize, blockSize>>>(d_inPixels, width, height, d_outPixels);
		cudaError_t errSync  = cudaGetLastError();
		cudaError_t errAsync = cudaDeviceSynchronize();
		if (errSync != cudaSuccess)
			printf("Sync kernel error: %s\n", cudaGetErrorString(errSync));
		if (errAsync != cudaSuccess)
			printf("Async kernel error: %s\n", cudaGetErrorString(errAsync));
		// TODO: Copy result from device memories
		CHECK(cudaMemcpy(outPixels, d_outPixels, height * width * sizeof(uint8_t), cudaMemcpyDeviceToHost));

		// TODO: Free device memories
		CHECK(cudaFree(d_inPixels));
		CHECK(cudaFree(d_outPixels));
	}
	timer.Stop();
	float time = timer.Elapsed();
	printf("Processing time (%s): %f ms\n\n", 
			useDevice == true? "use device" : "use host", time);
}

float computeError(uint8_t * a1, uint8_t * a2, int n)
{
	float err = 0;
	for (int i = 0; i < n; i++)
		err += abs((int)a1[i] - (int)a2[i]);
	err /= n;
	return err;
}

char * concatStr(const char * s1, const char * s2)
{
	char * result = (char *)malloc(strlen(s1) + strlen(s2) + 1);
	strcpy(result, s1);
	strcat(result, s2);
	return result;
}

int main(int argc, char ** argv)
{	
	if (argc != 3 && argc != 5)
	{
		printf("The number of arguments is invalid\n");
		return EXIT_FAILURE;
	}

	// Read input RGB image file
	int numChannels, width, height;
	uint8_t * inPixels;
	readPnm(argv[1], numChannels, width, height, inPixels);
	if (numChannels != 3)
		return EXIT_FAILURE; // Input image must be RGB
	printf("Image size (width x height): %i x %i\n\n", width, height);

	// Convert RGB to grayscale not using device
	uint8_t * correctOutPixels= (uint8_t *)malloc(width * height);
	convertRgb2Gray(inPixels, width, height, correctOutPixels);

	// Convert RGB to grayscale using device
	uint8_t * outPixels= (uint8_t *)malloc(width * height);
	dim3 blockSize(32, 32); // Default
	if (argc == 5)
	{
		blockSize.x = atoi(argv[3]);
		blockSize.y = atoi(argv[4]);
	} 
	convertRgb2Gray(inPixels, width, height, outPixels, true, blockSize); 

	// Compute mean absolute error between host result and device result
	float err = computeError(outPixels, correctOutPixels, width * height);
	printf("Error between device result and host result: %f\n", err);

	// Write results to files
	char * outFileNameBase = strtok(argv[2], "."); // Get rid of extension
	writePnm(correctOutPixels, 1, width, height, concatStr(outFileNameBase, "_host.pnm"));
	writePnm(outPixels, 1, width, height, concatStr(outFileNameBase, "_device.pnm"));

	// Free memories
	free(inPixels);
	free(outPixels);
}

	.file	"tmpxft_002553a6_00000000-6_bt01_p1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2044:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2044:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"r"
.LC1:
	.string	"Cannot read %s\n"
.LC2:
	.string	"%s"
.LC3:
	.string	"P2"
.LC4:
	.string	"P3"
.LC5:
	.string	"%i"
.LC6:
	.string	"%hhu"
	.text
	.globl	_Z7readPnmPcRiS0_S0_RPh
	.type	_Z7readPnmPcRiS0_S0_RPh, @function
_Z7readPnmPcRiS0_S0_RPh:
.LFB2036:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%r8, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdx, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rcx, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rsi, %rbp
	leaq	.LC0(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$40, %rsp
	.cfi_def_cfa_offset 96
	movq	%rdi, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	testq	%rax, %rax
	je	.L13
	leaq	21(%rsp), %r15
	movq	%rax, %rdi
	leaq	.LC2(%rip), %rsi
	movq	%rax, %rbx
	movq	%r15, %rdx
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	leaq	.LC3(%rip), %rsi
	movq	%r15, %rdi
	call	strcmp@PLT
	testl	%eax, %eax
	je	.L9
	leaq	.LC4(%rip), %rsi
	movq	%r15, %rdi
	call	strcmp@PLT
	movl	%eax, %edx
	movl	$3, %eax
	testl	%edx, %edx
	je	.L4
.L14:
	movq	%rbx, %rdi
	call	fclose@PLT
.L13:
	movq	8(%rsp), %rdx
	movl	$2, %edi
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	movl	$1, %eax
.L4:
	movl	%eax, 0(%rbp)
	leaq	.LC5(%rip), %r15
	movq	%r13, %rdx
	movq	%rbx, %rdi
	movq	%r15, %rsi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	movq	%r12, %rdx
	movq	%r15, %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__isoc23_fscanf@PLT
	xorl	%eax, %eax
	leaq	16(%rsp), %rdx
	movq	%r15, %rsi
	movq	%rbx, %rdi
	call	__isoc23_fscanf@PLT
	cmpl	$255, 16(%rsp)
	jg	.L14
	movl	0(%r13), %edi
	imull	(%r12), %edi
	xorl	%r15d, %r15d
	imull	0(%rbp), %edi
	movslq	%edi, %rdi
	call	malloc@PLT
	movq	%rax, (%r14)
.L6:
	movl	0(%r13), %eax
	imull	(%r12), %eax
	imull	0(%rbp), %eax
	cmpl	%r15d, %eax
	jle	.L16
	movq	(%r14), %rdx
	leaq	.LC6(%rip), %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	addq	%r15, %rdx
	incq	%r15
	call	__isoc23_fscanf@PLT
	jmp	.L6
.L16:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L8
	call	__stack_chk_fail@PLT
.L8:
	addq	$40, %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
.LFE2036:
	.size	_Z7readPnmPcRiS0_S0_RPh, .-_Z7readPnmPcRiS0_S0_RPh
	.section	.rodata.str1.1
.LC7:
	.string	"w"
.LC8:
	.string	"Cannot write %s\n"
.LC9:
	.string	"P2\n"
.LC10:
	.string	"P3\n"
.LC11:
	.string	"%i\n%i\n255\n"
.LC12:
	.string	"%hhu\n"
	.text
	.globl	_Z8writePnmPhiiiPc
	.type	_Z8writePnmPhiiiPc, @function
_Z8writePnmPhiiiPc:
.LFB2037:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%ecx, %r15d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%r8, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rdi, %r13
	movq	%r8, %rdi
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%esi, %r12d
	leaq	.LC7(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%edx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	pushq	%r9
	.cfi_def_cfa_offset 64
	call	fopen@PLT
	testq	%rax, %rax
	je	.L26
	movq	%rax, %rbx
	leaq	.LC9(%rip), %rdx
	cmpl	$1, %r12d
	je	.L25
	cmpl	$3, %r12d
	jne	.L21
	leaq	.LC10(%rip), %rdx
.L25:
	movl	$2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	%ebp, %ecx
	imull	%r15d, %ebp
	movl	%r15d, %r8d
	leaq	.LC11(%rip), %rdx
	movl	$2, %esi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	leaq	.LC12(%rip), %r14
	imull	%r12d, %ebp
	xorl	%r12d, %r12d
	jmp	.L22
.L21:
	movq	%rax, %rdi
	call	fclose@PLT
.L26:
	movl	$2, %edi
	movq	%r14, %rdx
	leaq	.LC8(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L22:
	cmpl	%r12d, %ebp
	jle	.L27
	movzbl	0(%r13,%r12), %ecx
	movq	%r14, %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	movq	%rbx, %rdi
	incq	%r12
	call	__fprintf_chk@PLT
	jmp	.L22
.L27:
	popq	%rax
	.cfi_def_cfa_offset 56
	movq	%rbx, %rdi
	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
	jmp	fclose@PLT
	.cfi_endproc
.LFE2037:
	.size	_Z8writePnmPhiiiPc, .-_Z8writePnmPhiiiPc
	.globl	_Z12computeErrorPhS_i
	.type	_Z12computeErrorPhS_i, @function
_Z12computeErrorPhS_i:
.LFB2039:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	xorps	%xmm0, %xmm0
.L29:
	cmpl	%eax, %edx
	jle	.L31
	movzbl	(%rsi,%rax), %r8d
	movzbl	(%rdi,%rax), %ecx
	subl	%r8d, %ecx
	movl	%ecx, %r8d
	negl	%r8d
	cmovns	%r8d, %ecx
	incq	%rax
	cvtsi2ssl	%ecx, %xmm1
	addss	%xmm1, %xmm0
	jmp	.L29
.L31:
	cvtsi2ssl	%edx, %xmm1
	divss	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2039:
	.size	_Z12computeErrorPhS_i, .-_Z12computeErrorPhS_i
	.globl	_Z9concatStrPKcS0_
	.type	_Z9concatStrPKcS0_, @function
_Z9concatStrPKcS0_:
.LFB2040:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rsi, %rbx
	call	strlen@PLT
	movq	%rbx, %rdi
	movq	%rax, %r12
	call	strlen@PLT
	leaq	1(%r12,%rax), %r12
	movq	%r12, %rdi
	call	malloc@PLT
	movq	%r12, %rdx
	movq	%rbp, %rsi
	movq	%rax, %rdi
	call	__strcpy_chk@PLT
	movq	%r12, %rdx
	movq	%rbx, %rsi
	movq	%rax, %rdi
	call	__strcat_chk@PLT
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2040:
	.size	_Z9concatStrPKcS0_, .-_Z9concatStrPKcS0_
	.globl	_Z45__device_stub__Z21convertRgb2GrayKernelPhiiS_PhiiS_
	.type	_Z45__device_stub__Z21convertRgb2GrayKernelPhiiS_PhiiS_, @function
_Z45__device_stub__Z21convertRgb2GrayKernelPhiiS_PhiiS_:
.LFB2066:
	.cfi_startproc
	endbr64
	subq	$152, %rsp
	.cfi_def_cfa_offset 160
	movq	%rdi, 24(%rsp)
	leaq	56(%rsp), %rdi
	movl	%esi, 20(%rsp)
	leaq	68(%rsp), %rsi
	movl	%edx, 16(%rsp)
	leaq	40(%rsp), %rdx
	movq	%rcx, 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	20(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	16(%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	.L34
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z21convertRgb2GrayKernelPhiiS_(%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
.L34:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L36
	call	__stack_chk_fail@PLT
.L36:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2066:
	.size	_Z45__device_stub__Z21convertRgb2GrayKernelPhiiS_PhiiS_, .-_Z45__device_stub__Z21convertRgb2GrayKernelPhiiS_PhiiS_
	.globl	_Z21convertRgb2GrayKernelPhiiS_
	.type	_Z21convertRgb2GrayKernelPhiiS_, @function
_Z21convertRgb2GrayKernelPhiiS_:
.LFB2067:
	.cfi_startproc
	endbr64
	jmp	_Z45__device_stub__Z21convertRgb2GrayKernelPhiiS_PhiiS_
	.cfi_endproc
.LFE2067:
	.size	_Z21convertRgb2GrayKernelPhiiS_, .-_Z21convertRgb2GrayKernelPhiiS_
	.section	.rodata.str1.1
.LC14:
	.string	"use device"
.LC15:
	.string	"use host"
.LC19:
	.string	"GPU name: %s\n"
.LC20:
	.string	"GPU compute capability: %d.%d\n"
.LC21:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/hmhuan/PG_GPU/master/assignment/BT01/1612858/bt01_p1.cu"
.LC22:
	.string	"Error: %s:%d, "
.LC23:
	.string	"code: %d, reason: %s\n"
.LC24:
	.string	"Sync kernel error: %s\n"
.LC25:
	.string	"Async kernel error: %s\n"
.LC26:
	.string	"Processing time (%s): %f ms\n\n"
	.text
	.globl	_Z15convertRgb2GrayPhiiS_b4dim3
	.type	_Z15convertRgb2GrayPhiiS_b4dim3, @function
_Z15convertRgb2GrayPhiiS_b4dim3:
.LFB2038:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA2038
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%r8d, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%edx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%esi, %ebx
	subq	$1112, %rsp
	.cfi_def_cfa_offset 1168
	movq	%rcx, 8(%rsp)
	leaq	48(%rsp), %rdi
	movq	%fs:40, %rax
	movq	%rax, 1096(%rsp)
	xorl	%eax, %eax
.LEHB0:
	call	cudaEventCreate@PLT
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
.LEHE0:
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
.LEHB1:
	call	cudaEventRecord@PLT
	movq	48(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	testb	%r13b, %r13b
	jne	.L40
	movss	.LC16(%rip), %xmm2
	movss	.LC17(%rip), %xmm3
	xorl	%esi, %esi
	xorl	%edi, %edi
	movss	.LC18(%rip), %xmm4
.L41:
	cmpl	%ebp, %edi
	jge	.L43
	movq	8(%rsp), %r10
	leal	(%rsi,%rsi,2), %eax
	movslq	%esi, %rcx
	xorl	%edx, %edx
	cltq
	addq	%r14, %rax
	addq	%r10, %rcx
.L44:
	cmpl	%edx, %ebx
	jle	.L93
	movzbl	(%rax), %r8d
	addq	$3, %rax
	cvtsi2ssl	%r8d, %xmm0
	movzbl	-2(%rax), %r8d
	cvtsi2ssl	%r8d, %xmm1
	movzbl	-1(%rax), %r8d
	mulss	%xmm2, %xmm0
	mulss	%xmm3, %xmm1
	addss	%xmm1, %xmm0
	cvtsi2ssl	%r8d, %xmm1
	mulss	%xmm4, %xmm1
	addss	%xmm1, %xmm0
	cvttss2sil	%xmm0, %r8d
	movb	%r8b, (%rcx,%rdx)
	incq	%rdx
	jmp	.L44
.L93:
	incl	%edi
	addl	%ebx, %esi
	jmp	.L41
.L40:
	leaq	64(%rsp), %r12
	xorl	%esi, %esi
	movq	%r12, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movq	%r12, %rdx
	leaq	.LC19(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	428(%rsp), %ecx
	movl	424(%rsp), %edx
	leaq	.LC20(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	%ebp, %r15d
	leaq	16(%rsp), %rdi
	imull	%ebx, %r15d
	leal	(%r15,%r15,2), %eax
	cltq
	movq	%rax, %rsi
	movq	%rax, (%rsp)
	call	cudaMalloc@PLT
	movl	%eax, %r12d
	testl	%eax, %eax
	je	.L45
	movl	$170, %r8d
.L88:
	movq	stderr(%rip), %rdi
	leaq	.LC21(%rip), %rcx
	leaq	.LC22(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%r12d, %edi
	call	cudaGetErrorString@PLT
	movq	%rax, %r8
	movl	%r12d, %ecx
.L74:
	movq	stderr(%rip), %rdi
	leaq	.LC23(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L45:
	movslq	%r15d, %r15
	leaq	24(%rsp), %rdi
	movq	%r15, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %r12d
	movl	$171, %r8d
	testl	%eax, %eax
	jne	.L88
	movq	(%rsp), %rdx
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movq	%r14, %rsi
	call	cudaMemcpy@PLT
	movl	%eax, %r12d
	movl	$174, %r8d
	testl	%eax, %eax
	jne	.L88
	leal	-1(%rbp), %eax
	xorl	%edx, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	divl	1172(%rsp)
	movl	$1, 44(%rsp)
	xorl	%edx, %edx
	movl	$1, %esi
	leal	1(%rax), %ecx
	leal	-1(%rbx), %eax
	divl	1168(%rsp)
	movl	%ecx, 40(%rsp)
	movq	1168(%rsp), %rdx
	movl	1176(%rsp), %ecx
	incl	%eax
	movl	%eax, 36(%rsp)
	movq	36(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	je	.L49
.L51:
	call	cudaGetLastError@PLT
	movl	%eax, %ebp
	call	cudaDeviceSynchronize@PLT
	jmp	.L94
.L49:
	movq	24(%rsp), %rcx
	movq	16(%rsp), %rdi
	movl	%ebp, %edx
	movl	%ebx, %esi
	call	_Z45__device_stub__Z21convertRgb2GrayKernelPhiiS_PhiiS_
	jmp	.L51
.L94:
	movl	%eax, %ebx
	testl	%ebp, %ebp
	jne	.L52
.L55:
	testl	%ebx, %ebx
	jne	.L53
	jmp	.L54
.L52:
	movl	%ebp, %edi
	call	cudaGetErrorString@PLT
	movq	%rax, %rdx
	leaq	.LC24(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L55
.L53:
	movl	%ebx, %edi
	call	cudaGetErrorString@PLT
	movq	%rax, %rdx
	leaq	.LC25(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L54:
	movq	24(%rsp), %rsi
	movq	8(%rsp), %rdi
	movl	$2, %ecx
	movq	%r15, %rdx
	call	cudaMemcpy@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L57
	movl	$186, %r8d
	jmp	.L91
.L57:
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movl	%eax, %ebx
	movl	$189, %r8d
	testl	%eax, %eax
	jne	.L91
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.L43
	movl	$190, %r8d
.L91:
	movq	stderr(%rip), %rdi
	leaq	.LC21(%rip), %rcx
	leaq	.LC22(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%ebx, %edi
	call	cudaGetErrorString@PLT
	movq	%rax, %r8
	movl	%ebx, %ecx
	jmp	.L74
.L43:
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	36(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC14(%rip), %rax
	testb	%r13b, %r13b
	leaq	.LC15(%rip), %rdx
	movl	$2, %edi
	cmovne	%rax, %rdx
	leaq	.LC26(%rip), %rsi
	movb	$1, %al
	cvtss2sd	36(%rsp), %xmm0
	call	__printf_chk@PLT
.LEHE1:
	movq	48(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	56(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	1096(%rsp), %rax
	subq	%fs:40, %rax
	je	.L63
	jmp	.L71
.L65:
	endbr64
	movq	%rax, %rbx
.L61:
	movq	48(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	56(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	1096(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L71
	movq	%rbx, %rdi
.LEHB2:
	call	_Unwind_Resume@PLT
.LEHE2:
.L71:
	call	__stack_chk_fail@PLT
.L63:
	addq	$1112, %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
.LFE2038:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA2038:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE2038-.LLSDACSB2038
.LLSDACSB2038:
	.uleb128 .LEHB0-.LFB2038
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB2038
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L65-.LFB2038
	.uleb128 0
	.uleb128 .LEHB2-.LFB2038
	.uleb128 .LEHE2-.LEHB2
	.uleb128 0
	.uleb128 0
.LLSDACSE2038:
	.text
	.size	_Z15convertRgb2GrayPhiiS_b4dim3, .-_Z15convertRgb2GrayPhiiS_b4dim3
	.section	.rodata.str1.1
.LC27:
	.string	"The number of arguments is invalid\n"
.LC28:
	.string	"Image size (width x height): %i x %i\n\n"
.LC29:
	.string	"Error between device result and host result: %f\n"
.LC30:
	.string	"."
.LC31:
	.string	"_host.pnm"
.LC32:
	.string	"_device.pnm"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2041:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	leal	-3(%rdi), %eax
	andl	$-3, %eax
	je	.L96
	leaq	.LC27(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L97
.L96:
	movq	%rsi, %rbx
	leaq	28(%rsp), %rcx
	leaq	24(%rsp), %rdx
	movl	%edi, %r12d
	movq	8(%rbx), %rdi
	leaq	20(%rsp), %rsi
	leaq	32(%rsp), %r8
	call	_Z7readPnmPcRiS0_S0_RPh
	cmpl	$3, 20(%rsp)
	je	.L98
.L97:
	movl	$1, %eax
	jmp	.L95
.L98:
	movl	28(%rsp), %ecx
	movl	24(%rsp), %edx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC28(%rip), %rsi
	call	__printf_chk@PLT
	movl	24(%rsp), %r14d
	movl	28(%rsp), %r15d
	movl	%r14d, %edi
	imull	%r15d, %edi
	movslq	%edi, %rdi
	call	malloc@PLT
	movq	32(%rsp), %r13
	xorl	%r8d, %r8d
	movl	%r15d, %edx
	movq	%rax, %rbp
	movl	$1, 52(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 44(%rsp)
	movq	%rbp, %rcx
	movq	%r13, %rdi
	pushq	%rsi
	.cfi_def_cfa_offset 136
	pushq	%rsi
	.cfi_def_cfa_offset 144
	movl	%r14d, %esi
	movq	%rax, (%rsp)
	movl	$1, 8(%rsp)
	call	_Z15convertRgb2GrayPhiiS_b4dim3
	movl	40(%rsp), %r14d
	movl	44(%rsp), %r15d
	movl	%r14d, %edi
	imull	%r15d, %edi
	movslq	%edi, %rdi
	call	malloc@PLT
	movl	$1, 68(%rsp)
	movq	%rax, 24(%rsp)
	movabsq	$137438953504, %rax
	movq	%rax, 60(%rsp)
	popq	%rdi
	.cfi_def_cfa_offset 136
	popq	%r8
	.cfi_def_cfa_offset 128
	cmpl	$5, %r12d
	jne	.L100
	movq	24(%rbx), %rdi
	call	atoi@PLT
	movq	32(%rbx), %rdi
	movl	%eax, 44(%rsp)
	call	atoi@PLT
	movl	%eax, 48(%rsp)
.L100:
	pushq	%rax
	.cfi_def_cfa_offset 136
	movl	$1, %r8d
	movl	%r15d, %edx
	movl	%r14d, %esi
	pushq	%rax
	.cfi_def_cfa_offset 144
	movq	60(%rsp), %rax
	movq	%r13, %rdi
	movq	24(%rsp), %rcx
	movq	%rax, (%rsp)
	movl	68(%rsp), %eax
	movl	%eax, 8(%rsp)
	call	_Z15convertRgb2GrayPhiiS_b4dim3
	movl	40(%rsp), %edx
	imull	44(%rsp), %edx
	movq	%rbp, %rsi
	movq	24(%rsp), %rdi
	call	_Z12computeErrorPhS_i
	popq	%rdx
	.cfi_def_cfa_offset 136
	leaq	.LC29(%rip), %rsi
	popq	%rcx
	.cfi_def_cfa_offset 128
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
	movq	16(%rbx), %rdi
	leaq	.LC30(%rip), %rsi
	call	strtok@PLT
	leaq	.LC31(%rip), %rsi
	movq	%rax, %rdi
	movq	%rax, %rbx
	call	_Z9concatStrPKcS0_
	movl	28(%rsp), %ecx
	movl	24(%rsp), %edx
	movq	%rbp, %rdi
	movq	%rax, %r8
	movl	$1, %esi
	call	_Z8writePnmPhiiiPc
	leaq	.LC32(%rip), %rsi
	movq	%rbx, %rdi
	call	_Z9concatStrPKcS0_
	movl	28(%rsp), %ecx
	movl	24(%rsp), %edx
	movl	$1, %esi
	movq	8(%rsp), %rdi
	movq	%rax, %r8
	call	_Z8writePnmPhiiiPc
	movq	%r13, %rdi
	call	free@PLT
	movq	8(%rsp), %rdi
	call	free@PLT
	xorl	%eax, %eax
.L95:
	movq	56(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L101
	call	__stack_chk_fail@PLT
.L101:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2041:
	.size	main, .-main
	.section	.rodata.str1.1
.LC33:
	.string	"_Z21convertRgb2GrayKernelPhiiS_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2069:
	.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	.LC33(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z21convertRgb2GrayKernelPhiiS_(%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
.LFE2069:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC16:
	.long	1050220167
	.align 4
.LC17:
	.long	1058424226
	.align 4
.LC18:
	.long	1038710997
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.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 <stdint.h>

#define CHECK(call)\
{\
	const hipError_t error = call;\
	if (error != hipSuccess)\
	{\
		fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__);\
		fprintf(stderr, "code: %d, reason: %s\n", error,\
				hipGetErrorString(error));\
		exit(EXIT_FAILURE);\
	}\
}

struct GpuTimer
{
	hipEvent_t start;
	hipEvent_t stop;

	GpuTimer()
	{
		hipEventCreate(&start);
		hipEventCreate(&stop);
	}

	~GpuTimer()
	{
		hipEventDestroy(start);
		hipEventDestroy(stop);
	}

	void Start()
	{
		hipEventRecord(start, 0);                                                                 
		hipEventSynchronize(start);
	}

	void Stop()
	{
		hipEventRecord(stop, 0);
	}

	float Elapsed()
	{
		float elapsed;
		hipEventSynchronize(stop);
		hipEventElapsedTime(&elapsed, start, stop);
		return elapsed;
	}
};

void readPnm(char * fileName, 
		int &numChannels, int &width, int &height, uint8_t * &pixels)
{
	FILE * f = fopen(fileName, "r");
	if (f == NULL)
	{
		printf("Cannot read %s\n", fileName);
		exit(EXIT_FAILURE);
	}

	char type[3];
	fscanf(f, "%s", type);
	if (strcmp(type, "P2") == 0)
		numChannels = 1;
	else if (strcmp(type, "P3") == 0)
		numChannels = 3;
	else // In this exercise, we don't touch other types
	{
		fclose(f);
		printf("Cannot read %s\n", fileName); 
		exit(EXIT_FAILURE); 
	}

	fscanf(f, "%i", &width);
	fscanf(f, "%i", &height);

	int max_val;
	fscanf(f, "%i", &max_val);
	if (max_val > 255) // In this exercise, we assume 1 byte per value
	{
		fclose(f);
		printf("Cannot read %s\n", fileName); 
		exit(EXIT_FAILURE); 
	}

	pixels = (uint8_t *)malloc(width * height * numChannels);
	for (int i = 0; i < width * height * numChannels; i++)
		fscanf(f, "%hhu", &pixels[i]);

	fclose(f);
}

void writePnm(uint8_t * pixels, int numChannels, int width, int height, 
		char * fileName)
{
	FILE * f = fopen(fileName, "w");
	if (f == NULL)
	{
		printf("Cannot write %s\n", fileName);
		exit(EXIT_FAILURE);
	}	

	if (numChannels == 1)
		fprintf(f, "P2\n");
	else if (numChannels == 3)
		fprintf(f, "P3\n");
	else
	{
		fclose(f);
		printf("Cannot write %s\n", fileName);
		exit(EXIT_FAILURE);
	}

	fprintf(f, "%i\n%i\n255\n", width, height); 

	for (int i = 0; i < width * height * numChannels; i++)
		fprintf(f, "%hhu\n", pixels[i]);

	fclose(f);
}

__global__ void convertRgb2GrayKernel(uint8_t * inPixels, int width, int height, 
		uint8_t * outPixels)
{
	// TODO
	int ix = blockIdx.x * blockDim.x + threadIdx.x;
	int iy = blockIdx.y * blockDim.y + threadIdx.y;
	if (ix < width && iy < height)
	{
		int i = iy * width + ix;
		uint8_t r = inPixels[i * 3];
		uint8_t g = inPixels[i * 3 + 1];
		uint8_t b = inPixels[i * 3 + 2];
		outPixels[i] = 0.299f * r + 0.587f * g + 0.114f * b;
	}
}

void convertRgb2Gray(uint8_t * inPixels, int width, int height,
		uint8_t * outPixels, 
		bool useDevice=false, dim3 blockSize=dim3(1))
{
	GpuTimer timer;
	timer.Start();
	if (useDevice == false)
	{
		for (int r = 0; r < height; r++)
		{
			for (int c = 0; c < width; c++)
			{
				int i = r * width + c;
				uint8_t r = inPixels[i * 3];
				uint8_t g = inPixels[i * 3 + 1];
				uint8_t b = inPixels[i * 3 + 2];
				outPixels[i] = 0.299f * r + 0.587f * g + 0.114f * b;
			}
		}
	}
	else // use device
	{
		int numChannels = 3;
		hipDeviceProp_t devProp;
		hipGetDeviceProperties(&devProp, 0);
		printf("GPU name: %s\n", devProp.name);
		printf("GPU compute capability: %d.%d\n", devProp.major, devProp.minor);

		// TODO: Allocate device memories
		uint8_t *d_inPixels, * d_outPixels;
		CHECK(hipMalloc(&d_inPixels, height * width * numChannels * sizeof(uint8_t)));
		CHECK(hipMalloc(&d_outPixels, height * width * sizeof(uint8_t)));

		// TODO: Copy data to device memories
		CHECK(hipMemcpy(d_inPixels, inPixels, height * width * numChannels * sizeof(uint8_t), hipMemcpyHostToDevice));

		// TODO: Set grid size and call kernel (remember to check kernel error)
		dim3 gridSize((width - 1)/blockSize.x + 1, (height - 1)/blockSize.y + 1);
		convertRgb2GrayKernel<<<gridSize, blockSize>>>(d_inPixels, width, height, d_outPixels);
		hipError_t errSync  = hipGetLastError();
		hipError_t errAsync = hipDeviceSynchronize();
		if (errSync != hipSuccess)
			printf("Sync kernel error: %s\n", hipGetErrorString(errSync));
		if (errAsync != hipSuccess)
			printf("Async kernel error: %s\n", hipGetErrorString(errAsync));
		// TODO: Copy result from device memories
		CHECK(hipMemcpy(outPixels, d_outPixels, height * width * sizeof(uint8_t), hipMemcpyDeviceToHost));

		// TODO: Free device memories
		CHECK(hipFree(d_inPixels));
		CHECK(hipFree(d_outPixels));
	}
	timer.Stop();
	float time = timer.Elapsed();
	printf("Processing time (%s): %f ms\n\n", 
			useDevice == true? "use device" : "use host", time);
}

float computeError(uint8_t * a1, uint8_t * a2, int n)
{
	float err = 0;
	for (int i = 0; i < n; i++)
		err += abs((int)a1[i] - (int)a2[i]);
	err /= n;
	return err;
}

char * concatStr(const char * s1, const char * s2)
{
	char * result = (char *)malloc(strlen(s1) + strlen(s2) + 1);
	strcpy(result, s1);
	strcat(result, s2);
	return result;
}

int main(int argc, char ** argv)
{	
	if (argc != 3 && argc != 5)
	{
		printf("The number of arguments is invalid\n");
		return EXIT_FAILURE;
	}

	// Read input RGB image file
	int numChannels, width, height;
	uint8_t * inPixels;
	readPnm(argv[1], numChannels, width, height, inPixels);
	if (numChannels != 3)
		return EXIT_FAILURE; // Input image must be RGB
	printf("Image size (width x height): %i x %i\n\n", width, height);

	// Convert RGB to grayscale not using device
	uint8_t * correctOutPixels= (uint8_t *)malloc(width * height);
	convertRgb2Gray(inPixels, width, height, correctOutPixels);

	// Convert RGB to grayscale using device
	uint8_t * outPixels= (uint8_t *)malloc(width * height);
	dim3 blockSize(32, 32); // Default
	if (argc == 5)
	{
		blockSize.x = atoi(argv[3]);
		blockSize.y = atoi(argv[4]);
	} 
	convertRgb2Gray(inPixels, width, height, outPixels, true, blockSize); 

	// Compute mean absolute error between host result and device result
	float err = computeError(outPixels, correctOutPixels, width * height);
	printf("Error between device result and host result: %f\n", err);

	// Write results to files
	char * outFileNameBase = strtok(argv[2], "."); // Get rid of extension
	writePnm(correctOutPixels, 1, width, height, concatStr(outFileNameBase, "_host.pnm"));
	writePnm(outPixels, 1, width, height, concatStr(outFileNameBase, "_device.pnm"));

	// Free memories
	free(inPixels);
	free(outPixels);
}

	.text
	.file	"bt01_p1.hip"
	.globl	_Z7readPnmPcRiS0_S0_RPh         # -- Begin function _Z7readPnmPcRiS0_S0_RPh
	.type	_Z7readPnmPcRiS0_S0_RPh,@function
_Z7readPnmPcRiS0_S0_RPh:                # @_Z7readPnmPcRiS0_S0_RPh
	.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	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r8, %rbx
	movq	%rcx, %r14
	movq	%rdx, %r15
	movq	%rsi, %r12
	movl	$.L.str, %esi
	movq	%rdi, 8(%rsp)                   # 8-byte Spill
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_10
# %bb.1:
	movq	%rax, %r13
	leaq	5(%rsp), %rbp
	movl	$.L.str.2, %esi
	movq	%rax, %rdi
	movq	%rbp, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movzwl	(%rbp), %eax
	xorl	$12880, %eax                    # imm = 0x3250
	movzbl	2(%rbp), %ecx
	orw	%ax, %cx
	je	.LBB0_2
# %bb.8:
	movzwl	5(%rsp), %ecx
	xorl	$13136, %ecx                    # imm = 0x3350
	movzbl	7(%rsp), %edx
	movl	$3, %eax
	orw	%cx, %dx
	je	.LBB0_3
	jmp	.LBB0_9
.LBB0_2:
	movl	$1, %eax
.LBB0_3:
	movl	%eax, (%r12)
	movl	$.L.str.5, %esi
	movq	%r13, %rdi
	movq	%r15, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	movl	$.L.str.5, %esi
	movq	%r13, %rdi
	movq	%r14, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	leaq	20(%rsp), %rbp
	movl	$.L.str.5, %esi
	movq	%r13, %rdi
	movq	%rbp, %rdx
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	cmpl	$256, (%rbp)                    # imm = 0x100
	jge	.LBB0_9
# %bb.4:
	movslq	(%r15), %rax
	movslq	(%r14), %rcx
	imulq	%rax, %rcx
	movslq	(%r12), %rbp
	imulq	%rcx, %rbp
	movq	%rbp, %rdi
	callq	malloc
	movq	%rax, (%rbx)
	testl	%ebp, %ebp
	jle	.LBB0_7
# %bb.5:                                # %.lr.ph.preheader
	xorl	%ebp, %ebp
.LBB0_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	(%rbx), %rdx
	addq	%rbp, %rdx
	movl	$.L.str.6, %esi
	movq	%r13, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	incq	%rbp
	movslq	(%r15), %rax
	movslq	(%r14), %rcx
	imulq	%rax, %rcx
	movslq	(%r12), %rax
	imulq	%rcx, %rax
	cmpq	%rax, %rbp
	jl	.LBB0_6
.LBB0_7:                                # %._crit_edge
	movq	%r13, %rdi
	callq	fclose
	addq	$24, %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
.LBB0_9:
	.cfi_def_cfa_offset 80
	movq	%r13, %rdi
	callq	fclose
.LBB0_10:
	movl	$.L.str.1, %edi
	movq	8(%rsp), %rsi                   # 8-byte Reload
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z7readPnmPcRiS0_S0_RPh, .Lfunc_end0-_Z7readPnmPcRiS0_S0_RPh
	.cfi_endproc
                                        # -- End function
	.globl	_Z8writePnmPhiiiPc              # -- Begin function _Z8writePnmPhiiiPc
	.type	_Z8writePnmPhiiiPc,@function
_Z8writePnmPhiiiPc:                     # @_Z8writePnmPhiiiPc
	.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
	pushq	%rax
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%r8, %r13
	movl	%ecx, %r15d
	movl	%edx, %r12d
	movl	%esi, %ebp
	movq	%rdi, %rbx
	movl	$.L.str.7, %esi
	movq	%r8, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB1_9
# %bb.1:
	movq	%rax, %r14
	movl	$.L.str.9, %edi
	cmpl	$1, %ebp
	je	.LBB1_4
# %bb.2:
	cmpl	$3, %ebp
	jne	.LBB1_8
# %bb.3:
	movl	$.L.str.10, %edi
.LBB1_4:
	movl	$3, %esi
	movl	$1, %edx
	movq	%r14, %rcx
	callq	fwrite@PLT
	movl	$.L.str.11, %esi
	movq	%r14, %rdi
	movl	%r12d, %edx
	movl	%r15d, %ecx
	xorl	%eax, %eax
	callq	fprintf
	imull	%r12d, %ebp
	imull	%r15d, %ebp
	testl	%ebp, %ebp
	jle	.LBB1_7
# %bb.5:                                # %.lr.ph.preheader
	movl	%ebp, %r15d
	xorl	%r12d, %r12d
.LBB1_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%rbx,%r12), %edx
	movl	$.L.str.12, %esi
	movq	%r14, %rdi
	xorl	%eax, %eax
	callq	fprintf
	incq	%r12
	cmpq	%r12, %r15
	jne	.LBB1_6
.LBB1_7:                                # %._crit_edge
	movq	%r14, %rdi
	addq	$8, %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
.LBB1_8:
	.cfi_def_cfa_offset 64
	movq	%r14, %rdi
	callq	fclose
.LBB1_9:
	movl	$.L.str.8, %edi
	movq	%r13, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z8writePnmPhiiiPc, .Lfunc_end1-_Z8writePnmPhiiiPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z36__device_stub__convertRgb2GrayKernelPhiiS_ # -- Begin function _Z36__device_stub__convertRgb2GrayKernelPhiiS_
	.type	_Z36__device_stub__convertRgb2GrayKernelPhiiS_,@function
_Z36__device_stub__convertRgb2GrayKernelPhiiS_: # @_Z36__device_stub__convertRgb2GrayKernelPhiiS_
	.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	12(%rsp), %rdi
	movl	%esi, (%rdi)
	leaq	8(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%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	$_Z21convertRgb2GrayKernelPhiiS_, %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	_Z36__device_stub__convertRgb2GrayKernelPhiiS_, .Lfunc_end2-_Z36__device_stub__convertRgb2GrayKernelPhiiS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function _Z15convertRgb2GrayPhiiS_b4dim3
.LCPI3_0:
	.long	0x3e991687                      # float 0.298999995
.LCPI3_1:
	.long	0x3f1645a2                      # float 0.587000012
.LCPI3_2:
	.long	0x3de978d5                      # float 0.114
	.text
	.globl	_Z15convertRgb2GrayPhiiS_b4dim3
	.type	_Z15convertRgb2GrayPhiiS_b4dim3,@function
_Z15convertRgb2GrayPhiiS_b4dim3:        # @_Z15convertRgb2GrayPhiiS_b4dim3
.Lfunc_begin0:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception0
# %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	$1528, %rsp                     # imm = 0x5F8
	.cfi_def_cfa_offset 1584
	.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	%r8d, %ebp
	movq	%rcx, %r14
	movl	%edx, %r13d
	movl	%esi, %r15d
	movq	%rdi, %r12
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	movq	-8(%rbx), %rdi
.Ltmp0:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp1:
# %bb.1:                                # %.noexc
	movq	8(%rsp), %rdi
.Ltmp2:
	callq	hipEventSynchronize
.Ltmp3:
# %bb.2:                                # %_ZN8GpuTimer5StartEv.exit
	testb	%bpl, %bpl
	je	.LBB3_3
# %bb.14:
.Ltmp4:
	movl	%ebp, 44(%rsp)                  # 4-byte Spill
	leaq	56(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
.Ltmp5:
# %bb.15:
	leaq	56(%rsp), %rbx
	movl	$.L.str.13, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	360(%rbx), %esi
	movl	364(%rbx), %edx
	movl	$.L.str.14, %edi
	xorl	%eax, %eax
	callq	printf
	movq	%r13, 48(%rsp)                  # 8-byte Spill
	movl	%r13d, %ebx
	imull	%r15d, %ebx
	leal	(%rbx,%rbx,2), %eax
	movslq	%eax, %r13
.Ltmp7:
	leaq	32(%rsp), %rdi
	movq	%r13, %rsi
	callq	hipMalloc
.Ltmp8:
# %bb.16:                               # %_ZL9hipMallocIhE10hipError_tPPT_m.exit
	movl	%eax, %ebp
	testl	%eax, %eax
	jne	.LBB3_17
# %bb.22:
	movslq	%ebx, %rbp
.Ltmp12:
	leaq	24(%rsp), %rdi
	movq	%rbp, %rsi
	callq	hipMalloc
.Ltmp13:
# %bb.23:                               # %_ZL9hipMallocIhE10hipError_tPPT_m.exit104
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.LBB3_24
# %bb.27:
	movq	32(%rsp), %rdi
.Ltmp17:
	movq	%r12, %rsi
	movq	%r13, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
.Ltmp18:
# %bb.28:
	movl	%eax, %ebx
	testl	%eax, %eax
	movq	48(%rsp), %r12                  # 8-byte Reload
	jne	.LBB3_29
# %bb.31:
	leaq	1584(%rsp), %rcx
	leal	-1(%r15), %eax
	xorl	%edx, %edx
	divl	(%rcx)
                                        # kill: def $eax killed $eax def $rax
	leal	1(%rax), %edi
	leal	-1(%r12), %eax
	xorl	%edx, %edx
	divl	4(%rcx)
                                        # kill: def $eax killed $eax def $rax
	incl	%eax
	shlq	$32, %rax
	orq	%rax, %rdi
	movq	(%rcx), %rdx
	movl	8(%rcx), %ecx
.Ltmp22:
	movl	$1, %esi
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
.Ltmp23:
# %bb.32:
	testl	%eax, %eax
	jne	.LBB3_34
# %bb.33:
	movq	32(%rsp), %rdi
	movq	24(%rsp), %rcx
.Ltmp24:
	movl	%r15d, %esi
	movl	%r12d, %edx
	callq	_Z36__device_stub__convertRgb2GrayKernelPhiiS_
.Ltmp25:
.LBB3_34:
.Ltmp27:
	callq	hipGetLastError
.Ltmp28:
# %bb.35:
.Ltmp30:
	movl	%eax, %r15d
	callq	hipDeviceSynchronize
.Ltmp31:
# %bb.36:
	movl	%eax, %ebx
	testl	%r15d, %r15d
	je	.LBB3_39
# %bb.37:
.Ltmp32:
	movl	%r15d, %edi
	callq	hipGetErrorString
.Ltmp33:
# %bb.38:
	movl	$.L.str.18, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB3_39:
	testl	%ebx, %ebx
	je	.LBB3_42
# %bb.40:
.Ltmp34:
	movl	%ebx, %edi
	callq	hipGetErrorString
.Ltmp35:
# %bb.41:
	movl	$.L.str.19, %edi
	movq	%rax, %rsi
	xorl	%eax, %eax
	callq	printf
.LBB3_42:
	movq	24(%rsp), %rsi
.Ltmp37:
	movq	%r14, %rdi
	movq	%rbp, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
.Ltmp38:
# %bb.43:
	movl	%eax, %ebx
	testl	%eax, %eax
	movl	44(%rsp), %ebp                  # 4-byte Reload
	jne	.LBB3_44
# %bb.48:
	movq	32(%rsp), %rdi
.Ltmp42:
	callq	hipFree
.Ltmp43:
# %bb.49:
	movl	%eax, %ebx
	testl	%eax, %eax
	jne	.LBB3_50
# %bb.52:
	movq	24(%rsp), %rdi
.Ltmp47:
	callq	hipFree
.Ltmp48:
# %bb.53:
	movl	%eax, %ebx
	testl	%eax, %eax
	je	.LBB3_9
# %bb.54:
	movq	stderr(%rip), %rdi
	movl	$.L.str.15, %esi
	movl	$.L.str.16, %edx
	movl	$192, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %r14
.Ltmp49:
	movl	%ebx, %edi
	callq	hipGetErrorString
.Ltmp50:
	jmp	.LBB3_25
.LBB3_3:                                # %.preheader108
	testl	%r13d, %r13d
	jle	.LBB3_9
# %bb.4:                                # %.preheader.lr.ph
	movl	%r13d, %eax
	movl	%r15d, %ecx
	xorl	%edx, %edx
	movss	.LCPI3_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	.LCPI3_1(%rip), %xmm1           # xmm1 = mem[0],zero,zero,zero
	movss	.LCPI3_2(%rip), %xmm2           # xmm2 = mem[0],zero,zero,zero
	xorl	%esi, %esi
.LBB3_5:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB3_7 Depth 2
	testl	%r15d, %r15d
	jle	.LBB3_8
# %bb.6:                                # %.lr.ph
                                        #   in Loop: Header=BB3_5 Depth=1
	movl	%edx, %r8d
	leaq	(%r14,%r8), %rdi
	leaq	(%r8,%r8,2), %r8
	xorl	%r9d, %r9d
.LBB3_7:                                #   Parent Loop BB3_5 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%r8d, %r10d
	movzbl	(%r12,%r10), %r11d
	movzbl	1(%r12,%r10), %ebx
	movzbl	2(%r12,%r10), %r10d
	xorps	%xmm3, %xmm3
	cvtsi2ss	%r11d, %xmm3
	mulss	%xmm0, %xmm3
	xorps	%xmm4, %xmm4
	cvtsi2ss	%ebx, %xmm4
	mulss	%xmm1, %xmm4
	addss	%xmm3, %xmm4
	xorps	%xmm3, %xmm3
	cvtsi2ss	%r10d, %xmm3
	mulss	%xmm2, %xmm3
	addss	%xmm4, %xmm3
	cvttss2si	%xmm3, %r10d
	movb	%r10b, (%rdi,%r9)
	incq	%r9
	addq	$3, %r8
	cmpq	%r9, %rcx
	jne	.LBB3_7
.LBB3_8:                                # %._crit_edge
                                        #   in Loop: Header=BB3_5 Depth=1
	incq	%rsi
	addl	%r15d, %edx
	cmpq	%rax, %rsi
	jne	.LBB3_5
.LBB3_9:                                # %.loopexit
	movq	16(%rsp), %rdi
.Ltmp52:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp53:
# %bb.10:                               # %_ZN8GpuTimer4StopEv.exit
	movq	16(%rsp), %rdi
.Ltmp55:
	callq	hipEventSynchronize
.Ltmp56:
# %bb.11:                               # %.noexc106
	movq	8(%rsp), %rsi
	movq	16(%rsp), %rdx
.Ltmp57:
	leaq	56(%rsp), %rdi
	callq	hipEventElapsedTime
.Ltmp58:
# %bb.12:
	xorps	%xmm0, %xmm0
	cvtss2sd	56(%rsp), %xmm0
	movl	$.L.str.21, %eax
	movl	$.L.str.22, %esi
	testb	%bpl, %bpl
	cmovneq	%rax, %rsi
	movl	$.L.str.20, %edi
	movb	$1, %al
	callq	printf
	leaq	8(%rsp), %rdi
	callq	_ZN8GpuTimerD2Ev
	addq	$1528, %rsp                     # imm = 0x5F8
	.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
.LBB3_17:
	.cfi_def_cfa_offset 1584
	movq	stderr(%rip), %rdi
	movl	$.L.str.15, %esi
	movl	$.L.str.16, %edx
	movl	$172, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
.Ltmp9:
	movl	%ebp, %edi
	callq	hipGetErrorString
.Ltmp10:
# %bb.18:
	movl	$.L.str.17, %esi
	movq	%rbx, %rdi
	movl	%ebp, %edx
	jmp	.LBB3_19
.LBB3_24:
	movq	stderr(%rip), %rdi
	movl	$.L.str.15, %esi
	movl	$.L.str.16, %edx
	movl	$173, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %r14
.Ltmp14:
	movl	%ebx, %edi
	callq	hipGetErrorString
.Ltmp15:
	jmp	.LBB3_25
.LBB3_29:
	movq	stderr(%rip), %rdi
	movl	$.L.str.15, %esi
	movl	$.L.str.16, %edx
	movl	$176, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %r14
.Ltmp19:
	movl	%ebx, %edi
	callq	hipGetErrorString
.Ltmp20:
	jmp	.LBB3_25
.LBB3_44:
	movq	stderr(%rip), %rdi
	movl	$.L.str.15, %esi
	movl	$.L.str.16, %edx
	movl	$188, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %r14
.Ltmp39:
	movl	%ebx, %edi
	callq	hipGetErrorString
.Ltmp40:
	jmp	.LBB3_25
.LBB3_50:
	movq	stderr(%rip), %rdi
	movl	$.L.str.15, %esi
	movl	$.L.str.16, %edx
	movl	$191, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %r14
.Ltmp44:
	movl	%ebx, %edi
	callq	hipGetErrorString
.Ltmp45:
.LBB3_25:
	movl	$.L.str.17, %esi
	movq	%r14, %rdi
	movl	%ebx, %edx
.LBB3_19:
	movq	%rax, %rcx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.LBB3_45:
.Ltmp29:
	jmp	.LBB3_57
.LBB3_20:
.Ltmp6:
	jmp	.LBB3_57
.LBB3_58:
.Ltmp26:
	jmp	.LBB3_57
.LBB3_46:
.Ltmp36:
	jmp	.LBB3_57
.LBB3_56:
.Ltmp59:
	jmp	.LBB3_57
.LBB3_13:
.Ltmp54:
	jmp	.LBB3_57
.LBB3_55:
.Ltmp51:
	jmp	.LBB3_57
.LBB3_51:
.Ltmp46:
	jmp	.LBB3_57
.LBB3_47:
.Ltmp41:
	jmp	.LBB3_57
.LBB3_30:
.Ltmp21:
	jmp	.LBB3_57
.LBB3_26:
.Ltmp16:
	jmp	.LBB3_57
.LBB3_21:
.Ltmp11:
.LBB3_57:
	movq	%rax, %rbx
	leaq	8(%rsp), %rdi
	callq	_ZN8GpuTimerD2Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end3:
	.size	_Z15convertRgb2GrayPhiiS_b4dim3, .Lfunc_end3-_Z15convertRgb2GrayPhiiS_b4dim3
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table3:
.Lexception0:
	.byte	255                             # @LPStart Encoding = omit
	.byte	255                             # @TType Encoding = omit
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end0-.Lcst_begin0
.Lcst_begin0:
	.uleb128 .Lfunc_begin0-.Lfunc_begin0    # >> Call Site 1 <<
	.uleb128 .Ltmp0-.Lfunc_begin0           #   Call between .Lfunc_begin0 and .Ltmp0
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp0-.Lfunc_begin0           # >> Call Site 2 <<
	.uleb128 .Ltmp3-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp3
	.uleb128 .Ltmp54-.Lfunc_begin0          #     jumps to .Ltmp54
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp4-.Lfunc_begin0           # >> Call Site 3 <<
	.uleb128 .Ltmp5-.Ltmp4                  #   Call between .Ltmp4 and .Ltmp5
	.uleb128 .Ltmp6-.Lfunc_begin0           #     jumps to .Ltmp6
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp7-.Lfunc_begin0           # >> Call Site 4 <<
	.uleb128 .Ltmp8-.Ltmp7                  #   Call between .Ltmp7 and .Ltmp8
	.uleb128 .Ltmp11-.Lfunc_begin0          #     jumps to .Ltmp11
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp12-.Lfunc_begin0          # >> Call Site 5 <<
	.uleb128 .Ltmp13-.Ltmp12                #   Call between .Ltmp12 and .Ltmp13
	.uleb128 .Ltmp16-.Lfunc_begin0          #     jumps to .Ltmp16
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp17-.Lfunc_begin0          # >> Call Site 6 <<
	.uleb128 .Ltmp18-.Ltmp17                #   Call between .Ltmp17 and .Ltmp18
	.uleb128 .Ltmp21-.Lfunc_begin0          #     jumps to .Ltmp21
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp22-.Lfunc_begin0          # >> Call Site 7 <<
	.uleb128 .Ltmp25-.Ltmp22                #   Call between .Ltmp22 and .Ltmp25
	.uleb128 .Ltmp26-.Lfunc_begin0          #     jumps to .Ltmp26
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp27-.Lfunc_begin0          # >> Call Site 8 <<
	.uleb128 .Ltmp28-.Ltmp27                #   Call between .Ltmp27 and .Ltmp28
	.uleb128 .Ltmp29-.Lfunc_begin0          #     jumps to .Ltmp29
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp30-.Lfunc_begin0          # >> Call Site 9 <<
	.uleb128 .Ltmp35-.Ltmp30                #   Call between .Ltmp30 and .Ltmp35
	.uleb128 .Ltmp36-.Lfunc_begin0          #     jumps to .Ltmp36
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp37-.Lfunc_begin0          # >> Call Site 10 <<
	.uleb128 .Ltmp38-.Ltmp37                #   Call between .Ltmp37 and .Ltmp38
	.uleb128 .Ltmp41-.Lfunc_begin0          #     jumps to .Ltmp41
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp42-.Lfunc_begin0          # >> Call Site 11 <<
	.uleb128 .Ltmp43-.Ltmp42                #   Call between .Ltmp42 and .Ltmp43
	.uleb128 .Ltmp46-.Lfunc_begin0          #     jumps to .Ltmp46
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp47-.Lfunc_begin0          # >> Call Site 12 <<
	.uleb128 .Ltmp50-.Ltmp47                #   Call between .Ltmp47 and .Ltmp50
	.uleb128 .Ltmp51-.Lfunc_begin0          #     jumps to .Ltmp51
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp52-.Lfunc_begin0          # >> Call Site 13 <<
	.uleb128 .Ltmp53-.Ltmp52                #   Call between .Ltmp52 and .Ltmp53
	.uleb128 .Ltmp54-.Lfunc_begin0          #     jumps to .Ltmp54
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp55-.Lfunc_begin0          # >> Call Site 14 <<
	.uleb128 .Ltmp58-.Ltmp55                #   Call between .Ltmp55 and .Ltmp58
	.uleb128 .Ltmp59-.Lfunc_begin0          #     jumps to .Ltmp59
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp9-.Lfunc_begin0           # >> Call Site 15 <<
	.uleb128 .Ltmp10-.Ltmp9                 #   Call between .Ltmp9 and .Ltmp10
	.uleb128 .Ltmp11-.Lfunc_begin0          #     jumps to .Ltmp11
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp14-.Lfunc_begin0          # >> Call Site 16 <<
	.uleb128 .Ltmp15-.Ltmp14                #   Call between .Ltmp14 and .Ltmp15
	.uleb128 .Ltmp16-.Lfunc_begin0          #     jumps to .Ltmp16
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp19-.Lfunc_begin0          # >> Call Site 17 <<
	.uleb128 .Ltmp20-.Ltmp19                #   Call between .Ltmp19 and .Ltmp20
	.uleb128 .Ltmp21-.Lfunc_begin0          #     jumps to .Ltmp21
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp39-.Lfunc_begin0          # >> Call Site 18 <<
	.uleb128 .Ltmp40-.Ltmp39                #   Call between .Ltmp39 and .Ltmp40
	.uleb128 .Ltmp41-.Lfunc_begin0          #     jumps to .Ltmp41
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp44-.Lfunc_begin0          # >> Call Site 19 <<
	.uleb128 .Ltmp45-.Ltmp44                #   Call between .Ltmp44 and .Ltmp45
	.uleb128 .Ltmp46-.Lfunc_begin0          #     jumps to .Ltmp46
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp45-.Lfunc_begin0          # >> Call Site 20 <<
	.uleb128 .Lfunc_end3-.Ltmp45            #   Call between .Ltmp45 and .Lfunc_end3
	.byte	0                               #     has no landing pad
	.byte	0                               #   On action: cleanup
.Lcst_end0:
	.p2align	2, 0x0
                                        # -- End function
	.section	.text._ZN8GpuTimerD2Ev,"axG",@progbits,_ZN8GpuTimerD2Ev,comdat
	.weak	_ZN8GpuTimerD2Ev                # -- Begin function _ZN8GpuTimerD2Ev
	.p2align	1, 0x90
	.type	_ZN8GpuTimerD2Ev,@function
_ZN8GpuTimerD2Ev:                       # @_ZN8GpuTimerD2Ev
.Lfunc_begin1:
	.cfi_startproc
	.cfi_personality 3, __gxx_personality_v0
	.cfi_lsda 3, .Lexception1
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
.Ltmp60:
	callq	hipEventDestroy
.Ltmp61:
# %bb.1:
	movq	8(%rbx), %rdi
.Ltmp62:
	callq	hipEventDestroy
.Ltmp63:
# %bb.2:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB4_3:
	.cfi_def_cfa_offset 16
.Ltmp64:
	movq	%rax, %rdi
	callq	__clang_call_terminate
.Lfunc_end4:
	.size	_ZN8GpuTimerD2Ev, .Lfunc_end4-_ZN8GpuTimerD2Ev
	.cfi_endproc
	.section	.gcc_except_table._ZN8GpuTimerD2Ev,"aG",@progbits,_ZN8GpuTimerD2Ev,comdat
	.p2align	2, 0x0
GCC_except_table4:
.Lexception1:
	.byte	255                             # @LPStart Encoding = omit
	.byte	3                               # @TType Encoding = udata4
	.uleb128 .Lttbase0-.Lttbaseref0
.Lttbaseref0:
	.byte	1                               # Call site Encoding = uleb128
	.uleb128 .Lcst_end1-.Lcst_begin1
.Lcst_begin1:
	.uleb128 .Ltmp60-.Lfunc_begin1          # >> Call Site 1 <<
	.uleb128 .Ltmp63-.Ltmp60                #   Call between .Ltmp60 and .Ltmp63
	.uleb128 .Ltmp64-.Lfunc_begin1          #     jumps to .Ltmp64
	.byte	1                               #   On action: 1
.Lcst_end1:
	.byte	1                               # >> Action Record 1 <<
                                        #   Catch TypeInfo 1
	.byte	0                               #   No further actions
	.p2align	2, 0x0
                                        # >> Catch TypeInfos <<
	.long	0                               # TypeInfo 1
.Lttbase0:
	.p2align	2, 0x0
                                        # -- End function
	.text
	.globl	_Z12computeErrorPhS_i           # -- Begin function _Z12computeErrorPhS_i
	.type	_Z12computeErrorPhS_i,@function
_Z12computeErrorPhS_i:                  # @_Z12computeErrorPhS_i
	.cfi_startproc
# %bb.0:
	testl	%edx, %edx
	jle	.LBB5_1
# %bb.3:                                # %.lr.ph.preheader
	movl	%edx, %eax
	xorps	%xmm0, %xmm0
	xorl	%ecx, %ecx
.LBB5_4:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%rdi,%rcx), %r8d
	movzbl	(%rsi,%rcx), %r9d
	subl	%r9d, %r8d
	movl	%r8d, %r9d
	negl	%r9d
	cmovsl	%r8d, %r9d
	movzbl	%r9b, %r8d
	xorps	%xmm1, %xmm1
	cvtsi2ss	%r8d, %xmm1
	addss	%xmm1, %xmm0
	incq	%rcx
	cmpq	%rcx, %rax
	jne	.LBB5_4
	jmp	.LBB5_2
.LBB5_1:
	xorps	%xmm0, %xmm0
.LBB5_2:                                # %._crit_edge
	xorps	%xmm1, %xmm1
	cvtsi2ss	%edx, %xmm1
	divss	%xmm1, %xmm0
	retq
.Lfunc_end5:
	.size	_Z12computeErrorPhS_i, .Lfunc_end5-_Z12computeErrorPhS_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z9concatStrPKcS0_              # -- Begin function _Z9concatStrPKcS0_
	.type	_Z9concatStrPKcS0_,@function
_Z9concatStrPKcS0_:                     # @_Z9concatStrPKcS0_
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	movq	%rdi, %r14
	callq	strlen
	movq	%rax, %r15
	movq	%rbx, %rdi
	callq	strlen
	leaq	(%r15,%rax), %rdi
	incq	%rdi
	callq	malloc
	movq	%rax, %r15
	movq	%rax, %rdi
	movq	%r14, %rsi
	callq	strcpy
	movq	%r15, %rdi
	movq	%rbx, %rsi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	strcat                          # TAILCALL
.Lfunc_end6:
	.size	_Z9concatStrPKcS0_, .Lfunc_end6-_Z9concatStrPKcS0_
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$72, %rsp
	.cfi_def_cfa_offset 128
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rsi, %r14
	movl	%edi, %ebp
	cmpl	$3, %edi
	je	.LBB7_3
# %bb.1:
	cmpl	$5, %ebp
	jne	.LBB7_2
.LBB7_3:
	movq	8(%r14), %rdi
	leaq	68(%rsp), %rbx
	leaq	4(%rsp), %rdx
	movq	%rsp, %rcx
	leaq	56(%rsp), %r8
	movq	%rbx, %rsi
	callq	_Z7readPnmPcRiS0_S0_RPh
	movl	$1, %eax
	cmpl	$3, (%rbx)
	jne	.LBB7_11
# %bb.4:
	movq	%r14, 8(%rsp)                   # 8-byte Spill
	movl	4(%rsp), %esi
	movl	(%rsp), %edx
	movl	$.L.str.24, %edi
	xorl	%eax, %eax
	callq	printf
	movl	4(%rsp), %ebx
	movl	(%rsp), %r14d
	movl	%r14d, %eax
	imull	%ebx, %eax
	movslq	%eax, %rdi
	callq	malloc
	movq	%rax, %r12
	movq	56(%rsp), %rdi
	movabsq	$4294967297, %rax               # imm = 0x100000001
	movq	%rax, 40(%rsp)
	movl	$1, %r9d
	movl	%r9d, 48(%rsp)
	movq	40(%rsp), %rax
	movq	%rdi, 16(%rsp)                  # 8-byte Spill
	movl	%ebx, %esi
	movl	%r14d, %edx
	movq	%r12, %rcx
	xorl	%r8d, %r8d
	pushq	%r9
	.cfi_adjust_cfa_offset 8
	pushq	%rax
	.cfi_adjust_cfa_offset 8
	callq	_Z15convertRgb2GrayPhiiS_b4dim3
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
	movl	4(%rsp), %r13d
	movl	(%rsp), %r14d
	movl	%r14d, %eax
	imull	%r13d, %eax
	movslq	%eax, %rdi
	callq	malloc
	movq	%rax, %r15
	movl	$32, %eax
	movl	$32, %ebx
	cmpl	$5, %ebp
	jne	.LBB7_6
# %bb.5:
	movq	8(%rsp), %rbp                   # 8-byte Reload
	movq	24(%rbp), %rdi
	callq	atoi
	movl	%eax, %ebx
	movq	32(%rbp), %rdi
	callq	atoi
.LBB7_6:
	movl	%ebx, 24(%rsp)
	movl	%eax, 28(%rsp)
	movl	$1, %r9d
	movl	%r9d, 32(%rsp)
	movq	24(%rsp), %rax
	movq	16(%rsp), %rdi                  # 8-byte Reload
	movl	%r13d, %esi
	movl	%r14d, %edx
	movq	%r15, %rcx
	movl	$1, %r8d
	pushq	%r9
	.cfi_adjust_cfa_offset 8
	pushq	%rax
	.cfi_adjust_cfa_offset 8
	callq	_Z15convertRgb2GrayPhiiS_b4dim3
	addq	$16, %rsp
	.cfi_adjust_cfa_offset -16
	movl	(%rsp), %eax
	imull	4(%rsp), %eax
	testl	%eax, %eax
	jle	.LBB7_7
# %bb.8:                                # %.lr.ph.preheader.i
	movl	%eax, %ecx
	xorps	%xmm0, %xmm0
	xorl	%edx, %edx
	movq	8(%rsp), %rbx                   # 8-byte Reload
.LBB7_9:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movzbl	(%r15,%rdx), %esi
	movzbl	(%r12,%rdx), %edi
	subl	%edi, %esi
	movl	%esi, %edi
	negl	%edi
	cmovsl	%esi, %edi
	movzbl	%dil, %esi
	xorps	%xmm1, %xmm1
	cvtsi2ss	%esi, %xmm1
	addss	%xmm1, %xmm0
	incq	%rdx
	cmpq	%rdx, %rcx
	jne	.LBB7_9
	jmp	.LBB7_10
.LBB7_2:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$1, %eax
	jmp	.LBB7_11
.LBB7_7:
	xorps	%xmm0, %xmm0
	movq	8(%rsp), %rbx                   # 8-byte Reload
.LBB7_10:                               # %_Z12computeErrorPhS_i.exit
	xorps	%xmm1, %xmm1
	cvtsi2ss	%eax, %xmm1
	divss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.25, %edi
	movb	$1, %al
	callq	printf
	movq	16(%rbx), %rdi
	movl	$.L.str.26, %esi
	callq	strtok
	movq	%rax, %rbx
	movl	4(%rsp), %ebp
	movl	(%rsp), %r14d
	movl	$.L.str.27, %esi
	movq	%rax, %rdi
	callq	_Z9concatStrPKcS0_
	movq	%r12, %rdi
	movl	$1, %esi
	movl	%ebp, %edx
	movl	%r14d, %ecx
	movq	%rax, %r8
	callq	_Z8writePnmPhiiiPc
	movl	4(%rsp), %ebp
	movl	(%rsp), %r14d
	movl	$.L.str.28, %esi
	movq	%rbx, %rdi
	callq	_Z9concatStrPKcS0_
	movq	%r15, %rdi
	movl	$1, %esi
	movl	%ebp, %edx
	movl	%r14d, %ecx
	movq	%rax, %r8
	callq	_Z8writePnmPhiiiPc
	movq	16(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%r15, %rdi
	callq	free
	xorl	%eax, %eax
.LBB7_11:
	addq	$72, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end7:
	.size	main, .Lfunc_end7-main
	.cfi_endproc
                                        # -- End function
	.section	.text.__clang_call_terminate,"axG",@progbits,__clang_call_terminate,comdat
	.hidden	__clang_call_terminate          # -- Begin function __clang_call_terminate
	.weak	__clang_call_terminate
	.type	__clang_call_terminate,@function
__clang_call_terminate:                 # @__clang_call_terminate
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__cxa_begin_catch
	callq	_ZSt9terminatev
.Lfunc_end8:
	.size	__clang_call_terminate, .Lfunc_end8-__clang_call_terminate
	.cfi_endproc
                                        # -- End function
	.text
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB9_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB9_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z21convertRgb2GrayKernelPhiiS_, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end9:
	.size	__hip_module_ctor, .Lfunc_end9-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB10_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB10_2:
	retq
.Lfunc_end10:
	.size	__hip_module_dtor, .Lfunc_end10-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"r"
	.size	.L.str, 2

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Cannot read %s\n"
	.size	.L.str.1, 16

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

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

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

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

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"%hhu"
	.size	.L.str.6, 5

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

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Cannot write %s\n"
	.size	.L.str.8, 17

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"P2\n"
	.size	.L.str.9, 4

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"P3\n"
	.size	.L.str.10, 4

	.type	.L.str.11,@object               # @.str.11
.L.str.11:
	.asciz	"%i\n%i\n255\n"
	.size	.L.str.11, 11

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

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

	.type	.L.str.13,@object               # @.str.13
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.13:
	.asciz	"GPU name: %s\n"
	.size	.L.str.13, 14

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"GPU compute capability: %d.%d\n"
	.size	.L.str.14, 31

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"Error: %s:%d, "
	.size	.L.str.15, 15

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/hmhuan/PG_GPU/master/assignment/BT01/1612858/bt01_p1.hip"
	.size	.L.str.16, 119

	.type	.L.str.17,@object               # @.str.17
.L.str.17:
	.asciz	"code: %d, reason: %s\n"
	.size	.L.str.17, 22

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"Sync kernel error: %s\n"
	.size	.L.str.18, 23

	.type	.L.str.19,@object               # @.str.19
.L.str.19:
	.asciz	"Async kernel error: %s\n"
	.size	.L.str.19, 24

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"Processing time (%s): %f ms\n\n"
	.size	.L.str.20, 30

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"use device"
	.size	.L.str.21, 11

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"use host"
	.size	.L.str.22, 9

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"Image size (width x height): %i x %i\n\n"
	.size	.L.str.24, 39

	.type	.L.str.25,@object               # @.str.25
.L.str.25:
	.asciz	"Error between device result and host result: %f\n"
	.size	.L.str.25, 49

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

	.type	.L.str.27,@object               # @.str.27
.L.str.27:
	.asciz	"_host.pnm"
	.size	.L.str.27, 10

	.type	.L.str.28,@object               # @.str.28
.L.str.28:
	.asciz	"_device.pnm"
	.size	.L.str.28, 12

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z21convertRgb2GrayKernelPhiiS_"
	.size	.L__unnamed_1, 32

	.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	"The number of arguments is invalid"
	.size	.Lstr, 35

	.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 _Z36__device_stub__convertRgb2GrayKernelPhiiS_
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _Z21convertRgb2GrayKernelPhiiS_
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z21convertRgb2GrayKernelPhiiS_ ; -- Begin function _Z21convertRgb2GrayKernelPhiiS_
	.globl	_Z21convertRgb2GrayKernelPhiiS_
	.p2align	8
	.type	_Z21convertRgb2GrayKernelPhiiS_,@function
_Z21convertRgb2GrayKernelPhiiS_:        ; @_Z21convertRgb2GrayKernelPhiiS_
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b64 s[4:5], s[0:1], 0x8
	v_and_b32_e32 v2, 0x3ff, v0
	v_bfe_u32 v3, v0, 10, 10
	s_waitcnt lgkmcnt(0)
	s_lshr_b32 s3, s2, 16
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[0:1], null, s14, s2, v[2:3]
	v_mad_u64_u32 v[1:2], null, s15, s3, v[3:4]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_cmp_gt_i32_e32 vcc_lo, s4, v0
	v_cmp_gt_i32_e64 s2, s5, v1
	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_2
; %bb.1:
	s_load_b64 s[2:3], s[0:1], 0x0
	v_mad_u64_u32 v[2:3], null, v1, s4, v[0:1]
	s_load_b64 s[0:1], s[0:1], 0x10
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshl_add_u32 v0, v2, 1, v2
	v_ashrrev_i32_e32 v1, 31, v0
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v0, vcc_lo, s2, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	s_clause 0x1
	global_load_u16 v3, v[0:1], off
	global_load_u8 v0, v[0:1], off offset:2
	s_waitcnt vmcnt(1)
	v_cvt_f32_ubyte1_e32 v1, v3
	v_cvt_f32_ubyte0_e32 v3, v3
	s_waitcnt vmcnt(0)
	v_cvt_f32_ubyte0_e32 v0, v0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v1, 0x3f1645a2, v1
	v_fmamk_f32 v1, v3, 0x3e991687, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fmamk_f32 v0, v0, 0x3de978d5, v1
	v_ashrrev_i32_e32 v1, 31, v2
	v_cvt_i32_f32_e32 v3, v0
	v_add_co_u32 v0, vcc_lo, s0, v2
	s_delay_alu instid0(VALU_DEP_3)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	global_store_b8 v[0:1], v3, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z21convertRgb2GrayKernelPhiiS_
		.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 5
		.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	_Z21convertRgb2GrayKernelPhiiS_, .Lfunc_end0-_Z21convertRgb2GrayKernelPhiiS_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 280
; NumSgprs: 18
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 5
; 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
      - .offset:         8
        .size:           4
        .value_kind:     by_value
      - .offset:         12
        .size:           4
        .value_kind:     by_value
      - .address_space:  global
        .offset:         16
        .size:           8
        .value_kind:     global_buffer
      - .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:           _Z21convertRgb2GrayKernelPhiiS_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z21convertRgb2GrayKernelPhiiS_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     5
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
