// File: /workspace/github_cpp_makefile_star2_O3/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::bilateral_filter(Image&, Image&, int, int, int)
void ImageProc::bilateral_filter(Image &img_orig, Image &img_modified, int window_size, int sigma_dist, int sigma_simi)
{
	int ws = window_size;
	double kernel[ws][ws], intensity_diff[ws][ws]; //discrete convolution operator
	double normalization_factor_sum = 0.0;
    int kernel_radius = ws / 2;
    double 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_dist * sigma_dist)));
            kernel[row + kernel_radius][col + kernel_radius] = frac;
        }


	for(int i = kernel_radius; i < img_orig.rows - kernel_radius; i++)
	 	for(int j = kernel_radius; j < img_orig.cols - kernel_radius; 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;
						intensity_diff[row][col] = 0.0;
					}
				int value = 0;
				normalization_factor_sum = 0.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))
						{
							intensity_diff[row][col] = exp(-1.0 * ((pow((img_orig.getvalue((i+m),(j+n),k) - img_orig.getvalue(i,j,k)),2.0))
													   /(2 * (sigma_simi*sigma_simi))));
						}
						else
						{
							intensity_diff[row][col] = exp(-1.0 * ((pow((img_orig.getvalue((i-m),(j-n),k) - img_orig.getvalue(i,j,k)),2.0))
													   /(2 * (sigma_simi*sigma_simi))));
						}
//std::cout << intensity_diff[row][col] << " " << img_orig.getvalue(i+m,j+n,k) << " " << img_orig(i,j,k) << " " << i+m << " " << j+n << " " << k << " " << std::endl;
					}

				for(int row = 0; row < ws; row++)
				 	for(int col = 0; col < ws; col++)
				 		normalization_factor_sum += kernel[row][col] * intensity_diff[row][col];
//std::cout << "\nstart" << std::endl;
				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] = img_orig.getvalue((i+m),(j+n),k) * kernel[row][col] * intensity_diff[row][col] / normalization_factor_sum;
						else
							values[row][col] = img_orig.getvalue((i+m),(j-n),k) * kernel[row][col] * intensity_diff[row][col] / normalization_factor_sum;
						//std::cout << values[row][col] << " " << img_orig((i+m),(j+n),k) << " " << kernel[row][col] << " " << intensity_diff[row][col] << " " << normalization_factor_sum << std::endl;
					}

				for(int row = 0; row < ws; row++)
					for(int col = 0; col < ws; col++)
					{
						//std::cout << values[row][col] << std::endl;
						value += values[row][col];
					}

					//std::cout << value << std::endl;
					//std::cout << img_orig.getvalue(i,j,k) << "\n " << std::endl;

				img_modified.setvalue(i,j,k,value);
			}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %r8d, -0x98(%rbp)
movq %rsi, -0x120(%rbp)
movq %rdi, -0x70(%rbp)
movl %edx, %eax
movq %rax, -0x80(%rbp)
imulq %rax, %rax
movq %rsp, %rsi
movq %rax, -0xe8(%rbp)
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, -0xa8(%rbp)
movq %rsi, %rsp
movq %rsp, %rsi
subq %rax, %rsi
movq %rsi, -0x68(%rbp)
movq %rsi, %rsp
movl %edx, %eax
shrl $0x1f, %eax
addl %edx, %eax
sarl %eax
movl %eax, %esi
negl %esi
movl %esi, -0x34(%rbp)
movq %rax, -0x58(%rbp)
cltq
movq %rax, -0xa0(%rbp)
movl %edx, -0x4c(%rbp)
cmpl $-0x1, %edx
jl 0x9e15
imull %ecx, %ecx
addl %ecx, %ecx
cvtsi2sd %rcx, %xmm1
movq -0xa0(%rbp), %rsi
movl %esi, %eax
negl %eax
cmovsl -0x58(%rbp), %eax
movslq -0x34(%rbp), %rbx
incl %eax
movl %eax, -0x30(%rbp)
leaq (%rsi,%rbx), %rax
movq -0x80(%rbp), %rdx
imulq %rdx, %rax
leaq (,%rsi,8), %rcx
leaq (%rcx,%rax,8), %rax
leaq (%rax,%rbx,8), %r15
addq -0xa8(%rbp), %r15
cmpq %rbx, %rsi
movq %rbx, %r12
cmovgq %rsi, %r12
leaq (,%rdx,8), %rax
movq %rax, -0x60(%rbp)
incq %r12
movapd 0x7835(%rip), %xmm2     # 0x115e0
movq %rbx, -0xd8(%rbp)
movsd %xmm1, -0x48(%rbp)
movq %rbx, %r14
imulq %rbx, %r14
movq %r15, -0x40(%rbp)
movq -0xd8(%rbp), %r13
movl %r13d, %eax
imull %r13d, %eax
addl %r14d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorpd %xmm2, %xmm0
divsd %xmm1, %xmm0
callq 0x22b0
movapd 0x77f1(%rip), %xmm2     # 0x115e0
movsd -0x48(%rbp), %xmm1
movsd %xmm0, (%r15)
incq %r13
addq $0x8, %r15
cmpl %r13d, %r12d
jne 0x9dc9
incq %rbx
movq -0x40(%rbp), %r15
addq -0x60(%rbp), %r15
cmpl %ebx, -0x30(%rbp)
jne 0x9db7
movq -0x70(%rbp), %r8
movl 0xc(%r8), %ecx
movl %ecx, %eax
movq -0x58(%rbp), %rdx
subl %edx, %eax
cmpl %eax, %edx
movq -0x80(%rbp), %rsi
jge 0xa362
movq -0xa0(%rbp), %r9
leal (%r9,%r9), %r10d
movl -0x98(%rbp), %eax
imull %eax, %eax
addl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0xe8(%rbp), %rdx
leaq (,%rdx,4), %rax
movq %rax, -0x118(%rbp)
movq -0x58(%rbp), %rdi
movl %edi, %eax
negl %eax
cmovsl %edi, %eax
leaq (,%rdx,8), %rdx
movq %rdx, -0x110(%rbp)
leal 0x1(%rdi,%rax), %eax
movq %rax, -0x100(%rbp)
movl -0x34(%rbp), %edx
movslq %edx, %r11
movl 0x8(%r8), %eax
cmpl %r11d, %edi
cmovgl %edi, %edx
leaq (,%rsi,8), %rbx
movq %rbx, -0xc8(%rbp)
leal 0x1(%rdi,%rdx), %edx
movq %rdx, -0x98(%rbp)
movq %r11, -0x130(%rbp)
leaq (%r9,%r11), %rdx
movq %rdx, -0x108(%rbp)
leaq (,%rsi,4), %rdx
movq %rdx, -0xf8(%rbp)
movq $0x0, -0x88(%rbp)
movl %r10d, -0x78(%rbp)
movq %rdi, -0x30(%rbp)
movl %r10d, -0xdc(%rbp)
cmpl %r10d, %eax
jle 0xa338
movl 0x10(%r8), %ecx
movq -0x108(%rbp), %rdx
movq %rdx, -0xd0(%rbp)
movl %r10d, -0x74(%rbp)
movq -0xa0(%rbp), %rdx
movq %rdx, -0x40(%rbp)
movl -0x4c(%rbp), %r10d
movq -0x68(%rbp), %r14
testl %ecx, %ecx
jle 0xa307
movl $0x0, -0x48(%rbp)
movq %rsp, -0x128(%rbp)
movq %rsp, %rdi
movq -0xe8(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
testl %r10d, %r10d
movq %rdi, -0xf0(%rbp)
jle 0x9f8b
xorl %esi, %esi
movq -0x118(%rbp), %rdx
callq 0x20c0
movq %r14, %rdi
xorl %esi, %esi
movq -0x110(%rbp), %rdx
callq 0x20c0
movq -0x70(%rbp), %r8
jmp 0x9f95
cmpl $-0x1, %r10d
jl 0xa13c
movq -0x88(%rbp), %rax
movl %eax, -0x90(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x60(%rbp)
xorl %ecx, %ecx
movl -0x34(%rbp), %eax
movl %eax, %edx
movq %rcx, -0xb8(%rbp)
movq -0x30(%rbp), %rax
movq %rdx, -0xb0(%rbp)
leal (%rdx,%rax), %r12d
movq -0xd0(%rbp), %rax
movl %eax, %r13d
movq -0x58(%rbp), %r15
movq -0x98(%rbp), %rbx
movq %r14, -0xc0(%rbp)
movl 0x8(%r8), %eax
testl %r12d, %r12d
js 0xa02e
cmpl 0xc(%r8), %r12d
jge 0xa02e
testl %r13d, %r13d
setns %cl
cmpl %eax, %r13d
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xa02e
movq 0x18(%r8), %rcx
movl 0x10(%r8), %esi
movl -0x90(%rbp), %edx
imull %eax, %edx
addl %r13d, %edx
imull %esi, %edx
movl -0x48(%rbp), %edi
addl %edi, %edx
movslq %edx, %rdx
addq %rcx, %rdx
imull -0x30(%rbp), %eax
addl -0x40(%rbp), %eax
jmp 0xa05b
movq 0x18(%r8), %rcx
movl 0x10(%r8), %esi
movl -0x60(%rbp), %edx
imull %eax, %edx
movq -0x40(%rbp), %r8
addl %r15d, %edx
addl %r8d, %edx
imull %esi, %edx
movl -0x48(%rbp), %edi
addl %edi, %edx
movslq %edx, %rdx
addq %rcx, %rdx
imull -0x30(%rbp), %eax
addl %r8d, %eax
imull %esi, %eax
movzbl (%rdx), %edx
addl %edi, %eax
cltq
movzbl (%rcx,%rax), %eax
subl %eax, %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
movapd %xmm1, %xmm0
xorpd 0x7562(%rip), %xmm0     # 0x115e0
mulsd %xmm1, %xmm0
divsd -0xd8(%rbp), %xmm0
callq 0x22b0
movsd %xmm0, (%r14)
addq $0x8, %r14
decl %r15d
incl %r13d
decq %rbx
movq -0x70(%rbp), %r8
jne 0x9fe1
movq -0xb0(%rbp), %rdx
incl %edx
movq -0xb8(%rbp), %rcx
incq %rcx
movq -0xc0(%rbp), %r14
addq -0xc8(%rbp), %r14
decl -0x60(%rbp)
incl -0x90(%rbp)
cmpq -0x100(%rbp), %rcx
jne 0x9faf
movl -0x4c(%rbp), %r10d
xorpd %xmm0, %xmm0
testl %r10d, %r10d
jle 0xa132
movq -0xa8(%rbp), %rax
movq -0x68(%rbp), %r14
movq %r14, %rcx
xorl %edx, %edx
movq -0x80(%rbp), %r9
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm1
mulsd (%rcx,%rsi,8), %xmm1
addsd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %r9
jne 0xa105
incq %rdx
movq -0xc8(%rbp), %rsi
addq %rsi, %rcx
addq %rsi, %rax
cmpq %r9, %rdx
jne 0xa103
jmp 0xa136
movq -0x68(%rbp), %r14
cmpl $-0x1, %r10d
jge 0xa143
xorl %eax, %eax
jmp 0xa2c7
movl 0xc(%r8), %eax
movl %eax, -0xb8(%rbp)
movl 0x10(%r8), %ecx
movslq 0x8(%r8), %rdx
movq 0x18(%r8), %rsi
movq -0x88(%rbp), %rax
movl %eax, %r13d
imull %edx, %r13d
addl -0x74(%rbp), %r13d
imull %ecx, %r13d
movl %edx, %eax
imull %ecx, %eax
movl %eax, -0xb0(%rbp)
movq -0xf0(%rbp), %r9
movq %r14, %r10
movq -0xa8(%rbp), %r11
xorl %edi, %edi
movl -0x34(%rbp), %r15d
movq %rdi, -0x90(%rbp)
movl %r15d, -0xc0(%rbp)
addl -0x30(%rbp), %r15d
setns %al
cmpl -0xb8(%rbp), %r15d
setl %r12b
andb %al, %r12b
imull %edx, %r15d
movl %r13d, -0x60(%rbp)
movq -0x130(%rbp), %rax
xorl %r8d, %r8d
movl %r13d, %edi
testb %r12b, %r12b
je 0xa1fb
movq -0xd0(%rbp), %rdi
addq %r8, %rdi
setns %bl
cmpq %rdx, %rdi
setl %r14b
andb %bl, %r14b
movl %r13d, %edi
cmpb $0x1, %r14b
jne 0xa1fb
movq -0x40(%rbp), %rdi
addq %rax, %rdi
addl %r15d, %edi
imull %ecx, %edi
addl -0x48(%rbp), %edi
movslq %edi, %rdi
movzbl (%rsi,%rdi), %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
mulsd (%r11,%r8,8), %xmm1
mulsd (%r10,%r8,8), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, (%r9,%r8,4)
incq %rax
incq %r8
subl %ecx, %r13d
cmpq %r8, -0x98(%rbp)
jne 0xa1c6
movl -0xc0(%rbp), %r15d
incl %r15d
movq -0x90(%rbp), %rdi
incq %rdi
movl -0x60(%rbp), %r13d
addl -0xb0(%rbp), %r13d
movq -0xc8(%rbp), %rax
addq %rax, %r11
addq %rax, %r10
addq -0xf8(%rbp), %r9
cmpq -0x100(%rbp), %rdi
jne 0xa191
xorl %eax, %eax
movl -0x4c(%rbp), %r10d
testl %r10d, %r10d
jle 0xa2bf
xorl %ecx, %ecx
movq -0x70(%rbp), %r8
movq -0x80(%rbp), %rsi
movq -0xf0(%rbp), %rdi
xorl %edx, %edx
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addss (%rdi,%rdx,4), %xmm0
cvttss2si %xmm0, %eax
incq %rdx
cmpq %rdx, %rsi
jne 0xa296
incq %rcx
addq -0xf8(%rbp), %rdi
cmpq %rsi, %rcx
jne 0xa294
jmp 0xa2c3
movq -0x70(%rbp), %r8
movq -0x68(%rbp), %r14
movq -0x128(%rbp), %rsp
movq -0x120(%rbp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull -0x30(%rbp), %edx
addl -0x40(%rbp), %edx
imull 0x10(%rsi), %edx
movl -0x48(%rbp), %esi
addl %esi, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %esi
movl 0x10(%r8), %ecx
movl %esi, -0x48(%rbp)
cmpl %ecx, %esi
jl 0x9f37
movl 0x8(%r8), %eax
movq -0x40(%rbp), %r9
incq %r9
movl %eax, %edx
subl -0x58(%rbp), %edx
movslq %edx, %rdx
incq -0xd0(%rbp)
incl -0x74(%rbp)
movq %r9, -0x40(%rbp)
cmpq %rdx, %r9
jl 0x9f28
movl 0xc(%r8), %ecx
movl -0xdc(%rbp), %r10d
movq -0x30(%rbp), %rdi
incl %edi
movl %ecx, %edx
subl -0x58(%rbp), %edx
incl -0x78(%rbp)
movq -0x88(%rbp), %rsi
incl %esi
movq %rsi, -0x88(%rbp)
movq %rdi, -0x30(%rbp)
cmpl %edx, %edi
jl 0x9ef6
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop