// File: brianwu02[P]ImageProcessing/median.cpp
// Function: median(imageS*, imageS*, int, int, imageS*)
void median(imageP I1, imageP paddedImg, int sz, int avg_nbrs, imageP I2) {
    
    int i, total, paddedTotal;
    unsigned char *in, *out, *paddedIn, *buf;
    

    total = I1->width * I1->height;
    paddedTotal = paddedImg->height * paddedImg->width;

    I2->height = I1->height;
    I2->width = I1->width;

    I2->image = (unsigned char *) malloc(total);

    in = I1->image;
    out = I2->image;
    paddedIn = paddedImg->image;

    if (I2->image == NULL) {
        cerr << "not enough memory\n";
        exit(1);
    }

    // number of extra columns required.
    int m = (sz - 1) / 2; 
    // number of extra rows required. 
    int n = (sz - 1) / 2;

    int w = I1->width;
    int h = I1->height;
    int paddedWidth = paddedImg->width;
    int paddedHeight = paddedImg->height;

    // number of buffer rows required. a 3x3 kernel requires 3 rows, 5x5 requires 5 rows.
    int bufRowsRequired = m + n + 1;

    // allocate memory for an array that will hold (m + n + 1) row buffer.
    unsigned char *arrayOfPointers[bufRowsRequired];

    // allocate memory for the (m+n+1) row of buffers.
    buf = (unsigned char *) malloc(paddedWidth * bufRowsRequired);

    // assign pointers to each 'row' in buffer. If there are 3 rows required for the kernel image,
    // there will be 3 pointers in arrayOfPointers pointing to each portion of the buffer.
    for (int i=0; i<bufRowsRequired; i++) {
        arrayOfPointers[i] = &buf[i*paddedWidth];
    }

    // need to copy bufRowsRequired - 1 rows to the buffer.
    for (int y=0; i<(bufRowsRequired-1); y++) {
        copyToBuffer(paddedImg, y, bufRowsRequired, buf);
    }

    int kernel_size = (sz * sz);
    static const size_t v_size = kernel_size;
    

    // try to get this working for 3 * 3 kernel 
    unsigned char *r0, *r1, *r2;
    unsigned char k[kernel_size];
    for (int y=0; y<h; y++) {
        // need to copy the last row required in to the buffer. should be row = bufRowsRequired
        copyToBuffer(paddedImg, y + (bufRowsRequired-1), bufRowsRequired, buf);
        r0 = arrayOfPointers[y % 3] + m;
        r1 = arrayOfPointers[(y+1) % 3] + m;
        r2 = arrayOfPointers[(y+2) % 3] + m;
        for (int x=0; x<w; x++) {
            

            k[0] = r0[-1];
            k[1] = r0[0];
            k[2] = r0[1];
            k[3] = r1[-1];
            k[4] = r1[0];
            k[5] = r1[1];
            k[6] = r2[-1];
            k[7] = r2[0];
            k[8] = r2[1];


            std::sort(k, k + kernel_size);
            // because im really lazy right now.
            if (avg_nbrs == 0) {
                out[y*w+x] = k[4];
            } else if (avg_nbrs == 1) {
                out[y*w+x] = (k[3] + k[4] + k[5]) / 3;
            } else if (avg_nbrs == 2) {
                out[y*w+x] = (k[2] + k[3] + k[4] + k[5] + k[6]) / 5;
            } else if (avg_nbrs == 3) {
                out[y*w+x] = (k[1] + k[2] + k[3] + k[4] + k[5] + k[6] + k[7]) / 7;
            } else if (avg_nbrs == 4) {
                out[y*w+x] = (k[0] + k[1] + k[2] + k[3] + k[4] + k[5] + k[6] + k[7] + k[8]) / 9;
            }

            r0++;
            r1++;
            r2++;

        }
    }
    free(buf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
imull 0x4(%rcx), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
imull (%rcx), %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movslq -0x28(%rbp), %rdi
callq 0x1120
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1a39
movq 0x45cd(%rip), %rdi      # 0x5ff0
leaq 0x26c9(%rip), %rsi      # 0x40f3
callq 0x10b0
movl $0x1, %edi
callq 0x10f0
movl -0x14(%rbp), %ecx
movl %ecx, %edx
decl %edx
movl %ecx, %eax
shrl $0x1f, %edx
movl %edx, %ecx
leal -0x1(%rax,%rcx), %eax
sarl %eax
movl %eax, -0x54(%rbp)
movl -0x14(%rbp), %ecx
movl %ecx, %edx
decl %edx
movl %ecx, %eax
shrl $0x1f, %edx
movl %edx, %ecx
leal -0x1(%rax,%rcx), %eax
sarl %eax
movl %eax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x5c(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x54(%rbp), %edx
movl -0x58(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal 0x1(%rax,%rcx), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x78(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x80(%rbp)
movl -0x64(%rbp), %eax
imull -0x6c(%rbp), %eax
movslq %eax, %rdi
callq 0x1120
movq %rax, -0x50(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x1b27
movq -0xc0(%rbp), %rax
movq -0x50(%rbp), %rdx
movl -0x84(%rbp), %ecx
imull -0x64(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rdx
movslq -0x84(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x1ae5
movl $0x0, -0x88(%rbp)
movl -0x24(%rbp), %eax
movl -0x6c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1b65
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %esi
movl -0x6c(%rbp), %edx
movq -0x50(%rbp), %rcx
callq 0x1f30
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x1b31
movl -0x14(%rbp), %eax
imull -0x14(%rbp), %eax
movl %eax, -0x8c(%rbp)
cmpb $0x0, 0x4597(%rip)      # 0x6110
jne 0x1ba6
leaq 0x458e(%rip), %rdi      # 0x6110
callq 0x11a0
cmpl $0x0, %eax
je 0x1ba6
movslq -0x8c(%rbp), %rax
movq %rax, 0x456e(%rip)      # 0x6108
leaq 0x456f(%rip), %rdi      # 0x6110
callq 0x1090
movl -0x8c(%rbp), %eax
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
movq %rax, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x1f1a
movq -0x10(%rbp), %rdi
movl -0xb4(%rbp), %esi
movl -0x6c(%rbp), %eax
subl $0x1, %eax
addl %eax, %esi
movl -0x6c(%rbp), %edx
movq -0x50(%rbp), %rcx
callq 0x1f30
movl -0xb4(%rbp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movq -0xc0(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x54(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movq -0xc0(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x54(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movl -0xb4(%rbp), %eax
addl $0x2, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movq -0xc0(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x54(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movl $0x0, -0xb8(%rbp)
movl -0xb8(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x1f04
movq -0xc8(%rbp), %rdi
movq -0x98(%rbp), %rax
movb -0x1(%rax), %al
movb %al, (%rdi)
movq -0x98(%rbp), %rax
movb (%rax), %al
movb %al, 0x1(%rdi)
movq -0x98(%rbp), %rax
movb 0x1(%rax), %al
movb %al, 0x2(%rdi)
movq -0xa0(%rbp), %rax
movb -0x1(%rax), %al
movb %al, 0x3(%rdi)
movq -0xa0(%rbp), %rax
movb (%rax), %al
movb %al, 0x4(%rdi)
movq -0xa0(%rbp), %rax
movb 0x1(%rax), %al
movb %al, 0x5(%rdi)
movq -0xa8(%rbp), %rax
movb -0x1(%rax), %al
movb %al, 0x6(%rdi)
movq -0xa8(%rbp), %rax
movb (%rax), %al
movb %al, 0x7(%rdi)
movq -0xa8(%rbp), %rax
movb 0x1(%rax), %al
movb %al, 0x8(%rdi)
movslq -0x8c(%rbp), %rax
movq %rdi, %rsi
addq %rax, %rsi
callq 0x2000
cmpl $0x0, -0x18(%rbp)
jne 0x1d5b
movq -0xc8(%rbp), %rax
movb 0x4(%rax), %dl
movq -0x40(%rbp), %rax
movl -0xb4(%rbp), %ecx
imull -0x5c(%rbp), %ecx
addl -0xb8(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1eba
cmpl $0x1, -0x18(%rbp)
jne 0x1da1
movq -0xc8(%rbp), %rcx
movzbl 0x3(%rcx), %eax
movzbl 0x4(%rcx), %edx
addl %edx, %eax
movzbl 0x5(%rcx), %ecx
addl %ecx, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movb %al, %dl
movq -0x40(%rbp), %rax
movl -0xb4(%rbp), %ecx
imull -0x5c(%rbp), %ecx
addl -0xb8(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1eb8
cmpl $0x2, -0x18(%rbp)
jne 0x1df3
movq -0xc8(%rbp), %rcx
movzbl 0x2(%rcx), %eax
movzbl 0x3(%rcx), %edx
addl %edx, %eax
movzbl 0x4(%rcx), %edx
addl %edx, %eax
movzbl 0x5(%rcx), %edx
addl %edx, %eax
movzbl 0x6(%rcx), %ecx
addl %ecx, %eax
movl $0x5, %ecx
cltd
idivl %ecx
movb %al, %dl
movq -0x40(%rbp), %rax
movl -0xb4(%rbp), %ecx
imull -0x5c(%rbp), %ecx
addl -0xb8(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1eb6
cmpl $0x3, -0x18(%rbp)
jne 0x1e4e
movq -0xc8(%rbp), %rcx
movzbl 0x1(%rcx), %eax
movzbl 0x2(%rcx), %edx
addl %edx, %eax
movzbl 0x3(%rcx), %edx
addl %edx, %eax
movzbl 0x4(%rcx), %edx
addl %edx, %eax
movzbl 0x5(%rcx), %edx
addl %edx, %eax
movzbl 0x6(%rcx), %edx
addl %edx, %eax
movzbl 0x7(%rcx), %ecx
addl %ecx, %eax
movl $0x7, %ecx
cltd
idivl %ecx
movb %al, %dl
movq -0x40(%rbp), %rax
movl -0xb4(%rbp), %ecx
imull -0x5c(%rbp), %ecx
addl -0xb8(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1eb4
cmpl $0x4, -0x18(%rbp)
jne 0x1eb2
movq -0xc8(%rbp), %rcx
movzbl (%rcx), %eax
movzbl 0x1(%rcx), %edx
addl %edx, %eax
movzbl 0x2(%rcx), %edx
addl %edx, %eax
movzbl 0x3(%rcx), %edx
addl %edx, %eax
movzbl 0x4(%rcx), %edx
addl %edx, %eax
movzbl 0x5(%rcx), %edx
addl %edx, %eax
movzbl 0x6(%rcx), %edx
addl %edx, %eax
movzbl 0x7(%rcx), %edx
addl %edx, %eax
movzbl 0x8(%rcx), %ecx
addl %ecx, %eax
movl $0x9, %ecx
cltd
idivl %ecx
movb %al, %dl
movq -0x40(%rbp), %rax
movl -0xb4(%rbp), %ecx
imull -0x5c(%rbp), %ecx
addl -0xb8(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1eb4
jmp 0x1eb6
jmp 0x1eb8
jmp 0x1eba
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x1c93
jmp 0x1f06
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x1bd8
movq -0x50(%rbp), %rdi
callq 0x10e0
movq -0x78(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop