#include <stdio.h>
#include <stdlib.h>
#include <cmath>

void box_blur(unsigned char* in_image, unsigned char* out_image, int width, int height, int radius) {
    const int fsize = radius; 
    int ox, oy, x, y;

    fprintf(stdout, "Doing box blur\n");

    for (y = 0; y < height; ++y) {
        for (x = 0; x < width; ++x) {
            int index_r = 4 * width * y + 4 * x + 0;
            int index_g = 4 * width * y + 4 * x + 1;
            int index_b = 4 * width * y + 4 * x + 2;
            int index_a = 4 * width * y + 4 * x + 3;

            float output_red = 0;
            float output_green = 0;
            float output_blue = 0;
            float output_alpha = 0;
            int hits = 0;

            for (ox = -fsize; ox < fsize+1; ++ox) {
                for (oy = -fsize; oy  < fsize+1; ++oy) {
                    if (x+ox > -1 && (x+ox) < width && (y+oy) > -1 && (y+oy) < height) {
                        //int currentoffset = (x + y + ox + oy)*4;
                        int currentoffset = 4 * width * (y+oy) + 4 * (x+ox);
                        output_red    += in_image[currentoffset + 0];
                        output_green  += in_image[currentoffset + 1];
                        output_blue   += in_image[currentoffset + 2];
                        output_alpha  += in_image[currentoffset + 3];
                        ++hits;
                    }
                }
            }
            out_image[index_r] = output_red / hits;
            out_image[index_g] = output_green / hits;
            out_image[index_b] = output_blue / hits;
            out_image[index_a] = output_alpha / hits;
        }
    }
}


	.file	"tmpxft_00350716_00000000-6_box_blur.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"Doing box blur\n"
	.text
	.globl	_Z8box_blurPhS_iii
	.type	_Z8box_blurPhS_iii, @function
_Z8box_blurPhS_iii:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%eax, %eax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movl	%ecx, %r13d
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movq	%rdi, %r12
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%r8d, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	%edx, %ebx
	leaq	.LC1(%rip), %rdx
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	stdout(%rip), %rdi
	movq	%rsi, (%rsp)
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movl	%ebx, %esi
	xorl	%r11d, %r11d
	xorl	%r8d, %r8d
	imull	%ebp, %esi
	leal	0(,%rbx,4), %eax
	movl	%eax, 8(%rsp)
	movl	%ebp, %eax
	negl	%eax
	movl	%eax, 12(%rsp)
	negl	%esi
.L3:
	cmpl	%r13d, %r8d
	jge	.L2
	movq	(%rsp), %rcx
	leal	0(,%r11,4), %eax
	cltq
	addq	%rcx, %rax
	xorl	%ecx, %ecx
.L11:
	cmpl	%ebx, %ecx
	jge	.L17
	xorps	%xmm0, %xmm0
	movl	%ecx, %edx
	xorl	%r14d, %r14d
	movaps	%xmm0, %xmm1
	movaps	%xmm0, %xmm2
	movaps	%xmm0, %xmm3
	subl	%ebp, %edx
.L4:
	movl	%edx, %edi
	subl	%ecx, %edi
	cmpl	%edi, %ebp
	jl	.L7
	leal	(%rdx,%rsi), %edi
	movl	12(%rsp), %r9d
	sall	$2, %edi
.L8:
	cmpl	%ebp, %r9d
	jg	.L18
	cmpl	%ebx, %edx
	jge	.L5
	leal	(%r9,%r8), %r10d
	movl	%r10d, %r15d
	orl	%edx, %r15d
	js	.L5
	cmpl	%r10d, %r13d
	jle	.L5
	movslq	%edi, %r10
	incl	%r14d
	movzbl	(%r12,%r10), %r15d
	cvtsi2ssl	%r15d, %xmm4
	movzbl	1(%r12,%r10), %r15d
	addss	%xmm4, %xmm3
	cvtsi2ssl	%r15d, %xmm4
	movzbl	2(%r12,%r10), %r15d
	movzbl	3(%r12,%r10), %r10d
	addss	%xmm4, %xmm2
	cvtsi2ssl	%r15d, %xmm4
	addss	%xmm4, %xmm1
	cvtsi2ssl	%r10d, %xmm4
	addss	%xmm4, %xmm0
.L5:
	movl	8(%rsp), %r15d
	incl	%r9d
	addl	%r15d, %edi
	jmp	.L8
.L18:
	incl	%edx
	jmp	.L4
.L7:
	cvtsi2ssl	%r14d, %xmm4
	incl	%ecx
	addq	$4, %rax
	divss	%xmm4, %xmm3
	divss	%xmm4, %xmm2
	cvttss2sil	%xmm3, %edx
	movb	%dl, -4(%rax)
	divss	%xmm4, %xmm1
	cvttss2sil	%xmm2, %edx
	movb	%dl, -3(%rax)
	divss	%xmm4, %xmm0
	cvttss2sil	%xmm1, %edx
	movb	%dl, -2(%rax)
	cvttss2sil	%xmm0, %edx
	movb	%dl, -1(%rax)
	jmp	.L11
.L17:
	incl	%r8d
	addl	%ebx, %r11d
	addl	%ebx, %esi
	jmp	.L3
.L2:
	addq	$24, %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
.LFE2027:
	.size	_Z8box_blurPhS_iii, .-_Z8box_blurPhS_iii
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2053:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2053:
	.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 <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>

void box_blur(unsigned char* in_image, unsigned char* out_image, int width, int height, int radius) {
    const int fsize = radius; 
    int ox, oy, x, y;

    fprintf(stdout, "Doing box blur\n");

    for (y = 0; y < height; ++y) {
        for (x = 0; x < width; ++x) {
            int index_r = 4 * width * y + 4 * x + 0;
            int index_g = 4 * width * y + 4 * x + 1;
            int index_b = 4 * width * y + 4 * x + 2;
            int index_a = 4 * width * y + 4 * x + 3;

            float output_red = 0;
            float output_green = 0;
            float output_blue = 0;
            float output_alpha = 0;
            int hits = 0;

            for (ox = -fsize; ox < fsize+1; ++ox) {
                for (oy = -fsize; oy  < fsize+1; ++oy) {
                    if (x+ox > -1 && (x+ox) < width && (y+oy) > -1 && (y+oy) < height) {
                        //int currentoffset = (x + y + ox + oy)*4;
                        int currentoffset = 4 * width * (y+oy) + 4 * (x+ox);
                        output_red    += in_image[currentoffset + 0];
                        output_green  += in_image[currentoffset + 1];
                        output_blue   += in_image[currentoffset + 2];
                        output_alpha  += in_image[currentoffset + 3];
                        ++hits;
                    }
                }
            }
            out_image[index_r] = output_red / hits;
            out_image[index_g] = output_green / hits;
            out_image[index_b] = output_blue / hits;
            out_image[index_a] = output_alpha / hits;
        }
    }
}


	.text
	.file	"box_blur.hip"
	.globl	_Z8box_blurPhS_iii              # -- Begin function _Z8box_blurPhS_iii
	.type	_Z8box_blurPhS_iii,@function
_Z8box_blurPhS_iii:                     # @_Z8box_blurPhS_iii
	.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	$120, %rsp
	.cfi_def_cfa_offset 176
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
                                        # kill: def $r8d killed $r8d def $r8
	movq	%r8, 56(%rsp)                   # 8-byte Spill
	movl	%ecx, %ebp
	movl	%edx, %r14d
	movq	%rsi, 104(%rsp)                 # 8-byte Spill
	movq	%rdi, %r12
	movq	stdout(%rip), %rcx
	movl	$.L.str, %edi
	movl	$15, %esi
	movl	$1, %edx
	callq	fwrite@PLT
	testl	%ebp, %ebp
	jle	.LBB0_15
# %bb.1:                                # %.preheader93.lr.ph
	leal	(,%r14,4), %ecx
	movq	56(%rsp), %rax                  # 8-byte Reload
	movl	%eax, %edx
	negl	%edx
	movl	%edx, 12(%rsp)                  # 4-byte Spill
	movslq	%edx, %rdx
	movl	%ecx, 40(%rsp)                  # 4-byte Spill
	movslq	%ecx, %rsi
	leal	1(%rax), %ecx
	movl	%ecx, 52(%rsp)                  # 4-byte Spill
	movl	%ebp, %ecx
	movq	%rcx, 80(%rsp)                  # 8-byte Spill
	movl	%r14d, %ecx
	movq	%rcx, 96(%rsp)                  # 8-byte Spill
	movq	%rdx, 88(%rsp)                  # 8-byte Spill
	imulq	%rsi, %rdx
	leal	(,%rax,4), %ecx
	negl	%ecx
	movl	%ecx, 36(%rsp)                  # 4-byte Spill
	movq	%r14, %r13
	shlq	$34, %r13
	movq	%rdx, 72(%rsp)                  # 8-byte Spill
	leaq	(%r12,%rdx), %rcx
	movq	%rcx, 64(%rsp)                  # 8-byte Spill
	leal	1(,%rax,2), %eax
	movl	%eax, 48(%rsp)                  # 4-byte Spill
	xorl	%eax, %eax
	movq	%rax, 16(%rsp)                  # 8-byte Spill
.LBB0_2:                                # %.preheader93
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
                                        #       Child Loop BB0_7 Depth 3
                                        #         Child Loop BB0_8 Depth 4
	testl	%r14d, %r14d
	jle	.LBB0_14
# %bb.3:                                # %.lr.ph119
                                        #   in Loop: Header=BB0_2 Depth=1
	movl	40(%rsp), %eax                  # 4-byte Reload
	imull	16(%rsp), %eax                  # 4-byte Folded Reload
	movl	%eax, %eax
	movq	%rax, 112(%rsp)                 # 8-byte Spill
	movl	36(%rsp), %eax                  # 4-byte Reload
	xorl	%ecx, %ecx
	movq	%rcx, 24(%rsp)                  # 8-byte Spill
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB0_7 Depth 3
                                        #         Child Loop BB0_8 Depth 4
	cmpl	$0, 56(%rsp)                    # 4-byte Folded Reload
	movl	%eax, 44(%rsp)                  # 4-byte Spill
	js	.LBB0_5
# %bb.6:                                # %.preheader.preheader
                                        #   in Loop: Header=BB0_4 Depth=2
	xorps	%xmm0, %xmm0
	xorl	%r9d, %r9d
	movl	%eax, %r8d
	movq	88(%rsp), %r15                  # 8-byte Reload
	xorps	%xmm1, %xmm1
	xorps	%xmm2, %xmm2
	xorps	%xmm3, %xmm3
.LBB0_7:                                # %.preheader
                                        #   Parent Loop BB0_2 Depth=1
                                        #     Parent Loop BB0_4 Depth=2
                                        # =>    This Loop Header: Depth=3
                                        #         Child Loop BB0_8 Depth 4
	movslq	%r8d, %rdx
	movq	72(%rsp), %rax                  # 8-byte Reload
	leal	(%rax,%r8), %ebx
	shlq	$32, %rbx
	addq	64(%rsp), %rdx                  # 8-byte Folded Reload
	movq	24(%rsp), %rax                  # 8-byte Reload
	leal	(%r15,%rax), %ecx
	movl	48(%rsp), %r11d                 # 4-byte Reload
	movl	12(%rsp), %eax                  # 4-byte Reload
.LBB0_8:                                #   Parent Loop BB0_2 Depth=1
                                        #     Parent Loop BB0_4 Depth=2
                                        #       Parent Loop BB0_7 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	cmpl	%r14d, %ecx
	jae	.LBB0_11
# %bb.9:                                #   in Loop: Header=BB0_8 Depth=4
	cmpl	%ebp, %eax
	jae	.LBB0_11
# %bb.10:                               #   in Loop: Header=BB0_8 Depth=4
	movzbl	(%rdx), %r10d
	xorps	%xmm4, %xmm4
	cvtsi2ss	%r10d, %xmm4
	movq	%rbx, %rdi
	sarq	$32, %rdi
	movq	%rdi, %r10
	orq	$1, %r10
	movzbl	(%r12,%r10), %r10d
	xorps	%xmm5, %xmm5
	cvtsi2ss	%r10d, %xmm5
	addss	%xmm4, %xmm3
	movq	%rdi, %r10
	orq	$2, %r10
	movzbl	(%r12,%r10), %r10d
	xorps	%xmm4, %xmm4
	cvtsi2ss	%r10d, %xmm4
	orq	$3, %rdi
	movzbl	(%r12,%rdi), %edi
	xorps	%xmm6, %xmm6
	cvtsi2ss	%edi, %xmm6
	addss	%xmm5, %xmm2
	addss	%xmm4, %xmm1
	addss	%xmm6, %xmm0
	incl	%r9d
.LBB0_11:                               #   in Loop: Header=BB0_8 Depth=4
	incl	%eax
	addq	%r13, %rbx
	addq	%rsi, %rdx
	decl	%r11d
	jne	.LBB0_8
# %bb.12:                               # %._crit_edge
                                        #   in Loop: Header=BB0_7 Depth=3
	incq	%r15
	addl	$4, %r8d
	cmpl	%r15d, 52(%rsp)                 # 4-byte Folded Reload
	jne	.LBB0_7
	jmp	.LBB0_13
.LBB0_5:                                #   in Loop: Header=BB0_4 Depth=2
	xorl	%r9d, %r9d
	xorps	%xmm3, %xmm3
	xorps	%xmm2, %xmm2
	xorps	%xmm1, %xmm1
	xorps	%xmm0, %xmm0
.LBB0_13:                               # %._crit_edge112
                                        #   in Loop: Header=BB0_4 Depth=2
	movq	24(%rsp), %r10                  # 8-byte Reload
	leal	(,%r10,4), %eax
	addq	112(%rsp), %rax                 # 8-byte Folded Reload
	movl	%eax, %ecx
	movq	%rcx, %rdx
	orq	$1, %rdx
	movq	%rcx, %rdi
	orq	$2, %rdi
	xorps	%xmm4, %xmm4
	cvtsi2ss	%r9d, %xmm4
	orq	$3, %rcx
	divss	%xmm4, %xmm3
	cvttss2si	%xmm3, %r8d
	movq	104(%rsp), %r9                  # 8-byte Reload
	movb	%r8b, (%r9,%rax)
	divss	%xmm4, %xmm2
	cvttss2si	%xmm2, %eax
	movb	%al, (%r9,%rdx)
	divss	%xmm4, %xmm1
	cvttss2si	%xmm1, %eax
	movb	%al, (%r9,%rdi)
	divss	%xmm4, %xmm0
	cvttss2si	%xmm0, %eax
	movb	%al, (%r9,%rcx)
	incq	%r10
	movl	44(%rsp), %eax                  # 4-byte Reload
	addl	$4, %eax
	movq	%r10, 24(%rsp)                  # 8-byte Spill
	cmpq	96(%rsp), %r10                  # 8-byte Folded Reload
	jne	.LBB0_4
.LBB0_14:                               # %._crit_edge120
                                        #   in Loop: Header=BB0_2 Depth=1
	movq	16(%rsp), %rcx                  # 8-byte Reload
	incq	%rcx
	incl	12(%rsp)                        # 4-byte Folded Spill
	addq	%rsi, 72(%rsp)                  # 8-byte Folded Spill
	addq	%rsi, 64(%rsp)                  # 8-byte Folded Spill
	movq	%rcx, %rax
	movq	%rcx, 16(%rsp)                  # 8-byte Spill
	cmpq	80(%rsp), %rcx                  # 8-byte Folded Reload
	jne	.LBB0_2
.LBB0_15:                               # %._crit_edge122
	addq	$120, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z8box_blurPhS_iii, .Lfunc_end0-_Z8box_blurPhS_iii
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Doing box blur\n"
	.size	.L.str, 16

	.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 __hip_cuid_

	.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_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
