// File: /workspace/github_cpp_makefile_star2_O3/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, -0xa0(%rbp)
movl %edx, -0x2c(%rbp)
movl %edx, %eax
imull %eax, %eax
movq %rsp, %rbx
movq %rax, -0x70(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rdi, -0x38(%rbp)
movl 0xc(%rdi), %ecx
testl %ecx, %ecx
jle 0x8dd0
movl -0x2c(%rbp), %eax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movq -0x70(%rbp), %rax
leal -0x1(%rax), %edx
movl %edx, -0x58(%rbp)
movl %eax, %edi
shrl %edi
cmpq $0x1, %rax
adcq $0x0, %rax
shlq $0x2, %rax
movq %rax, -0x90(%rbp)
movl %esi, %r8d
negl %r8d
movslq %r8d, %rdx
movl %esi, %eax
cmovnsl %edx, %eax
movl %eax, -0x5c(%rbp)
movq -0x38(%rbp), %rax
movl 0x8(%rax), %eax
cmpl %edx, %esi
movq %rdx, -0x78(%rbp)
cmovgl %esi, %edx
movq %rsi, -0x88(%rbp)
leal 0x1(%rsi,%rdx), %r14d
movq $0x0, -0x48(%rbp)
movl %r8d, -0x54(%rbp)
movl %r8d, -0x3c(%rbp)
movq %rbx, -0xa8(%rbp)
movq %rdi, -0x98(%rbp)
testl %eax, %eax
jle 0x8dbb
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %r12d
movq -0x78(%rbp), %rcx
movq %rcx, -0x80(%rbp)
movq -0x88(%rbp), %rcx
movl %ecx, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
testl %r12d, %r12d
jle 0x8d96
xorl %r15d, %r15d
cmpl $0x0, -0x2c(%rbp)
je 0x8c5a
movq %rbx, %rdi
xorl %esi, %esi
movq -0x90(%rbp), %rdx
callq 0x20c0
cmpl $-0x1, -0x2c(%rbp)
jl 0x8d25
movq -0x38(%rbp), %rax
movl 0xc(%rax), %ecx
movl %ecx, -0x60(%rbp)
movslq 0x8(%rax), %rcx
movq 0x18(%rax), %rdx
movl -0x3c(%rbp), %esi
imull %ecx, %esi
addl -0x40(%rbp), %esi
xorl %edi, %edi
movl -0x54(%rbp), %eax
movl %eax, %r8d
movl %edi, -0x64(%rbp)
movslq %edi, %rax
leaq (%rbx,%rax,4), %r9
movq %r8, -0xb0(%rbp)
movl %r8d, %r10d
addl -0x48(%rbp), %r10d
setns %al
cmpl -0x60(%rbp), %r10d
setl %r11b
andb %al, %r11b
imull %ecx, %r10d
movl %esi, -0x68(%rbp)
xorl %r13d, %r13d
movq -0x78(%rbp), %r8
movl %esi, %eax
testb %r11b, %r11b
je 0x8cdd
movq -0x80(%rbp), %rax
addq %r13, %rax
setns %dil
cmpq %rcx, %rax
setl %bl
andb %dil, %bl
movl %esi, %eax
cmpb $0x1, %bl
jne 0x8cdd
movq -0x50(%rbp), %rax
addq %r8, %rax
addl %r10d, %eax
imull %r12d, %eax
addl %r15d, %eax
cltq
movzbl (%rdx,%rax), %eax
movl %eax, (%r9,%r13,4)
incq %r8
incq %r13
decl %esi
cmpl %r13d, %r14d
jne 0x8cb1
movq -0xb0(%rbp), %r8
leal 0x1(%r8), %eax
movl -0x64(%rbp), %edi
addl %r14d, %edi
movl -0x68(%rbp), %esi
addl %ecx, %esi
cmpl -0x5c(%rbp), %r8d
movl %eax, %r8d
movq -0xa8(%rbp), %rbx
jne 0x8c7d
movq %rbx, %rdi
xorl %esi, %esi
movl -0x58(%rbp), %edx
callq 0x98b6
movq -0x98(%rbp), %rdi
movl (%rbx,%rdi,4), %eax
testb $0x1, -0x70(%rbp)
jne 0x8d59
addl -0x4(%rbx,%rdi,4), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x83d3(%rip), %xmm0     # 0x11128
cvttsd2si %xmm0, %eax
movq -0xa0(%rbp), %rsi
movq 0x18(%rsi), %rcx
movl 0x8(%rsi), %edx
imull -0x48(%rbp), %edx
addl -0x50(%rbp), %edx
imull 0x10(%rsi), %edx
addl %r15d, %edx
movslq %edx, %rdx
movb %al, (%rcx,%rdx)
incl %r15d
movq -0x38(%rbp), %rax
movl 0x10(%rax), %r12d
cmpl %r12d, %r15d
jl 0x8c39
movq -0x38(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x50(%rbp), %rsi
incq %rsi
movslq %eax, %rcx
incl -0x40(%rbp)
incq -0x80(%rbp)
movq %rsi, -0x50(%rbp)
cmpq %rcx, %rsi
jl 0x8c2d
movq -0x38(%rbp), %rcx
movl 0xc(%rcx), %ecx
movq -0x48(%rbp), %rdx
incl %edx
incl -0x3c(%rbp)
movq %rdx, -0x48(%rbp)
cmpl %ecx, %edx
jl 0x8c03
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop