#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>

#include <cuda_runtime.h>


const size_t BMP_HEADER_SIZE = 54;

const size_t BMP_HEADER_WIDTH_OFFSET = 18;
const size_t BMP_HEADER_HEIGHT_OFFSET = 22;

const size_t PIXEL_REAL_SIZE = 3;

size_t getThreadsPerBlock(int deviceNum)
{
	cudaDeviceProp properties;
	cudaGetDeviceProperties(&properties, deviceNum);

	return (size_t)properties.maxThreadsPerBlock;
}

size_t getWidth(const unsigned char * header)
{
	const unsigned char * start_position = header + BMP_HEADER_WIDTH_OFFSET;
	unsigned int value = *((const unsigned int*)start_position);
	return (size_t)value;
}

size_t getHeight(const unsigned char * header)
{
	const unsigned char * start_position = header + BMP_HEADER_HEIGHT_OFFSET;
	unsigned int value = *((const unsigned int*)start_position);
	return (size_t)value;
}

struct RgbPixel
{
	unsigned char b;
	unsigned char g;
	unsigned char r;
};

struct RgbPixel * allocateMemory(size_t size)
{
	struct RgbPixel * data = (struct RgbPixel *)malloc(sizeof(struct RgbPixel) * size);
	return data;
}

struct RgbPixel * cudaAllocateMemory(size_t size)
{
	struct RgbPixel * data = NULL;
	cudaError_t cudaError = cudaMalloc((void **)&data, sizeof(struct RgbPixel) * size);
	return cudaError == cudaSuccess ? data : NULL;
}

void deallocateMemory(struct RgbPixel ** data)
{
	free(*data);
	*data = NULL;
}

void cudaDeallocateMemory(struct RgbPixel ** data)
{
	cudaFree(*data);
	*data = NULL;
}

bool readBmp(
	const char * filename,
	unsigned char * header,
	struct RgbPixel ** bmp
)
{
	size_t read;

	FILE * fileBmp = fopen(filename, "rb");
	if (!fileBmp)
		return false;

	read = fread(header, sizeof(unsigned char), BMP_HEADER_SIZE, fileBmp);
	if (read != BMP_HEADER_SIZE)
	{
		fclose(fileBmp);
		return false;
	}

	size_t width = getWidth(header), height = getHeight(header);
	size_t size = width * height;

	*bmp = allocateMemory(size);
	for (size_t i = 0; i < size; i++)
	{
		read = fread((*bmp) + i, sizeof(unsigned char), PIXEL_REAL_SIZE, fileBmp);
		if (read != PIXEL_REAL_SIZE)
		{
			deallocateMemory(bmp);
			fclose(fileBmp);
			return false;
		}
	}

	fclose(fileBmp);
	return true;
}

bool writeBmp(
	const char * filename,
	const unsigned char * header,
	struct RgbPixel * bmp
)
{
	FILE * bmpFile = fopen(filename, "wb");
	if (!bmpFile)
	{
		fclose(bmpFile);
		return false;
	}

	fwrite(header, sizeof(unsigned char), BMP_HEADER_SIZE, bmpFile);
	size_t width = getWidth(header), height = getHeight(header);
	size_t size = width * height;

	for (size_t i = 0; i < size; i++)
		fwrite(bmp + i, sizeof(unsigned char), PIXEL_REAL_SIZE, bmpFile);

	fclose(bmpFile);
	return true;
}

__global__
void processImageSmoothFilter(struct RgbPixel * bmp, size_t width, size_t height, size_t radius, struct RgbPixel * result)
{
#define ROUND(x) (unsigned char)((x) + 0.5)
	size_t index = blockDim.x * blockIdx.x + threadIdx.x;
	size_t i = index / width, j = index % width;

	unsigned int r = 0, g = 0, b = 0;

	size_t
		starti = (i < radius ? 0 : i - radius),
		endi = (i + radius >= height ? height - 1 : i + radius),
		startj = (j < radius ? 0 : j - radius),
		endj = (j + radius >= width ? width - 1 : j + radius);

	size_t count = (endi - starti + 1) * (endj - startj + 1);
	for (size_t ii = starti; ii <= endi; ii++)
		for (size_t jj = startj; jj <= endj; jj++)
		{
			size_t position = ii * width + jj;
			r += bmp[position].r;
			g += bmp[position].g;
			b += bmp[position].b;
		}

	result[index].r = ROUND((float)r / count);
	result[index].g = ROUND((float)g / count);
	result[index].b = ROUND((float)b / count);
#undef ROUND
}

int main(int argc, char * argv[])
{
	if (argc != 4)
	{
		printf("%s\n", "Usage: <program name> <input bmp file name> <output bmp file name> <radius>");
		return 0;
	}

	const char * inputBmpFileName = argv[1];
	const char * outputBmpFileName = argv[2];
	size_t radius = (size_t)atoi(argv[3]);

	unsigned char header[BMP_HEADER_SIZE];
	struct RgbPixel * bmp = NULL;

	time_t totalBegin = time(NULL);

	bool readResult = readBmp(inputBmpFileName, header, &bmp);
	if (!readResult)
	{
		printf("Cannot read bmp file %s\n", inputBmpFileName);
		return 1;
	}

	size_t width = getWidth(header), height = getHeight(header);
	size_t size = width * height;
	struct RgbPixel * cudaBmp = cudaAllocateMemory(size);
	struct RgbPixel * cudaResultBmp = cudaAllocateMemory(size);

	if (!cudaBmp && !cudaResultBmp)
	{
		printf("%s\n", "Cannot allocate memory in GPU");
		return 1;
	}

	cudaMemcpy(cudaBmp, bmp, size * sizeof(struct RgbPixel), cudaMemcpyHostToDevice);
	time_t begin = time(NULL);
	size_t threadsPerBlock = getThreadsPerBlock(0);
	size_t blocks = (size + threadsPerBlock - 1) / threadsPerBlock;
	for (int i = 0; i < 100; i++)
	{
		processImageSmoothFilter<<<blocks, threadsPerBlock>>>(cudaBmp, width, height, radius, cudaResultBmp);
		cudaDeviceSynchronize();
	}
	time_t end = time(NULL);

	struct RgbPixel * resultBmp = allocateMemory(size);
	cudaMemcpy(resultBmp, cudaResultBmp, size * sizeof(struct RgbPixel), cudaMemcpyDeviceToHost);

	bool writeResult = writeBmp(outputBmpFileName, header, resultBmp);
	if (!writeResult)
	{
		printf("Cannot write bmp file %s\n", outputBmpFileName);
		return 1;
	}

	deallocateMemory(&bmp);
	deallocateMemory(&resultBmp);

	cudaDeallocateMemory(&cudaBmp);
	cudaDeallocateMemory(&cudaResultBmp);

	time_t totalEnd = time(NULL);

	printf("Algorithm time: %.2f sec.\n", (double)(end - begin));
	printf("Total time: %.2f sec.\n", (double)(totalEnd - totalBegin));

	return 0;
}


	.file	"tmpxft_00255901_00000000-6_smooth_filter_cuda.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2039:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2039:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z18getThreadsPerBlocki
	.type	_Z18getThreadsPerBlocki, @function
_Z18getThreadsPerBlocki:
.LFB2027:
	.cfi_startproc
	endbr64
	subq	$1048, %rsp
	.cfi_def_cfa_offset 1056
	movl	%edi, %esi
	movq	%fs:40, %rax
	movq	%rax, 1032(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	movslq	320(%rsp), %rax
	movq	1032(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$1048, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z18getThreadsPerBlocki, .-_Z18getThreadsPerBlocki
	.globl	_Z8getWidthPKh
	.type	_Z8getWidthPKh, @function
_Z8getWidthPKh:
.LFB2028:
	.cfi_startproc
	endbr64
	movl	18(%rdi), %eax
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z8getWidthPKh, .-_Z8getWidthPKh
	.globl	_Z9getHeightPKh
	.type	_Z9getHeightPKh, @function
_Z9getHeightPKh:
.LFB2029:
	.cfi_startproc
	endbr64
	movl	22(%rdi), %eax
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z9getHeightPKh, .-_Z9getHeightPKh
	.globl	_Z14allocateMemorym
	.type	_Z14allocateMemorym, @function
_Z14allocateMemorym:
.LFB2030:
	.cfi_startproc
	endbr64
	leaq	(%rdi,%rdi,2), %rdi
	jmp	malloc@PLT
	.cfi_endproc
.LFE2030:
	.size	_Z14allocateMemorym, .-_Z14allocateMemorym
	.globl	_Z18cudaAllocateMemorym
	.type	_Z18cudaAllocateMemorym, @function
_Z18cudaAllocateMemorym:
.LFB2031:
	.cfi_startproc
	endbr64
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	leaq	(%rdi,%rdi,2), %rsi
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	xorl	%eax, %eax
	movq	%rax, (%rsp)
	call	cudaMalloc@PLT
	movl	%eax, %edx
	movq	(%rsp), %rax
	testl	%edx, %edx
	je	.L9
	xorl	%eax, %eax
.L9:
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2031:
	.size	_Z18cudaAllocateMemorym, .-_Z18cudaAllocateMemorym
	.globl	_Z16deallocateMemoryPP8RgbPixel
	.type	_Z16deallocateMemoryPP8RgbPixel, @function
_Z16deallocateMemoryPP8RgbPixel:
.LFB2032:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	call	free@PLT
	xorl	%eax, %eax
	movq	%rax, (%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2032:
	.size	_Z16deallocateMemoryPP8RgbPixel, .-_Z16deallocateMemoryPP8RgbPixel
	.globl	_Z20cudaDeallocateMemoryPP8RgbPixel
	.type	_Z20cudaDeallocateMemoryPP8RgbPixel, @function
_Z20cudaDeallocateMemoryPP8RgbPixel:
.LFB2033:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
	movq	%rax, (%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2033:
	.size	_Z20cudaDeallocateMemoryPP8RgbPixel, .-_Z20cudaDeallocateMemoryPP8RgbPixel
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"rb"
	.text
	.globl	_Z7readBmpPKcPhPP8RgbPixel
	.type	_Z7readBmpPKcPhPP8RgbPixel, @function
_Z7readBmpPKcPhPP8RgbPixel:
.LFB2034:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movq	%rsi, %r12
	leaq	.LC0(%rip), %rsi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	fopen@PLT
	testq	%rax, %rax
	je	.L20
	orq	$-1, %rsi
	movq	%rax, %r8
	movl	$54, %ecx
	movq	%r12, %rdi
	movl	$1, %edx
	movq	%rax, %rbx
	call	__fread_chk@PLT
	cmpq	$54, %rax
	je	.L21
.L30:
	movq	%rbx, %rdi
	call	fclose@PLT
.L20:
	xorl	%eax, %eax
	jmp	.L19
.L21:
	movl	22(%r12), %edx
	movl	18(%r12), %eax
	xorl	%r13d, %r13d
	imulq	%rax, %rdx
	movq	%rdx, %rdi
	movq	%rdx, %r12
	call	_Z14allocateMemorym
	movq	%rax, 0(%rbp)
.L23:
	cmpq	%r12, %r13
	je	.L31
	leaq	0(%r13,%r13,2), %rdi
	orq	$-1, %rsi
	addq	0(%rbp), %rdi
	movq	%rbx, %r8
	movl	$3, %ecx
	movl	$1, %edx
	call	__fread_chk@PLT
	cmpq	$3, %rax
	je	.L24
	movq	%rbp, %rdi
	call	_Z16deallocateMemoryPP8RgbPixel
	jmp	.L30
.L24:
	incq	%r13
	jmp	.L23
.L31:
	movq	%rbx, %rdi
	call	fclose@PLT
	movb	$1, %al
.L19:
	popq	%rdx
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2034:
	.size	_Z7readBmpPKcPhPP8RgbPixel, .-_Z7readBmpPKcPhPP8RgbPixel
	.section	.rodata.str1.1
.LC1:
	.string	"wb"
	.text
	.globl	_Z8writeBmpPKcPKhP8RgbPixel
	.type	_Z8writeBmpPKcPKhP8RgbPixel, @function
_Z8writeBmpPKcPKhP8RgbPixel:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movq	%rsi, %r13
	leaq	.LC1(%rip), %rsi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movq	%rdx, %rbx
	pushq	%rcx
	.cfi_def_cfa_offset 48
	call	fopen@PLT
	testq	%rax, %rax
	jne	.L33
	xorl	%edi, %edi
	call	fclose@PLT
	xorl	%eax, %eax
	jmp	.L32
.L33:
	movq	%rax, %rcx
	movl	$54, %edx
	movl	$1, %esi
	movq	%r13, %rdi
	movq	%rax, %r12
	call	fwrite@PLT
	movl	22(%r13), %ebp
	movl	18(%r13), %eax
	imulq	%rax, %rbp
	leaq	0(%rbp,%rbp,2), %rbp
	addq	%rbx, %rbp
.L35:
	cmpq	%rbp, %rbx
	je	.L38
	movq	%rbx, %rdi
	movq	%r12, %rcx
	movl	$3, %edx
	movl	$1, %esi
	call	fwrite@PLT
	addq	$3, %rbx
	jmp	.L35
.L38:
	movq	%r12, %rdi
	call	fclose@PLT
	movb	$1, %al
.L32:
	popq	%rdx
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2035:
	.size	_Z8writeBmpPKcPKhP8RgbPixel, .-_Z8writeBmpPKcPKhP8RgbPixel
	.globl	_Z58__device_stub__Z24processImageSmoothFilterP8RgbPixelmmmS0_P8RgbPixelmmmS0_
	.type	_Z58__device_stub__Z24processImageSmoothFilterP8RgbPixelmmmS0_P8RgbPixelmmmS0_, @function
_Z58__device_stub__Z24processImageSmoothFilterP8RgbPixelmmmS0_P8RgbPixelmmmS0_:
.LFB2061:
	.cfi_startproc
	endbr64
	subq	$168, %rsp
	.cfi_def_cfa_offset 176
	movq	%rdi, 40(%rsp)
	leaq	64(%rsp), %rdi
	movq	%rsi, 32(%rsp)
	leaq	76(%rsp), %rsi
	movq	%rdx, 24(%rsp)
	leaq	48(%rsp), %rdx
	movq	%rcx, 16(%rsp)
	leaq	56(%rsp), %rcx
	movq	%r8, 8(%rsp)
	movq	%fs:40, %rax
	movq	%rax, 152(%rsp)
	xorl	%eax, %eax
	leaq	40(%rsp), %rax
	movl	$1, 72(%rsp)
	movq	%rax, 112(%rsp)
	leaq	32(%rsp), %rax
	movq	%rax, 120(%rsp)
	leaq	24(%rsp), %rax
	movq	%rax, 128(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 136(%rsp)
	leaq	8(%rsp), %rax
	movq	%rax, 144(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 64(%rsp)
	movq	%rax, 76(%rsp)
	movl	$1, 84(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L39
	pushq	56(%rsp)
	.cfi_def_cfa_offset 184
	leaq	_Z24processImageSmoothFilterP8RgbPixelmmmS0_(%rip), %rdi
	pushq	56(%rsp)
	.cfi_def_cfa_offset 192
	movq	92(%rsp), %rcx
	movl	100(%rsp), %r8d
	movq	80(%rsp), %rsi
	movl	88(%rsp), %edx
	leaq	128(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 184
	popq	%rdx
	.cfi_def_cfa_offset 176
.L39:
	movq	152(%rsp), %rax
	subq	%fs:40, %rax
	je	.L41
	call	__stack_chk_fail@PLT
.L41:
	addq	$168, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2061:
	.size	_Z58__device_stub__Z24processImageSmoothFilterP8RgbPixelmmmS0_P8RgbPixelmmmS0_, .-_Z58__device_stub__Z24processImageSmoothFilterP8RgbPixelmmmS0_P8RgbPixelmmmS0_
	.globl	_Z24processImageSmoothFilterP8RgbPixelmmmS0_
	.type	_Z24processImageSmoothFilterP8RgbPixelmmmS0_, @function
_Z24processImageSmoothFilterP8RgbPixelmmmS0_:
.LFB2062:
	.cfi_startproc
	endbr64
	jmp	_Z58__device_stub__Z24processImageSmoothFilterP8RgbPixelmmmS0_P8RgbPixelmmmS0_
	.cfi_endproc
.LFE2062:
	.size	_Z24processImageSmoothFilterP8RgbPixelmmmS0_, .-_Z24processImageSmoothFilterP8RgbPixelmmmS0_
	.section	.rodata.str1.1
.LC2:
	.string	"Usage: <program name> <input bmp file name> <output bmp file name> <radius>"
.LC3:
	.string	"%s\n"
.LC4:
	.string	"Cannot read bmp file %s\n"
.LC5:
	.string	"Cannot allocate memory in GPU"
.LC6:
	.string	"Cannot write bmp file %s\n"
.LC7:
	.string	"Algorithm time: %.2f sec.\n"
.LC8:
	.string	"Total time: %.2f sec.\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.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
	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	$216, %rsp
	.cfi_def_cfa_offset 272
	movq	%fs:40, %rax
	movq	%rax, 200(%rsp)
	xorl	%eax, %eax
	cmpl	$4, %edi
	je	.L45
	leaq	.LC2(%rip), %rdx
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L46
.L45:
	movq	16(%rsi), %rax
	movq	24(%rsi), %rdi
	movq	8(%rsi), %rbx
	movq	%rax, 8(%rsp)
	call	atoi@PLT
	xorl	%edi, %edi
	movl	%eax, %r12d
	xorl	%eax, %eax
	movq	%rax, 88(%rsp)
	call	time@PLT
	leaq	146(%rsp), %rsi
	movq	%rbx, %rdi
	movq	%rax, 56(%rsp)
	leaq	88(%rsp), %rax
	movq	%rax, %rdx
	movq	%rax, 24(%rsp)
	call	_Z7readBmpPKcPhPP8RgbPixel
	movq	%rbx, %rdx
	leaq	.LC4(%rip), %rsi
	testb	%al, %al
	je	.L58
	movl	168(%rsp), %eax
	movl	164(%rsp), %r14d
	movq	%rax, %r13
	movq	%rax, 16(%rsp)
	imulq	%r14, %r13
	movq	%r13, %rdi
	call	_Z18cudaAllocateMemorym
	movq	%r13, %rdi
	movq	%rax, %rbp
	movq	%rax, 96(%rsp)
	call	_Z18cudaAllocateMemorym
	movq	%rax, 104(%rsp)
	movq	%rax, %rbx
	orq	%rbp, %rax
	jne	.L49
	leaq	.LC5(%rip), %rdx
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
.L57:
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L44
.L49:
	movslq	%r12d, %rax
	movq	88(%rsp), %rsi
	movl	$1, %ecx
	movq	%rbp, %rdi
	movabsq	$4294967297, %r15
	movq	%rax, 48(%rsp)
	leaq	0(%r13,%r13,2), %rax
	movq	%rax, %rdx
	movq	%rax, 40(%rsp)
	call	cudaMemcpy@PLT
	xorl	%edi, %edi
	call	time@PLT
	xorl	%edi, %edi
	movq	%rax, 64(%rsp)
	call	_Z18getThreadsPerBlocki
	xorl	%edx, %edx
	movl	$100, 36(%rsp)
	movq	%rax, %r12
	leaq	-1(%r13,%rax), %rax
	divq	%r12
	movq	%rax, 72(%rsp)
.L52:
	movl	72(%rsp), %eax
	movq	%r15, 120(%rsp)
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	movq	%r15, 132(%rsp)
	movl	124(%rsp), %esi
	movl	%r12d, 128(%rsp)
	movl	136(%rsp), %ecx
	movl	%eax, 116(%rsp)
	movq	128(%rsp), %rdx
	movq	116(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L51
	movq	48(%rsp), %rcx
	movq	16(%rsp), %rdx
	movq	%rbx, %r8
	movq	%r14, %rsi
	movq	%rbp, %rdi
	call	_Z58__device_stub__Z24processImageSmoothFilterP8RgbPixelmmmS0_P8RgbPixelmmmS0_
.L51:
	call	cudaDeviceSynchronize@PLT
	decl	36(%rsp)
	jne	.L52
	xorl	%edi, %edi
	call	time@PLT
	movq	%r13, %rdi
	movq	%rax, %rbp
	call	_Z14allocateMemorym
	movq	40(%rsp), %rdx
	movq	%rbx, %rsi
	movl	$2, %ecx
	movq	%rax, %rdi
	movq	%rax, %r12
	movq	%rax, 128(%rsp)
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	leaq	146(%rsp), %rsi
	movq	%r12, %rdx
	call	_Z8writeBmpPKcPKhP8RgbPixel
	testb	%al, %al
	jne	.L53
	movq	8(%rsp), %rdx
	leaq	.LC6(%rip), %rsi
.L58:
	movl	$2, %edi
	jmp	.L57
.L53:
	movq	24(%rsp), %rdi
	call	_Z16deallocateMemoryPP8RgbPixel
	leaq	128(%rsp), %rdi
	call	_Z16deallocateMemoryPP8RgbPixel
	leaq	96(%rsp), %rdi
	call	_Z20cudaDeallocateMemoryPP8RgbPixel
	leaq	104(%rsp), %rdi
	call	_Z20cudaDeallocateMemoryPP8RgbPixel
	xorl	%edi, %edi
	call	time@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	movq	%rax, %rbx
	movq	64(%rsp), %rax
	subq	%rax, %rbp
	movb	$1, %al
	cvtsi2sdq	%rbp, %xmm0
	call	__printf_chk@PLT
	movq	56(%rsp), %rax
	movl	$2, %edi
	leaq	.LC8(%rip), %rsi
	subq	%rax, %rbx
	movb	$1, %al
	cvtsi2sdq	%rbx, %xmm0
	call	__printf_chk@PLT
.L46:
	xorl	%eax, %eax
.L44:
	movq	200(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L54
	call	__stack_chk_fail@PLT
.L54:
	addq	$216, %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	main, .-main
	.section	.rodata.str1.1
.LC9:
	.string	"_Z24processImageSmoothFilterP8RgbPixelmmmS0_"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2064:
	.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	.LC9(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z24processImageSmoothFilterP8RgbPixelmmmS0_(%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
.LFE2064:
	.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
	.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 <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>

#include <hip/hip_runtime.h>


const size_t BMP_HEADER_SIZE = 54;

const size_t BMP_HEADER_WIDTH_OFFSET = 18;
const size_t BMP_HEADER_HEIGHT_OFFSET = 22;

const size_t PIXEL_REAL_SIZE = 3;

size_t getThreadsPerBlock(int deviceNum)
{
	hipDeviceProp_t properties;
	hipGetDeviceProperties(&properties, deviceNum);

	return (size_t)properties.maxThreadsPerBlock;
}

size_t getWidth(const unsigned char * header)
{
	const unsigned char * start_position = header + BMP_HEADER_WIDTH_OFFSET;
	unsigned int value = *((const unsigned int*)start_position);
	return (size_t)value;
}

size_t getHeight(const unsigned char * header)
{
	const unsigned char * start_position = header + BMP_HEADER_HEIGHT_OFFSET;
	unsigned int value = *((const unsigned int*)start_position);
	return (size_t)value;
}

struct RgbPixel
{
	unsigned char b;
	unsigned char g;
	unsigned char r;
};

struct RgbPixel * allocateMemory(size_t size)
{
	struct RgbPixel * data = (struct RgbPixel *)malloc(sizeof(struct RgbPixel) * size);
	return data;
}

struct RgbPixel * cudaAllocateMemory(size_t size)
{
	struct RgbPixel * data = NULL;
	hipError_t hipError_t = hipMalloc((void **)&data, sizeof(struct RgbPixel) * size);
	return hipError_t == hipSuccess ? data : NULL;
}

void deallocateMemory(struct RgbPixel ** data)
{
	free(*data);
	*data = NULL;
}

void cudaDeallocateMemory(struct RgbPixel ** data)
{
	hipFree(*data);
	*data = NULL;
}

bool readBmp(
	const char * filename,
	unsigned char * header,
	struct RgbPixel ** bmp
)
{
	size_t read;

	FILE * fileBmp = fopen(filename, "rb");
	if (!fileBmp)
		return false;

	read = fread(header, sizeof(unsigned char), BMP_HEADER_SIZE, fileBmp);
	if (read != BMP_HEADER_SIZE)
	{
		fclose(fileBmp);
		return false;
	}

	size_t width = getWidth(header), height = getHeight(header);
	size_t size = width * height;

	*bmp = allocateMemory(size);
	for (size_t i = 0; i < size; i++)
	{
		read = fread((*bmp) + i, sizeof(unsigned char), PIXEL_REAL_SIZE, fileBmp);
		if (read != PIXEL_REAL_SIZE)
		{
			deallocateMemory(bmp);
			fclose(fileBmp);
			return false;
		}
	}

	fclose(fileBmp);
	return true;
}

bool writeBmp(
	const char * filename,
	const unsigned char * header,
	struct RgbPixel * bmp
)
{
	FILE * bmpFile = fopen(filename, "wb");
	if (!bmpFile)
	{
		fclose(bmpFile);
		return false;
	}

	fwrite(header, sizeof(unsigned char), BMP_HEADER_SIZE, bmpFile);
	size_t width = getWidth(header), height = getHeight(header);
	size_t size = width * height;

	for (size_t i = 0; i < size; i++)
		fwrite(bmp + i, sizeof(unsigned char), PIXEL_REAL_SIZE, bmpFile);

	fclose(bmpFile);
	return true;
}

__global__
void processImageSmoothFilter(struct RgbPixel * bmp, size_t width, size_t height, size_t radius, struct RgbPixel * result)
{
#define ROUND(x) (unsigned char)((x) + 0.5)
	size_t index = blockDim.x * blockIdx.x + threadIdx.x;
	size_t i = index / width, j = index % width;

	unsigned int r = 0, g = 0, b = 0;

	size_t
		starti = (i < radius ? 0 : i - radius),
		endi = (i + radius >= height ? height - 1 : i + radius),
		startj = (j < radius ? 0 : j - radius),
		endj = (j + radius >= width ? width - 1 : j + radius);

	size_t count = (endi - starti + 1) * (endj - startj + 1);
	for (size_t ii = starti; ii <= endi; ii++)
		for (size_t jj = startj; jj <= endj; jj++)
		{
			size_t position = ii * width + jj;
			r += bmp[position].r;
			g += bmp[position].g;
			b += bmp[position].b;
		}

	result[index].r = ROUND((float)r / count);
	result[index].g = ROUND((float)g / count);
	result[index].b = ROUND((float)b / count);
#undef ROUND
}

int main(int argc, char * argv[])
{
	if (argc != 4)
	{
		printf("%s\n", "Usage: <program name> <input bmp file name> <output bmp file name> <radius>");
		return 0;
	}

	const char * inputBmpFileName = argv[1];
	const char * outputBmpFileName = argv[2];
	size_t radius = (size_t)atoi(argv[3]);

	unsigned char header[BMP_HEADER_SIZE];
	struct RgbPixel * bmp = NULL;

	time_t totalBegin = time(NULL);

	bool readResult = readBmp(inputBmpFileName, header, &bmp);
	if (!readResult)
	{
		printf("Cannot read bmp file %s\n", inputBmpFileName);
		return 1;
	}

	size_t width = getWidth(header), height = getHeight(header);
	size_t size = width * height;
	struct RgbPixel * cudaBmp = cudaAllocateMemory(size);
	struct RgbPixel * cudaResultBmp = cudaAllocateMemory(size);

	if (!cudaBmp && !cudaResultBmp)
	{
		printf("%s\n", "Cannot allocate memory in GPU");
		return 1;
	}

	hipMemcpy(cudaBmp, bmp, size * sizeof(struct RgbPixel), hipMemcpyHostToDevice);
	time_t begin = time(NULL);
	size_t threadsPerBlock = getThreadsPerBlock(0);
	size_t blocks = (size + threadsPerBlock - 1) / threadsPerBlock;
	for (int i = 0; i < 100; i++)
	{
		processImageSmoothFilter<<<blocks, threadsPerBlock>>>(cudaBmp, width, height, radius, cudaResultBmp);
		hipDeviceSynchronize();
	}
	time_t end = time(NULL);

	struct RgbPixel * resultBmp = allocateMemory(size);
	hipMemcpy(resultBmp, cudaResultBmp, size * sizeof(struct RgbPixel), hipMemcpyDeviceToHost);

	bool writeResult = writeBmp(outputBmpFileName, header, resultBmp);
	if (!writeResult)
	{
		printf("Cannot write bmp file %s\n", outputBmpFileName);
		return 1;
	}

	deallocateMemory(&bmp);
	deallocateMemory(&resultBmp);

	cudaDeallocateMemory(&cudaBmp);
	cudaDeallocateMemory(&cudaResultBmp);

	time_t totalEnd = time(NULL);

	printf("Algorithm time: %.2f sec.\n", (double)(end - begin));
	printf("Total time: %.2f sec.\n", (double)(totalEnd - totalBegin));

	return 0;
}


	.text
	.file	"smooth_filter_cuda.hip"
	.globl	_Z18getThreadsPerBlocki         # -- Begin function _Z18getThreadsPerBlocki
	.type	_Z18getThreadsPerBlocki,@function
_Z18getThreadsPerBlocki:                # @_Z18getThreadsPerBlocki
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$1472, %rsp                     # imm = 0x5C0
	.cfi_def_cfa_offset 1488
	.cfi_offset %rbx, -16
	movl	%edi, %esi
	movq	%rsp, %rbx
	movq	%rbx, %rdi
	callq	hipGetDevicePropertiesR0600
	movslq	320(%rbx), %rax
	addq	$1472, %rsp                     # imm = 0x5C0
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z18getThreadsPerBlocki, .Lfunc_end0-_Z18getThreadsPerBlocki
	.cfi_endproc
                                        # -- End function
	.globl	_Z8getWidthPKh                  # -- Begin function _Z8getWidthPKh
	.type	_Z8getWidthPKh,@function
_Z8getWidthPKh:                         # @_Z8getWidthPKh
	.cfi_startproc
# %bb.0:
	movl	18(%rdi), %eax
	retq
.Lfunc_end1:
	.size	_Z8getWidthPKh, .Lfunc_end1-_Z8getWidthPKh
	.cfi_endproc
                                        # -- End function
	.globl	_Z9getHeightPKh                 # -- Begin function _Z9getHeightPKh
	.type	_Z9getHeightPKh,@function
_Z9getHeightPKh:                        # @_Z9getHeightPKh
	.cfi_startproc
# %bb.0:
	movl	22(%rdi), %eax
	retq
.Lfunc_end2:
	.size	_Z9getHeightPKh, .Lfunc_end2-_Z9getHeightPKh
	.cfi_endproc
                                        # -- End function
	.globl	_Z14allocateMemorym             # -- Begin function _Z14allocateMemorym
	.type	_Z14allocateMemorym,@function
_Z14allocateMemorym:                    # @_Z14allocateMemorym
	.cfi_startproc
# %bb.0:
	leaq	(%rdi,%rdi,2), %rdi
	jmp	malloc                          # TAILCALL
.Lfunc_end3:
	.size	_Z14allocateMemorym, .Lfunc_end3-_Z14allocateMemorym
	.cfi_endproc
                                        # -- End function
	.globl	_Z18cudaAllocateMemorym         # -- Begin function _Z18cudaAllocateMemorym
	.type	_Z18cudaAllocateMemorym,@function
_Z18cudaAllocateMemorym:                # @_Z18cudaAllocateMemorym
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	subq	$16, %rsp
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -16
	xorl	%ebx, %ebx
	leaq	8(%rsp), %rax
	movq	%rbx, (%rax)
	leaq	(%rdi,%rdi,2), %rsi
	movq	%rax, %rdi
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB4_2
# %bb.1:
	movq	8(%rsp), %rbx
.LBB4_2:
	movq	%rbx, %rax
	addq	$16, %rsp
	.cfi_def_cfa_offset 16
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	_Z18cudaAllocateMemorym, .Lfunc_end4-_Z18cudaAllocateMemorym
	.cfi_endproc
                                        # -- End function
	.globl	_Z16deallocateMemoryPP8RgbPixel # -- Begin function _Z16deallocateMemoryPP8RgbPixel
	.type	_Z16deallocateMemoryPP8RgbPixel,@function
_Z16deallocateMemoryPP8RgbPixel:        # @_Z16deallocateMemoryPP8RgbPixel
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	callq	free
	movq	$0, (%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end5:
	.size	_Z16deallocateMemoryPP8RgbPixel, .Lfunc_end5-_Z16deallocateMemoryPP8RgbPixel
	.cfi_endproc
                                        # -- End function
	.globl	_Z20cudaDeallocateMemoryPP8RgbPixel # -- Begin function _Z20cudaDeallocateMemoryPP8RgbPixel
	.type	_Z20cudaDeallocateMemoryPP8RgbPixel,@function
_Z20cudaDeallocateMemoryPP8RgbPixel:    # @_Z20cudaDeallocateMemoryPP8RgbPixel
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movq	(%rdi), %rdi
	callq	hipFree
	movq	$0, (%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end6:
	.size	_Z20cudaDeallocateMemoryPP8RgbPixel, .Lfunc_end6-_Z20cudaDeallocateMemoryPP8RgbPixel
	.cfi_endproc
                                        # -- End function
	.globl	_Z7readBmpPKcPhPP8RgbPixel      # -- Begin function _Z7readBmpPKcPhPP8RgbPixel
	.type	_Z7readBmpPKcPhPP8RgbPixel,@function
_Z7readBmpPKcPhPP8RgbPixel:             # @_Z7readBmpPKcPhPP8RgbPixel
	.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
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdx, %r14
	movq	%rsi, %r15
	movl	$.L.str, %esi
	callq	fopen
	testq	%rax, %rax
	je	.LBB7_1
# %bb.2:
	movq	%rax, %rbx
	movl	$1, %esi
	movl	$54, %edx
	movq	%r15, %rdi
	movq	%rax, %rcx
	callq	fread
	cmpq	$54, %rax
	jne	.LBB7_8
# %bb.3:
	movl	18(%r15), %eax
	movl	22(%r15), %r15d
	imulq	%rax, %r15
	leaq	(%r15,%r15,2), %rdi
	callq	malloc
	movq	%rax, (%r14)
	movb	$1, %bpl
	testq	%r15, %r15
	je	.LBB7_9
# %bb.4:                                # %.lr.ph.preheader
	xorl	%r12d, %r12d
.LBB7_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movq	(%r14), %rdi
	addq	%r12, %rdi
	movl	$1, %esi
	movl	$3, %edx
	movq	%rbx, %rcx
	callq	fread
	cmpq	$3, %rax
	jne	.LBB7_7
# %bb.5:                                #   in Loop: Header=BB7_6 Depth=1
	addq	$3, %r12
	decq	%r15
	jne	.LBB7_6
	jmp	.LBB7_9
.LBB7_1:
	xorl	%ebp, %ebp
	jmp	.LBB7_10
.LBB7_7:
	movq	(%r14), %rdi
	callq	free
	movq	$0, (%r14)
.LBB7_8:                                # %.sink.split
	xorl	%ebp, %ebp
.LBB7_9:                                # %.sink.split
	movq	%rbx, %rdi
	callq	fclose
.LBB7_10:
	movl	%ebp, %eax
	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
.Lfunc_end7:
	.size	_Z7readBmpPKcPhPP8RgbPixel, .Lfunc_end7-_Z7readBmpPKcPhPP8RgbPixel
	.cfi_endproc
                                        # -- End function
	.globl	_Z8writeBmpPKcPKhP8RgbPixel     # -- Begin function _Z8writeBmpPKcPKhP8RgbPixel
	.type	_Z8writeBmpPKcPKhP8RgbPixel,@function
_Z8writeBmpPKcPKhP8RgbPixel:            # @_Z8writeBmpPKcPKhP8RgbPixel
	.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	%rdx, %r14
	movq	%rsi, %r15
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, %rbx
	testq	%rax, %rax
	je	.LBB8_1
# %bb.2:
	movl	$1, %esi
	movl	$54, %edx
	movq	%r15, %rdi
	movq	%rbx, %rcx
	callq	fwrite
	movl	18(%r15), %eax
	movl	22(%r15), %r15d
	imulq	%rax, %r15
	testq	%r15, %r15
	je	.LBB8_3
.LBB8_5:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movl	$1, %esi
	movl	$3, %edx
	movq	%r14, %rdi
	movq	%rbx, %rcx
	callq	fwrite
	addq	$3, %r14
	decq	%r15
	jne	.LBB8_5
.LBB8_3:                                # %._crit_edge
	movq	%rbx, %rdi
	jmp	.LBB8_4
.LBB8_1:
	xorl	%edi, %edi
.LBB8_4:
	callq	fclose
	testq	%rbx, %rbx
	setne	%al
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end8:
	.size	_Z8writeBmpPKcPKhP8RgbPixel, .Lfunc_end8-_Z8writeBmpPKcPKhP8RgbPixel
	.cfi_endproc
                                        # -- End function
	.globl	_Z39__device_stub__processImageSmoothFilterP8RgbPixelmmmS0_ # -- Begin function _Z39__device_stub__processImageSmoothFilterP8RgbPixelmmmS0_
	.type	_Z39__device_stub__processImageSmoothFilterP8RgbPixelmmmS0_,@function
_Z39__device_stub__processImageSmoothFilterP8RgbPixelmmmS0_: # @_Z39__device_stub__processImageSmoothFilterP8RgbPixelmmmS0_
	.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	56(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	48(%rsp), %rdi
	movq	%rsi, (%rdi)
	leaq	40(%rsp), %rsi
	movq	%rdx, (%rsi)
	leaq	32(%rsp), %rdx
	movq	%rcx, (%rdx)
	leaq	24(%rsp), %rcx
	movq	%r8, (%rcx)
	leaq	96(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rdi, 8(%rbx)
	movq	%rsi, 16(%rbx)
	movq	%rdx, 24(%rbx)
	movq	%rcx, 32(%rbx)
	leaq	80(%rsp), %r14
	leaq	64(%rsp), %r15
	leaq	16(%rsp), %r12
	leaq	8(%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	$_Z24processImageSmoothFilterP8RgbPixelmmmS0_, %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_end9:
	.size	_Z39__device_stub__processImageSmoothFilterP8RgbPixelmmmS0_, .Lfunc_end9-_Z39__device_stub__processImageSmoothFilterP8RgbPixelmmmS0_
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$1592, %rsp                     # imm = 0x638
	.cfi_def_cfa_offset 1648
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	cmpl	$4, %edi
	jne	.LBB10_13
# %bb.1:
	movq	8(%rsi), %rbx
	movq	16(%rsi), %r13
	movq	24(%rsi), %rdi
	callq	atoi
	movl	%eax, %r14d
	leaq	56(%rsp), %r15
	movq	$0, (%r15)
	xorl	%edi, %edi
	callq	time
	movq	%rax, %r12
	leaq	64(%rsp), %rsi
	movq	%rbx, %rdi
	movq	%r15, %rdx
	callq	_Z7readBmpPKcPhPP8RgbPixel
	testb	%al, %al
	je	.LBB10_14
# %bb.2:
	movq	%r13, 40(%rsp)                  # 8-byte Spill
	movl	82(%rsp), %eax
	movl	86(%rsp), %ebx
	movq	%rbx, %rbp
	movq	%rax, 48(%rsp)                  # 8-byte Spill
	imulq	%rax, %rbp
	xorl	%r15d, %r15d
	leaq	120(%rsp), %rdi
	movq	%r15, (%rdi)
	leaq	(,%rbp,2), %rsi
	addq	%rbp, %rsi
	movq	%rsi, (%rsp)                    # 8-byte Spill
	callq	hipMalloc
	movl	$0, %r13d
	testl	%eax, %eax
	jne	.LBB10_4
# %bb.3:
	movq	120(%rsp), %r13
.LBB10_4:
	leaq	120(%rsp), %rdi
	movq	%r15, (%rdi)
	movq	(%rsp), %rsi                    # 8-byte Reload
	callq	hipMalloc
	testl	%eax, %eax
	jne	.LBB10_6
# %bb.5:
	movq	120(%rsp), %r15
.LBB10_6:
	movq	%r13, %rax
	orq	%r15, %rax
	je	.LBB10_16
# %bb.7:
	movq	%r12, 32(%rsp)                  # 8-byte Spill
	movslq	%r14d, %r14
	movq	56(%rsp), %rsi
	movq	%r13, 8(%rsp)                   # 8-byte Spill
	movq	%r13, %rdi
	movq	%rsi, 16(%rsp)                  # 8-byte Spill
	movq	(%rsp), %rdx                    # 8-byte Reload
	movl	$1, %ecx
	callq	hipMemcpy
	xorl	%edi, %edi
	callq	time
	movq	%rax, 24(%rsp)                  # 8-byte Spill
	leaq	120(%rsp), %r13
	movq	%r13, %rdi
	xorl	%esi, %esi
	callq	hipGetDevicePropertiesR0600
	movl	320(%r13), %r13d
	movslq	%r13d, %rcx
	leaq	(%rcx,%rbp), %rax
	decq	%rax
	xorl	%edx, %edx
	divq	%rcx
	movl	%eax, %ebp
	btsq	$32, %rbp
	btsq	$32, %r13
	movl	$100, %r12d
.LBB10_8:                               # =>This Inner Loop Header: Depth=1
	movq	%rbp, %rdi
	movl	$1, %esi
	movq	%r13, %rdx
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB10_10
# %bb.9:                                #   in Loop: Header=BB10_8 Depth=1
	movq	8(%rsp), %rdi                   # 8-byte Reload
	movq	48(%rsp), %rsi                  # 8-byte Reload
	movq	%rbx, %rdx
	movq	%r14, %rcx
	movq	%r15, %r8
	callq	_Z39__device_stub__processImageSmoothFilterP8RgbPixelmmmS0_
.LBB10_10:                              #   in Loop: Header=BB10_8 Depth=1
	callq	hipDeviceSynchronize
	decl	%r12d
	jne	.LBB10_8
# %bb.11:
	xorl	%ebx, %ebx
	xorl	%edi, %edi
	callq	time
	movq	%rax, %r14
	movq	(%rsp), %r12                    # 8-byte Reload
	movq	%r12, %rdi
	callq	malloc
	movq	%rax, %r13
	movq	%rax, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	leaq	64(%rsp), %rsi
	movq	40(%rsp), %r12                  # 8-byte Reload
	movq	%r12, %rdi
	movq	%r13, %rdx
	callq	_Z8writeBmpPKcPKhP8RgbPixel
	testb	%al, %al
	je	.LBB10_19
# %bb.12:
	movq	16(%rsp), %rdi                  # 8-byte Reload
	callq	free
	movq	%r13, %rdi
	callq	free
	movq	8(%rsp), %rdi                   # 8-byte Reload
	callq	hipFree
	movq	%r15, %rdi
	callq	hipFree
	xorl	%edi, %edi
	callq	time
	movq	%rax, %r15
	subq	24(%rsp), %r14                  # 8-byte Folded Reload
	cvtsi2sd	%r14, %xmm0
	movl	$.L.str.7, %edi
	movb	$1, %al
	callq	printf
	subq	32(%rsp), %r15                  # 8-byte Folded Reload
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15, %xmm0
	movl	$.L.str.8, %edi
	movb	$1, %al
	callq	printf
	jmp	.LBB10_18
.LBB10_13:
	movl	$.L.str.3, %edi
	callq	puts@PLT
	xorl	%ebx, %ebx
	jmp	.LBB10_18
.LBB10_14:
	movl	$.L.str.4, %edi
	movq	%rbx, %rsi
.LBB10_15:
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB10_17
.LBB10_16:
	movl	$.L.str.5, %edi
	callq	puts@PLT
.LBB10_17:
	movl	$1, %ebx
.LBB10_18:
	movl	%ebx, %eax
	addq	$1592, %rsp                     # imm = 0x638
	.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
.LBB10_19:
	.cfi_def_cfa_offset 1648
	movl	$.L.str.6, %edi
	movq	%r12, %rsi
	jmp	.LBB10_15
.Lfunc_end10:
	.size	main, .Lfunc_end10-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	.LBB11_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB11_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z24processImageSmoothFilterP8RgbPixelmmmS0_, %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_end11:
	.size	__hip_module_ctor, .Lfunc_end11-__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	.LBB12_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
.LBB12_2:
	retq
.Lfunc_end12:
	.size	__hip_module_dtor, .Lfunc_end12-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"rb"
	.size	.L.str, 3

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

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

	.type	.L.str.3,@object                # @.str.3
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.3:
	.asciz	"Usage: <program name> <input bmp file name> <output bmp file name> <radius>"
	.size	.L.str.3, 76

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"Cannot read bmp file %s\n"
	.size	.L.str.4, 25

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"Cannot allocate memory in GPU"
	.size	.L.str.5, 30

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"Cannot write bmp file %s\n"
	.size	.L.str.6, 26

	.type	.L.str.7,@object                # @.str.7
.L.str.7:
	.asciz	"Algorithm time: %.2f sec.\n"
	.size	.L.str.7, 27

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"Total time: %.2f sec.\n"
	.size	.L.str.8, 23

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z24processImageSmoothFilterP8RgbPixelmmmS0_"
	.size	.L__unnamed_1, 45

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

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

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

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z24processImageSmoothFilterP8RgbPixelmmmS0_ ; -- Begin function _Z24processImageSmoothFilterP8RgbPixelmmmS0_
	.globl	_Z24processImageSmoothFilterP8RgbPixelmmmS0_
	.p2align	8
	.type	_Z24processImageSmoothFilterP8RgbPixelmmmS0_,@function
_Z24processImageSmoothFilterP8RgbPixelmmmS0_: ; @_Z24processImageSmoothFilterP8RgbPixelmmmS0_
; %bb.0:
	s_clause 0x1
	s_load_b256 s[4:11], s[0:1], 0x0
	s_load_b32 s2, s[0:1], 0x34
	s_waitcnt lgkmcnt(0)
	v_cvt_f32_u32_e32 v1, s6
	v_cvt_f32_u32_e32 v2, s7
	s_and_b32 s2, s2, 0xffff
	s_sub_u32 s3, 0, s6
	s_subb_u32 s12, 0, s7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_fmamk_f32 v1, v2, 0x4f800000, v1
	v_rcp_f32_e32 v1, v1
	s_waitcnt_depctr 0xfff
	v_mul_f32_e32 v1, 0x5f7ffffc, v1
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_mul_f32_e32 v2, 0x2f800000, v1
	v_trunc_f32_e32 v2, v2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_fmamk_f32 v1, v2, 0xcf800000, v1
	v_cvt_u32_f32_e32 v2, v2
	v_cvt_u32_f32_e32 v1, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v3, s3, v2
	v_mul_hi_u32 v4, s3, v1
	v_mul_lo_u32 v5, s12, v1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v3, v4, v3
	v_mul_lo_u32 v4, s3, v1
	v_add_nc_u32_e32 v3, v3, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_hi_u32 v5, v1, v4
	v_mul_lo_u32 v6, v1, v3
	v_mul_hi_u32 v7, v1, v3
	v_mul_hi_u32 v8, v2, v4
	v_mul_lo_u32 v4, v2, v4
	v_mul_hi_u32 v9, v2, v3
	v_mul_lo_u32 v3, v2, v3
	v_add_co_u32 v5, vcc_lo, v5, v6
	v_add_co_ci_u32_e32 v6, vcc_lo, 0, v7, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v4, vcc_lo, v5, v4
	v_add_co_ci_u32_e32 v4, vcc_lo, v6, v8, vcc_lo
	v_add_co_ci_u32_e32 v5, vcc_lo, 0, v9, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v3, vcc_lo, v4, v3
	v_add_co_ci_u32_e32 v4, vcc_lo, 0, v5, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v3, vcc_lo, v1, v3
	v_add_co_ci_u32_e32 v4, vcc_lo, v2, v4, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_3)
	v_mul_hi_u32 v1, s3, v3
	v_mul_lo_u32 v5, s12, v3
	v_mul_lo_u32 v2, s3, v4
	s_add_u32 s12, s8, -1
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_nc_u32_e32 v1, v1, v2
	v_mul_lo_u32 v2, s3, v3
	v_add_nc_u32_e32 v1, v1, v5
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_hi_u32 v5, v3, v2
	v_mul_lo_u32 v6, v3, v1
	v_mul_hi_u32 v7, v3, v1
	v_mul_hi_u32 v8, v4, v2
	v_mul_lo_u32 v2, v4, v2
	v_mul_hi_u32 v9, v4, v1
	v_add_co_u32 v5, vcc_lo, v5, v6
	v_add_co_ci_u32_e32 v6, vcc_lo, 0, v7, vcc_lo
	v_mul_lo_u32 v7, v4, v1
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_co_u32 v1, vcc_lo, v5, v2
	v_add_co_ci_u32_e32 v5, vcc_lo, v6, v8, vcc_lo
	v_add_co_ci_u32_e32 v6, vcc_lo, 0, v9, vcc_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	v_add_co_u32 v0, vcc_lo, v5, v7
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v2, vcc_lo, 0, v6, vcc_lo
	v_add_co_u32 v0, vcc_lo, v3, v0
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v8, vcc_lo, v4, v2, vcc_lo
	v_mul_hi_u32 v9, v1, v0
	v_mad_u64_u32 v[4:5], null, 0, v0, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[2:3], null, v1, v8, 0
	v_mad_u64_u32 v[6:7], null, 0, v8, 0
	v_add_co_u32 v0, vcc_lo, v9, v2
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v2, vcc_lo, 0, v3, vcc_lo
	v_add_co_u32 v0, vcc_lo, v0, v4
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v0, vcc_lo, v2, v5, vcc_lo
	v_add_co_ci_u32_e32 v2, vcc_lo, 0, v7, vcc_lo
	v_add_co_u32 v4, vcc_lo, v0, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_ci_u32_e32 v5, vcc_lo, 0, v2, vcc_lo
	v_mul_lo_u32 v0, s7, v4
	v_mad_u64_u32 v[2:3], null, s6, v4, 0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v6, s6, v5
	v_sub_co_u32 v2, vcc_lo, v1, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add3_u32 v0, v3, v6, v0
	v_sub_nc_u32_e32 v3, 0, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_subrev_co_ci_u32_e64 v3, s2, s7, v3, vcc_lo
	v_add_co_u32 v6, s2, v4, 2
	v_add_co_ci_u32_e64 v7, s2, 0, v5, s2
	v_sub_co_u32 v8, s2, v2, s6
	v_sub_co_ci_u32_e32 v0, vcc_lo, 0, v0, vcc_lo
	v_subrev_co_ci_u32_e64 v3, s2, 0, v3, s2
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cmp_le_u32_e32 vcc_lo, s6, v8
	v_cmp_eq_u32_e64 s2, s7, v0
	v_cndmask_b32_e64 v8, 0, -1, vcc_lo
	s_delay_alu instid0(VALU_DEP_4)
	v_cmp_le_u32_e32 vcc_lo, s7, v3
	v_cndmask_b32_e64 v9, 0, -1, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s6, v2
	v_cndmask_b32_e64 v2, 0, -1, vcc_lo
	v_cmp_le_u32_e32 vcc_lo, s7, v0
	v_cndmask_b32_e64 v10, 0, -1, vcc_lo
	v_cmp_eq_u32_e32 vcc_lo, s7, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v0, v10, v2, s2
	v_cndmask_b32_e32 v3, v9, v8, vcc_lo
	v_add_co_u32 v8, vcc_lo, v4, 1
	v_add_co_ci_u32_e32 v9, vcc_lo, 0, v5, vcc_lo
	v_cmp_ne_u32_e32 vcc_lo, 0, v3
	s_load_b64 s[2:3], s[0:1], 0x20
	s_addc_u32 s1, s9, -1
	s_add_u32 s13, s6, -1
	v_cndmask_b32_e32 v2, v9, v7, vcc_lo
	v_cndmask_b32_e32 v6, v8, v6, vcc_lo
	v_cmp_ne_u32_e32 vcc_lo, 0, v0
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_dual_mov_b32 v0, 0 :: v_dual_cndmask_b32 v3, v5, v2
	v_cndmask_b32_e32 v2, v4, v6, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mul_lo_u32 v10, v3, s6
	v_mul_lo_u32 v11, v2, s7
	v_mad_u64_u32 v[4:5], null, v2, s6, 0
	v_sub_co_u32 v6, vcc_lo, v2, s10
	v_subrev_co_ci_u32_e32 v7, vcc_lo, s11, v3, vcc_lo
	v_add_co_u32 v8, vcc_lo, v2, s10
	s_delay_alu instid0(VALU_DEP_4) | instskip(SKIP_2) | instid1(VALU_DEP_1)
	v_add3_u32 v5, v5, v11, v10
	v_add_co_ci_u32_e32 v9, vcc_lo, s11, v3, vcc_lo
	v_sub_co_u32 v10, s0, v1, v4
	v_sub_co_ci_u32_e64 v11, s0, 0, v5, s0
	s_delay_alu instid0(VALU_DEP_3) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	v_cmp_gt_u64_e64 s0, s[8:9], v[8:9]
	v_cmp_gt_u64_e32 vcc_lo, v[6:7], v[2:3]
	s_addc_u32 s8, s7, -1
	s_mov_b32 s9, 0
	v_cndmask_b32_e64 v5, s1, v9, s0
	v_sub_co_u32 v12, s1, v10, s10
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_subrev_co_ci_u32_e64 v13, s1, s11, v11, s1
	v_add_co_u32 v14, s1, v10, s10
	v_add_co_ci_u32_e64 v15, s1, s11, v11, s1
	v_cndmask_b32_e64 v3, v7, 0, vcc_lo
	v_cndmask_b32_e64 v2, v6, 0, vcc_lo
	v_cndmask_b32_e64 v4, s12, v8, s0
	v_cmp_gt_u64_e32 vcc_lo, v[12:13], v[10:11]
	v_mov_b32_e32 v10, 0
	v_cmp_gt_u64_e64 s0, s[6:7], v[14:15]
	v_mov_b32_e32 v11, 0
	v_cndmask_b32_e64 v7, v13, 0, vcc_lo
	v_cndmask_b32_e64 v6, v12, 0, vcc_lo
	s_delay_alu instid0(VALU_DEP_4)
	v_cndmask_b32_e64 v9, s8, v15, s0
	v_cndmask_b32_e64 v8, s13, v14, s0
	s_mov_b32 s8, exec_lo
	v_cmpx_ge_u64_e64 v[4:5], v[2:3]
	s_cbranch_execz .LBB0_8
; %bb.1:                                ; %.preheader.lr.ph
	s_delay_alu instid0(VALU_DEP_2)
	v_cmp_ge_u64_e32 vcc_lo, v[8:9], v[6:7]
	v_dual_mov_b32 v0, 0 :: v_dual_mov_b32 v11, v3
	v_dual_mov_b32 v10, v2 :: v_dual_mov_b32 v17, 0
	v_mov_b32_e32 v16, 0
.LBB0_2:                                ; %.preheader
                                        ; =>This Loop Header: Depth=1
                                        ;     Child Loop BB0_4 Depth 2
	s_and_saveexec_b32 s1, vcc_lo
	s_cbranch_execz .LBB0_6
; %bb.3:                                ; %.lr.ph
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_3)
	v_mad_u64_u32 v[14:15], null, v10, s6, 0
	v_mul_lo_u32 v18, v11, s6
	v_mul_lo_u32 v19, v10, s7
	s_mov_b32 s10, 0
	v_mad_u64_u32 v[12:13], null, v14, 3, s[4:5]
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add3_u32 v18, v15, v19, v18
	v_mad_u64_u32 v[14:15], null, v18, 3, v[13:14]
	s_delay_alu instid0(VALU_DEP_1)
	v_mov_b32_e32 v13, v14
	v_dual_mov_b32 v15, v7 :: v_dual_mov_b32 v14, v6
.LBB0_4:                                ;   Parent Loop BB0_2 Depth=1
                                        ; =>  This Inner Loop Header: Depth=2
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[18:19], null, v14, 3, v[12:13]
	v_add_co_u32 v14, s0, v14, 1
	v_mad_u64_u32 v[20:21], null, v15, 3, v[19:20]
	v_add_co_ci_u32_e64 v15, s0, 0, v15, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cmp_gt_u64_e64 s0, v[14:15], v[8:9]
	v_mov_b32_e32 v19, v20
	s_clause 0x1
	global_load_u16 v20, v[18:19], off
	global_load_u8 v18, v[18:19], off offset:2
	s_or_b32 s10, s0, s10
	s_waitcnt vmcnt(1)
	v_lshrrev_b32_e32 v19, 8, v20
	v_and_b32_e32 v20, 0xff, v20
	s_waitcnt vmcnt(0)
	v_add_nc_u32_e32 v0, v0, v18
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_nc_u32_e32 v16, v16, v19
	v_add_nc_u32_e32 v17, v17, v20
	s_and_not1_b32 exec_lo, exec_lo, s10
	s_cbranch_execnz .LBB0_4
; %bb.5:                                ; %Flow
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_or_b32 exec_lo, exec_lo, s10
.LBB0_6:                                ; %Flow118
                                        ;   in Loop: Header=BB0_2 Depth=1
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	s_or_b32 exec_lo, exec_lo, s1
	v_add_co_u32 v10, s0, v10, 1
	v_add_co_ci_u32_e64 v11, s0, 0, v11, s0
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cmp_gt_u64_e64 s0, v[10:11], v[4:5]
	s_or_b32 s9, s0, s9
	s_delay_alu instid0(SALU_CYCLE_1)
	s_and_not1_b32 exec_lo, exec_lo, s9
	s_cbranch_execnz .LBB0_2
; %bb.7:                                ; %._crit_edge87.loopexit
	s_or_b32 exec_lo, exec_lo, s9
	v_cvt_f32_u32_e32 v0, v0
	v_cvt_f32_u32_e32 v10, v16
	v_cvt_f32_u32_e32 v11, v17
.LBB0_8:                                ; %Flow119
	s_or_b32 exec_lo, exec_lo, s8
	v_sub_co_u32 v2, vcc_lo, v4, v2
	v_sub_co_ci_u32_e32 v3, vcc_lo, v5, v3, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_add_co_u32 v2, vcc_lo, v2, 1
	v_add_co_ci_u32_e32 v3, vcc_lo, 0, v3, vcc_lo
	v_sub_co_u32 v6, vcc_lo, v8, v6
	v_sub_co_ci_u32_e32 v7, vcc_lo, v9, v7, vcc_lo
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mad_u64_u32 v[4:5], null, v2, v6, v[2:3]
	v_mul_lo_u32 v2, v2, v7
	v_mul_lo_u32 v3, v3, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_add3_u32 v5, v3, v5, v2
	v_clz_i32_u32_e32 v2, v5
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_min_u32_e32 v6, 32, v2
	v_lshlrev_b64 v[2:3], v6, v[4:5]
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_min_u32_e32 v2, 1, v2
	v_or_b32_e32 v2, v3, v2
	v_sub_nc_u32_e32 v3, 32, v6
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cvt_f32_u32_e32 v2, v2
	v_ldexp_f32 v4, v2, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_div_scale_f32 v2, null, v4, v4, v0
	v_div_scale_f32 v3, null, v4, v4, v10
	v_div_scale_f32 v6, null, v4, v4, v11
	v_rcp_f32_e32 v5, v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_rcp_f32_e32 v7, v3
	v_div_scale_f32 v12, vcc_lo, v0, v4, v0
	v_rcp_f32_e32 v8, v6
	v_div_scale_f32 v14, s0, v10, v4, v10
	s_delay_alu instid0(TRANS32_DEP_3) | instskip(SKIP_4) | instid1(VALU_DEP_3)
	v_fma_f32 v9, -v2, v5, 1.0
	s_waitcnt_depctr 0xfff
	v_fma_f32 v13, -v3, v7, 1.0
	v_fmac_f32_e32 v5, v9, v5
	v_fma_f32 v9, -v6, v8, 1.0
	v_fmac_f32_e32 v7, v13, v7
	v_div_scale_f32 v13, s1, v11, v4, v11
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_dual_mul_f32 v15, v12, v5 :: v_dual_fmac_f32 v8, v9, v8
	v_mul_f32_e32 v9, v14, v7
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f32 v16, -v2, v15, v12
	v_mul_f32_e32 v17, v13, v8
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f32 v18, -v3, v9, v14
	v_fmac_f32_e32 v15, v16, v5
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f32 v16, -v6, v17, v13
	v_fmac_f32_e32 v9, v18, v7
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f32 v2, -v2, v15, v12
	v_fmac_f32_e32 v17, v16, v8
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_fma_f32 v3, -v3, v9, v14
	v_div_fmas_f32 v5, v2, v5, v15
	s_mov_b32 vcc_lo, s0
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_3)
	v_div_fmas_f32 v2, v3, v7, v9
	v_fma_f32 v3, -v6, v17, v13
	s_mov_b32 vcc_lo, s1
	v_div_fixup_f32 v0, v5, v4, v0
	v_div_fixup_f32 v2, v2, v4, v10
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_div_fmas_f32 v6, v3, v8, v17
	v_cvt_f64_f32_e32 v[2:3], v2
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_div_fixup_f32 v6, v6, v4, v11
	v_cvt_f64_f32_e32 v[4:5], v6
	v_cvt_f64_f32_e32 v[6:7], v0
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[2:3], v[2:3], 0.5
	v_add_f64 v[4:5], v[4:5], 0.5
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_add_f64 v[6:7], v[6:7], 0.5
	v_cvt_i32_f64_e32 v0, v[2:3]
	s_waitcnt lgkmcnt(0)
	v_mad_u64_u32 v[2:3], null, v1, 3, s[2:3]
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_cvt_i32_f64_e32 v4, v[4:5]
	v_cvt_i32_f64_e32 v5, v[6:7]
	s_delay_alu instid0(VALU_DEP_4) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_lshlrev_b16 v0, 8, v0
	v_or_b32_e32 v0, v4, v0
	s_clause 0x1
	global_store_b8 v[2:3], v5, off offset:2
	global_store_b16 v[2:3], v0, off
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z24processImageSmoothFilterP8RgbPixelmmmS0_
		.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 22
		.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	_Z24processImageSmoothFilterP8RgbPixelmmmS0_, .Lfunc_end0-_Z24processImageSmoothFilterP8RgbPixelmmmS0_
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 1884
; NumSgprs: 18
; NumVgprs: 22
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 2
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 22
; 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
      - .offset:         8
        .size:           8
        .value_kind:     by_value
      - .offset:         16
        .size:           8
        .value_kind:     by_value
      - .offset:         24
        .size:           8
        .value_kind:     by_value
      - .address_space:  global
        .offset:         32
        .size:           8
        .value_kind:     global_buffer
      - .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:           _Z24processImageSmoothFilterP8RgbPixelmmmS0_
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z24processImageSmoothFilterP8RgbPixelmmmS0_.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     22
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
