#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <iostream>
#include <ctype.h>
#include <cuda.h>

#define DATAMB(bytes)			(bytes/1024/1024)
#define DATABW(bytes,timems)	((float)bytes/(timems * 1.024*1024.0*1024.0))

typedef unsigned char uch;
typedef unsigned long ul;
typedef unsigned int  ui;

ui origSize, TgtSize;
ui xshrink, yshrink;
ui origVpixels, origHpixels, origHbytes;

uch *TheImage, *NewImage;					// Where images are stored in CPU
uch *GPUSrcImage, *GPUTgtImage, *GPUResult;	// Where images are stored in GPU



struct ImgProp{
	int Hpixels;
	int Vpixels;
	uch HeaderInfo[54];
	ul Hbytes;
} ip;

#define	IPHB		ip.Hbytes
#define	IPH			ip.Hpixels
#define	IPV			ip.Vpixels
#define	IMAGESIZE	(IPHB*IPV)
#define	IMAGEPIX	(IPH*IPV)


// Read a 24-bit/pixel BMP file into a 1D linear array.
// Allocate memory to store the 1D image and return its pointer.
uch *ReadBMPlin(char* fn)
{
	static uch *Img;
	FILE* f = fopen(fn, "rb");
	if (f == NULL){	printf("\n\n%s NOT FOUND\n\n", fn);	exit(EXIT_FAILURE); }

	uch HeaderInfo[54];
	fread(HeaderInfo, sizeof(uch), 54, f); // read the 54-byte header
	// extract image height and width from header
	int width = *(int*)&HeaderInfo[18];			ip.Hpixels = width;
	int height = *(int*)&HeaderInfo[22];		ip.Vpixels = height;
	int RowBytes = (width * 3 + 3) & (~3);		ip.Hbytes = RowBytes;
	//save header for re-use
	memcpy(ip.HeaderInfo, HeaderInfo,54);
	printf("\n Input File name: %17s  (%u x %u)   File Size=%u", fn, 
			ip.Hpixels, ip.Vpixels, IMAGESIZE);
	// allocate memory to store the main image (1 Dimensional array)
	Img  = (uch *)malloc(IMAGESIZE);
	if (Img == NULL) return Img;      // Cannot allocate memory
	// read the image from disk
	fread(Img, sizeof(uch), IMAGESIZE, f);
	fclose(f);
	return Img;
}

void WriteBMPlin(uch *Img, char* fn)
{
	FILE* f = fopen(fn, "wb");
	if (f == NULL){ printf("\n\nFILE CREATION ERROR: %s\n\n", fn); exit(1); }
	//write header
	*(int*)&ip.HeaderInfo[2] = ip.Hbytes*ip.Vpixels+54;
	*(int*)&ip.HeaderInfo[18] = ip.Hpixels;
	*(int*)&ip.HeaderInfo[22] = ip.Vpixels;
	*(int*)&ip.HeaderInfo[34] = ip.Hbytes*ip.Vpixels;
	fwrite(ip.HeaderInfo, sizeof(uch), 54, f);
	//write data
	fwrite(Img, sizeof(uch), IMAGESIZE, f);
	printf("\nOutput File name: %17s  (%u x %u)   File Size=%u\n\n", fn, ip.Hpixels, ip.Vpixels, IMAGESIZE);
	fclose(f);
}
__global__
void ImShrunk(uch *TgtImg, uch *SrcImg, ui TgtHpixels, ui xshrink, ui yshrink, ui origHpixels, ui origHbytes)
{
	ui ThrPerBlk = blockDim.x;
	ui TgtBid = blockIdx.x;
	ui TgtTid = threadIdx.x;
	ui TgtGtid = ThrPerBlk * TgtBid + TgtTid;


	ui BlkPerRow = (TgtHpixels + ThrPerBlk - 1) / ThrPerBlk;  // ceil
	ui TgtRowBytes = (TgtHpixels * 3 + 3) & (~3);
	// ui SrcRowBytes = (origHpixels * 3 + 3) & (~3);
	ui SrcRowBytes = origHbytes;

	ui Tgtrow = TgtBid / BlkPerRow;
	ui TgtCol = TgtGtid - Tgtrow*BlkPerRow*ThrPerBlk;
	if(TgtCol >= TgtHpixels) return;
	
	ui SrcRow = Tgtrow * yshrink;
	ui SrcCol = TgtCol * xshrink;
	// if(SrcCol >= origHpixels) return;
	
	///////////////
	ui TgtOffset = Tgtrow * TgtRowBytes;
	ui SrcOffset = SrcRow * SrcRowBytes;

	ui TgtIndex = TgtOffset + 3*TgtCol;
	ui SrcIndex = SrcOffset + 3*SrcCol;

	TgtImg[TgtIndex] = SrcImg[SrcIndex];
	TgtImg[TgtIndex+1] = SrcImg[SrcIndex+1];
	TgtImg[TgtIndex+2] = SrcImg[SrcIndex+2];

}



int main(int argc, char** argv){
	
	float totalTime, tfrCPUtoGPU, tfrGPUtoCPU, kernelExecutionTime; // GPU code run times
	cudaError_t cudaStatus, cudaStatus2;
	cudaEvent_t time1, time2, time3, time4;
	char InputFileName[255], OutputFileName[255], ProgName[255];
	ui BlkPerRow, ThrPerBlk=256, NumBlocks, GPUDataTransfer;
	cudaDeviceProp GPUprop;
	ul SupportedKBlocks, SupportedMBlocks, MaxThrPerBlk;		char SupportedBlocks[100];

	strcpy(ProgName, "imShrunk");
	if(argc!=5){
		printf("\n\nUsage: imshrunk input output xshrink yshrink");
		return 0;
	}

	xshrink = atoi(argv[3]);
	yshrink = atoi(argv[4]);
	strcpy(InputFileName, argv[1]);
	strcpy(OutputFileName, argv[2]);



	TheImage = ReadBMPlin(argv[1]);
	if (TheImage == NULL){
		printf("Cannot allocate memory for the input image...\n");
		exit(EXIT_FAILURE);
	}
	origHpixels = ip.Hpixels;
	origVpixels = ip.Vpixels;
	origHbytes = ip.Hbytes;
	origSize = origHbytes * origVpixels;

	ip.Hpixels = ip.Hpixels/xshrink;
	ip.Hbytes = (ip.Hpixels*3 + 3) & (~3);
	ip.Vpixels = ip.Vpixels/yshrink;
	// TgtSize = ip.Hbytes * ip.Vpixels;
	// printf("\n new Hpixels %u", ip.Hpixels);
	// printf("\n new Vpixels %u", ip.Vpixels);
	
	NewImage = (uch *)malloc(IMAGESIZE);
	if (NewImage == NULL){
		free(NewImage);
		printf("Cannot allocate memory for the input image...\n");
		exit(EXIT_FAILURE);
	}

		// Choose which GPU to run on, change this on a multi-GPU system.
		int NumGPUs = 0;
		cudaGetDeviceCount(&NumGPUs);
		if (NumGPUs == 0){
			printf("\nNo CUDA Device is available\n");
			exit(EXIT_FAILURE);
		}
		cudaStatus = cudaSetDevice(0);
		if (cudaStatus != cudaSuccess) {
			fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
			exit(EXIT_FAILURE);
		}
		cudaGetDeviceProperties(&GPUprop, 0);
		SupportedKBlocks = (ui)GPUprop.maxGridSize[0] * (ui)GPUprop.maxGridSize[1] * (ui)GPUprop.maxGridSize[2] / 1024;
		SupportedMBlocks = SupportedKBlocks / 1024;
		sprintf(SupportedBlocks, "%u %c", (SupportedMBlocks >= 5) ? SupportedMBlocks : SupportedKBlocks, (SupportedMBlocks >= 5) ? 'M' : 'K');
		MaxThrPerBlk = (ui)GPUprop.maxThreadsPerBlock;
	
		cudaEventCreate(&time1);
		cudaEventCreate(&time2);
		cudaEventCreate(&time3);
		cudaEventCreate(&time4);
	
		cudaEventRecord(time1, 0);

		// allocate GPU buffer
		cudaStatus = cudaMalloc((void**)&GPUSrcImage, origSize);
		cudaStatus2 = cudaMalloc((void**)&GPUTgtImage, IMAGESIZE);
		if ((cudaStatus != cudaSuccess) || (cudaStatus2 != cudaSuccess)){
			fprintf(stderr, "cudaMalloc failed! Can't allocate GPU memory");
			exit(EXIT_FAILURE);
		}

		// Copy input vectors from host memory to GPU buffers.
		cudaStatus = cudaMemcpy(GPUSrcImage, TheImage, origSize, cudaMemcpyHostToDevice);
		if (cudaStatus != cudaSuccess) {
			fprintf(stderr, "cudaMemcpy  CPU to GPU  failed!");
			exit(EXIT_FAILURE);
		}
		
		cudaEventRecord(time2, 0);		// Time stamp after the CPU --> GPU tfr is done
		BlkPerRow = (IPH + ThrPerBlk -1 ) / ThrPerBlk;
		NumBlocks = IPV*BlkPerRow;
	
		ImShrunk <<< NumBlocks, ThrPerBlk>>> (GPUTgtImage, GPUSrcImage, IPH, xshrink, yshrink, origHpixels, origHbytes);


		GPUResult = GPUTgtImage;
		GPUDataTransfer = origSize + IMAGESIZE;

		// cudaDeviceSynchronize waits for the kernel to finish, and returns
		// any errors encountered during the launch.
		cudaStatus = cudaDeviceSynchronize();
		if (cudaStatus != cudaSuccess) {
			fprintf(stderr, "\n\ncudaDeviceSynchronize returned error code %d after launching the kernel!\n", cudaStatus);
			exit(EXIT_FAILURE);
		}
		cudaEventRecord(time3, 0);

			// Copy output (results) from GPU buffer to host (CPU) memory.
		cudaStatus = cudaMemcpy(NewImage, GPUResult, IMAGESIZE, cudaMemcpyDeviceToHost);
		if (cudaStatus != cudaSuccess) {
			fprintf(stderr, "cudaMemcpy GPU to CPU  failed!");
			exit(EXIT_FAILURE);
		}

		
		cudaEventRecord(time4, 0);

		cudaEventSynchronize(time1);
		cudaEventSynchronize(time2);
		cudaEventSynchronize(time3);
		cudaEventSynchronize(time4);

		cudaEventElapsedTime(&totalTime, time1, time4);
		cudaEventElapsedTime(&tfrCPUtoGPU, time1, time2);
		cudaEventElapsedTime(&kernelExecutionTime, time2, time3);
		cudaEventElapsedTime(&tfrGPUtoCPU, time3, time4);

		cudaStatus = cudaDeviceSynchronize();

		//checkError(cudaGetLastError());	// screen for errors in kernel launches
		if (cudaStatus != cudaSuccess) {
			fprintf(stderr, "\n Program failed after cudaDeviceSynchronize()!");
			free(TheImage);
			free(NewImage);
			exit(EXIT_FAILURE);
		}
		WriteBMPlin(NewImage, argv[2]);		// Write the flipped image back to disk

		////////////////// change from here
		printf("\n\n--------------------------------------------------------------------------\n");
		printf("%s    ComputeCapab=%d.%d  [max %s blocks; %d thr/blk] \n", 
				GPUprop.name, GPUprop.major, GPUprop.minor, SupportedBlocks, MaxThrPerBlk);
		printf("--------------------------------------------------------------------------\n");
		printf("%s %s %s %u   [%u BLOCKS, %u BLOCKS/ROW]\n", ProgName, InputFileName, OutputFileName,
				ThrPerBlk, NumBlocks, BlkPerRow);
		printf("--------------------------------------------------------------------------\n");
		printf("CPU->GPU Transfer   =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n", tfrCPUtoGPU, DATAMB(origSize), DATABW(origSize, tfrCPUtoGPU));
		printf("Kernel Execution    =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n", kernelExecutionTime, DATAMB(GPUDataTransfer), DATABW(GPUDataTransfer, kernelExecutionTime));
		printf("GPU->CPU Transfer   =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n", tfrGPUtoCPU, DATAMB(IMAGESIZE), DATABW(IMAGESIZE, tfrGPUtoCPU));
		printf("--------------------------------------------------------------------------\n");
		printf("Total time elapsed  =%7.2f ms       %4d MB  ...  %6.2f GB/s\n", totalTime, DATAMB((origSize + IMAGESIZE + GPUDataTransfer)), DATABW((origSize + IMAGESIZE+ GPUDataTransfer), totalTime));
		printf("--------------------------------------------------------------------------\n\n");

			// Deallocate CPU, GPU memory and destroy events.
		cudaFree(GPUSrcImage);
		cudaFree(GPUTgtImage);
		cudaEventDestroy(time1);
		cudaEventDestroy(time2);
		cudaEventDestroy(time3);
		cudaEventDestroy(time4);
		// cudaDeviceReset must be called before exiting in order for profiling and
		// tracing tools such as Parallel Nsight and Visual Profiler to show complete traces.
		cudaStatus = cudaDeviceReset();
		if (cudaStatus != cudaSuccess) {
			fprintf(stderr, "cudaDeviceReset failed!");
			free(TheImage);
			free(NewImage);
			exit(EXIT_FAILURE);
		}
		free(TheImage);
		free(NewImage);
		return(EXIT_SUCCESS);
}


	.file	"tmpxft_0024abb2_00000000-6_imshrunkG.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3640:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3640:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rb"
.LC1:
	.string	"\n\n%s NOT FOUND\n\n"
.LC2:
	.string	"\n Input File name: %17s  (%u x %u)   File Size=%u"
	.text
	.globl	_Z10ReadBMPlinPc
	.type	_Z10ReadBMPlinPc, @function
_Z10ReadBMPlinPc:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	leaq	.LC0(%rip), %rsi
	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
	subq	$64, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L3
	movl	$2, %edi
	movq	%rbp, %rdx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L3:
	leaq	2(%rsp), %r12
	movq	%rax, %r8
	movl	$54, %ecx
	movq	%rax, %rbx
	movl	$1, %edx
	movl	$54, %esi
	movq	%r12, %rdi
	call	__fread_chk@PLT
	movl	20(%rsp), %edx
	movl	24(%rsp), %r8d
	movq	%r12, %rsi
	leaq	8+ip(%rip), %r9
	movl	$54, %ecx
	leal	1(%rdx), %eax
	movq	%r9, %rdi
	movslq	%r8d, %r9
	movl	%edx, ip(%rip)
	rep movsb
	leal	(%rax,%rax,2), %eax
	movl	%edx, %ecx
	leaq	.LC2(%rip), %rsi
	andl	$-4, %eax
	movq	%rbp, %rdx
	movl	$2, %edi
	movl	%r8d, 4+ip(%rip)
	cltq
	imulq	%rax, %r9
	movq	%rax, 64+ip(%rip)
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	4+ip(%rip), %rbp
	imulq	64+ip(%rip), %rbp
	movq	%rbp, %rdi
	call	malloc@PLT
	movq	%rax, _ZZ10ReadBMPlinPcE3Img(%rip)
	testq	%rax, %rax
	je	.L2
	movq	%rbx, %r8
	movq	%rbp, %rcx
	movl	$1, %edx
	movq	%rbp, %rsi
	movq	%rax, %rdi
	call	__fread_chk@PLT
	movq	%rbx, %rdi
	call	fclose@PLT
	movq	_ZZ10ReadBMPlinPcE3Img(%rip), %rax
.L2:
	movq	56(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L5
	call	__stack_chk_fail@PLT
.L5:
	addq	$64, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3635:
	.size	_Z10ReadBMPlinPc, .-_Z10ReadBMPlinPc
	.section	.rodata.str1.1
.LC3:
	.string	"wb"
.LC4:
	.string	"\n\nFILE CREATION ERROR: %s\n\n"
.LC5:
	.string	"\nOutput File name: %17s  (%u x %u)   File Size=%u\n\n"
	.text
	.globl	_Z11WriteBMPlinPhPc
	.type	_Z11WriteBMPlinPhPc, @function
_Z11WriteBMPlinPhPc:
.LFB3636:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movq	%rsi, %rbp
	leaq	.LC3(%rip), %rsi
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	movq	%rbp, %rdi
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L12
	movl	$2, %edi
	movq	%rbp, %rdx
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %edi
	call	exit@PLT
.L12:
	movq	%rax, %rbx
	movl	4+ip(%rip), %edx
	movl	64+ip(%rip), %eax
	movl	$1, %esi
	leaq	8+ip(%rip), %rdi
	imull	%edx, %eax
	movl	%edx, 30+ip(%rip)
	movl	$54, %edx
	leal	54(%rax), %ecx
	movl	%eax, 42+ip(%rip)
	movl	%ecx, 10+ip(%rip)
	movl	ip(%rip), %ecx
	movl	%ecx, 26+ip(%rip)
	movq	%rbx, %rcx
	call	fwrite@PLT
	movq	%rbx, %rcx
	movq	%r12, %rdi
	movl	$1, %esi
	movslq	4+ip(%rip), %rdx
	imulq	64+ip(%rip), %rdx
	call	fwrite@PLT
	movl	ip(%rip), %ecx
	movq	%rbp, %rdx
	xorl	%eax, %eax
	movslq	4+ip(%rip), %r9
	imulq	64+ip(%rip), %r9
	movl	$2, %edi
	leaq	.LC5(%rip), %rsi
	movl	4+ip(%rip), %r8d
	call	__printf_chk@PLT
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	jmp	fclose@PLT
	.cfi_endproc
.LFE3636:
	.size	_Z11WriteBMPlinPhPc, .-_Z11WriteBMPlinPhPc
	.globl	_Z34__device_stub__Z8ImShrunkPhS_jjjjjPhS_jjjjj
	.type	_Z34__device_stub__Z8ImShrunkPhS_jjjjjPhS_jjjjj, @function
_Z34__device_stub__Z8ImShrunkPhS_jjjjjPhS_jjjjj:
.LFB3662:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 24(%rsp)
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movl	%ecx, 8(%rsp)
	leaq	40(%rsp), %rcx
	movl	%r8d, 4(%rsp)
	movl	%r9d, (%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	4(%rsp), %rax
	movq	%rax, 128(%rsp)
	movq	%rsp, %rax
	movq	%rax, 136(%rsp)
	leaq	176(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L14
	pushq	40(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z8ImShrunkPhS_jjjjj(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 192
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L14:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L16
	call	__stack_chk_fail@PLT
.L16:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3662:
	.size	_Z34__device_stub__Z8ImShrunkPhS_jjjjjPhS_jjjjj, .-_Z34__device_stub__Z8ImShrunkPhS_jjjjjPhS_jjjjj
	.globl	_Z8ImShrunkPhS_jjjjj
	.type	_Z8ImShrunkPhS_jjjjj, @function
_Z8ImShrunkPhS_jjjjj:
.LFB3663:
	.cfi_startproc
	endbr64
	jmp	_Z34__device_stub__Z8ImShrunkPhS_jjjjjPhS_jjjjj
	.cfi_endproc
.LFE3663:
	.size	_Z8ImShrunkPhS_jjjjj, .-_Z8ImShrunkPhS_jjjjj
	.section	.rodata.str1.1
.LC6:
	.string	"\n\nUsage: imshrunk input output xshrink yshrink"
.LC7:
	.string	"Cannot allocate memory for the input image...\n"
.LC8:
	.string	"\nNo CUDA Device is available\n"
.LC9:
	.string	"cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?"
.LC10:
	.string	"%u %c"
.LC11:
	.string	"cudaMalloc failed! Can't allocate GPU memory"
.LC12:
	.string	"cudaMemcpy  CPU to GPU  failed!"
.LC13:
	.string	"\n\ncudaDeviceSynchronize returned error code %d after launching the kernel!\n"
.LC14:
	.string	"cudaMemcpy GPU to CPU  failed!"
.LC15:
	.string	"\n Program failed after cudaDeviceSynchronize()!"
.LC16:
	.string	"\n\n--------------------------------------------------------------------------\n"
.LC17:
	.string	"%s    ComputeCapab=%d.%d  [max %s blocks; %d thr/blk] \n"
.LC18:
	.string	"--------------------------------------------------------------------------\n"
.LC19:
	.string	"%s %s %s %u   [%u BLOCKS, %u BLOCKS/ROW]\n"
.LC22:
	.string	"CPU->GPU Transfer   =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n"
.LC23:
	.string	"Kernel Execution    =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n"
.LC24:
	.string	"GPU->CPU Transfer   =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n"
.LC25:
	.string	"Total time elapsed  =%7.2f ms       %4d MB  ...  %6.2f GB/s\n"
.LC26:
	.string	"--------------------------------------------------------------------------\n\n"
.LC27:
	.string	"cudaDeviceReset failed!"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3637:
	.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	$2024, %rsp
	.cfi_def_cfa_offset 2080
	movq	%fs:40, %rax
	movq	%rax, 2008(%rsp)
	movabsq	$7741253943733480809, %rax
	movb	$0, 1761(%rsp)
	movq	%rax, 1753(%rsp)
	cmpl	$5, %edi
	je	.L20
	leaq	.LC6(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L21
.L20:
	movq	24(%rsi), %rdi
	movq	%rsi, %rbx
	call	atoi@PLT
	movq	32(%rbx), %rdi
	movl	%eax, xshrink(%rip)
	call	atoi@PLT
	movq	8(%rbx), %rbp
	movl	$255, %edx
	movl	%eax, yshrink(%rip)
	leaq	1243(%rsp), %rax
	movq	%rbp, %rsi
	movq	%rax, %rdi
	movq	%rax, 8(%rsp)
	call	__strcpy_chk@PLT
	movq	16(%rbx), %rsi
	movl	$255, %edx
	leaq	1498(%rsp), %rax
	movq	%rax, %rdi
	movq	%rax, 16(%rsp)
	call	__strcpy_chk@PLT
	movq	%rbp, %rdi
	call	_Z10ReadBMPlinPc
	movq	%rax, TheImage(%rip)
	testq	%rax, %rax
	jne	.L22
.L23:
	leaq	.LC7(%rip), %rsi
	jmp	.L48
.L22:
	movl	4+ip(%rip), %edi
	movq	64+ip(%rip), %rdx
	movl	ip(%rip), %eax
	movl	%edx, origHbytes(%rip)
	imull	%edi, %edx
	movl	%eax, origHpixels(%rip)
	movl	%edi, origVpixels(%rip)
	movl	%edx, origSize(%rip)
	xorl	%edx, %edx
	divl	xshrink(%rip)
	xorl	%edx, %edx
	movl	%eax, ip(%rip)
	leal	1(%rax), %ecx
	movl	%edi, %eax
	divl	yshrink(%rip)
	leal	(%rcx,%rcx,2), %ecx
	andl	$-4, %ecx
	movslq	%ecx, %rcx
	movq	%rcx, 64+ip(%rip)
	movslq	%eax, %rdi
	movl	%edi, 4+ip(%rip)
	imulq	%rcx, %rdi
	call	malloc@PLT
	movq	%rax, NewImage(%rip)
	testq	%rax, %rax
	je	.L23
	xorl	%r11d, %r11d
	leaq	44(%rsp), %rdi
	movl	%r11d, 44(%rsp)
	call	cudaGetDeviceCount@PLT
	cmpl	$0, 44(%rsp)
	jne	.L24
	leaq	.LC8(%rip), %rsi
.L48:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L49:
	movl	$1, %edi
	call	exit@PLT
.L24:
	xorl	%edi, %edi
	call	cudaSetDevice@PLT
	leaq	.LC9(%rip), %rdx
	testl	%eax, %eax
	jne	.L50
	leaq	104(%rsp), %rax
	xorl	%esi, %esi
	movq	%rax, %rdi
	movq	%rax, 24(%rsp)
	call	cudaGetDeviceProperties_v2@PLT
	movl	440(%rsp), %eax
	imull	444(%rsp), %eax
	movl	$75, %r9d
	imull	448(%rsp), %eax
	shrl	$10, %eax
	movl	%eax, %r8d
	cmpl	$5119, %eax
	jbe	.L26
	shrq	$10, %r8
	movl	$77, %r9d
.L26:
	leaq	1143(%rsp), %r14
	movl	$100, %edx
	leaq	.LC10(%rip), %rcx
	xorl	%eax, %eax
	movl	$2, %esi
	movq	%r14, %rdi
	call	__sprintf_chk@PLT
	leaq	48(%rsp), %rdi
	movl	424(%rsp), %r15d
	call	cudaEventCreate@PLT
	leaq	56(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	64(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	72(%rsp), %rdi
	call	cudaEventCreate@PLT
	movq	48(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	origSize(%rip), %esi
	leaq	GPUSrcImage(%rip), %rdi
	call	cudaMalloc@PLT
	movslq	4+ip(%rip), %rsi
	imulq	64+ip(%rip), %rsi
	leaq	GPUTgtImage(%rip), %rdi
	movl	%eax, %ebp
	call	cudaMalloc@PLT
	leaq	.LC11(%rip), %rdx
	orl	%ebp, %eax
	jne	.L50
	movl	origSize(%rip), %edx
	movq	TheImage(%rip), %rsi
	movl	$1, %ecx
	movq	GPUSrcImage(%rip), %rdi
	call	cudaMemcpy@PLT
	leaq	.LC12(%rip), %rdx
	testl	%eax, %eax
	jne	.L50
	movq	56(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movl	$16777217, %edx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movl	ip(%rip), %eax
	movl	4+ip(%rip), %r13d
	salq	$8, %rdx
	movl	$1, %ecx
	movq	%rdx, 92(%rsp)
	leal	255(%rax), %ebp
	movl	$1, 100(%rsp)
	movabsq	$4294967297, %rax
	shrl	$8, %ebp
	movq	%rax, 84(%rsp)
	movl	88(%rsp), %esi
	imull	%ebp, %r13d
	movl	%r13d, 80(%rsp)
	movq	80(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L29
	movl	origHbytes(%rip), %eax
	movl	origHpixels(%rip), %r9d
	pushq	%r8
	.cfi_def_cfa_offset 2088
	movl	xshrink(%rip), %ecx
	movl	yshrink(%rip), %r8d
	movl	ip(%rip), %edx
	movq	GPUSrcImage(%rip), %rsi
	pushq	%rax
	.cfi_def_cfa_offset 2096
	movq	GPUTgtImage(%rip), %rdi
	call	_Z34__device_stub__Z8ImShrunkPhS_jjjjjPhS_jjjjj
	popq	%r9
	.cfi_def_cfa_offset 2088
	popq	%r10
	.cfi_def_cfa_offset 2080
.L29:
	movq	GPUTgtImage(%rip), %rax
	movq	%rax, GPUResult(%rip)
	movl	4+ip(%rip), %eax
	imull	64+ip(%rip), %eax
	addl	origSize(%rip), %eax
	movl	%eax, %r12d
	call	cudaDeviceSynchronize@PLT
	movl	%eax, %ecx
	testl	%eax, %eax
	je	.L30
	movq	stderr(%rip), %rdi
	leaq	.LC13(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L49
.L30:
	movq	64(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movslq	4+ip(%rip), %rdx
	movl	$2, %ecx
	imulq	64+ip(%rip), %rdx
	movq	GPUResult(%rip), %rsi
	movq	NewImage(%rip), %rdi
	call	cudaMemcpy@PLT
	testl	%eax, %eax
	je	.L31
	leaq	.LC14(%rip), %rdx
.L50:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	jmp	.L49
.L31:
	movq	72(%rsp), %rdi
	xorl	%esi, %esi
	call	cudaEventRecord@PLT
	movq	48(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	56(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	64(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdi
	call	cudaEventSynchronize@PLT
	movq	72(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	36(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	56(%rsp), %rdx
	movq	48(%rsp), %rsi
	leaq	40(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	64(%rsp), %rdx
	movq	56(%rsp), %rsi
	leaq	92(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movq	72(%rsp), %rdx
	movq	64(%rsp), %rsi
	leaq	80(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	call	cudaDeviceSynchronize@PLT
	leaq	.LC15(%rip), %rdx
	testl	%eax, %eax
	jne	.L51
	movq	16(%rbx), %rsi
	movq	NewImage(%rip), %rdi
	leaq	.LC18(%rip), %rbx
	call	_Z11WriteBMPlinPhPc
	leaq	.LC16(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	%r14, %r9
	movl	$2, %edi
	leaq	.LC17(%rip), %rsi
	pushq	%rax
	.cfi_def_cfa_offset 2088
	xorl	%eax, %eax
	pushq	%r15
	.cfi_def_cfa_offset 2096
	movl	484(%rsp), %r8d
	movl	480(%rsp), %ecx
	movq	40(%rsp), %rdx
	call	__printf_chk@PLT
	popq	%rdx
	.cfi_def_cfa_offset 2088
	movq	%rbx, %rsi
	popq	%rcx
	.cfi_def_cfa_offset 2080
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	leaq	1753(%rsp), %rdx
	pushq	%rbp
	.cfi_def_cfa_offset 2088
	movl	$2, %edi
	pushq	%r13
	.cfi_def_cfa_offset 2096
	movq	32(%rsp), %r8
	movl	$256, %r9d
	xorl	%eax, %eax
	movq	24(%rsp), %rcx
	leaq	.LC19(%rip), %rsi
	movl	%r12d, %ebp
	call	__printf_chk@PLT
	popq	%rsi
	.cfi_def_cfa_offset 2088
	xorl	%eax, %eax
	popq	%rdi
	.cfi_def_cfa_offset 2080
	movq	%rbx, %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	cvtss2sd	40(%rsp), %xmm2
	movsd	.LC20(%rip), %xmm0
	movl	origSize(%rip), %eax
	leaq	.LC22(%rip), %rsi
	movl	$2, %edi
	mulsd	%xmm2, %xmm0
	cvtsi2ssq	%rax, %xmm1
	movq	%rax, %rdx
	movb	$2, %al
	mulsd	.LC21(%rip), %xmm0
	shrl	$20, %edx
	mulsd	.LC21(%rip), %xmm0
	cvtss2sd	%xmm1, %xmm1
	divsd	%xmm0, %xmm1
	movaps	%xmm2, %xmm0
	call	__printf_chk@PLT
	cvtss2sd	92(%rsp), %xmm2
	cvtsi2ssq	%rbp, %xmm1
	movsd	.LC20(%rip), %xmm0
	movl	%r12d, %edx
	leaq	.LC23(%rip), %rsi
	movl	$2, %edi
	movb	$2, %al
	mulsd	%xmm2, %xmm0
	shrl	$20, %edx
	mulsd	.LC21(%rip), %xmm0
	mulsd	.LC21(%rip), %xmm0
	cvtss2sd	%xmm1, %xmm1
	divsd	%xmm0, %xmm1
	movaps	%xmm2, %xmm0
	call	__printf_chk@PLT
	movslq	4+ip(%rip), %rdx
	imulq	64+ip(%rip), %rdx
	cvtss2sd	80(%rsp), %xmm2
	testq	%rdx, %rdx
	js	.L37
	cvtsi2ssq	%rdx, %xmm1
	jmp	.L38
.L37:
	movq	%rdx, %rax
	movq	%rdx, %rcx
	shrq	%rax
	andl	$1, %ecx
	orq	%rcx, %rax
	cvtsi2ssq	%rax, %xmm1
	addss	%xmm1, %xmm1
.L38:
	cvtss2sd	%xmm1, %xmm1
	shrq	$20, %rdx
	movb	$2, %al
	movsd	.LC20(%rip), %xmm0
	leaq	.LC24(%rip), %rsi
	movl	$2, %edi
	mulsd	%xmm2, %xmm0
	mulsd	.LC21(%rip), %xmm0
	mulsd	.LC21(%rip), %xmm0
	divsd	%xmm0, %xmm1
	movaps	%xmm2, %xmm0
	call	__printf_chk@PLT
	movq	%rbx, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movslq	4+ip(%rip), %rdx
	imulq	64+ip(%rip), %rdx
	cvtss2sd	36(%rsp), %xmm2
	movl	origSize(%rip), %eax
	addq	%rax, %rdx
	addq	%rbp, %rdx
	js	.L39
	cvtsi2ssq	%rdx, %xmm1
	jmp	.L40
.L39:
	movq	%rdx, %rax
	movq	%rdx, %rcx
	shrq	%rax
	andl	$1, %ecx
	orq	%rcx, %rax
	cvtsi2ssq	%rax, %xmm1
	addss	%xmm1, %xmm1
.L40:
	cvtss2sd	%xmm1, %xmm1
	shrq	$20, %rdx
	movb	$2, %al
	movsd	.LC20(%rip), %xmm0
	leaq	.LC25(%rip), %rsi
	movl	$2, %edi
	mulsd	%xmm2, %xmm0
	mulsd	.LC21(%rip), %xmm0
	mulsd	.LC21(%rip), %xmm0
	divsd	%xmm0, %xmm1
	movaps	%xmm2, %xmm0
	call	__printf_chk@PLT
	leaq	.LC26(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movq	GPUSrcImage(%rip), %rdi
	call	cudaFree@PLT
	movq	GPUTgtImage(%rip), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	56(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	64(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	72(%rsp), %rdi
	call	cudaEventDestroy@PLT
	call	cudaDeviceReset@PLT
	testl	%eax, %eax
	je	.L41
	leaq	.LC27(%rip), %rdx
.L51:
	movq	stderr(%rip), %rdi
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	TheImage(%rip), %rdi
	call	free@PLT
	movq	NewImage(%rip), %rdi
	call	free@PLT
	jmp	.L49
.L41:
	movq	TheImage(%rip), %rdi
	call	free@PLT
	movq	NewImage(%rip), %rdi
	call	free@PLT
.L21:
	movq	2008(%rsp), %rax
	subq	%fs:40, %rax
	je	.L42
	call	__stack_chk_fail@PLT
.L42:
	addq	$2024, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3637:
	.size	main, .-main
	.section	.rodata.str1.1
.LC28:
	.string	"_Z8ImShrunkPhS_jjjjj"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3665:
	.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	.LC28(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z8ImShrunkPhS_jjjjj(%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
.LFE3665:
	.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
	.local	_ZZ10ReadBMPlinPcE3Img
	.comm	_ZZ10ReadBMPlinPcE3Img,8,8
	.globl	ip
	.bss
	.align 32
	.type	ip, @object
	.size	ip, 72
ip:
	.zero	72
	.globl	GPUResult
	.align 8
	.type	GPUResult, @object
	.size	GPUResult, 8
GPUResult:
	.zero	8
	.globl	GPUTgtImage
	.align 8
	.type	GPUTgtImage, @object
	.size	GPUTgtImage, 8
GPUTgtImage:
	.zero	8
	.globl	GPUSrcImage
	.align 8
	.type	GPUSrcImage, @object
	.size	GPUSrcImage, 8
GPUSrcImage:
	.zero	8
	.globl	NewImage
	.align 8
	.type	NewImage, @object
	.size	NewImage, 8
NewImage:
	.zero	8
	.globl	TheImage
	.align 8
	.type	TheImage, @object
	.size	TheImage, 8
TheImage:
	.zero	8
	.globl	origHbytes
	.align 4
	.type	origHbytes, @object
	.size	origHbytes, 4
origHbytes:
	.zero	4
	.globl	origHpixels
	.align 4
	.type	origHpixels, @object
	.size	origHpixels, 4
origHpixels:
	.zero	4
	.globl	origVpixels
	.align 4
	.type	origVpixels, @object
	.size	origVpixels, 4
origVpixels:
	.zero	4
	.globl	yshrink
	.align 4
	.type	yshrink, @object
	.size	yshrink, 4
yshrink:
	.zero	4
	.globl	xshrink
	.align 4
	.type	xshrink, @object
	.size	xshrink, 4
xshrink:
	.zero	4
	.globl	TgtSize
	.align 4
	.type	TgtSize, @object
	.size	TgtSize, 4
TgtSize:
	.zero	4
	.globl	origSize
	.align 4
	.type	origSize, @object
	.size	origSize, 4
origSize:
	.zero	4
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC20:
	.long	-755914244
	.long	1072718413
	.align 8
.LC21:
	.long	0
	.long	1083179008
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

#include <hip/hip_runtime.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <iostream>
#include <ctype.h>


#define DATAMB(bytes)			(bytes/1024/1024)
#define DATABW(bytes,timems)	((float)bytes/(timems * 1.024*1024.0*1024.0))

typedef unsigned char uch;
typedef unsigned long ul;
typedef unsigned int  ui;

ui origSize, TgtSize;
ui xshrink, yshrink;
ui origVpixels, origHpixels, origHbytes;

uch *TheImage, *NewImage;					// Where images are stored in CPU
uch *GPUSrcImage, *GPUTgtImage, *GPUResult;	// Where images are stored in GPU



struct ImgProp{
	int Hpixels;
	int Vpixels;
	uch HeaderInfo[54];
	ul Hbytes;
} ip;

#define	IPHB		ip.Hbytes
#define	IPH			ip.Hpixels
#define	IPV			ip.Vpixels
#define	IMAGESIZE	(IPHB*IPV)
#define	IMAGEPIX	(IPH*IPV)


// Read a 24-bit/pixel BMP file into a 1D linear array.
// Allocate memory to store the 1D image and return its pointer.
uch *ReadBMPlin(char* fn)
{
	static uch *Img;
	FILE* f = fopen(fn, "rb");
	if (f == NULL){	printf("\n\n%s NOT FOUND\n\n", fn);	exit(EXIT_FAILURE); }

	uch HeaderInfo[54];
	fread(HeaderInfo, sizeof(uch), 54, f); // read the 54-byte header
	// extract image height and width from header
	int width = *(int*)&HeaderInfo[18];			ip.Hpixels = width;
	int height = *(int*)&HeaderInfo[22];		ip.Vpixels = height;
	int RowBytes = (width * 3 + 3) & (~3);		ip.Hbytes = RowBytes;
	//save header for re-use
	memcpy(ip.HeaderInfo, HeaderInfo,54);
	printf("\n Input File name: %17s  (%u x %u)   File Size=%u", fn, 
			ip.Hpixels, ip.Vpixels, IMAGESIZE);
	// allocate memory to store the main image (1 Dimensional array)
	Img  = (uch *)malloc(IMAGESIZE);
	if (Img == NULL) return Img;      // Cannot allocate memory
	// read the image from disk
	fread(Img, sizeof(uch), IMAGESIZE, f);
	fclose(f);
	return Img;
}

void WriteBMPlin(uch *Img, char* fn)
{
	FILE* f = fopen(fn, "wb");
	if (f == NULL){ printf("\n\nFILE CREATION ERROR: %s\n\n", fn); exit(1); }
	//write header
	*(int*)&ip.HeaderInfo[2] = ip.Hbytes*ip.Vpixels+54;
	*(int*)&ip.HeaderInfo[18] = ip.Hpixels;
	*(int*)&ip.HeaderInfo[22] = ip.Vpixels;
	*(int*)&ip.HeaderInfo[34] = ip.Hbytes*ip.Vpixels;
	fwrite(ip.HeaderInfo, sizeof(uch), 54, f);
	//write data
	fwrite(Img, sizeof(uch), IMAGESIZE, f);
	printf("\nOutput File name: %17s  (%u x %u)   File Size=%u\n\n", fn, ip.Hpixels, ip.Vpixels, IMAGESIZE);
	fclose(f);
}
__global__
void ImShrunk(uch *TgtImg, uch *SrcImg, ui TgtHpixels, ui xshrink, ui yshrink, ui origHpixels, ui origHbytes)
{
	ui ThrPerBlk = blockDim.x;
	ui TgtBid = blockIdx.x;
	ui TgtTid = threadIdx.x;
	ui TgtGtid = ThrPerBlk * TgtBid + TgtTid;


	ui BlkPerRow = (TgtHpixels + ThrPerBlk - 1) / ThrPerBlk;  // ceil
	ui TgtRowBytes = (TgtHpixels * 3 + 3) & (~3);
	// ui SrcRowBytes = (origHpixels * 3 + 3) & (~3);
	ui SrcRowBytes = origHbytes;

	ui Tgtrow = TgtBid / BlkPerRow;
	ui TgtCol = TgtGtid - Tgtrow*BlkPerRow*ThrPerBlk;
	if(TgtCol >= TgtHpixels) return;
	
	ui SrcRow = Tgtrow * yshrink;
	ui SrcCol = TgtCol * xshrink;
	// if(SrcCol >= origHpixels) return;
	
	///////////////
	ui TgtOffset = Tgtrow * TgtRowBytes;
	ui SrcOffset = SrcRow * SrcRowBytes;

	ui TgtIndex = TgtOffset + 3*TgtCol;
	ui SrcIndex = SrcOffset + 3*SrcCol;

	TgtImg[TgtIndex] = SrcImg[SrcIndex];
	TgtImg[TgtIndex+1] = SrcImg[SrcIndex+1];
	TgtImg[TgtIndex+2] = SrcImg[SrcIndex+2];

}



int main(int argc, char** argv){
	
	float totalTime, tfrCPUtoGPU, tfrGPUtoCPU, kernelExecutionTime; // GPU code run times
	hipError_t cudaStatus, cudaStatus2;
	hipEvent_t time1, time2, time3, time4;
	char InputFileName[255], OutputFileName[255], ProgName[255];
	ui BlkPerRow, ThrPerBlk=256, NumBlocks, GPUDataTransfer;
	hipDeviceProp_t GPUprop;
	ul SupportedKBlocks, SupportedMBlocks, MaxThrPerBlk;		char SupportedBlocks[100];

	strcpy(ProgName, "imShrunk");
	if(argc!=5){
		printf("\n\nUsage: imshrunk input output xshrink yshrink");
		return 0;
	}

	xshrink = atoi(argv[3]);
	yshrink = atoi(argv[4]);
	strcpy(InputFileName, argv[1]);
	strcpy(OutputFileName, argv[2]);



	TheImage = ReadBMPlin(argv[1]);
	if (TheImage == NULL){
		printf("Cannot allocate memory for the input image...\n");
		exit(EXIT_FAILURE);
	}
	origHpixels = ip.Hpixels;
	origVpixels = ip.Vpixels;
	origHbytes = ip.Hbytes;
	origSize = origHbytes * origVpixels;

	ip.Hpixels = ip.Hpixels/xshrink;
	ip.Hbytes = (ip.Hpixels*3 + 3) & (~3);
	ip.Vpixels = ip.Vpixels/yshrink;
	// TgtSize = ip.Hbytes * ip.Vpixels;
	// printf("\n new Hpixels %u", ip.Hpixels);
	// printf("\n new Vpixels %u", ip.Vpixels);
	
	NewImage = (uch *)malloc(IMAGESIZE);
	if (NewImage == NULL){
		free(NewImage);
		printf("Cannot allocate memory for the input image...\n");
		exit(EXIT_FAILURE);
	}

		// Choose which GPU to run on, change this on a multi-GPU system.
		int NumGPUs = 0;
		hipGetDeviceCount(&NumGPUs);
		if (NumGPUs == 0){
			printf("\nNo CUDA Device is available\n");
			exit(EXIT_FAILURE);
		}
		cudaStatus = hipSetDevice(0);
		if (cudaStatus != hipSuccess) {
			fprintf(stderr, "hipSetDevice failed!  Do you have a CUDA-capable GPU installed?");
			exit(EXIT_FAILURE);
		}
		hipGetDeviceProperties(&GPUprop, 0);
		SupportedKBlocks = (ui)GPUprop.maxGridSize[0] * (ui)GPUprop.maxGridSize[1] * (ui)GPUprop.maxGridSize[2] / 1024;
		SupportedMBlocks = SupportedKBlocks / 1024;
		sprintf(SupportedBlocks, "%u %c", (SupportedMBlocks >= 5) ? SupportedMBlocks : SupportedKBlocks, (SupportedMBlocks >= 5) ? 'M' : 'K');
		MaxThrPerBlk = (ui)GPUprop.maxThreadsPerBlock;
	
		hipEventCreate(&time1);
		hipEventCreate(&time2);
		hipEventCreate(&time3);
		hipEventCreate(&time4);
	
		hipEventRecord(time1, 0);

		// allocate GPU buffer
		cudaStatus = hipMalloc((void**)&GPUSrcImage, origSize);
		cudaStatus2 = hipMalloc((void**)&GPUTgtImage, IMAGESIZE);
		if ((cudaStatus != hipSuccess) || (cudaStatus2 != hipSuccess)){
			fprintf(stderr, "hipMalloc failed! Can't allocate GPU memory");
			exit(EXIT_FAILURE);
		}

		// Copy input vectors from host memory to GPU buffers.
		cudaStatus = hipMemcpy(GPUSrcImage, TheImage, origSize, hipMemcpyHostToDevice);
		if (cudaStatus != hipSuccess) {
			fprintf(stderr, "hipMemcpy  CPU to GPU  failed!");
			exit(EXIT_FAILURE);
		}
		
		hipEventRecord(time2, 0);		// Time stamp after the CPU --> GPU tfr is done
		BlkPerRow = (IPH + ThrPerBlk -1 ) / ThrPerBlk;
		NumBlocks = IPV*BlkPerRow;
	
		ImShrunk <<< NumBlocks, ThrPerBlk>>> (GPUTgtImage, GPUSrcImage, IPH, xshrink, yshrink, origHpixels, origHbytes);


		GPUResult = GPUTgtImage;
		GPUDataTransfer = origSize + IMAGESIZE;

		// cudaDeviceSynchronize waits for the kernel to finish, and returns
		// any errors encountered during the launch.
		cudaStatus = hipDeviceSynchronize();
		if (cudaStatus != hipSuccess) {
			fprintf(stderr, "\n\nhipDeviceSynchronize returned error code %d after launching the kernel!\n", cudaStatus);
			exit(EXIT_FAILURE);
		}
		hipEventRecord(time3, 0);

			// Copy output (results) from GPU buffer to host (CPU) memory.
		cudaStatus = hipMemcpy(NewImage, GPUResult, IMAGESIZE, hipMemcpyDeviceToHost);
		if (cudaStatus != hipSuccess) {
			fprintf(stderr, "hipMemcpy GPU to CPU  failed!");
			exit(EXIT_FAILURE);
		}

		
		hipEventRecord(time4, 0);

		hipEventSynchronize(time1);
		hipEventSynchronize(time2);
		hipEventSynchronize(time3);
		hipEventSynchronize(time4);

		hipEventElapsedTime(&totalTime, time1, time4);
		hipEventElapsedTime(&tfrCPUtoGPU, time1, time2);
		hipEventElapsedTime(&kernelExecutionTime, time2, time3);
		hipEventElapsedTime(&tfrGPUtoCPU, time3, time4);

		cudaStatus = hipDeviceSynchronize();

		//checkError(cudaGetLastError());	// screen for errors in kernel launches
		if (cudaStatus != hipSuccess) {
			fprintf(stderr, "\n Program failed after cudaDeviceSynchronize()!");
			free(TheImage);
			free(NewImage);
			exit(EXIT_FAILURE);
		}
		WriteBMPlin(NewImage, argv[2]);		// Write the flipped image back to disk

		////////////////// change from here
		printf("\n\n--------------------------------------------------------------------------\n");
		printf("%s    ComputeCapab=%d.%d  [max %s blocks; %d thr/blk] \n", 
				GPUprop.name, GPUprop.major, GPUprop.minor, SupportedBlocks, MaxThrPerBlk);
		printf("--------------------------------------------------------------------------\n");
		printf("%s %s %s %u   [%u BLOCKS, %u BLOCKS/ROW]\n", ProgName, InputFileName, OutputFileName,
				ThrPerBlk, NumBlocks, BlkPerRow);
		printf("--------------------------------------------------------------------------\n");
		printf("CPU->GPU Transfer   =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n", tfrCPUtoGPU, DATAMB(origSize), DATABW(origSize, tfrCPUtoGPU));
		printf("Kernel Execution    =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n", kernelExecutionTime, DATAMB(GPUDataTransfer), DATABW(GPUDataTransfer, kernelExecutionTime));
		printf("GPU->CPU Transfer   =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n", tfrGPUtoCPU, DATAMB(IMAGESIZE), DATABW(IMAGESIZE, tfrGPUtoCPU));
		printf("--------------------------------------------------------------------------\n");
		printf("Total time elapsed  =%7.2f ms       %4d MB  ...  %6.2f GB/s\n", totalTime, DATAMB((origSize + IMAGESIZE + GPUDataTransfer)), DATABW((origSize + IMAGESIZE+ GPUDataTransfer), totalTime));
		printf("--------------------------------------------------------------------------\n\n");

			// Deallocate CPU, GPU memory and destroy events.
		hipFree(GPUSrcImage);
		hipFree(GPUTgtImage);
		hipEventDestroy(time1);
		hipEventDestroy(time2);
		hipEventDestroy(time3);
		hipEventDestroy(time4);
		// cudaDeviceReset must be called before exiting in order for profiling and
		// tracing tools such as Parallel Nsight and Visual Profiler to show complete traces.
		cudaStatus = hipDeviceReset();
		if (cudaStatus != hipSuccess) {
			fprintf(stderr, "hipDeviceReset failed!");
			free(TheImage);
			free(NewImage);
			exit(EXIT_FAILURE);
		}
		free(TheImage);
		free(NewImage);
		return(EXIT_SUCCESS);
}


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

                                        # End of file scope inline assembly
	.globl	_Z10ReadBMPlinPc                # -- Begin function _Z10ReadBMPlinPc
	.type	_Z10ReadBMPlinPc,@function
_Z10ReadBMPlinPc:                       # @_Z10ReadBMPlinPc
	.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
	subq	$64, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rdi, %r14
	movl	$.L.str, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB0_4
# %bb.1:
	movq	%rax, %rbx
	movq	%rsp, %r15
	movl	$1, %esi
	movl	$54, %edx
	movq	%r15, %rdi
	movq	%rax, %rcx
	callq	fread
	movslq	18(%r15), %rdx
	movl	%edx, ip(%rip)
	movslq	22(%r15), %rcx
	movl	%ecx, ip+4(%rip)
	leaq	(%rdx,%rdx,2), %r8
	addq	$3, %r8
	andq	$-4, %r8
	movq	%r8, ip+64(%rip)
	movaps	(%r15), %xmm0
	movaps	16(%r15), %xmm1
	movaps	32(%r15), %xmm2
	movups	%xmm0, ip+8(%rip)
	movups	%xmm1, ip+24(%rip)
	movups	%xmm2, ip+40(%rip)
	movq	46(%r15), %rax
	movq	%rax, ip+54(%rip)
	imulq	%rcx, %r8
	xorl	%r15d, %r15d
	movl	$.L.str.2, %edi
	movq	%r14, %rsi
                                        # kill: def $edx killed $edx killed $rdx
                                        # kill: def $ecx killed $ecx killed $rcx
	xorl	%eax, %eax
	callq	printf
	movslq	ip+4(%rip), %r14
	imulq	ip+64(%rip), %r14
	movq	%r14, %rdi
	callq	malloc
	movq	%rax, _ZZ10ReadBMPlinPcE3Img(%rip)
	testq	%rax, %rax
	je	.LBB0_3
# %bb.2:
	movl	$1, %esi
	movq	%rax, %rdi
	movq	%r14, %rdx
	movq	%rbx, %rcx
	callq	fread
	movq	%rbx, %rdi
	callq	fclose
	movq	_ZZ10ReadBMPlinPcE3Img(%rip), %r15
.LBB0_3:
	movq	%r15, %rax
	addq	$64, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB0_4:
	.cfi_def_cfa_offset 96
	movl	$.L.str.1, %edi
	movq	%r14, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end0:
	.size	_Z10ReadBMPlinPc, .Lfunc_end0-_Z10ReadBMPlinPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z11WriteBMPlinPhPc             # -- Begin function _Z11WriteBMPlinPhPc
	.type	_Z11WriteBMPlinPhPc,@function
_Z11WriteBMPlinPhPc:                    # @_Z11WriteBMPlinPhPc
	.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, %r15
	movl	$.L.str.3, %esi
	movq	%rbx, %rdi
	callq	fopen
	testq	%rax, %rax
	je	.LBB1_1
# %bb.2:
	movq	%rax, %r14
	movl	ip+4(%rip), %eax
	movl	ip+64(%rip), %ecx
	imull	%eax, %ecx
	leal	54(%rcx), %edx
	movl	%edx, ip+10(%rip)
	movl	ip(%rip), %edx
	movl	%edx, ip+26(%rip)
	movl	%eax, ip+30(%rip)
	movl	%ecx, ip+42(%rip)
	movl	$ip+8, %edi
	movl	$1, %esi
	movl	$54, %edx
	movq	%r14, %rcx
	callq	fwrite
	movslq	ip+4(%rip), %rdx
	imulq	ip+64(%rip), %rdx
	movl	$1, %esi
	movq	%r15, %rdi
	movq	%r14, %rcx
	callq	fwrite
	movl	ip(%rip), %edx
	movslq	ip+4(%rip), %rcx
	movq	%rcx, %r8
	imulq	ip+64(%rip), %r8
	movl	$.L.str.5, %edi
	movq	%rbx, %rsi
                                        # kill: def $ecx killed $ecx killed $rcx
	xorl	%eax, %eax
	callq	printf
	movq	%r14, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	jmp	fclose                          # TAILCALL
.LBB1_1:
	.cfi_def_cfa_offset 32
	movl	$.L.str.4, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movl	$1, %edi
	callq	exit
.Lfunc_end1:
	.size	_Z11WriteBMPlinPhPc, .Lfunc_end1-_Z11WriteBMPlinPhPc
	.cfi_endproc
                                        # -- End function
	.globl	_Z23__device_stub__ImShrunkPhS_jjjjj # -- Begin function _Z23__device_stub__ImShrunkPhS_jjjjj
	.type	_Z23__device_stub__ImShrunkPhS_jjjjj,@function
_Z23__device_stub__ImShrunkPhS_jjjjj:   # @_Z23__device_stub__ImShrunkPhS_jjjjj
	.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	$144, %rsp
	.cfi_def_cfa_offset 192
	.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	4(%rsp), %rcx
	movl	%r8d, (%rcx)
	movq	%rsp, %r8
	movl	%r9d, (%r8)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	movq	%r8, 40(%rbx)
	leaq	192(%rsp), %rax
	movq	%rax, 48(%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	$_Z8ImShrunkPhS_jjjjj, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$160, %rsp
	.cfi_adjust_cfa_offset -160
	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	_Z23__device_stub__ImShrunkPhS_jjjjj, .Lfunc_end2-_Z23__device_stub__ImShrunkPhS_jjjjj
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI3_0:
	.quad	0x3ff0624dd2f1a9fc              # double 1.024
.LCPI3_1:
	.quad	0x4090000000000000              # double 1024
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$2416, %rsp                     # imm = 0x970
	.cfi_def_cfa_offset 2464
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movabsq	$7741253943733480809, %rax      # imm = 0x6B6E757268536D69
	movq	%rax, 176(%rsp)
	movb	$0, 184(%rsp)
	cmpl	$5, %edi
	jne	.LBB3_14
# %bb.1:
	movq	%rsi, %rbx
	movq	24(%rsi), %rdi
	callq	atoi
	movl	%eax, xshrink(%rip)
	movq	32(%rbx), %rdi
	callq	atoi
	movl	%eax, yshrink(%rip)
	movq	8(%rbx), %r14
	leaq	688(%rsp), %rdi
	movq	%r14, %rsi
	callq	strcpy
	movq	16(%rbx), %rsi
	leaq	432(%rsp), %rdi
	callq	strcpy
	movq	%r14, %rdi
	callq	_Z10ReadBMPlinPc
	movq	%rax, TheImage(%rip)
	testq	%rax, %rax
	je	.LBB3_22
# %bb.2:
	movl	ip(%rip), %eax
	movl	%eax, origHpixels(%rip)
	movl	ip+4(%rip), %ecx
	movl	%ecx, origVpixels(%rip)
	movl	ip+64(%rip), %edx
	movl	%edx, origHbytes(%rip)
	imull	%ecx, %edx
	movl	%edx, origSize(%rip)
	xorl	%edx, %edx
	divl	xshrink(%rip)
                                        # kill: def $eax killed $eax def $rax
	movl	%eax, ip(%rip)
	leal	(%rax,%rax,2), %eax
	addl	$3, %eax
	andl	$-4, %eax
	movslq	%eax, %rsi
	movq	%rsi, ip+64(%rip)
	movl	%ecx, %eax
	xorl	%edx, %edx
	divl	yshrink(%rip)
	movl	%eax, ip+4(%rip)
	movslq	%eax, %rdi
	imulq	%rsi, %rdi
	callq	malloc
	movq	%rax, NewImage(%rip)
	testq	%rax, %rax
	je	.LBB3_22
# %bb.3:
	leaq	60(%rsp), %r14
	movl	$0, (%r14)
	movq	%r14, %rdi
	callq	hipGetDeviceCount
	cmpl	$0, (%r14)
	je	.LBB3_23
# %bb.4:
	xorl	%edi, %edi
	callq	hipSetDevice
	testl	%eax, %eax
	jne	.LBB3_26
# %bb.5:
	leaq	944(%rsp), %r14
	movq	%r14, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movl	340(%r14), %eax
	imull	336(%r14), %eax
	imull	344(%r14), %eax
	movl	%eax, %ecx
	shrl	$10, %ecx
	movl	%eax, %edx
	shrl	$20, %edx
	xorl	%esi, %esi
	cmpl	$5242880, %eax                  # imm = 0x500000
	setae	%sil
	cmovbq	%rcx, %rdx
	leal	75(,%rsi,2), %ecx
	leaq	64(%rsp), %rdi
	movl	$.L.str.11, %esi
	xorl	%eax, %eax
	callq	sprintf
	movl	320(%r14), %r14d
	leaq	32(%rsp), %r15
	movq	%r15, %rdi
	callq	hipEventCreate
	leaq	24(%rsp), %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %rdi
	callq	hipEventCreate
	movq	(%r15), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	origSize(%rip), %esi
	movl	$GPUSrcImage, %edi
	callq	hipMalloc
	movl	%eax, %ebp
	movslq	ip+4(%rip), %rsi
	imulq	ip+64(%rip), %rsi
	movl	$GPUTgtImage, %edi
	callq	hipMalloc
	orl	%ebp, %eax
	jne	.LBB3_27
# %bb.6:
	movq	GPUSrcImage(%rip), %rdi
	movq	TheImage(%rip), %rsi
	movl	origSize(%rip), %edx
	movl	$1, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_28
# %bb.7:
	movq	24(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movl	$255, %r12d
	addl	ip(%rip), %r12d
	shrl	$8, %r12d
	movl	ip+4(%rip), %r15d
	imull	%r12d, %r15d
	movq	%r15, %rdi
	btsq	$32, %rdi
	movabsq	$4294967296, %rdx               # imm = 0x100000000
	orq	$256, %rdx                      # imm = 0x100
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB3_9
# %bb.8:
	movq	GPUTgtImage(%rip), %rdi
	movq	GPUSrcImage(%rip), %rsi
	movl	ip(%rip), %edx
	movl	xshrink(%rip), %ecx
	movl	yshrink(%rip), %r8d
	movl	origHpixels(%rip), %r9d
	movl	origHbytes(%rip), %eax
	movl	%eax, (%rsp)
	callq	_Z23__device_stub__ImShrunkPhS_jjjjj
.LBB3_9:
	movq	GPUTgtImage(%rip), %rax
	movq	%rax, GPUResult(%rip)
	movl	ip+4(%rip), %ebp
	imull	ip+64(%rip), %ebp
	addl	origSize(%rip), %ebp
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB3_29
# %bb.10:
	movq	16(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	NewImage(%rip), %rdi
	movq	GPUResult(%rip), %rsi
	movslq	ip+4(%rip), %rdx
	imulq	ip+64(%rip), %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	testl	%eax, %eax
	jne	.LBB3_30
# %bb.11:
	movq	8(%rsp), %rdi
	xorl	%esi, %esi
	callq	hipEventRecord
	movq	32(%rsp), %rdi
	callq	hipEventSynchronize
	movq	24(%rsp), %rdi
	callq	hipEventSynchronize
	movq	16(%rsp), %rdi
	callq	hipEventSynchronize
	movq	8(%rsp), %rdi
	callq	hipEventSynchronize
	movq	32(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	56(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	32(%rsp), %rsi
	movq	24(%rsp), %rdx
	leaq	52(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdx
	leaq	44(%rsp), %rdi
	callq	hipEventElapsedTime
	movq	16(%rsp), %rsi
	movq	8(%rsp), %rdx
	leaq	48(%rsp), %rdi
	callq	hipEventElapsedTime
	callq	hipDeviceSynchronize
	testl	%eax, %eax
	jne	.LBB3_32
# %bb.12:
	movq	NewImage(%rip), %rdi
	movq	16(%rbx), %rsi
	callq	_Z11WriteBMPlinPhPc
	movl	$.Lstr, %edi
	callq	puts@PLT
	leaq	944(%rsp), %rsi
	movl	360(%rsi), %edx
	movl	364(%rsi), %ecx
	leaq	64(%rsp), %r8
	movl	$.L.str.18, %edi
	movq	%r14, %r9
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	%r12d, (%rsp)
	leaq	176(%rsp), %rsi
	leaq	688(%rsp), %rdx
	leaq	432(%rsp), %rcx
	movl	$.L.str.20, %edi
	movl	$256, %r8d                      # imm = 0x100
	movl	%r15d, %r9d
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	movl	origSize(%rip), %eax
	cvtsi2ss	%rax, %xmm1
	cvtss2sd	52(%rsp), %xmm0
	movl	%eax, %esi
	shrl	$20, %esi
	cvtss2sd	%xmm1, %xmm1
	movaps	%xmm0, %xmm2
	mulsd	.LCPI3_0(%rip), %xmm2
	movsd	.LCPI3_1(%rip), %xmm3           # xmm3 = mem[0],zero
	mulsd	%xmm3, %xmm2
	mulsd	%xmm3, %xmm2
	divsd	%xmm2, %xmm1
	movl	$.L.str.21, %edi
	movb	$2, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	44(%rsp), %xmm0
	movl	%ebp, %ebx
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rbx, %xmm1
	shrl	$20, %ebp
	cvtss2sd	%xmm1, %xmm1
	movaps	%xmm0, %xmm2
	mulsd	.LCPI3_0(%rip), %xmm2
	movsd	.LCPI3_1(%rip), %xmm3           # xmm3 = mem[0],zero
	mulsd	%xmm3, %xmm2
	mulsd	%xmm3, %xmm2
	divsd	%xmm2, %xmm1
	movl	$.L.str.22, %edi
	movl	%ebp, %esi
	movb	$2, %al
	callq	printf
	xorps	%xmm0, %xmm0
	cvtss2sd	48(%rsp), %xmm0
	movslq	ip+4(%rip), %rax
	imulq	ip+64(%rip), %rax
	movq	%rax, %rsi
	shrq	$20, %rsi
	testq	%rax, %rax
	js	.LBB3_15
# %bb.13:
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rax, %xmm1
	jmp	.LBB3_16
.LBB3_14:
	movl	$.L.str.7, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB3_21
.LBB3_15:
	movq	%rax, %rcx
	shrq	%rcx
	andl	$1, %eax
	orq	%rcx, %rax
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rax, %xmm1
	addss	%xmm1, %xmm1
.LBB3_16:
	cvtss2sd	%xmm1, %xmm1
	movaps	%xmm0, %xmm2
	mulsd	.LCPI3_0(%rip), %xmm2
	movsd	.LCPI3_1(%rip), %xmm3           # xmm3 = mem[0],zero
	mulsd	%xmm3, %xmm2
	mulsd	%xmm3, %xmm2
	divsd	%xmm2, %xmm1
	movl	$.L.str.23, %edi
	movb	$2, %al
	callq	printf
	movl	$.Lstr.3, %edi
	callq	puts@PLT
	xorps	%xmm0, %xmm0
	cvtss2sd	56(%rsp), %xmm0
	movl	origSize(%rip), %ecx
	movslq	ip+4(%rip), %rax
	imulq	ip+64(%rip), %rax
	addq	%rcx, %rax
	addq	%rbx, %rax
	movq	%rax, %rsi
	shrq	$20, %rsi
	testq	%rax, %rax
	js	.LBB3_18
# %bb.17:
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rax, %xmm1
	jmp	.LBB3_19
.LBB3_18:
	movq	%rax, %rcx
	shrq	%rcx
	andl	$1, %eax
	orq	%rcx, %rax
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rax, %xmm1
	addss	%xmm1, %xmm1
.LBB3_19:
	cvtss2sd	%xmm1, %xmm1
	movsd	.LCPI3_0(%rip), %xmm2           # xmm2 = mem[0],zero
	mulsd	%xmm0, %xmm2
	movsd	.LCPI3_1(%rip), %xmm3           # xmm3 = mem[0],zero
	mulsd	%xmm3, %xmm2
	mulsd	%xmm3, %xmm2
	divsd	%xmm2, %xmm1
	movl	$.L.str.24, %edi
	movb	$2, %al
	callq	printf
	movl	$.Lstr.4, %edi
	callq	puts@PLT
	movq	GPUSrcImage(%rip), %rdi
	callq	hipFree
	movq	GPUTgtImage(%rip), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	movq	24(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipEventDestroy
	callq	hipDeviceReset
	testl	%eax, %eax
	jne	.LBB3_33
# %bb.20:
	movq	TheImage(%rip), %rdi
	callq	free
	movq	NewImage(%rip), %rdi
	callq	free
.LBB3_21:
	xorl	%eax, %eax
	addq	$2416, %rsp                     # imm = 0x970
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.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_22:
	.cfi_def_cfa_offset 2464
	movl	$.Lstr.7, %edi
	jmp	.LBB3_24
.LBB3_23:
	movl	$.Lstr.5, %edi
.LBB3_24:
	callq	puts@PLT
	jmp	.LBB3_25
.LBB3_26:
	movq	stderr(%rip), %rcx
	movl	$.L.str.10, %edi
	movl	$63, %esi
	jmp	.LBB3_31
.LBB3_27:
	movq	stderr(%rip), %rcx
	movl	$.L.str.12, %edi
	movl	$43, %esi
	jmp	.LBB3_31
.LBB3_28:
	movq	stderr(%rip), %rcx
	movl	$.L.str.13, %edi
	movl	$30, %esi
	jmp	.LBB3_31
.LBB3_29:
	movq	stderr(%rip), %rdi
	movl	$.L.str.14, %esi
	movl	%eax, %edx
	xorl	%eax, %eax
	callq	fprintf
	jmp	.LBB3_25
.LBB3_30:
	movq	stderr(%rip), %rcx
	movl	$.L.str.15, %edi
	movl	$29, %esi
.LBB3_31:
	movl	$1, %edx
	callq	fwrite
	jmp	.LBB3_25
.LBB3_32:
	movq	stderr(%rip), %rcx
	movl	$.L.str.16, %edi
	movl	$47, %esi
	jmp	.LBB3_34
.LBB3_33:
	movq	stderr(%rip), %rcx
	movl	$.L.str.26, %edi
	movl	$22, %esi
.LBB3_34:
	movl	$1, %edx
	callq	fwrite
	movq	TheImage(%rip), %rdi
	callq	free
	movq	NewImage(%rip), %rdi
	callq	free
.LBB3_25:
	movl	$1, %edi
	callq	exit
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB4_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB4_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z8ImShrunkPhS_jjjjj, %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_end4:
	.size	__hip_module_ctor, .Lfunc_end4-__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	.LBB5_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
.LBB5_2:
	retq
.Lfunc_end5:
	.size	__hip_module_dtor, .Lfunc_end5-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	origSize,@object                # @origSize
	.bss
	.globl	origSize
	.p2align	2, 0x0
origSize:
	.long	0                               # 0x0
	.size	origSize, 4

	.type	TgtSize,@object                 # @TgtSize
	.globl	TgtSize
	.p2align	2, 0x0
TgtSize:
	.long	0                               # 0x0
	.size	TgtSize, 4

	.type	xshrink,@object                 # @xshrink
	.globl	xshrink
	.p2align	2, 0x0
xshrink:
	.long	0                               # 0x0
	.size	xshrink, 4

	.type	yshrink,@object                 # @yshrink
	.globl	yshrink
	.p2align	2, 0x0
yshrink:
	.long	0                               # 0x0
	.size	yshrink, 4

	.type	origVpixels,@object             # @origVpixels
	.globl	origVpixels
	.p2align	2, 0x0
origVpixels:
	.long	0                               # 0x0
	.size	origVpixels, 4

	.type	origHpixels,@object             # @origHpixels
	.globl	origHpixels
	.p2align	2, 0x0
origHpixels:
	.long	0                               # 0x0
	.size	origHpixels, 4

	.type	origHbytes,@object              # @origHbytes
	.globl	origHbytes
	.p2align	2, 0x0
origHbytes:
	.long	0                               # 0x0
	.size	origHbytes, 4

	.type	TheImage,@object                # @TheImage
	.globl	TheImage
	.p2align	3, 0x0
TheImage:
	.quad	0
	.size	TheImage, 8

	.type	NewImage,@object                # @NewImage
	.globl	NewImage
	.p2align	3, 0x0
NewImage:
	.quad	0
	.size	NewImage, 8

	.type	GPUSrcImage,@object             # @GPUSrcImage
	.globl	GPUSrcImage
	.p2align	3, 0x0
GPUSrcImage:
	.quad	0
	.size	GPUSrcImage, 8

	.type	GPUTgtImage,@object             # @GPUTgtImage
	.globl	GPUTgtImage
	.p2align	3, 0x0
GPUTgtImage:
	.quad	0
	.size	GPUTgtImage, 8

	.type	GPUResult,@object               # @GPUResult
	.globl	GPUResult
	.p2align	3, 0x0
GPUResult:
	.quad	0
	.size	GPUResult, 8

	.type	ip,@object                      # @ip
	.globl	ip
	.p2align	3, 0x0
ip:
	.zero	72
	.size	ip, 72

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

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"\n\n%s NOT FOUND\n\n"
	.size	.L.str.1, 17

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"\n Input File name: %17s  (%u x %u)   File Size=%u"
	.size	.L.str.2, 50

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

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"\n\nFILE CREATION ERROR: %s\n\n"
	.size	.L.str.4, 28

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"\nOutput File name: %17s  (%u x %u)   File Size=%u\n\n"
	.size	.L.str.5, 52

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

	.type	.L.str.6,@object                # @.str.6
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.6:
	.asciz	"imShrunk"
	.size	.L.str.6, 9

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"\n\nUsage: imshrunk input output xshrink yshrink"
	.size	.L.str.7, 47

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"hipSetDevice failed!  Do you have a CUDA-capable GPU installed?"
	.size	.L.str.10, 64

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

	.type	.L.str.12,@object               # @.str.12
.L.str.12:
	.asciz	"hipMalloc failed! Can't allocate GPU memory"
	.size	.L.str.12, 44

	.type	.L.str.13,@object               # @.str.13
.L.str.13:
	.asciz	"hipMemcpy  CPU to GPU  failed!"
	.size	.L.str.13, 31

	.type	.L.str.14,@object               # @.str.14
.L.str.14:
	.asciz	"\n\nhipDeviceSynchronize returned error code %d after launching the kernel!\n"
	.size	.L.str.14, 75

	.type	.L.str.15,@object               # @.str.15
.L.str.15:
	.asciz	"hipMemcpy GPU to CPU  failed!"
	.size	.L.str.15, 30

	.type	.L.str.16,@object               # @.str.16
.L.str.16:
	.asciz	"\n Program failed after cudaDeviceSynchronize()!"
	.size	.L.str.16, 48

	.type	.L.str.18,@object               # @.str.18
.L.str.18:
	.asciz	"%s    ComputeCapab=%d.%d  [max %s blocks; %d thr/blk] \n"
	.size	.L.str.18, 56

	.type	.L.str.20,@object               # @.str.20
.L.str.20:
	.asciz	"%s %s %s %u   [%u BLOCKS, %u BLOCKS/ROW]\n"
	.size	.L.str.20, 42

	.type	.L.str.21,@object               # @.str.21
.L.str.21:
	.asciz	"CPU->GPU Transfer   =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n"
	.size	.L.str.21, 61

	.type	.L.str.22,@object               # @.str.22
.L.str.22:
	.asciz	"Kernel Execution    =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n"
	.size	.L.str.22, 61

	.type	.L.str.23,@object               # @.str.23
.L.str.23:
	.asciz	"GPU->CPU Transfer   =%7.2f ms  ...  %4d MB  ...  %6.2f GB/s\n"
	.size	.L.str.23, 61

	.type	.L.str.24,@object               # @.str.24
.L.str.24:
	.asciz	"Total time elapsed  =%7.2f ms       %4d MB  ...  %6.2f GB/s\n"
	.size	.L.str.24, 61

	.type	.L.str.26,@object               # @.str.26
.L.str.26:
	.asciz	"hipDeviceReset failed!"
	.size	.L.str.26, 23

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

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

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

	.type	.Lstr,@object                   # @str
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr:
	.asciz	"\n\n--------------------------------------------------------------------------"
	.size	.Lstr, 77

	.type	.Lstr.3,@object                 # @str.3
.Lstr.3:
	.asciz	"--------------------------------------------------------------------------"
	.size	.Lstr.3, 75

	.type	.Lstr.4,@object                 # @str.4
.Lstr.4:
	.asciz	"--------------------------------------------------------------------------\n"
	.size	.Lstr.4, 76

	.type	.Lstr.5,@object                 # @str.5
.Lstr.5:
	.asciz	"\nNo CUDA Device is available"
	.size	.Lstr.5, 29

	.type	.Lstr.7,@object                 # @str.7
.Lstr.7:
	.asciz	"Cannot allocate memory for the input image..."
	.size	.Lstr.7, 46

	.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 _Z23__device_stub__ImShrunkPhS_jjjjj
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym GPUSrcImage
	.addrsig_sym GPUTgtImage
	.addrsig_sym ip
	.addrsig_sym _Z8ImShrunkPhS_jjjjj
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z8ImShrunkPhS_jjjjj    ; -- Begin function _Z8ImShrunkPhS_jjjjj
	.globl	_Z8ImShrunkPhS_jjjjj
	.p2align	8
	.type	_Z8ImShrunkPhS_jjjjj,@function
_Z8ImShrunkPhS_jjjjj:                   ; @_Z8ImShrunkPhS_jjjjj
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x34
	s_load_b128 s[4:7], s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s3, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	v_cvt_f32_u32_e32 v1, s3
	s_sub_i32 s7, 0, s3
	s_add_i32 s8, s4, s3
	s_add_i32 s8, s8, -1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	v_cvt_u32_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_readfirstlane_b32 s2, v1
	s_mul_i32 s7, s7, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_hi_u32 s7, s2, s7
	s_add_i32 s2, s2, s7
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_hi_u32 s2, s8, s2
	s_mul_i32 s7, s2, s3
	s_delay_alu instid0(SALU_CYCLE_1)
	s_sub_i32 s7, s8, s7
	s_add_i32 s8, s2, 1
	s_sub_i32 s9, s7, s3
	s_cmp_ge_u32 s7, s3
	s_cselect_b32 s2, s8, s2
	s_cselect_b32 s7, s9, s7
	s_add_i32 s8, s2, 1
	s_cmp_ge_u32 s7, s3
	s_cselect_b32 s7, s8, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_cvt_f32_u32_e32 v1, s7
	s_sub_i32 s8, 0, s7
	v_rcp_iflag_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x4f7ffffe, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_u32_f32_e32 v1, v1
	v_readfirstlane_b32 s2, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_i32 s8, s8, s2
	s_mul_hi_u32 s8, s2, s8
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s2, s2, s8
	s_mul_hi_u32 s2, s15, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_2) | instid1(SALU_CYCLE_1)
	s_mul_i32 s8, s2, s7
	s_add_i32 s9, s2, 1
	s_sub_i32 s8, s15, s8
	s_sub_i32 s10, s8, s7
	s_cmp_ge_u32 s8, s7
	s_cselect_b32 s2, s9, s2
	s_cselect_b32 s8, s10, s8
	s_add_i32 s9, s2, 1
	s_cmp_ge_u32 s8, s7
	s_cselect_b32 s2, s9, s2
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_mul_i32 s7, s2, s7
	s_sub_i32 s7, s15, s7
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s7, s3, v[0:1]
	s_mov_b32 s3, exec_lo
	v_cmpx_gt_u32_e64 s4, v1
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_clause 0x1
	s_load_b32 s3, s[0:1], 0x20
	s_load_b128 s[8:11], s[0:1], 0x0
	v_lshl_add_u32 v0, v1, 1, v1
	s_waitcnt lgkmcnt(0)
	s_mul_i32 s0, s3, s6
	s_delay_alu instid0(SALU_CYCLE_1)
	s_mul_i32 s0, s0, s2
	s_delay_alu instid0(VALU_DEP_1) | instid1(SALU_CYCLE_1)
	v_mad_u64_u32 v[1:2], null, v0, s5, s[0:1]
	s_mul_i32 s0, s4, 3
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(NEXT) | instid1(SALU_CYCLE_1)
	s_add_i32 s0, s0, 3
	s_and_b32 s0, s0, -4
	global_load_u8 v4, v1, s[10:11]
	v_mad_u64_u32 v[2:3], null, s2, s0, v[0:1]
	v_add_nc_u32_e32 v0, 1, v1
	v_add_nc_u32_e32 v1, 2, v1
	s_delay_alu instid0(VALU_DEP_3)
	v_add_nc_u32_e32 v3, 1, v2
	s_waitcnt vmcnt(0)
	global_store_b8 v2, v4, s[8:9]
	global_load_u8 v0, v0, s[10:11]
	s_waitcnt vmcnt(0)
	global_store_b8 v3, v0, s[8:9]
	global_load_u8 v0, v1, s[10:11]
	v_add_nc_u32_e32 v1, 2, v2
	s_waitcnt vmcnt(0)
	global_store_b8 v1, v0, s[8:9]
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z8ImShrunkPhS_jjjjj
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 296
		.amdhsa_user_sgpr_count 15
		.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 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 5
		.amdhsa_next_free_sgpr 16
		.amdhsa_reserve_vcc 0
		.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	_Z8ImShrunkPhS_jjjjj, .Lfunc_end0-_Z8ImShrunkPhS_jjjjj
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 472
; NumSgprs: 16
; NumVgprs: 5
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 1
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 16
; NumVGPRsForWavesPerEU: 5
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.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:     by_value
      - .offset:         28
        .size:           4
        .value_kind:     by_value
      - .offset:         32
        .size:           4
        .value_kind:     by_value
      - .offset:         40
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         44
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         48
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         52
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         54
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         56
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         58
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         60
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         62
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         88
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         96
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         104
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 296
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z8ImShrunkPhS_jjjjj
    .private_segment_fixed_size: 0
    .sgpr_count:     16
    .sgpr_spill_count: 0
    .symbol:         _Z8ImShrunkPhS_jjjjj.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
