// File: /workspace/github_cpp_makefile_star2_O1/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
movq %r8, -0x58(%rbp)
movq %rsi, -0x120(%rbp)
movq %rdi, -0x60(%rbp)
movl %edx, %eax
movq %rax, -0x80(%rbp)
imulq %rax, %rax
movq %rsp, %rsi
movq %rax, -0xf0(%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, -0x88(%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, -0x68(%rbp)
cltq
movq %rax, -0xa0(%rbp)
movl %edx, -0x4c(%rbp)
cmpl $-0x1, %edx
jl 0x9d79
imull %ecx, %ecx
addl %ecx, %ecx
cvtsi2sd %rcx, %xmm1
movq -0xa0(%rbp), %rsi
movl %esi, %eax
negl %eax
cmovsl -0x68(%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, -0xe0(%rbp)
incq %r12
movapd 0x7854(%rip), %xmm2     # 0x11560
movq %rbx, -0x98(%rbp)
movsd %xmm1, -0x48(%rbp)
movq %rbx, %r14
imulq %rbx, %r14
movq %r15, -0x40(%rbp)
movq -0x98(%rbp), %r13
movl %r13d, %eax
imull %r13d, %eax
addl %r14d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorpd %xmm2, %xmm0
divsd %xmm1, %xmm0
callq 0x22c0
movapd 0x7810(%rip), %xmm2     # 0x11560
movsd -0x48(%rbp), %xmm1
movsd %xmm0, (%r15)
incq %r13
addq $0x8, %r15
cmpl %r13d, %r12d
jne 0x9d2a
incq %rbx
movq -0x40(%rbp), %r15
addq -0xe0(%rbp), %r15
cmpl %ebx, -0x30(%rbp)
jne 0x9d18
movq -0x60(%rbp), %rdi
movl 0xc(%rdi), %eax
movq -0x68(%rbp), %rcx
subl %ecx, %eax
cmpl %eax, %ecx
movl -0x4c(%rbp), %ebx
movq -0x80(%rbp), %rcx
jge 0xa292
movq -0xa0(%rbp), %rsi
leal (%rsi,%rsi), %r8d
movq -0xf0(%rbp), %rax
leaq (,%rax,4), %rdx
movq %rdx, -0x118(%rbp)
leaq (,%rax,8), %rax
movq %rax, -0x110(%rbp)
movq -0x68(%rbp), %rdx
movl %edx, %eax
negl %eax
cmovsl %edx, %eax
leal 0x1(%rdx,%rax), %eax
movq %rax, -0x100(%rbp)
movl -0x34(%rbp), %eax
movslq %eax, %r9
movq -0x58(%rbp), %r10
imull %r10d, %r10d
addl %r10d, %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10, %xmm0
movsd %xmm0, -0xe0(%rbp)
cmpl %r9d, %edx
cmovgl %edx, %eax
leaq (,%rcx,8), %r10
movq %r10, -0xd0(%rbp)
leal 0x1(%rdx,%rax), %eax
movq %rax, -0x98(%rbp)
movq %r9, -0x130(%rbp)
leaq (%rsi,%r9), %rax
movq %rax, -0x108(%rbp)
leaq (,%rcx,4), %r14
movq $0x0, -0x78(%rbp)
movl %r8d, -0xe4(%rbp)
movl %r8d, -0x6c(%rbp)
movq %rdx, -0x30(%rbp)
movq %r14, -0xb0(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0xe4(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0xa0(%rbp), %rcx
movq %rcx, -0x40(%rbp)
cmpl %eax, 0x8(%rdi)
jle 0xa26d
cmpl $0x0, 0x10(%rdi)
jle 0xa244
movl $0x0, -0x48(%rbp)
movq %rsp, -0x128(%rbp)
movq %rsp, %r14
movq -0xf0(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
testl %ebx, %ebx
jle 0x9ee1
movq %r14, %rdi
xorl %esi, %esi
movq -0x118(%rbp), %rdx
callq 0x20c0
movq -0x88(%rbp), %rdi
xorl %esi, %esi
movq -0x110(%rbp), %rdx
callq 0x20c0
movq -0x60(%rbp), %rdi
movq %r14, -0xf8(%rbp)
cmpl $-0x1, %ebx
jl 0xa025
movq -0x78(%rbp), %rax
movl %eax, -0x90(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x88(%rbp), %rbx
xorl %ecx, %ecx
movl -0x34(%rbp), %eax
movl %eax, %edx
movq %rcx, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movq %rdx, -0xb8(%rbp)
leal (%rdx,%rax), %r13d
movq -0xd8(%rbp), %rax
movl %eax, %r12d
movl -0x50(%rbp), %r14d
movq -0x98(%rbp), %r15
movq %rbx, -0xc8(%rbp)
testl %r13d, %r13d
js 0x9f77
cmpl 0xc(%rdi), %r13d
jge 0x9f77
testl %r12d, %r12d
setns %cl
movl 0x8(%rdi), %eax
cmpl %eax, %r12d
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x9f77
movq 0x18(%rdi), %rcx
movl 0x10(%rdi), %esi
movl -0x90(%rbp), %edx
imull %eax, %edx
addl %r12d, %edx
jmp 0x9f8a
movq 0x18(%rdi), %rcx
movl 0x8(%rdi), %eax
movl 0x10(%rdi), %esi
movl -0x58(%rbp), %edx
imull %eax, %edx
addl %r14d, %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
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 0x759e(%rip), %xmm0     # 0x11560
mulsd %xmm1, %xmm0
divsd -0xe0(%rbp), %xmm0
callq 0x22c0
movsd %xmm0, (%rbx)
addq $0x8, %rbx
decl %r14d
incl %r12d
decq %r15
movq -0x60(%rbp), %rdi
jne 0x9f41
movq -0xb8(%rbp), %rdx
incl %edx
movq -0xc0(%rbp), %rcx
incq %rcx
movq -0xc8(%rbp), %rbx
addq -0xd0(%rbp), %rbx
decl -0x58(%rbp)
incl -0x90(%rbp)
cmpq -0x100(%rbp), %rcx
jne 0x9f0f
movl -0x4c(%rbp), %edi
xorpd %xmm0, %xmm0
testl %edi, %edi
jle 0xa07a
movq -0xa8(%rbp), %rax
movq -0x88(%rbp), %rcx
xorl %edx, %edx
movq -0x80(%rbp), %r8
movq -0xb0(%rbp), %r14
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm1
mulsd (%rcx,%rsi,8), %xmm1
addsd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %r8
jne 0xa04d
incq %rdx
movq -0xd0(%rbp), %rsi
addq %rsi, %rcx
addq %rsi, %rax
cmpq %r8, %rdx
jne 0xa04b
jmp 0xa081
movq -0xb0(%rbp), %r14
cmpl $-0x1, %edi
jl 0xa1c5
movq -0x60(%rbp), %rax
movl 0xc(%rax), %ecx
movl %ecx, -0xc0(%rbp)
movl 0x10(%rax), %ecx
movslq 0x8(%rax), %rdx
movq 0x18(%rax), %rsi
movq -0x78(%rbp), %rax
movl %eax, %r13d
imull %edx, %r13d
addl -0x50(%rbp), %r13d
imull %ecx, %r13d
movl %edx, %eax
imull %ecx, %eax
movl %eax, -0xb8(%rbp)
movq -0xf8(%rbp), %r9
movq -0x88(%rbp), %r10
movq -0xa8(%rbp), %r11
xorl %edi, %edi
movl -0x34(%rbp), %r15d
movq %rdi, -0x90(%rbp)
movl %r15d, -0xc8(%rbp)
addl -0x30(%rbp), %r15d
setns %al
cmpl -0xc0(%rbp), %r15d
setl %r12b
andb %al, %r12b
imull %edx, %r15d
movl %r13d, -0x58(%rbp)
movq -0x130(%rbp), %rax
xorl %r8d, %r8d
movl %r13d, %edi
testb %r12b, %r12b
je 0xa145
movq -0xd8(%rbp), %rdi
addq %r8, %rdi
setns %bl
cmpq %rdx, %rdi
setl %r14b
andb %bl, %r14b
movl %r13d, %edi
cmpb $0x1, %r14b
jne 0xa145
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 0xa110
movl -0xc8(%rbp), %r15d
incl %r15d
movq -0x90(%rbp), %rdi
incq %rdi
movl -0x58(%rbp), %r13d
addl -0xb8(%rbp), %r13d
movq -0xd0(%rbp), %rax
addq %rax, %r11
addq %rax, %r10
movq -0xb0(%rbp), %r14
addq %r14, %r9
cmpq -0x100(%rbp), %rdi
jne 0xa0db
xorl %eax, %eax
movl -0x4c(%rbp), %ebx
testl %ebx, %ebx
jle 0xa207
xorl %ecx, %ecx
movq -0x60(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0xf8(%rbp), %r8
xorl %edx, %edx
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addss (%r8,%rdx,4), %xmm0
cvttss2si %xmm0, %eax
incq %rdx
cmpq %rdx, %rsi
jne 0xa1e1
incq %rcx
addq %r14, %r8
cmpq %rsi, %rcx
jne 0xa1df
jmp 0xa20b
movq -0x60(%rbp), %rdi
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 %esi, -0x48(%rbp)
cmpl 0x10(%rdi), %esi
jl 0x9e90
movq -0x40(%rbp), %rdx
incq %rdx
movslq 0x8(%rdi), %rax
movslq -0x68(%rbp), %rcx
subq %rcx, %rax
incl -0x50(%rbp)
incq -0xd8(%rbp)
movq %rdx, -0x40(%rbp)
cmpq %rax, %rdx
jl 0x9e7f
movq -0x30(%rbp), %rdx
incl %edx
movl 0xc(%rdi), %eax
subl -0x68(%rbp), %eax
incl -0x6c(%rbp)
movq -0x78(%rbp), %rcx
incl %ecx
movq %rcx, -0x78(%rbp)
movq %rdx, -0x30(%rbp)
cmpl %eax, %edx
jl 0x9e54
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop