#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>



void mean_filter_h(int **img, int **res, int N, int M, int k)
{
  int count;
  float temp;
  for(int n = 0; n < N; n++) {
    for(int m = 0; m < M; m++) {
      count = 0;
      temp = 0.0;
      for(int i = N - k; i <= N + k; i++) {
        for(int j = M - k; j <= M + k; j++) {
          if(i >= 0 && i < N && j >= 0 && j < M) {
              count = count + 1;
              temp = res[n][m] + img[i][j];
          }
        }
        temp = temp / count;
        res[n][m] = (int)temp;
      }
    }
  }
}

int main()
{
  const int N = 4;
  const int M = 4;
  int k = 3;

  int *a[N], *b[N];

  for(int i = 0; i < N; i++ ) {
    a[i] = (int *)malloc(M * sizeof(int));
    b[i] = (int *)malloc(M * sizeof(int));
  }

  for(int i = 0; i < N; i++ ) {
    for(int j = 0; j < M; j++ ) {
      a[i][j] = 1;
      b[i][j] = 0;
    }
  }


  //clock_t start_h = clock();
  mean_filter_h(a, b, N, M, k / 2);
  //clock_t end_h = clock();

  //double time_h = (double)(end_h-start_h)/CLOCKS_PER_SEC;
  //printf("CPU Time: %f\n", time_h);

  for(int i = 0; i < N; i++ ) {
    for(int j = 0; j < M; j++ ) {
      printf("%d ", b[i][j]);
    }
  }

  return 0;
}

	.file	"tmpxft_0021a297_00000000-6_mean_filter_2.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3639:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3639:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z13mean_filter_hPPiS0_iii
	.type	_Z13mean_filter_hPPiS0_iii, @function
_Z13mean_filter_hPPiS0_iii:
.LFB3635:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movl	%edx, %eax
	movl	%ecx, %edx
	movq	%rdi, %r9
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rsi, %r14
	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
	movl	%ecx, %ebx
	subl	%r8d, %ebx
	movslq	%ebx, %rcx
	movl	%ebx, -16(%rsp)
	movl	%eax, %ebx
	leaq	0(,%rcx,4), %rdi
	leal	(%rax,%r8), %ecx
	subl	%r8d, %ebx
	movl	%ecx, -8(%rsp)
	movl	%ebx, -12(%rsp)
	leal	(%rdx,%r8), %ebx
	movl	%ebx, -4(%rsp)
	movq	%rdi, -24(%rsp)
	xorl	%edi, %edi
.L3:
	cmpl	%edi, %eax
	jle	.L17
	xorl	%r8d, %r8d
.L10:
	cmpl	%r8d, %edx
	jle	.L18
	movslq	-12(%rsp), %rcx
	leaq	0(,%r8,4), %r13
	xorps	%xmm0, %xmm0
	xorl	%r10d, %r10d
.L4:
	movl	-8(%rsp), %esi
	movl	%ecx, %ebx
	cmpl	%esi, %ecx
	jg	.L19
	movq	(%r14,%rdi,8), %rbp
	movq	-24(%rsp), %r11
	movl	-16(%rsp), %esi
	addq	%r13, %rbp
.L5:
	cmpl	%esi, -4(%rsp)
	jl	.L20
	movl	%ebx, %r15d
	orl	%esi, %r15d
	js	.L6
	cmpl	%eax, %ebx
	jge	.L6
	cmpl	%edx, %esi
	jge	.L6
	movq	(%r9,%rcx,8), %r12
	movl	0(%rbp), %r15d
	incl	%r10d
	addl	(%r12,%r11), %r15d
	cvtsi2ssl	%r15d, %xmm0
.L6:
	incl	%esi
	addq	$4, %r11
	jmp	.L5
.L20:
	cvtsi2ssl	%r10d, %xmm1
	incq	%rcx
	divss	%xmm1, %xmm0
	cvttss2sil	%xmm0, %esi
	movl	%esi, 0(%rbp)
	jmp	.L4
.L19:
	incq	%r8
	jmp	.L10
.L18:
	incq	%rdi
	jmp	.L3
.L17:
	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
.LFE3635:
	.size	_Z13mean_filter_hPPiS0_iii, .-_Z13mean_filter_hPPiS0_iii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC1:
	.string	"%d "
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3636:
	.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
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	subq	$88, %rsp
	.cfi_def_cfa_offset 128
	movq	%fs:40, %rax
	movq	%rax, 72(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rbp
.L22:
	movl	$16, %edi
	leaq	40(%rsp), %r12
	call	malloc@PLT
	movl	$16, %edi
	movq	%rax, 0(%rbp,%rbx)
	call	malloc@PLT
	movq	%rax, 40(%rsp,%rbx)
	addq	$8, %rbx
	cmpq	$32, %rbx
	jne	.L22
	movabsq	$4294967297, %rcx
	xorl	%eax, %eax
	xorps	%xmm0, %xmm0
.L23:
	movq	0(%rbp,%rax), %rdx
	movq	(%r12,%rax), %rsi
	addq	$8, %rax
	movq	%rcx, (%rdx)
	movq	%rcx, 8(%rdx)
	movups	%xmm0, (%rsi)
	cmpq	$32, %rax
	jne	.L23
	movq	%rbp, %rdi
	movl	$1, %r8d
	movl	$4, %ecx
	xorl	%ebp, %ebp
	movl	$4, %edx
	movq	%r12, %rsi
	leaq	.LC1(%rip), %r13
	call	_Z13mean_filter_hPPiS0_iii
.L24:
	xorl	%ebx, %ebx
.L25:
	movq	(%r12,%rbp,8), %rax
	movq	%r13, %rsi
	movl	$2, %edi
	movl	(%rax,%rbx), %edx
	xorl	%eax, %eax
	addq	$4, %rbx
	call	__printf_chk@PLT
	cmpq	$16, %rbx
	jne	.L25
	incq	%rbp
	cmpq	$4, %rbp
	jne	.L24
	movq	72(%rsp), %rax
	subq	%fs:40, %rax
	je	.L27
	call	__stack_chk_fail@PLT
.L27:
	addq	$88, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	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
.LFE3636:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3662:
	.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
.LFE3662:
	.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 <math.h>
#include <iostream>



void mean_filter_h(int **img, int **res, int N, int M, int k)
{
  int count;
  float temp;
  for(int n = 0; n < N; n++) {
    for(int m = 0; m < M; m++) {
      count = 0;
      temp = 0.0;
      for(int i = N - k; i <= N + k; i++) {
        for(int j = M - k; j <= M + k; j++) {
          if(i >= 0 && i < N && j >= 0 && j < M) {
              count = count + 1;
              temp = res[n][m] + img[i][j];
          }
        }
        temp = temp / count;
        res[n][m] = (int)temp;
      }
    }
  }
}

int main()
{
  const int N = 4;
  const int M = 4;
  int k = 3;

  int *a[N], *b[N];

  for(int i = 0; i < N; i++ ) {
    a[i] = (int *)malloc(M * sizeof(int));
    b[i] = (int *)malloc(M * sizeof(int));
  }

  for(int i = 0; i < N; i++ ) {
    for(int j = 0; j < M; j++ ) {
      a[i][j] = 1;
      b[i][j] = 0;
    }
  }


  //clock_t start_h = clock();
  mean_filter_h(a, b, N, M, k / 2);
  //clock_t end_h = clock();

  //double time_h = (double)(end_h-start_h)/CLOCKS_PER_SEC;
  //printf("CPU Time: %f\n", time_h);

  for(int i = 0; i < N; i++ ) {
    for(int j = 0; j < M; j++ ) {
      printf("%d ", b[i][j]);
    }
  }

  return 0;
}

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

                                        # End of file scope inline assembly
	.globl	_Z13mean_filter_hPPiS0_iii      # -- Begin function _Z13mean_filter_hPPiS0_iii
	.type	_Z13mean_filter_hPPiS0_iii,@function
_Z13mean_filter_hPPiS0_iii:             # @_Z13mean_filter_hPPiS0_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
	.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
                                        # kill: def $edx killed $edx def $rdx
	movq	%rsi, -16(%rsp)                 # 8-byte Spill
	testl	%edx, %edx
	jle	.LBB0_16
# %bb.1:                                # %.preheader.lr.ph
	movl	%edx, %eax
	subl	%r8d, %eax
	movl	%eax, -48(%rsp)                 # 4-byte Spill
	leal	(%r8,%rdx), %eax
	movl	%eax, -44(%rsp)                 # 4-byte Spill
	movl	%ecx, %eax
	subl	%r8d, %eax
	movl	%edx, %esi
	movq	%rsi, -24(%rsp)                 # 8-byte Spill
	movl	%ecx, %esi
	movq	%rsi, -8(%rsp)                  # 8-byte Spill
	movl	%r8d, %esi
	addl	%ecx, %r8d
	leal	1(,%rsi,2), %esi
	movl	%esi, -28(%rsp)                 # 4-byte Spill
	xorl	%esi, %esi
	movq	%rsi, -40(%rsp)                 # 8-byte Spill
.LBB0_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
                                        #       Child Loop BB0_6 Depth 3
                                        #         Child Loop BB0_8 Depth 4
	testl	%ecx, %ecx
	jle	.LBB0_15
# %bb.3:                                # %.lr.ph61
                                        #   in Loop: Header=BB0_2 Depth=1
	xorl	%r15d, %r15d
.LBB0_4:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Loop Header: Depth=2
                                        #       Child Loop BB0_6 Depth 3
                                        #         Child Loop BB0_8 Depth 4
	movl	-44(%rsp), %esi                 # 4-byte Reload
	cmpl	%esi, -48(%rsp)                 # 4-byte Folded Reload
	jg	.LBB0_14
# %bb.5:                                # %.lr.ph58
                                        #   in Loop: Header=BB0_4 Depth=2
	movq	-16(%rsp), %rsi                 # 8-byte Reload
	movq	-40(%rsp), %r9                  # 8-byte Reload
	movq	(%rsi,%r9,8), %r12
	movl	(%r12,%r15,4), %r14d
	xorl	%r13d, %r13d
	xorps	%xmm0, %xmm0
	movl	-48(%rsp), %esi                 # 4-byte Reload
	movl	%esi, %r11d
.LBB0_6:                                #   Parent Loop BB0_2 Depth=1
                                        #     Parent Loop BB0_4 Depth=2
                                        # =>    This Loop Header: Depth=3
                                        #         Child Loop BB0_8 Depth 4
	cmpl	%r8d, %eax
	jg	.LBB0_13
# %bb.7:                                # %.lr.ph
                                        #   in Loop: Header=BB0_6 Depth=3
	movl	%r11d, %esi
	movl	-28(%rsp), %ebp                 # 4-byte Reload
	movl	%eax, %r10d
.LBB0_8:                                #   Parent Loop BB0_2 Depth=1
                                        #     Parent Loop BB0_4 Depth=2
                                        #       Parent Loop BB0_6 Depth=3
                                        # =>      This Inner Loop Header: Depth=4
	testl	%r11d, %r11d
	js	.LBB0_12
# %bb.9:                                #   in Loop: Header=BB0_8 Depth=4
	cmpl	%edx, %r11d
	jge	.LBB0_12
# %bb.10:                               #   in Loop: Header=BB0_8 Depth=4
	cmpl	%ecx, %r10d
	jae	.LBB0_12
# %bb.11:                               #   in Loop: Header=BB0_8 Depth=4
	incl	%r13d
	movq	(%rdi,%rsi,8), %rbx
	movl	%r10d, %r9d
	movl	(%rbx,%r9,4), %r9d
	addl	%r14d, %r9d
	xorps	%xmm0, %xmm0
	cvtsi2ss	%r9d, %xmm0
.LBB0_12:                               #   in Loop: Header=BB0_8 Depth=4
	incl	%r10d
	decl	%ebp
	jne	.LBB0_8
.LBB0_13:                               # %._crit_edge
                                        #   in Loop: Header=BB0_6 Depth=3
	xorps	%xmm1, %xmm1
	cvtsi2ss	%r13d, %xmm1
	divss	%xmm1, %xmm0
	cvttss2si	%xmm0, %r14d
	movl	%r14d, (%r12,%r15,4)
	leal	1(%r11), %esi
	cmpl	-44(%rsp), %r11d                # 4-byte Folded Reload
	movl	%esi, %r11d
	jne	.LBB0_6
.LBB0_14:                               # %._crit_edge59
                                        #   in Loop: Header=BB0_4 Depth=2
	incq	%r15
	cmpq	-8(%rsp), %r15                  # 8-byte Folded Reload
	jne	.LBB0_4
.LBB0_15:                               # %._crit_edge62
                                        #   in Loop: Header=BB0_2 Depth=1
	movq	-40(%rsp), %r9                  # 8-byte Reload
	incq	%r9
	movq	%r9, %rsi
	movq	%r9, -40(%rsp)                  # 8-byte Spill
	cmpq	-24(%rsp), %r9                  # 8-byte Folded Reload
	jne	.LBB0_2
.LBB0_16:                               # %._crit_edge64
	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	_Z13mean_filter_hPPiS0_iii, .Lfunc_end0-_Z13mean_filter_hPPiS0_iii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.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
	xorl	%ebx, %ebx
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movl	$16, %edi
	callq	malloc
	movq	%rax, 32(%rsp,%rbx,8)
	movl	$16, %edi
	callq	malloc
	movq	%rax, (%rsp,%rbx,8)
	incq	%rbx
	cmpq	$4, %rbx
	jne	.LBB1_1
# %bb.2:                                # %.preheader24.preheader
	xorl	%eax, %eax
.LBB1_3:                                # %.preheader24
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_4 Depth 2
	movq	32(%rsp,%rax,8), %rcx
	movq	(%rsp,%rax,8), %rdx
	xorl	%esi, %esi
.LBB1_4:                                #   Parent Loop BB1_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	$1, (%rcx,%rsi,4)
	movl	$0, (%rdx,%rsi,4)
	incq	%rsi
	cmpq	$4, %rsi
	jne	.LBB1_4
# %bb.5:                                #   in Loop: Header=BB1_3 Depth=1
	incq	%rax
	cmpq	$4, %rax
	jne	.LBB1_3
# %bb.6:
	leaq	32(%rsp), %rdi
	movq	%rsp, %rsi
	movl	$4, %edx
	movl	$4, %ecx
	movl	$1, %r8d
	callq	_Z13mean_filter_hPPiS0_iii
	xorl	%ebx, %ebx
.LBB1_7:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_8 Depth 2
	movq	(%rsp,%rbx,8), %r14
	xorl	%r15d, %r15d
.LBB1_8:                                #   Parent Loop BB1_7 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%r14,%r15,4), %esi
	movl	$.L.str, %edi
	xorl	%eax, %eax
	callq	printf
	incq	%r15
	cmpq	$4, %r15
	jne	.LBB1_8
# %bb.9:                                #   in Loop: Header=BB1_7 Depth=1
	incq	%rbx
	cmpq	$4, %rbx
	jne	.LBB1_7
# %bb.10:
	xorl	%eax, %eax
	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
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"%d "
	.size	.L.str, 4

	.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
