// File: 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
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %eax
imull %eax, %eax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xdf6f
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xdf5f
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0xdf4f
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl -0x18(%rbp), %ecx
imull -0x18(%rbp), %ecx
cmpl %ecx, %eax
jge 0xde2d
movq -0x58(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xde05
movl $0x0, -0x34(%rbp)
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jg 0xdf03
xorl %eax, %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jg 0xdef3
movl -0x3c(%rbp), %eax
addl -0x48(%rbp), %eax
cmpl $0x0, %eax
jl 0xdeb7
movl -0x3c(%rbp), %eax
addl -0x48(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0xdeb7
movl -0x40(%rbp), %eax
addl -0x4c(%rbp), %eax
cmpl $0x0, %eax
jl 0xdeb7
movl -0x40(%rbp), %eax
addl -0x4c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0xdeb7
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
addl -0x48(%rbp), %esi
movl -0x40(%rbp), %edx
addl -0x4c(%rbp), %edx
movl -0x44(%rbp), %ecx
callq 0x83b0
movl %eax, %edx
movq -0x58(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0xdedc
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
addl -0x48(%rbp), %esi
movl -0x40(%rbp), %edx
subl -0x4c(%rbp), %edx
movl -0x44(%rbp), %ecx
callq 0x83b0
movl %eax, %edx
movq -0x58(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xde50
jmp 0xdef5
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xde3c
movq -0x58(%rbp), %rdi
movl -0x18(%rbp), %edx
imull -0x18(%rbp), %edx
subl $0x1, %edx
xorl %esi, %esi
callq 0xf130
movq -0x58(%rbp), %rdi
movl -0x18(%rbp), %esi
imull -0x18(%rbp), %esi
callq 0xf1a0
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl -0x40(%rbp), %edx
movl -0x44(%rbp), %ecx
movl -0x38(%rbp), %r8d
callq 0x83f0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xddee
jmp 0xdf51
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0xddd7
jmp 0xdf61
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xddc0
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)