// File: /workspace/github_cpp_makefile_star2_O1/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::gaussian_filter(Image&, Image&, int, float)
void ImageProc::gaussian_filter(Image &img_orig, Image &img_modified, int window_size, float sigma)
{
	int ws = window_size;
	float kernel[ws][ws]; //discrete convolution operator

	//gaussian_kernel(kernel, ws, sigma);
	float kernel_sum = 0.0;
    int kernel_radius = ws / 2;
    //double coeff = 1.0 / (2.0 * M_PI * (sigma * sigma));
    float frac = 0.0;

    for (int row = -kernel_radius; row <= kernel_radius; row++)
        for (int col = -kernel_radius; col <= kernel_radius; col++)
        {
            frac = exp(-1.0 * ((row*row) + (col*col)) / (2.0 * (sigma*sigma)));

            kernel[row + kernel_radius][col + kernel_radius] = frac;

            kernel_sum += kernel[row + kernel_radius][col + kernel_radius];
        }

    for (int i = 0; i < ws; i++)
        for (int j = 0; j < ws; j++)
            kernel[i][j] = kernel[i][j] / kernel_sum; //Normalized


	for(int i = 0; i < img_orig.rows; i++)
	 	for(int j = 0; j < img_orig.cols; j++)
	 		for(int k = 0; k < img_orig.channels; k++)
		 	{
				float values[ws][ws];
				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
						values[row][col] = 0;
				float values_avg = 0;

				for(int m = -kernel_radius, row = 0; m <= kernel_radius; m++, row++)
					for(int n = -kernel_radius, col = 0; n <= kernel_radius; n++, col++)
					{
						if((i+m >= 0) && (i+m < img_orig.rows) && (j+n >= 0) && (j+n < img_orig.cols))
						{
							values[row][col] = 1.0 * img_orig.getvalue((i+m),(j+n),k) * kernel[row][col];
						}
						else
						{
							values[row][col] = 1.0 * img_orig.getvalue((i+m),(j-n),k) * kernel[row][col];
						}
					}

				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
						values_avg += values[row][col];

				img_modified.setvalue(i,j,k,(int)values_avg);
			}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %edx, %r12d
movq %rsi, -0xc8(%rbp)
movq %rdi, -0x50(%rbp)
movl %edx, %ecx
movq %rcx, -0x40(%rbp)
imulq %rcx, %rcx
movq %rsp, %rax
movq %rcx, -0xb0(%rbp)
leaq 0xf(,%rcx,4), %rcx
andq $-0x10, %rcx
movq %rcx, -0xc0(%rbp)
subq %rcx, %rax
movq %rax, -0x78(%rbp)
movq %rax, %rsp
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
movq %rax, -0x70(%rbp)
negl %eax
movl %eax, -0x2c(%rbp)
xorps %xmm1, %xmm1
cmpl $-0x1, %edx
jl 0x8e43
movslq -0x2c(%rbp), %rbx
mulss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm2
addsd %xmm2, %xmm2
movq -0x70(%rbp), %rax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
movslq %eax, %r15
incl %ecx
movl %ecx, -0x88(%rbp)
leaq (%r15,%rbx), %rax
movq -0x40(%rbp), %rdx
imulq %rdx, %rax
leaq (,%r15,4), %rcx
leaq (%rcx,%rax,4), %rax
leaq (%rax,%rbx,4), %r13
addq -0x78(%rbp), %r13
leaq (,%rdx,4), %rax
movq %rax, -0x80(%rbp)
cmpq %rbx, %r15
cmovleq %rbx, %r15
incq %r15
movapd 0x87a7(%rip), %xmm3     # 0x11560
movq %rbx, -0x90(%rbp)
movsd %xmm2, -0x68(%rbp)
movq %rbx, -0x38(%rbp)
imulq %rbx, %rbx
movq %r13, -0x60(%rbp)
movq -0x90(%rbp), %r14
movss %xmm1, -0x98(%rbp)
movl %r14d, %eax
imull %r14d, %eax
addl %ebx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorpd %xmm3, %xmm0
divsd %xmm2, %xmm0
callq 0x22c0
movapd 0x875b(%rip), %xmm3     # 0x11560
movsd -0x68(%rbp), %xmm2
movss -0x98(%rbp), %xmm1
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r13)
addss %xmm0, %xmm1
incq %r14
addq $0x4, %r13
cmpl %r14d, %r15d
jne 0x8dd8
movq -0x38(%rbp), %rbx
incq %rbx
movq -0x60(%rbp), %r13
addq -0x80(%rbp), %r13
cmpl %ebx, -0x88(%rbp)
jne 0x8dc5
testl %r12d, %r12d
movq -0x50(%rbp), %rdi
jle 0x8e85
movq -0x40(%rbp), %rax
leaq (,%rax,4), %rax
xorl %ecx, %ecx
movq -0x78(%rbp), %rdx
movq -0x40(%rbp), %r8
xorl %esi, %esi
movss (%rdx,%rsi,4), %xmm0
divss %xmm1, %xmm0
movss %xmm0, (%rdx,%rsi,4)
incq %rsi
cmpq %rsi, %r8
jne 0x8e64
incq %rcx
addq %rax, %rdx
cmpq %r8, %rcx
jne 0x8e62
cmpl $0x0, 0xc(%rdi)
jle 0x9120
shlq $0x2, -0xb0(%rbp)
movl -0x2c(%rbp), %esi
movslq %esi, %rcx
movq -0x70(%rbp), %rdx
movl %edx, %eax
negl %eax
cmovsl %edx, %eax
addl %edx, %eax
incl %eax
movq %rax, -0xe0(%rbp)
movq %rcx, -0xb8(%rbp)
cmpl %ecx, %edx
movl %esi, %eax
cmovgl %edx, %eax
movq -0x40(%rbp), %rcx
leaq (,%rcx,4), %rbx
leal 0x1(%rdx,%rax), %eax
movq %rax, -0x98(%rbp)
movq $0x0, -0x58(%rbp)
movl %esi, -0x44(%rbp)
movl %r12d, -0x9c(%rbp)
movq %rbx, -0xd8(%rbp)
cmpl $0x0, 0x8(%rdi)
jle 0x910a
movq -0xb8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x70(%rbp), %rax
movl %eax, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
cmpl $0x0, 0x10(%rdi)
jle 0x90eb
xorl %r15d, %r15d
movq %rsp, -0xd0(%rbp)
movq %rsp, %r13
subq -0xc0(%rbp), %r13
movq %r13, %rsp
testl %r12d, %r12d
jle 0x8f4c
movq %r13, %rdi
xorl %esi, %esi
movq -0xb0(%rbp), %rdx
callq 0x20c0
cmpl $-0x1, %r12d
jl 0x9074
movq -0x50(%rbp), %rax
movl 0xc(%rax), %ecx
movl %ecx, -0xa4(%rbp)
movl 0x10(%rax), %ecx
movslq 0x8(%rax), %rdx
movq 0x18(%rax), %rsi
movl -0x44(%rbp), %r11d
imull %edx, %r11d
addl -0x48(%rbp), %r11d
imull %ecx, %r11d
movq %rdx, -0x68(%rbp)
movl %edx, %eax
imull %ecx, %eax
movl %eax, -0xa0(%rbp)
movq %r13, %r9
movq -0x78(%rbp), %r10
xorl %eax, %eax
movl -0x2c(%rbp), %edx
movq %rax, -0x88(%rbp)
movl %edx, -0x80(%rbp)
movl %edx, %eax
addl -0x58(%rbp), %eax
setns %r8b
cmpl -0xa4(%rbp), %eax
setl %dil
andb %r8b, %dil
imull -0x68(%rbp), %eax
movl %r11d, -0x90(%rbp)
movq -0xb8(%rbp), %r8
xorl %r14d, %r14d
movl %r11d, %r12d
testb %dil, %dil
je 0x9000
movq -0x60(%rbp), %r12
addq %r14, %r12
setns %bl
cmpq -0x68(%rbp), %r12
setl %dl
andb %bl, %dl
movl %r11d, %r12d
cmpb $0x1, %dl
jne 0x9000
movq -0x38(%rbp), %rdx
leaq (%r8,%rdx), %r12
addl %eax, %r12d
imull %ecx, %r12d
addl %r15d, %r12d
movslq %r12d, %rdx
movzbl (%rsi,%rdx), %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r10,%r14,4), %xmm1
mulsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, (%r9,%r14,4)
incq %r8
incq %r14
subl %ecx, %r11d
cmpq %r14, -0x98(%rbp)
jne 0x8fce
movl -0x80(%rbp), %edx
incl %edx
movq -0x88(%rbp), %rax
incq %rax
movl -0x90(%rbp), %r11d
addl -0xa0(%rbp), %r11d
movq -0xd8(%rbp), %rbx
addq %rbx, %r10
addq %rbx, %r9
cmpq -0xe0(%rbp), %rax
jne 0x8f99
movl -0x9c(%rbp), %r12d
testl %r12d, %r12d
jle 0x90af
xorps %xmm0, %xmm0
xorl %eax, %eax
movq -0x50(%rbp), %rdi
movq -0x40(%rbp), %rdx
xorl %ecx, %ecx
addss (%r13,%rcx,4), %xmm0
incq %rcx
cmpq %rcx, %rdx
jne 0x908f
incq %rax
addq %rbx, %r13
cmpq %rdx, %rax
jne 0x908d
cvttss2si %xmm0, %eax
jmp 0x90b5
xorl %eax, %eax
movq -0x50(%rbp), %rdi
movq -0xd0(%rbp), %rsp
movq -0xc8(%rbp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull -0x58(%rbp), %edx
addl -0x38(%rbp), %edx
imull 0x10(%rsi), %edx
addl %r15d, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r15d
cmpl 0x10(%rdi), %r15d
jl 0x8f22
movq -0x38(%rbp), %rdx
incq %rdx
movslq 0x8(%rdi), %rax
incl -0x48(%rbp)
incq -0x60(%rbp)
movq %rdx, -0x38(%rbp)
cmpq %rax, %rdx
jl 0x8f15
movq -0x58(%rbp), %rax
incl %eax
incl -0x44(%rbp)
movq %rax, -0x58(%rbp)
cmpl 0xc(%rdi), %eax
jl 0x8ef1
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop