// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rsi, -0xb8(%rbp)
movq %rdi, -0x30(%rbp)
movl %edx, %esi
movq %rsi, %rax
imulq %rsi, %rax
movq %rsp, %r13
movq %rax, -0x98(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rax, -0xb0(%rbp)
subq %rax, %r13
movq %r13, %rsp
movl %edx, %ecx
shrl $0x1f, %ecx
addl %edx, %ecx
sarl %ecx
movl %ecx, %r12d
negl %r12d
movl %edx, -0x48(%rbp)
cmpl $-0x1, %edx
movq %rsi, -0x68(%rbp)
movq %rcx, -0x88(%rbp)
movq %r13, -0x90(%rbp)
jge 0x8e5c
xorl %r8d, %r8d
jmp 0x8f45
movslq %r12d, %r14
mulss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm2
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
addsd %xmm2, %xmm2
leaq (,%rsi,4), %rdx
movq %rdx, -0x38(%rbp)
leal (%rax,%rcx), %r15d
incl %r15d
incl %eax
movl %eax, -0x3c(%rbp)
xorps %xmm1, %xmm1
movapd 0x874b(%rip), %xmm3     # 0x115e0
movsd %xmm2, -0x60(%rbp)
movq %r14, -0x58(%rbp)
imulq %r14, %r14
xorl %ebx, %ebx
movss %xmm1, -0x70(%rbp)
leal (%r12,%rbx), %eax
imull %eax, %eax
addl %r14d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorpd %xmm3, %xmm0
divsd %xmm2, %xmm0
callq 0x22b0
movapd 0x8711(%rip), %xmm3     # 0x115e0
movsd -0x60(%rbp), %xmm2
movss -0x70(%rbp), %xmm1
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r13,%rbx,4)
addss %xmm0, %xmm1
incq %rbx
cmpl %ebx, %r15d
jne 0x8ea4
movq -0x58(%rbp), %r14
incq %r14
addq -0x38(%rbp), %r13
cmpl %r14d, -0x3c(%rbp)
jne 0x8e9a
cmpl $0x0, -0x48(%rbp)
setg %r8b
jle 0x8f3e
xorl %eax, %eax
movq -0x90(%rbp), %rcx
movq -0x68(%rbp), %rsi
xorl %edx, %edx
movss (%rcx,%rdx,4), %xmm0
divss %xmm1, %xmm0
movss %xmm0, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rsi
jne 0x8f1a
incq %rax
addq -0x38(%rbp), %rcx
cmpq %rsi, %rax
jne 0x8f18
jmp 0x8f45
xorl %r8d, %r8d
movq -0x68(%rbp), %rsi
movq -0x30(%rbp), %rax
movl 0xc(%rax), %ecx
testl %ecx, %ecx
jle 0x91e8
shlq $0x2, -0x98(%rbp)
movslq %r12d, %rdx
movq -0x88(%rbp), %rdi
movl %edi, %eax
negl %eax
cmovsl %edi, %eax
leal 0x1(%rdi,%rax), %eax
movq %rax, -0xd0(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
movq %rdx, -0xa0(%rbp)
cmpl %edx, %edi
movl %r12d, %edx
cmovgl %edi, %edx
leaq (,%rsi,4), %rbx
leal 0x1(%rdi,%rdx), %edx
movq %rdx, -0x70(%rbp)
movq $0x0, -0x50(%rbp)
movl %r12d, -0x40(%rbp)
movq %r12, -0xa8(%rbp)
movl %r8d, -0x74(%rbp)
movq %rbx, -0xc8(%rbp)
testl %eax, %eax
jle 0x91d3
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq -0xa0(%rbp), %rdx
movq %rdx, -0x58(%rbp)
movq -0x88(%rbp), %rdx
movl %edx, -0x44(%rbp)
movq $0x0, -0x38(%rbp)
testl %ecx, %ecx
jle 0x91ae
xorl %r15d, %r15d
movq %rsp, -0xc0(%rbp)
movq %rsp, %r13
subq -0xb0(%rbp), %r13
movq %r13, %rsp
testb %r8b, %r8b
je 0x901d
movq %r13, %rdi
xorl %esi, %esi
movq -0x98(%rbp), %rdx
callq 0x20c0
cmpl $-0x1, -0x48(%rbp)
jl 0x9135
movq -0x30(%rbp), %rax
movl 0xc(%rax), %ecx
movl %ecx, -0x7c(%rbp)
movl 0x10(%rax), %ecx
movslq 0x8(%rax), %rdx
movq 0x18(%rax), %rsi
movl -0x40(%rbp), %r11d
imull %edx, %r11d
addl -0x44(%rbp), %r11d
imull %ecx, %r11d
movq %rdx, -0x60(%rbp)
movl %edx, %eax
imull %ecx, %eax
movl %eax, -0x78(%rbp)
movq %r13, %r9
movq -0x90(%rbp), %r10
xorl %edx, %edx
movq -0xa8(%rbp), %rax
movq %rdx, -0xd8(%rbp)
movl %eax, -0x80(%rbp)
addl -0x50(%rbp), %eax
setns %r8b
cmpl -0x7c(%rbp), %eax
setl %dil
andb %r8b, %dil
imull -0x60(%rbp), %eax
movl %r11d, -0x3c(%rbp)
movq -0xa0(%rbp), %r14
xorl %r8d, %r8d
movl %r11d, %r12d
testb %dil, %dil
je 0x90ca
movq -0x58(%rbp), %r12
addq %r8, %r12
setns %bl
cmpq -0x60(%rbp), %r12
setl %dl
andb %bl, %dl
movl %r11d, %r12d
cmpb $0x1, %dl
jne 0x90ca
movq -0x38(%rbp), %rdx
leaq (%r14,%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,%r8,4), %xmm1
mulsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, (%r9,%r8,4)
incq %r14
incq %r8
subl %ecx, %r11d
cmpq %r8, -0x70(%rbp)
jne 0x9098
movl -0x80(%rbp), %eax
incl %eax
movq -0xd8(%rbp), %rdx
incq %rdx
movl -0x3c(%rbp), %r11d
addl -0x78(%rbp), %r11d
movq -0xc8(%rbp), %rbx
addq %rbx, %r10
addq %rbx, %r9
cmpq -0xd0(%rbp), %rdx
jne 0x906b
movl -0x74(%rbp), %r8d
testb %r8b, %r8b
je 0x9169
xorps %xmm0, %xmm0
xorl %eax, %eax
movq -0x68(%rbp), %rdx
xorl %ecx, %ecx
addss (%r13,%rcx,4), %xmm0
incq %rcx
cmpq %rcx, %rdx
jne 0x9149
incq %rax
addq %rbx, %r13
cmpq %rdx, %rax
jne 0x9147
cvttss2si %xmm0, %eax
jmp 0x916b
xorl %eax, %eax
movq -0xc0(%rbp), %rsp
movq -0xb8(%rbp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull -0x50(%rbp), %edx
addl -0x38(%rbp), %edx
imull 0x10(%rsi), %edx
addl %r15d, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r15d
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
cmpl %ecx, %r15d
jl 0x8ff3
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x38(%rbp), %rdi
incq %rdi
movslq %eax, %rdx
incl -0x44(%rbp)
incq -0x58(%rbp)
movq %rdi, -0x38(%rbp)
cmpq %rdx, %rdi
jl 0x8fe8
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %ecx
movq -0x50(%rbp), %rdx
incl %edx
incl -0x40(%rbp)
movq %rdx, -0x50(%rbp)
cmpl %ecx, %edx
jl 0x8fbc
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop