#include <stdio.h>
#include <stdint.h>
#include <cuda.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);
    }

    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 
	{
		fclose(f);
		printf("Cannot read %s\n", fileName); // In this exercise, we don't touch other types
		exit(EXIT_FAILURE); 
	}

	fscanf(f, "%i", &width);
	fscanf(f, "%i", &height);
	
	uint8_t max_val;
	fscanf(f, "%hhu", &max_val);
	if (max_val > 255)
	{
		fclose(f);
		printf("Cannot read %s\n", fileName); // In this exercise, we assume 1 byte per value
		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(char * fileName, int numChannels, int width, int height, uint8_t * pixels)
{
	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);
}

void compare2Pnms(char * fileName1, char * fileName2)
{
	int numChannels1, width1, height1;
	uint8_t * pixels1;
	readPnm(fileName1, numChannels1, width1, height1, pixels1);

	int numChannels2, width2, height2;
	uint8_t * pixels2;
	readPnm(fileName2, numChannels2, width2, height2, pixels2);

	if (numChannels1 != numChannels2)
	{
		printf("'%s' is DIFFERENT from '%s' (num channels: %i vs %i)\n", fileName1, fileName2, numChannels1, numChannels2);
		return;
	}
	if (width1 != width2)
	{
		printf("'%s' is DIFFERENT from '%s' (width: %i vs %i)\n", fileName1, fileName2, width1, width2);
		return;
	}
	if (height1 != height2)
	{
		printf("'%s' is DIFFERENT from '%s' (width: %i vs %i)\n", fileName1, fileName2, height1, height2);
		return;
	}
	float mae = 0;
	for (int i = 0; i < width1 * height1 * numChannels1; i++)
	{
		mae += abs((int)pixels1[i]-(int)pixels2[i]);
	}
	mae /= (width1 * height1 * numChannels1);
	printf("The average pixel difference between '%s' and '%s': %f\n", fileName1, fileName2, mae);
}

void convertRgb2GrayByHost(uint8_t * inPixels, uint8_t * outPixels, int width, int height)
{
	// TODO
	int size = width * height;

	for(int i = 0; i < size; i++){
		outPixels[i] = 0.299 * inPixels[i*3] +
						 0.114 * inPixels[i*3 + 2] + 
						 0.587 * inPixels[i*3 + 1];
	}
}

__global__ void convertRgb2GrayByDevice(uint8_t * inPixels, uint8_t * outPixels, int width, int height)
{
	// TODO
	int i_r = blockIdx.y * blockDim.y + threadIdx.y;
	int i_c = blockIdx.x * blockDim.x + threadIdx.x;

	if(i_c < width && i_r < height){
		outPixels[i_r * width + i_c] = 0.299 * inPixels[(i_r * width + i_c)*3] + 
										0.114 * inPixels[(i_r * width + i_c)*3 + 2] + 
										0.587 * inPixels[(i_r * width + i_c)*3 + 1];
	}
}


int main(int argc, char ** argv)
{
	// -----READ INPUT DATA-----
	if (argc < 5 || argc > 7)
	{
		printf("The number of arguments is invalid\n");
		return EXIT_FAILURE;
	}

	int numChannels, width, height;
	uint8_t * inPixels;
	readPnm(argv[1], numChannels, width, height, inPixels);
	printf("Image size (width x height): %i x %i\n", width, height);
	// -----PROCESS INPUT DATA-----
	uint8_t * outPixels= (uint8_t *)malloc(width * height);
	GpuTimer timer;
    timer.Start();
	if (strcmp(argv[4], "cpu") == 0){ // Use CPU
		convertRgb2GrayByHost(inPixels, outPixels, width, height);
	}
	else // Use GPU
	{
		// TODO: Query and print GPU name and compute capability
		cudaDeviceProp prop;
		CHECK(cudaGetDeviceProperties(&prop, 0));

		printf("GPU name: %s\n", prop.name);
		printf("GPU compute capability: %d\n", prop.major);

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

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

		// TODO: Set block size (already done for you) and grid size,
		//       and invoke kernel function with these settings (remember to check kernel error)
		dim3 blockSize(32, 32); // Default
		if (argc == 7)
		{
			blockSize.x = atoi(argv[5]);
			blockSize.y = atoi(argv[6]);
		}
		dim3 gridSize((width-1)/blockSize.x + 1, (height-1)/blockSize.y + 1);
		printf("Block size: %d x %d\n", blockSize.x, blockSize.y);
		printf("Grid size: %d x %d\n", gridSize.x, gridSize.y);

		convertRgb2GrayByDevice<<<gridSize, blockSize>>>(d_inPixels, d_outPixels, width, height);

		// TODO: Copy result from device memories
		CHECK(cudaMemcpy(outPixels, d_outPixels, width * height, cudaMemcpyDeviceToHost));

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

    // -----WRITE OUTPUT DATA TO FILE-----
	writePnm(argv[2], 1, width, height, outPixels);

	free(inPixels);
	free(outPixels);

	// -----CHECK CORRECTNESS-----
	compare2Pnms(argv[2], argv[3]);
}
	.file	"tmpxft_003a770c_00000000-6_bt1.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2043:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2043:
	.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	.L12
	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	.L8
	leaq	.LC4(%rip), %rsi
	movq	%r15, %rdi
	call	strcmp@PLT
	movl	%eax, %edx
	movl	$3, %eax
	testl	%edx, %edx
	je	.L4
	movq	%rbx, %rdi
	call	fclose@PLT
.L12:
	movq	8(%rsp), %rdx
	movl	$2, %edi
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L8:
	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	%r15, %rsi
	movq	%r12, %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	xorl	%r15d, %r15d
	call	__isoc23_fscanf@PLT
	leaq	20(%rsp), %rdx
	movq	%rbx, %rdi
	xorl	%eax, %eax
	leaq	.LC6(%rip), %rsi
	call	__isoc23_fscanf@PLT
	movl	0(%r13), %edi
	imull	(%r12), %edi
	imull	0(%rbp), %edi
	movslq	%edi, %rdi
	call	malloc@PLT
	movq	%rax, (%r14)
.L5:
	movl	0(%r13), %eax
	imull	(%r12), %eax
	imull	0(%rbp), %eax
	cmpl	%r15d, %eax
	jle	.L14
	movq	(%r14), %rdx
	leaq	.LC6(%rip), %rsi
	movq	%rbx, %rdi
	xorl	%eax, %eax
	addq	%r15, %rdx
	incq	%r15
	call	__isoc23_fscanf@PLT
	jmp	.L5
.L14:
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L7
	call	__stack_chk_fail@PLT
.L7:
	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	_Z8writePnmPciiiPh
	.type	_Z8writePnmPciiiPh, @function
_Z8writePnmPciiiPh:
.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	%rdi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%r8, %r13
	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	.L24
	movq	%rax, %rbx
	leaq	.LC9(%rip), %rdx
	cmpl	$1, %r12d
	je	.L23
	cmpl	$3, %r12d
	jne	.L19
	leaq	.LC10(%rip), %rdx
.L23:
	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	.L20
.L19:
	movq	%rax, %rdi
	call	fclose@PLT
.L24:
	movl	$2, %edi
	movq	%r14, %rdx
	leaq	.LC8(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L20:
	cmpl	%r12d, %ebp
	jle	.L25
	movzbl	0(%r13,%r12), %ecx
	movq	%r14, %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	movq	%rbx, %rdi
	incq	%r12
	call	__fprintf_chk@PLT
	jmp	.L20
.L25:
	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	_Z8writePnmPciiiPh, .-_Z8writePnmPciiiPh
	.section	.rodata.str1.1
.LC14:
	.string	"'%s' is DIFFERENT from '%s' (num channels: %i vs %i)\n"
.LC15:
	.string	"'%s' is DIFFERENT from '%s' (width: %i vs %i)\n"
.LC16:
	.string	"The average pixel difference between '%s' and '%s': %f\n"
	.text
	.globl	_Z12compare2PnmsPcS_
	.type	_Z12compare2PnmsPcS_, @function
_Z12compare2PnmsPcS_:
.LFB2038:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rdi, %rbx
	subq	$56, %rsp
	.cfi_def_cfa_offset 80
	leaq	8(%rsp), %rcx
	leaq	4(%rsp), %rdx
	movq	%rsp, %rsi
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %r8
	call	_Z7readPnmPcRiS0_S0_RPh
	leaq	32(%rsp), %r8
	leaq	20(%rsp), %rcx
	movq	%rbp, %rdi
	leaq	16(%rsp), %rdx
	leaq	12(%rsp), %rsi
	call	_Z7readPnmPcRiS0_S0_RPh
	movl	(%rsp), %r8d
	movl	12(%rsp), %r9d
	cmpl	%r9d, %r8d
	je	.L27
	movq	%rbp, %rcx
	movq	%rbx, %rdx
	leaq	.LC14(%rip), %rsi
	jmp	.L37
.L27:
	movl	4(%rsp), %eax
	movl	16(%rsp), %r9d
	cmpl	%r9d, %eax
	je	.L29
	movl	%eax, %r8d
	jmp	.L36
.L29:
	movl	8(%rsp), %edx
	movl	20(%rsp), %r9d
	cmpl	%r9d, %edx
	jne	.L30
	imull	%edx, %eax
	movq	24(%rsp), %rdi
	xorl	%edx, %edx
	xorps	%xmm0, %xmm0
	imull	%r8d, %eax
	movq	32(%rsp), %r8
	jmp	.L31
.L30:
	movl	%edx, %r8d
.L36:
	movq	%rbp, %rcx
	movq	%rbx, %rdx
	leaq	.LC15(%rip), %rsi
.L37:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L26
.L31:
	cmpl	%edx, %eax
	jle	.L38
	movzbl	(%r8,%rdx), %esi
	movzbl	(%rdi,%rdx), %ecx
	subl	%esi, %ecx
	movl	%ecx, %esi
	negl	%esi
	cmovns	%esi, %ecx
	incq	%rdx
	cvtsi2ssl	%ecx, %xmm1
	addss	%xmm1, %xmm0
	jmp	.L31
.L38:
	cvtsi2ssl	%eax, %xmm1
	movq	%rbp, %rcx
	movq	%rbx, %rdx
	movl	$2, %edi
	leaq	.LC16(%rip), %rsi
	movb	$1, %al
	divss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	call	__printf_chk@PLT
.L26:
	movq	40(%rsp), %rax
	subq	%fs:40, %rax
	je	.L34
	call	__stack_chk_fail@PLT
.L34:
	addq	$56, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2038:
	.size	_Z12compare2PnmsPcS_, .-_Z12compare2PnmsPcS_
	.globl	_Z21convertRgb2GrayByHostPhS_ii
	.type	_Z21convertRgb2GrayByHostPhS_ii, @function
_Z21convertRgb2GrayByHostPhS_ii:
.LFB2039:
	.cfi_startproc
	endbr64
	imull	%ecx, %edx
	movsd	.LC17(%rip), %xmm2
	xorl	%eax, %eax
	movsd	.LC18(%rip), %xmm3
	movsd	.LC19(%rip), %xmm4
.L40:
	cmpl	%eax, %edx
	jle	.L42
	movzbl	(%rdi), %ecx
	addq	$3, %rdi
	cvtsi2sdl	%ecx, %xmm0
	movzbl	-1(%rdi), %ecx
	cvtsi2sdl	%ecx, %xmm1
	movzbl	-2(%rdi), %ecx
	mulsd	%xmm2, %xmm0
	mulsd	%xmm3, %xmm1
	addsd	%xmm1, %xmm0
	cvtsi2sdl	%ecx, %xmm1
	mulsd	%xmm4, %xmm1
	addsd	%xmm1, %xmm0
	cvttsd2sil	%xmm0, %ecx
	movb	%cl, (%rsi,%rax)
	incq	%rax
	jmp	.L40
.L42:
	ret
	.cfi_endproc
.LFE2039:
	.size	_Z21convertRgb2GrayByHostPhS_ii, .-_Z21convertRgb2GrayByHostPhS_ii
	.globl	_Z47__device_stub__Z23convertRgb2GrayByDevicePhS_iiPhS_ii
	.type	_Z47__device_stub__Z23convertRgb2GrayByDevicePhS_iiPhS_ii, @function
_Z47__device_stub__Z23convertRgb2GrayByDevicePhS_iiPhS_ii:
.LFB2065:
	.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	.L43
	pushq	48(%rsp)
	.cfi_def_cfa_offset 168
	leaq	_Z23convertRgb2GrayByDevicePhS_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
.L43:
	movq	136(%rsp), %rax
	subq	%fs:40, %rax
	je	.L45
	call	__stack_chk_fail@PLT
.L45:
	addq	$152, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2065:
	.size	_Z47__device_stub__Z23convertRgb2GrayByDevicePhS_iiPhS_ii, .-_Z47__device_stub__Z23convertRgb2GrayByDevicePhS_iiPhS_ii
	.globl	_Z23convertRgb2GrayByDevicePhS_ii
	.type	_Z23convertRgb2GrayByDevicePhS_ii, @function
_Z23convertRgb2GrayByDevicePhS_ii:
.LFB2066:
	.cfi_startproc
	endbr64
	jmp	_Z47__device_stub__Z23convertRgb2GrayByDevicePhS_iiPhS_ii
	.cfi_endproc
.LFE2066:
	.size	_Z23convertRgb2GrayByDevicePhS_ii, .-_Z23convertRgb2GrayByDevicePhS_ii
	.section	.rodata.str1.1
.LC20:
	.string	"The number of arguments is invalid\n"
.LC21:
	.string	"Image size (width x height): %i x %i\n"
.LC22:
	.string	"cpu"
.LC23:
	.string	"/home/ubuntu/Datasets/stackv2/train-structured/TruongNgocTai/TH_01/master/bt1.cu"
.LC24:
	.string	"Error: %s:%d, "
.LC25:
	.string	"code: %d, reason: %s\n"
.LC26:
	.string	"GPU name: %s\n"
.LC27:
	.string	"GPU compute capability: %d\n"
.LC28:
	.string	"Block size: %d x %d\n"
.LC29:
	.string	"Grid size: %d x %d\n"
.LC30:
	.string	"Processing time: %f ms\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2040:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA2040
	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	$1128, %rsp
	.cfi_def_cfa_offset 1184
	movq	%fs:40, %rax
	movq	%rax, 1112(%rsp)
	xorl	%eax, %eax
	leal	-5(%rdi), %eax
	cmpl	$2, %eax
	jbe	.L49
	leaq	.LC20(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
.LEHB0:
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L48
.L49:
	movq	%rsi, %rbx
	leaq	16(%rsp), %r8
	leaq	12(%rsp), %rcx
	movl	%edi, %ebp
	movq	8(%rbx), %rdi
	leaq	8(%rsp), %rdx
	leaq	4(%rsp), %rsi
	call	_Z7readPnmPcRiS0_S0_RPh
	movl	12(%rsp), %ecx
	movl	8(%rsp), %edx
	xorl	%eax, %eax
	leaq	.LC21(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	8(%rsp), %edi
	imull	12(%rsp), %edi
	movslq	%edi, %rdi
	call	malloc@PLT
	leaq	64(%rsp), %rdi
	movq	%rax, %r15
	call	cudaEventCreate@PLT
	leaq	72(%rsp), %rdi
	call	cudaEventCreate@PLT
.LEHE0:
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
.LEHB1:
	call	cudaEventRecord@PLT
	movq	32(%rbx), %rdi
	leaq	.LC22(%rip), %rsi
	call	strcmp@PLT
	testl	%eax, %eax
	jne	.L51
	movl	12(%rsp), %ecx
	movl	8(%rsp), %edx
	movq	%r15, %rsi
	movq	16(%rsp), %rdi
	call	_Z21convertRgb2GrayByHostPhS_ii
	jmp	.L52
.L51:
	leaq	80(%rsp), %r13
	xorl	%esi, %esi
	movq	%r13, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movl	%eax, %r12d
	testl	%eax, %eax
	je	.L53
	movl	$206, %r8d
.L80:
	movq	stderr(%rip), %rdi
	leaq	.LC23(%rip), %rcx
	leaq	.LC24(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%r12d, %edi
	call	cudaGetErrorString@PLT
	movq	%rax, %r8
	movl	%r12d, %ecx
.L70:
	movq	stderr(%rip), %rdi
	leaq	.LC25(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L53:
	movq	%r13, %rdx
	leaq	.LC26(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	440(%rsp), %edx
	leaq	.LC27(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	8(%rsp), %esi
	imull	12(%rsp), %esi
	leaq	24(%rsp), %rdi
	movl	4(%rsp), %r12d
	imull	%r12d, %esi
	movslq	%esi, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %r13d
	movl	$213, %r8d
	testl	%eax, %eax
	jne	.L82
	movl	8(%rsp), %esi
	imull	12(%rsp), %esi
	leaq	32(%rsp), %rdi
	movslq	%esi, %rsi
	call	cudaMalloc@PLT
	movl	%eax, %r13d
	testl	%eax, %eax
	je	.L56
	movl	$214, %r8d
.L82:
	movq	stderr(%rip), %rdi
	leaq	.LC23(%rip), %rcx
	leaq	.LC24(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%r13d, %edi
	call	cudaGetErrorString@PLT
	movq	%rax, %r8
	movl	%r13d, %ecx
	jmp	.L70
.L56:
	movl	8(%rsp), %edx
	imull	12(%rsp), %edx
	movl	$1, %ecx
	movq	16(%rsp), %rsi
	movq	24(%rsp), %rdi
	imull	%r12d, %edx
	movslq	%edx, %rdx
	call	cudaMemcpy@PLT
	movl	%eax, %r12d
	movl	$217, %r8d
	testl	%eax, %eax
	jne	.L80
	movl	$1, 48(%rsp)
	cmpl	$7, %ebp
	jne	.L64
	movq	40(%rbx), %rdi
	call	atoi@PLT
	movq	48(%rbx), %rdi
	movl	%eax, %r14d
	call	atoi@PLT
	movl	%eax, %r13d
	jmp	.L58
.L64:
	movl	$32, %r13d
	movl	$32, %r14d
.L58:
	movl	12(%rsp), %eax
	xorl	%edx, %edx
	movl	%r13d, %ecx
	leaq	.LC28(%rip), %rsi
	movl	$2, %edi
	movl	$1, 60(%rsp)
	decl	%eax
	divl	%r13d
	xorl	%edx, %edx
	leal	1(%rax), %ebp
	movl	8(%rsp), %eax
	decl	%eax
	divl	%r14d
	movl	%r14d, %edx
	leal	1(%rax), %r12d
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebp, %ecx
	movl	%r12d, %edx
	leaq	.LC29(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	%r12d, 52(%rsp)
	movl	48(%rsp), %ecx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	%ebp, 56(%rsp)
	movl	60(%rsp), %esi
	movl	%r14d, 40(%rsp)
	movq	52(%rsp), %rdi
	movl	%r13d, 44(%rsp)
	movq	40(%rsp), %rdx
	call	__cudaPushCallConfiguration@PLT
	movl	12(%rsp), %ecx
	movl	8(%rsp), %edx
	movq	32(%rsp), %rsi
	testl	%eax, %eax
	jne	.L59
	movq	24(%rsp), %rdi
	call	_Z47__device_stub__Z23convertRgb2GrayByDevicePhS_iiPhS_ii
.L59:
	movl	8(%rsp), %edx
	imull	12(%rsp), %edx
	movl	$2, %ecx
	movq	%r15, %rdi
	movq	32(%rsp), %rsi
	movslq	%edx, %rdx
	call	cudaMemcpy@PLT
	movl	%eax, %ebp
	testl	%eax, %eax
	je	.L60
	movq	stderr(%rip), %rdi
	movl	$234, %r8d
	xorl	%eax, %eax
	leaq	.LC23(%rip), %rcx
	leaq	.LC24(%rip), %rdx
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%ebp, %edi
	call	cudaGetErrorString@PLT
	movq	%rax, %r8
	movl	%ebp, %ecx
	jmp	.L70
.L60:
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
.L52:
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	leaq	52(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	leaq	.LC30(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	52(%rsp), %xmm0
	call	__printf_chk@PLT
	movq	16(%rbx), %rdi
	movl	12(%rsp), %ecx
	movq	%r15, %r8
	movl	$1, %esi
	movl	8(%rsp), %edx
	call	_Z8writePnmPciiiPh
	movq	16(%rsp), %rdi
	call	free@PLT
	movq	%r15, %rdi
	call	free@PLT
	movq	24(%rbx), %rsi
	movq	16(%rbx), %rdi
	call	_Z12compare2PnmsPcS_
.LEHE1:
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	xorl	%eax, %eax
	jmp	.L48
.L65:
	endbr64
	movq	%rax, %rbx
.L61:
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	1112(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L73
	movq	%rbx, %rdi
.LEHB2:
	call	_Unwind_Resume@PLT
.LEHE2:
.L48:
	movq	1112(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L63
.L73:
	call	__stack_chk_fail@PLT
.L63:
	addq	$1128, %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
.LFE2040:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA2040:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE2040-.LLSDACSB2040
.LLSDACSB2040:
	.uleb128 .LEHB0-.LFB2040
	.uleb128 .LEHE0-.LEHB0
	.uleb128 0
	.uleb128 0
	.uleb128 .LEHB1-.LFB2040
	.uleb128 .LEHE1-.LEHB1
	.uleb128 .L65-.LFB2040
	.uleb128 0
	.uleb128 .LEHB2-.LFB2040
	.uleb128 .LEHE2-.LEHB2
	.uleb128 0
	.uleb128 0
.LLSDACSE2040:
	.section	.text.startup
	.size	main, .-main
	.section	.rodata.str1.1
.LC31:
	.string	"_Z23convertRgb2GrayByDevicePhS_ii"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2068:
	.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	.LC31(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z23convertRgb2GrayByDevicePhS_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
.LFE2068:
	.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
.LC17:
	.long	-446676599
	.long	1070801616
	.align 8
.LC18:
	.long	-1614907703
	.long	1069362970
	.align 8
.LC19:
	.long	962072674
	.long	1071827124
	.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 <stdio.h>
#include <stdint.h>
#include <hip/hip_runtime.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);
    }

    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 
	{
		fclose(f);
		printf("Cannot read %s\n", fileName); // In this exercise, we don't touch other types
		exit(EXIT_FAILURE); 
	}

	fscanf(f, "%i", &width);
	fscanf(f, "%i", &height);
	
	uint8_t max_val;
	fscanf(f, "%hhu", &max_val);
	if (max_val > 255)
	{
		fclose(f);
		printf("Cannot read %s\n", fileName); // In this exercise, we assume 1 byte per value
		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(char * fileName, int numChannels, int width, int height, uint8_t * pixels)
{
	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);
}

void compare2Pnms(char * fileName1, char * fileName2)
{
	int numChannels1, width1, height1;
	uint8_t * pixels1;
	readPnm(fileName1, numChannels1, width1, height1, pixels1);

	int numChannels2, width2, height2;
	uint8_t * pixels2;
	readPnm(fileName2, numChannels2, width2, height2, pixels2);

	if (numChannels1 != numChannels2)
	{
		printf("'%s' is DIFFERENT from '%s' (num channels: %i vs %i)\n", fileName1, fileName2, numChannels1, numChannels2);
		return;
	}
	if (width1 != width2)
	{
		printf("'%s' is DIFFERENT from '%s' (width: %i vs %i)\n", fileName1, fileName2, width1, width2);
		return;
	}
	if (height1 != height2)
	{
		printf("'%s' is DIFFERENT from '%s' (width: %i vs %i)\n", fileName1, fileName2, height1, height2);
		return;
	}
	float mae = 0;
	for (int i = 0; i < width1 * height1 * numChannels1; i++)
	{
		mae += abs((int)pixels1[i]-(int)pixels2[i]);
	}
	mae /= (width1 * height1 * numChannels1);
	printf("The average pixel difference between '%s' and '%s': %f\n", fileName1, fileName2, mae);
}

void convertRgb2GrayByHost(uint8_t * inPixels, uint8_t * outPixels, int width, int height)
{
	// TODO
	int size = width * height;

	for(int i = 0; i < size; i++){
		outPixels[i] = 0.299 * inPixels[i*3] +
						 0.114 * inPixels[i*3 + 2] + 
						 0.587 * inPixels[i*3 + 1];
	}
}

__global__ void convertRgb2GrayByDevice(uint8_t * inPixels, uint8_t * outPixels, int width, int height)
{
	// TODO
	int i_r = blockIdx.y * blockDim.y + threadIdx.y;
	int i_c = blockIdx.x * blockDim.x + threadIdx.x;

	if(i_c < width && i_r < height){
		outPixels[i_r * width + i_c] = 0.299 * inPixels[(i_r * width + i_c)*3] + 
										0.114 * inPixels[(i_r * width + i_c)*3 + 2] + 
										0.587 * inPixels[(i_r * width + i_c)*3 + 1];
	}
}


int main(int argc, char ** argv)
{
	// -----READ INPUT DATA-----
	if (argc < 5 || argc > 7)
	{
		printf("The number of arguments is invalid\n");
		return EXIT_FAILURE;
	}

	int numChannels, width, height;
	uint8_t * inPixels;
	readPnm(argv[1], numChannels, width, height, inPixels);
	printf("Image size (width x height): %i x %i\n", width, height);
	// -----PROCESS INPUT DATA-----
	uint8_t * outPixels= (uint8_t *)malloc(width * height);
	GpuTimer timer;
    timer.Start();
	if (strcmp(argv[4], "cpu") == 0){ // Use CPU
		convertRgb2GrayByHost(inPixels, outPixels, width, height);
	}
	else // Use GPU
	{
		// TODO: Query and print GPU name and compute capability
		hipDeviceProp_t prop;
		CHECK(hipGetDeviceProperties(&prop, 0));

		printf("GPU name: %s\n", prop.name);
		printf("GPU compute capability: %d\n", prop.major);

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

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

		// TODO: Set block size (already done for you) and grid size,
		//       and invoke kernel function with these settings (remember to check kernel error)
		dim3 blockSize(32, 32); // Default
		if (argc == 7)
		{
			blockSize.x = atoi(argv[5]);
			blockSize.y = atoi(argv[6]);
		}
		dim3 gridSize((width-1)/blockSize.x + 1, (height-1)/blockSize.y + 1);
		printf("Block size: %d x %d\n", blockSize.x, blockSize.y);
		printf("Grid size: %d x %d\n", gridSize.x, gridSize.y);

		convertRgb2GrayByDevice<<<gridSize, blockSize>>>(d_inPixels, d_outPixels, width, height);

		// TODO: Copy result from device memories
		CHECK(hipMemcpy(outPixels, d_outPixels, width * height, hipMemcpyDeviceToHost));

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

    // -----WRITE OUTPUT DATA TO FILE-----
	writePnm(argv[2], 1, width, height, outPixels);

	free(inPixels);
	free(outPixels);

	// -----CHECK CORRECTNESS-----
	compare2Pnms(argv[2], argv[3]);
}
	.text
	.file	"bt1.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, 16(%rsp)                  # 8-byte Spill
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_9
# %bb.1:
	movq	%rax, %r13
	leaq	13(%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.7:
	movzwl	13(%rsp), %ecx
	xorl	$13136, %ecx                    # imm = 0x3350
	movzbl	15(%rsp), %edx
	movl	$3, %eax
	orw	%cx, %dx
	je	.LBB0_3
# %bb.8:
	movq	%r13, %rdi
	callq	fclose
.LBB0_9:
	movl	$.L.str.1, %edi
	movq	16(%rsp), %rsi                  # 8-byte Reload
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.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	12(%rsp), %rdx
	movl	$.L.str.6, %esi
	movq	%r13, %rdi
	xorl	%eax, %eax
	callq	__isoc23_fscanf
	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_6
# %bb.4:                                # %.lr.ph.preheader
	xorl	%ebp, %ebp
.LBB0_5:                                # %.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_5
.LBB0_6:                                # %._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
.Lfunc_end0:
	.size	_Z7readPnmPcRiS0_S0_RPh, .Lfunc_end0-_Z7readPnmPcRiS0_S0_RPh
	.cfi_endproc
                                        # -- End function
	.globl	_Z8writePnmPciiiPh              # -- Begin function _Z8writePnmPciiiPh
	.type	_Z8writePnmPciiiPh,@function
_Z8writePnmPciiiPh:                     # @_Z8writePnmPciiiPh
	.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, %rbx
	movl	%ecx, %r15d
	movl	%edx, %r12d
	movl	%esi, %ebp
	movq	%rdi, %r13
	movl	$.L.str.7, %esi
	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	_Z8writePnmPciiiPh, .Lfunc_end1-_Z8writePnmPciiiPh
	.cfi_endproc
                                        # -- End function
	.globl	_Z12compare2PnmsPcS_            # -- Begin function _Z12compare2PnmsPcS_
	.type	_Z12compare2PnmsPcS_,@function
_Z12compare2PnmsPcS_:                   # @_Z12compare2PnmsPcS_
	.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	%rsi, %rbx
	movq	%rdi, %r14
	leaq	36(%rsp), %r15
	leaq	12(%rsp), %rdx
	leaq	8(%rsp), %rcx
	leaq	24(%rsp), %r8
	movq	%r15, %rsi
	callq	_Z7readPnmPcRiS0_S0_RPh
	leaq	32(%rsp), %r12
	leaq	4(%rsp), %rdx
	movq	%rsp, %rcx
	leaq	16(%rsp), %r8
	movq	%rbx, %rdi
	movq	%r12, %rsi
	callq	_Z7readPnmPcRiS0_S0_RPh
	movl	(%r15), %eax
	movl	(%r12), %r8d
	cmpl	%r8d, %eax
	jne	.LBB2_1
# %bb.2:
	movl	12(%rsp), %ecx
	movl	4(%rsp), %r8d
	cmpl	%r8d, %ecx
	jne	.LBB2_3
# %bb.5:
	movl	8(%rsp), %r9d
	movl	(%rsp), %r8d
	cmpl	%r8d, %r9d
	jne	.LBB2_12
# %bb.6:                                # %.preheader
	imull	%eax, %ecx
	imull	%r9d, %ecx
	testl	%ecx, %ecx
	jle	.LBB2_7
# %bb.10:                               # %.lr.ph
	movq	24(%rsp), %rax
	movq	16(%rsp), %rdx
	movl	%ecx, %esi
	xorps	%xmm0, %xmm0
	xorl	%edi, %edi
.LBB2_11:                               # =>This Inner Loop Header: Depth=1
	movzbl	(%rax,%rdi), %r8d
	movzbl	(%rdx,%rdi), %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	%rdi
	cmpq	%rdi, %rsi
	jne	.LBB2_11
	jmp	.LBB2_8
.LBB2_1:
	movl	$.L.str.13, %edi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	%eax, %ecx
	jmp	.LBB2_4
.LBB2_3:
	movl	$.L.str.14, %edi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	jmp	.LBB2_4
.LBB2_12:
	movl	$.L.str.14, %edi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movl	%r9d, %ecx
.LBB2_4:
	xorl	%eax, %eax
	callq	printf
.LBB2_9:
	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
.LBB2_7:
	.cfi_def_cfa_offset 80
	xorps	%xmm0, %xmm0
.LBB2_8:                                # %._crit_edge
	xorps	%xmm1, %xmm1
	cvtsi2ss	%ecx, %xmm1
	divss	%xmm1, %xmm0
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.15, %edi
	movq	%r14, %rsi
	movq	%rbx, %rdx
	movb	$1, %al
	callq	printf
	jmp	.LBB2_9
.Lfunc_end2:
	.size	_Z12compare2PnmsPcS_, .Lfunc_end2-_Z12compare2PnmsPcS_
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z21convertRgb2GrayByHostPhS_ii
.LCPI3_0:
	.quad	0x3fd322d0e5604189              # double 0.29899999999999999
.LCPI3_1:
	.quad	0x3fbd2f1a9fbe76c9              # double 0.114
.LCPI3_2:
	.quad	0x3fe2c8b439581062              # double 0.58699999999999997
	.text
	.globl	_Z21convertRgb2GrayByHostPhS_ii
	.type	_Z21convertRgb2GrayByHostPhS_ii,@function
_Z21convertRgb2GrayByHostPhS_ii:        # @_Z21convertRgb2GrayByHostPhS_ii
	.cfi_startproc
# %bb.0:
	imull	%ecx, %edx
	testl	%edx, %edx
	jle	.LBB3_3
# %bb.1:                                # %.lr.ph.preheader
	movl	%edx, %eax
	addq	$2, %rdi
	xorl	%ecx, %ecx
	movsd	.LCPI3_0(%rip), %xmm0           # xmm0 = mem[0],zero
	movsd	.LCPI3_1(%rip), %xmm1           # xmm1 = mem[0],zero
	movsd	.LCPI3_2(%rip), %xmm2           # xmm2 = mem[0],zero
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movzbl	-2(%rdi), %edx
	xorps	%xmm3, %xmm3
	cvtsi2sd	%edx, %xmm3
	mulsd	%xmm0, %xmm3
	movzbl	(%rdi), %edx
	xorps	%xmm4, %xmm4
	cvtsi2sd	%edx, %xmm4
	mulsd	%xmm1, %xmm4
	addsd	%xmm3, %xmm4
	movzbl	-1(%rdi), %edx
	xorps	%xmm3, %xmm3
	cvtsi2sd	%edx, %xmm3
	mulsd	%xmm2, %xmm3
	addsd	%xmm4, %xmm3
	cvttsd2si	%xmm3, %edx
	movb	%dl, (%rsi,%rcx)
	incq	%rcx
	addq	$3, %rdi
	cmpq	%rcx, %rax
	jne	.LBB3_2
.LBB3_3:                                # %._crit_edge
	retq
.Lfunc_end3:
	.size	_Z21convertRgb2GrayByHostPhS_ii, .Lfunc_end3-_Z21convertRgb2GrayByHostPhS_ii
	.cfi_endproc
                                        # -- End function
	.globl	_Z38__device_stub__convertRgb2GrayByDevicePhS_ii # -- Begin function _Z38__device_stub__convertRgb2GrayByDevicePhS_ii
	.type	_Z38__device_stub__convertRgb2GrayByDevicePhS_ii,@function
_Z38__device_stub__convertRgb2GrayByDevicePhS_ii: # @_Z38__device_stub__convertRgb2GrayByDevicePhS_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	$_Z23convertRgb2GrayByDevicePhS_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	_Z38__device_stub__convertRgb2GrayByDevicePhS_ii, .Lfunc_end4-_Z38__device_stub__convertRgb2GrayByDevicePhS_ii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
.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	$1544, %rsp                     # imm = 0x608
	.cfi_def_cfa_offset 1600
	.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	%edi, %r12d
	leal	-8(%r12), %eax
	cmpl	$-4, %eax
	ja	.LBB5_2
# %bb.1:
	movl	$.Lstr, %edi
	callq	puts@PLT
	movl	$1, %eax
	jmp	.LBB5_40
.LBB5_2:
	movq	%rsi, %rbx
	movq	8(%rsi), %rdi
	leaq	52(%rsp), %rsi
	leaq	12(%rsp), %r14
	leaq	8(%rsp), %r15
	leaq	56(%rsp), %r8
	movq	%r14, %rdx
	movq	%r15, %rcx
	callq	_Z7readPnmPcRiS0_S0_RPh
	movl	(%r14), %esi
	movl	(%r15), %edx
	movl	$.L.str.17, %edi
	xorl	%eax, %eax
	callq	printf
	movslq	(%r14), %rax
	movslq	(%r15), %rdi
	imulq	%rax, %rdi
	callq	malloc
	movq	%rax, %r13
	leaq	32(%rsp), %rdi
	callq	hipEventCreate
	leaq	40(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventCreate
	movq	-8(%r15), %rdi
.Ltmp0:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp1:
# %bb.3:                                # %_ZN8GpuTimer5StartEv.exit
	movq	32(%rbx), %rdi
	movl	$.L.str.18, %esi
	callq	strcmp
	testl	%eax, %eax
	je	.LBB5_4
# %bb.6:
.Ltmp2:
	leaq	72(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
.Ltmp3:
# %bb.7:
	movl	%eax, %ebp
	testl	%eax, %eax
	jne	.LBB5_8
# %bb.11:
	leaq	72(%rsp), %r15
	movl	$.L.str.22, %edi
	movq	%r15, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	360(%r15), %esi
	movl	$.L.str.23, %edi
	xorl	%eax, %eax
	callq	printf
	movl	8(%rsp), %eax
	imull	12(%rsp), %eax
	movl	52(%rsp), %r14d
	imull	%r14d, %eax
	movslq	%eax, %rsi
.Ltmp7:
	leaq	24(%rsp), %rdi
	callq	hipMalloc
.Ltmp8:
# %bb.12:                               # %_ZL9hipMallocIhE10hipError_tPPT_m.exit
	movl	%eax, %ebp
	testl	%eax, %eax
	jne	.LBB5_13
# %bb.15:
	movslq	12(%rsp), %rax
	movslq	8(%rsp), %rsi
	imulq	%rax, %rsi
.Ltmp12:
	leaq	16(%rsp), %rdi
	callq	hipMalloc
.Ltmp13:
# %bb.16:                               # %_ZL9hipMallocIhE10hipError_tPPT_m.exit62
	movl	%eax, %ebp
	testl	%eax, %eax
	jne	.LBB5_17
# %bb.19:
	movq	24(%rsp), %rdi
	imull	12(%rsp), %r14d
	movq	56(%rsp), %r15
	imull	8(%rsp), %r14d
	movslq	%r14d, %rdx
.Ltmp17:
	movq	%r15, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
.Ltmp18:
# %bb.20:
	movl	%eax, %ebp
	testl	%eax, %eax
	jne	.LBB5_21
# %bb.23:
	cmpl	$7, %r12d
	movq	%r13, 64(%rsp)                  # 8-byte Spill
	jne	.LBB5_24
# %bb.25:
	movq	40(%rbx), %rdi
	callq	atoi
	movl	%eax, %r14d
	movq	48(%rbx), %rdi
	callq	atoi
	movl	%eax, %r12d
	shlq	$32, %r12
	orq	%r14, %r12
	jmp	.LBB5_26
.LBB5_4:
	movq	56(%rsp), %r15
	movl	12(%rsp), %edx
	movl	8(%rsp), %ecx
	movq	%r15, %rdi
	movq	%r13, %rsi
	callq	_Z21convertRgb2GrayByHostPhS_ii
	jmp	.LBB5_35
.LBB5_24:
	movabsq	$137438953504, %r12             # imm = 0x2000000020
.LBB5_26:
	movl	12(%rsp), %eax
	decl	%eax
	xorl	%edx, %edx
	divl	%r12d
                                        # kill: def $eax killed $eax def $rax
	leal	1(%rax), %r13d
	movl	8(%rsp), %eax
	decl	%eax
	movq	%r12, %rcx
	shrq	$32, %rcx
	xorl	%edx, %edx
	divl	%ecx
                                        # kill: def $eax killed $eax def $rax
	leal	1(%rax), %r14d
	movq	%r14, %rbp
	shlq	$32, %rbp
	orq	%r13, %rbp
	movl	$.L.str.24, %edi
	movl	%r12d, %esi
	movl	%ecx, %edx
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.25, %edi
	movl	%r13d, %esi
	movl	%r14d, %edx
	xorl	%eax, %eax
	callq	printf
.Ltmp22:
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%r12, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
.Ltmp23:
	movq	64(%rsp), %r13                  # 8-byte Reload
# %bb.27:
	testl	%eax, %eax
	jne	.LBB5_29
# %bb.28:
	movq	24(%rsp), %rdi
	movq	16(%rsp), %rsi
	movl	12(%rsp), %edx
	movl	8(%rsp), %ecx
.Ltmp24:
	callq	_Z38__device_stub__convertRgb2GrayByDevicePhS_ii
.Ltmp25:
.LBB5_29:
	movq	16(%rsp), %rsi
	movslq	12(%rsp), %rax
	movslq	8(%rsp), %rdx
	imulq	%rax, %rdx
.Ltmp26:
	movq	%r13, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
.Ltmp27:
# %bb.30:
	movl	%eax, %ebp
	testl	%eax, %eax
	jne	.LBB5_31
# %bb.33:
	movq	24(%rsp), %rdi
.Ltmp31:
	callq	hipFree
.Ltmp32:
# %bb.34:
	movq	16(%rsp), %rdi
.Ltmp33:
	callq	hipFree
.Ltmp34:
.LBB5_35:
	movq	40(%rsp), %rdi
.Ltmp36:
	xorl	%esi, %esi
	callq	hipEventRecord
.Ltmp37:
# %bb.36:                               # %_ZN8GpuTimer4StopEv.exit
	movq	40(%rsp), %rdi
.Ltmp39:
	callq	hipEventSynchronize
.Ltmp40:
# %bb.37:                               # %.noexc
	movq	32(%rsp), %rsi
	movq	40(%rsp), %rdx
.Ltmp41:
	leaq	72(%rsp), %rdi
	callq	hipEventElapsedTime
.Ltmp42:
# %bb.38:
	cvtss2sd	72(%rsp), %xmm0
	movl	$.L.str.26, %edi
	movb	$1, %al
	callq	printf
	movq	16(%rbx), %rdi
	movl	12(%rsp), %edx
	movl	8(%rsp), %ecx
	movl	$1, %esi
	movq	%r13, %r8
	callq	_Z8writePnmPciiiPh
	movq	%r15, %rdi
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	16(%rbx), %rdi
	movq	24(%rbx), %rsi
.Ltmp43:
	callq	_Z12compare2PnmsPcS_
.Ltmp44:
# %bb.39:
	leaq	32(%rsp), %rdi
	callq	_ZN8GpuTimerD2Ev
	xorl	%eax, %eax
.LBB5_40:
	addq	$1544, %rsp                     # imm = 0x608
	.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
.LBB5_8:
	.cfi_def_cfa_offset 1600
	movq	stderr(%rip), %rdi
	movl	$.L.str.19, %esi
	movl	$.L.str.20, %edx
	movl	$206, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
.Ltmp4:
	movl	%ebp, %edi
	callq	hipGetErrorString
.Ltmp5:
	jmp	.LBB5_9
.LBB5_13:
	movq	stderr(%rip), %rdi
	movl	$.L.str.19, %esi
	movl	$.L.str.20, %edx
	movl	$213, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
.Ltmp9:
	movl	%ebp, %edi
	callq	hipGetErrorString
.Ltmp10:
	jmp	.LBB5_9
.LBB5_17:
	movq	stderr(%rip), %rdi
	movl	$.L.str.19, %esi
	movl	$.L.str.20, %edx
	movl	$214, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
.Ltmp14:
	movl	%ebp, %edi
	callq	hipGetErrorString
.Ltmp15:
	jmp	.LBB5_9
.LBB5_21:
	movq	stderr(%rip), %rdi
	movl	$.L.str.19, %esi
	movl	$.L.str.20, %edx
	movl	$217, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
.Ltmp19:
	movl	%ebp, %edi
	callq	hipGetErrorString
.Ltmp20:
	jmp	.LBB5_9
.LBB5_31:
	movq	stderr(%rip), %rdi
	movl	$.L.str.19, %esi
	movl	$.L.str.20, %edx
	movl	$234, %ecx
	xorl	%eax, %eax
	callq	fprintf
	movq	stderr(%rip), %rbx
.Ltmp28:
	movl	%ebp, %edi
	callq	hipGetErrorString
.Ltmp29:
.LBB5_9:
	movl	$.L.str.21, %esi
	movq	%rbx, %rdi
	movl	%ebp, %edx
	movq	%rax, %rcx
	xorl	%eax, %eax
	callq	fprintf
	movl	$1, %edi
	callq	exit
.LBB5_43:
.Ltmp35:
	jmp	.LBB5_42
.LBB5_5:
.Ltmp38:
	jmp	.LBB5_42
.LBB5_41:
.Ltmp45:
	jmp	.LBB5_42
.LBB5_32:
.Ltmp30:
	jmp	.LBB5_42
.LBB5_22:
.Ltmp21:
	jmp	.LBB5_42
.LBB5_18:
.Ltmp16:
	jmp	.LBB5_42
.LBB5_14:
.Ltmp11:
	jmp	.LBB5_42
.LBB5_10:
.Ltmp6:
.LBB5_42:
	movq	%rax, %rbx
	leaq	32(%rsp), %rdi
	callq	_ZN8GpuTimerD2Ev
	movq	%rbx, %rdi
	callq	_Unwind_Resume@PLT
.Lfunc_end5:
	.size	main, .Lfunc_end5-main
	.cfi_endproc
	.section	.gcc_except_table,"a",@progbits
	.p2align	2, 0x0
GCC_except_table5:
.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 .Ltmp1-.Ltmp0                  #   Call between .Ltmp0 and .Ltmp1
	.uleb128 .Ltmp38-.Lfunc_begin0          #     jumps to .Ltmp38
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp2-.Lfunc_begin0           # >> Call Site 3 <<
	.uleb128 .Ltmp3-.Ltmp2                  #   Call between .Ltmp2 and .Ltmp3
	.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 .Ltmp35-.Lfunc_begin0          #     jumps to .Ltmp35
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp26-.Lfunc_begin0          # >> Call Site 8 <<
	.uleb128 .Ltmp27-.Ltmp26                #   Call between .Ltmp26 and .Ltmp27
	.uleb128 .Ltmp30-.Lfunc_begin0          #     jumps to .Ltmp30
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp31-.Lfunc_begin0          # >> Call Site 9 <<
	.uleb128 .Ltmp34-.Ltmp31                #   Call between .Ltmp31 and .Ltmp34
	.uleb128 .Ltmp35-.Lfunc_begin0          #     jumps to .Ltmp35
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp36-.Lfunc_begin0          # >> Call Site 10 <<
	.uleb128 .Ltmp37-.Ltmp36                #   Call between .Ltmp36 and .Ltmp37
	.uleb128 .Ltmp38-.Lfunc_begin0          #     jumps to .Ltmp38
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp39-.Lfunc_begin0          # >> Call Site 11 <<
	.uleb128 .Ltmp44-.Ltmp39                #   Call between .Ltmp39 and .Ltmp44
	.uleb128 .Ltmp45-.Lfunc_begin0          #     jumps to .Ltmp45
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp4-.Lfunc_begin0           # >> Call Site 12 <<
	.uleb128 .Ltmp5-.Ltmp4                  #   Call between .Ltmp4 and .Ltmp5
	.uleb128 .Ltmp6-.Lfunc_begin0           #     jumps to .Ltmp6
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp9-.Lfunc_begin0           # >> Call Site 13 <<
	.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 14 <<
	.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 15 <<
	.uleb128 .Ltmp20-.Ltmp19                #   Call between .Ltmp19 and .Ltmp20
	.uleb128 .Ltmp21-.Lfunc_begin0          #     jumps to .Ltmp21
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp28-.Lfunc_begin0          # >> Call Site 16 <<
	.uleb128 .Ltmp29-.Ltmp28                #   Call between .Ltmp28 and .Ltmp29
	.uleb128 .Ltmp30-.Lfunc_begin0          #     jumps to .Ltmp30
	.byte	0                               #   On action: cleanup
	.uleb128 .Ltmp29-.Lfunc_begin0          # >> Call Site 17 <<
	.uleb128 .Lfunc_end5-.Ltmp29            #   Call between .Ltmp29 and .Lfunc_end5
	.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
.Ltmp46:
	callq	hipEventDestroy
.Ltmp47:
# %bb.1:
	movq	8(%rbx), %rdi
.Ltmp48:
	callq	hipEventDestroy
.Ltmp49:
# %bb.2:
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.LBB6_3:
	.cfi_def_cfa_offset 16
.Ltmp50:
	movq	%rax, %rdi
	callq	__clang_call_terminate
.Lfunc_end6:
	.size	_ZN8GpuTimerD2Ev, .Lfunc_end6-_ZN8GpuTimerD2Ev
	.cfi_endproc
	.section	.gcc_except_table._ZN8GpuTimerD2Ev,"aG",@progbits,_ZN8GpuTimerD2Ev,comdat
	.p2align	2, 0x0
GCC_except_table6:
.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 .Ltmp46-.Lfunc_begin1          # >> Call Site 1 <<
	.uleb128 .Ltmp49-.Ltmp46                #   Call between .Ltmp46 and .Ltmp49
	.uleb128 .Ltmp50-.Lfunc_begin1          #     jumps to .Ltmp50
	.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
	.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_end7:
	.size	__clang_call_terminate, .Lfunc_end7-__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	.LBB8_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB8_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z23convertRgb2GrayByDevicePhS_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_end8:
	.size	__hip_module_ctor, .Lfunc_end8-__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	.LBB9_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
.LBB9_2:
	retq
.Lfunc_end9:
	.size	__hip_module_dtor, .Lfunc_end9-__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	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"'%s' is DIFFERENT from '%s' (num channels: %i vs %i)\n"
	.size	.L.str.13, 54

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"'%s' is DIFFERENT from '%s' (width: %i vs %i)\n"
	.size	.L.str.14, 47

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"The average pixel difference between '%s' and '%s': %f\n"
	.size	.L.str.15, 56

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

	.type	.L.str.17,@object               # @.str.17
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.17:
	.asciz	"Image size (width x height): %i x %i\n"
	.size	.L.str.17, 38

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"cpu"
	.size	.L.str.18, 4

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

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"/home/ubuntu/Datasets/stackv2/train-structured-repos-hip-sm89/TruongNgocTai/TH_01/master/bt1.hip"
	.size	.L.str.20, 97

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

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"GPU name: %s\n"
	.size	.L.str.22, 14

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

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"Block size: %d x %d\n"
	.size	.L.str.24, 21

	.type	.L.str.25,@object               # @.str.25
.L.str.25:
	.asciz	"Grid size: %d x %d\n"
	.size	.L.str.25, 20

	.type	.L.str.26,@object               # @.str.26
.L.str.26:
	.asciz	"Processing time: %f ms\n"
	.size	.L.str.26, 24

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

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

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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"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 _Z38__device_stub__convertRgb2GrayByDevicePhS_ii
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Unwind_Resume
	.addrsig_sym _Z23convertRgb2GrayByDevicePhS_ii
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z23convertRgb2GrayByDevicePhS_ii ; -- Begin function _Z23convertRgb2GrayByDevicePhS_ii
	.globl	_Z23convertRgb2GrayByDevicePhS_ii
	.p2align	8
	.type	_Z23convertRgb2GrayByDevicePhS_ii,@function
_Z23convertRgb2GrayByDevicePhS_ii:      ; @_Z23convertRgb2GrayByDevicePhS_ii
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b64 s[4:5], s[0:1], 0x10
	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, s2, vcc_lo
	s_and_saveexec_b32 s3, s2
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_mad_u64_u32 v[2:3], null, v1, s4, v[0:1]
	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, s0, v0
	s_delay_alu instid0(VALU_DEP_2)
	v_add_co_ci_u32_e32 v1, vcc_lo, s1, v1, vcc_lo
	s_mov_b32 s0, 0x9fbe76c9
	s_mov_b32 s1, 0x3fbd2f1a
	s_clause 0x1
	global_load_u8 v3, v[0:1], off offset:2
	global_load_u16 v5, v[0:1], off
	s_waitcnt vmcnt(1)
	v_cvt_f64_u32_e32 v[0:1], v3
	s_waitcnt vmcnt(0)
	v_and_b32_e32 v3, 0xff, v5
	v_bfe_u32 v5, v5, 8, 8
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_and_b32_e32 v3, 0xffff, v3
	v_cvt_f64_u32_e32 v[3:4], v3
	v_mul_f64 v[0:1], v[0:1], s[0:1]
	s_mov_b32 s0, 0xe5604189
	s_mov_b32 s1, 0x3fd322d0
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_fma_f64 v[0:1], v[3:4], s[0:1], v[0:1]
	v_cvt_f64_u32_e32 v[3:4], v5
	s_mov_b32 s0, 0x39581062
	s_mov_b32 s1, 0x3fe2c8b4
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_fma_f64 v[0:1], v[3:4], s[0:1], v[0:1]
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_2)
	v_cvt_i32_f64_e32 v3, v[0:1]
	v_ashrrev_i32_e32 v1, 31, v2
	v_add_co_u32 v0, vcc_lo, s2, v2
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, 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 _Z23convertRgb2GrayByDevicePhS_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 6
		.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	_Z23convertRgb2GrayByDevicePhS_ii, .Lfunc_end0-_Z23convertRgb2GrayByDevicePhS_ii
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 348
; NumSgprs: 18
; NumVgprs: 6
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 6
; 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:           _Z23convertRgb2GrayByDevicePhS_ii
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z23convertRgb2GrayByDevicePhS_ii.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     6
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
