// File: /workspace/github_cpp_makefile_star2_O1/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::median_filter(Image&, Image&, int)
void ImageProc::median_filter(Image &img_orig, Image &img_modified, int window_size)
{
	int ws = window_size;
	int kernel_radius = ws / 2;
	int values[ws*ws];
	int index = 0;
	int median_value;

	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++)
		 	{
				for(index = 0; index < (ws*ws); index++)
					values[index] = 0;

				index = 0;

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

				mergesort(values, 0, ((ws*ws)-1));
				median_value = get_median(values, (ws*ws));

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

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, -0xa8(%rbp)
movl %edx, %eax
imull %eax, %eax
movq %rsp, %r8
movq %rax, -0x78(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r8
movq %r8, %rsp
cmpl $0x0, 0xc(%rdi)
jle 0x8cd4
movl %edx, %ebx
movl %edx, %ecx
shrl $0x1f, %ecx
addl %edx, %ecx
sarl %ecx
movq -0x78(%rbp), %rax
movl %eax, %edx
shrl %edx
movq %rdx, -0x70(%rbp)
cmpl $0x1, %eax
movl %eax, %edx
adcl $0x0, %edx
decl %eax
movl %eax, -0x58(%rbp)
shlq $0x2, %rdx
movq %rdx, -0xa0(%rbp)
movl %ecx, %edx
negl %edx
movslq %edx, %rax
movl %ecx, %esi
cmovnsl %eax, %esi
movl %esi, -0x5c(%rbp)
cmpl %eax, %ecx
movq %rax, -0x80(%rbp)
cmovgl %ecx, %eax
movq %rcx, -0x98(%rbp)
leal 0x1(%rcx,%rax), %r15d
movq $0x0, -0x40(%rbp)
movl %edx, -0x54(%rbp)
movl %edx, -0x2c(%rbp)
movq %r8, -0x88(%rbp)
movl %ebx, -0x34(%rbp)
movq %rdi, -0x50(%rbp)
cmpl $0x0, 0x8(%rdi)
jle 0x8cbe
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x98(%rbp), %rax
movl %eax, -0x30(%rbp)
movq $0x0, -0x48(%rbp)
movl 0x10(%rdi), %r14d
testl %r14d, %r14d
jle 0x8c9c
xorl %r12d, %r12d
testl %ebx, %ebx
je 0x8b4b
movq %r8, %rdi
xorl %esi, %esi
movq -0xa0(%rbp), %rdx
callq 0x20c0
movq -0x88(%rbp), %r8
cmpl $-0x1, %ebx
jl 0x8c20
movq -0x50(%rbp), %rax
movl 0xc(%rax), %ecx
movl %ecx, -0x60(%rbp)
movslq 0x8(%rax), %rcx
movq 0x18(%rax), %rdx
movl -0x2c(%rbp), %esi
imull %ecx, %esi
addl -0x30(%rbp), %esi
xorl %edi, %edi
movl -0x54(%rbp), %eax
movq %rax, -0xb0(%rbp)
movl %eax, %r9d
addl -0x40(%rbp), %r9d
setns %al
cmpl -0x60(%rbp), %r9d
setl %r10b
andb %al, %r10b
imull %ecx, %r9d
movl %edi, -0x64(%rbp)
movslq %edi, %rax
leaq (%r8,%rax,4), %rax
movl %esi, -0x68(%rbp)
xorl %r11d, %r11d
movq -0x80(%rbp), %r8
movl %esi, %ebx
testb %r10b, %r10b
je 0x8bda
movq -0x90(%rbp), %rbx
addq %r11, %rbx
setns %dil
cmpq %rcx, %rbx
setl %r13b
andb %dil, %r13b
movl %esi, %ebx
cmpb $0x1, %r13b
jne 0x8bda
movq -0x48(%rbp), %rdi
leaq (%r8,%rdi), %rbx
addl %r9d, %ebx
imull %r14d, %ebx
addl %r12d, %ebx
movslq %ebx, %rdi
movzbl (%rdx,%rdi), %edi
movl %edi, (%rax,%r11,4)
incq %r8
incq %r11
decl %esi
cmpl %r11d, %r15d
jne 0x8ba8
movl -0x64(%rbp), %edi
addl %r11d, %edi
movq -0xb0(%rbp), %r8
leal 0x1(%r8), %eax
movl -0x68(%rbp), %esi
addl %ecx, %esi
cmpl -0x5c(%rbp), %r8d
movq -0x88(%rbp), %r8
jne 0x8b74
movq %r8, %rdi
xorl %esi, %esi
movl -0x58(%rbp), %edx
movq %r8, %r14
callq 0x9828
testb $0x1, -0x78(%rbp)
jne 0x8c58
movq -0x70(%rbp), %rcx
movl -0x4(%r14,%rcx,4), %eax
addl (%r14,%rcx,4), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x84d6(%rip), %xmm0     # 0x11128
cvttsd2si %xmm0, %eax
jmp 0x8c60
movq -0x70(%rbp), %rax
movl (%r14,%rax,4), %eax
movl -0x34(%rbp), %ebx
movq -0x50(%rbp), %rdi
movq %r14, %r8
movq -0xa8(%rbp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull -0x40(%rbp), %edx
addl -0x48(%rbp), %edx
imull 0x10(%rsi), %edx
addl %r12d, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r12d
movl 0x10(%rdi), %r14d
cmpl %r14d, %r12d
jl 0x8b2f
movq -0x48(%rbp), %rdx
incq %rdx
movslq 0x8(%rdi), %rax
incl -0x30(%rbp)
incq -0x90(%rbp)
movq %rdx, -0x48(%rbp)
cmpq %rax, %rdx
jl 0x8b1f
movq -0x40(%rbp), %rax
incl %eax
incl -0x2c(%rbp)
movq %rax, -0x40(%rbp)
cmpl 0xc(%rdi), %eax
jl 0x8af8
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop