// File: brianwu02[P]ImageProcessing/blur.cpp
// Function: blur(imageS*, imageS*, int, int, imageS*)
void blur(imageP I1, imageP tmp_img, int xsz, int ysz, imageP I2) {

    int i, total, sum;
    unsigned char *in, *out, *tmp;

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

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

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

    out = I2->image;
    in = I1->image;
    tmp = tmp_img->image;

    if (I2->image == NULL) {
        cerr << "not enough mem\n";
        exit(1);
    }
    int w = I1->width;
    int h = I1->height;
    
    int s = (xsz - 1) / 2;
    int t = (ysz - 1) / 2;

    // fixed example for 5x5 kernel so 2 pad on left and right

    int bufx_size = (w + s*2);
    int bufy_size = (h + t*2);
    
    short bufx[(w + s*2)]; // s*2 is padding required.
    short bufy[(h + t*2)]; // t*2 padding required.
    
    cout << "s: " << s << endl;
    cout << "t: " << t << endl;
    cout << "w: " << w << endl;
    cout << "h: " << h << endl;

    // evaluate by row
    for (int y=0; y<h; y++) {
        // copy to row to buffer for X.
        for (int x=0; x<bufx_size; x++) {
            // pad the buffer with neighboring pixels.
            if (x < s) {
                // if x is a pad pixel to the left portion of the buffer.
                bufx[x] = in[y*w+(x+s)];
            } else if (x >= w) {
                // if x is a pad pixel to the right portion of buffer.
                //cout << x << endl;
                bufx[x] = in[y*w+(x-s)];
            } else {
                bufx[x] = in[y*w+x];
            }
        }

        // dynamically calcuate sum for rows.
        int sum = 0;
        for (int i=0; i<(s*2 + 1); i++) {
            sum += bufx[i];
        }
        for (int x=0; x<w; x++) {
            // for each row in the buffer, average the image and store in tmp_out.
            tmp[y*w+x] = sum / (s*2+1);
            sum += (bufx[x+(s*2+1)] - bufx[x]);
        }
    }

    // evaluate by column
    for (int x=0; x<w; x++) {
        //copy column to buffer Y.
        for (int y=0; y<bufy_size; y++) {
            //cout << y << endl;
            if (y < t) {
                //cout << "y<t " << y << endl;
                bufy[y] = tmp[(y+t)*w+x];
            } else if (y >= h) {
                bufy[y] = tmp[(y-(t*2)) * w+x];
            } else {
                //cout << "in else : " << y << endl;
                bufy[y] = tmp[y*w+x];
            }
        }

        // dynamically calculate sum for columns.
        int sum = 0;
        for (int i=0; i<(t*2 + 1); i++) {
            sum += bufy[i];
        }

        for (int y=0; y<h; y++) {
            // for each column in buffer, average the image and store in out 
            out[y*w+x] = sum / (t*2+1);
            sum += (bufy[y+(t*2+1)] - bufy[y]);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %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 -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movslq -0x28(%rbp), %rdi
callq 0x1110
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movslq -0x28(%rbp), %rdi
callq 0x1110
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x1586
movq 0x3a80(%rip), %rdi      # 0x4ff0
leaq 0x1ac9(%rip), %rsi      # 0x3040
callq 0x10a0
movl $0x1, %edi
callq 0x10e0
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x50(%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, -0x54(%rbp)
movl -0x18(%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)
movl -0x4c(%rbp), %edx
movl -0x54(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,2), %eax
movl %eax, -0x5c(%rbp)
movl -0x50(%rbp), %edx
movl -0x58(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,2), %eax
movl %eax, -0x60(%rbp)
movl -0x4c(%rbp), %edx
movl -0x54(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,2), %eax
movq %rsp, %rcx
movq %rcx, -0x68(%rbp)
leaq 0xf(%rax,%rax), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb0(%rbp)
movq %rcx, %rsp
movq %rax, -0x70(%rbp)
movl -0x50(%rbp), %edx
movl -0x58(%rbp), %eax
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,2), %eax
leaq 0xf(%rax,%rax), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa8(%rbp)
movq %rcx, %rsp
movq %rax, -0x78(%rbp)
movq 0x3987(%rip), %rdi      # 0x4fd0
leaq 0x1a00(%rip), %rsi      # 0x3050
callq 0x10a0
movq %rax, %rdi
movl -0x54(%rbp), %esi
callq 0x1160
movq %rax, %rdi
movq 0x3956(%rip), %rsi      # 0x4fc0
callq 0x10b0
movq 0x395a(%rip), %rdi      # 0x4fd0
leaq 0x19d7(%rip), %rsi      # 0x3054
callq 0x10a0
movq %rax, %rdi
movl -0x58(%rbp), %esi
callq 0x1160
movq %rax, %rdi
movq 0x3929(%rip), %rsi      # 0x4fc0
callq 0x10b0
movq 0x392d(%rip), %rdi      # 0x4fd0
leaq 0x19ae(%rip), %rsi      # 0x3058
callq 0x10a0
movq %rax, %rdi
movl -0x4c(%rbp), %esi
callq 0x1160
movq %rax, %rdi
movq 0x38fc(%rip), %rsi      # 0x4fc0
callq 0x10b0
movq 0x3900(%rip), %rdi      # 0x4fd0
leaq 0x1985(%rip), %rsi      # 0x305c
callq 0x10a0
movq %rax, %rdi
movl -0x50(%rbp), %esi
callq 0x1160
movq %rax, %rdi
movq 0x38cf(%rip), %rsi      # 0x4fc0
callq 0x10b0
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x18ae
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x17c1
movl -0x80(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x1752
movq -0xb0(%rbp), %rax
movq -0x38(%rbp), %rcx
movl -0x7c(%rbp), %edx
imull -0x4c(%rbp), %edx
movl -0x80(%rbp), %esi
addl -0x54(%rbp), %esi
addl %esi, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movw %cx, %dx
movslq -0x80(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x17b1
movl -0x80(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jl 0x1788
movq -0xb0(%rbp), %rax
movq -0x38(%rbp), %rcx
movl -0x7c(%rbp), %edx
imull -0x4c(%rbp), %edx
movl -0x80(%rbp), %esi
subl -0x54(%rbp), %esi
addl %esi, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movw %cx, %dx
movslq -0x80(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x17af
movq -0xb0(%rbp), %rax
movq -0x38(%rbp), %rcx
movl -0x7c(%rbp), %edx
imull -0x4c(%rbp), %edx
addl -0x80(%rbp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movw %cx, %dx
movslq -0x80(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x17b1
jmp 0x17b3
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x1710
movl $0x0, -0x84(%rbp)
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
movl -0x54(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1816
movq -0xb0(%rbp), %rax
movslq -0x88(%rbp), %rcx
movswl (%rax,%rcx,2), %eax
addl -0x84(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x17d5
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x189e
movq -0xb0(%rbp), %rcx
movl -0x84(%rbp), %eax
movl -0x54(%rbp), %esi
shll %esi
addl $0x1, %esi
cltd
idivl %esi
movb %al, %sil
movq -0x48(%rbp), %rax
movl -0x7c(%rbp), %edx
imull -0x4c(%rbp), %edx
addl -0x8c(%rbp), %edx
movslq %edx, %rdx
movb %sil, (%rax,%rdx)
movl -0x8c(%rbp), %eax
movl -0x54(%rbp), %edx
shll %edx
addl $0x1, %edx
addl %edx, %eax
cltq
movswl (%rcx,%rax,2), %eax
movslq -0x8c(%rbp), %rdx
movswl (%rcx,%rdx,2), %ecx
subl %ecx, %eax
addl -0x84(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x1820
jmp 0x18a0
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x16fd
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x1aa5
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x19ac
movl -0x94(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x1920
movq -0xa8(%rbp), %rax
movq -0x48(%rbp), %rcx
movl -0x94(%rbp), %edx
addl -0x58(%rbp), %edx
imull -0x4c(%rbp), %edx
addl -0x90(%rbp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movw %cx, %dx
movslq -0x94(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x1996
movl -0x94(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x1964
movq -0xa8(%rbp), %rax
movq -0x48(%rbp), %rcx
movl -0x94(%rbp), %edx
movl -0x58(%rbp), %esi
shll %esi
subl %esi, %edx
imull -0x4c(%rbp), %edx
addl -0x90(%rbp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movw %cx, %dx
movslq -0x94(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x1994
movq -0xa8(%rbp), %rax
movq -0x48(%rbp), %rcx
movl -0x94(%rbp), %edx
imull -0x4c(%rbp), %edx
addl -0x90(%rbp), %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movw %cx, %dx
movslq -0x94(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
jmp 0x1996
jmp 0x1998
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x18d1
movl $0x0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl -0x58(%rbp), %ecx
shll %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1a01
movq -0xa8(%rbp), %rax
movslq -0x9c(%rbp), %rcx
movswl (%rax,%rcx,2), %eax
addl -0x98(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x19c0
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x1a8f
movq -0xa8(%rbp), %rcx
movl -0x98(%rbp), %eax
movl -0x58(%rbp), %esi
shll %esi
addl $0x1, %esi
cltd
idivl %esi
movb %al, %sil
movq -0x40(%rbp), %rax
movl -0xa0(%rbp), %edx
imull -0x4c(%rbp), %edx
addl -0x90(%rbp), %edx
movslq %edx, %rdx
movb %sil, (%rax,%rdx)
movl -0xa0(%rbp), %eax
movl -0x58(%rbp), %edx
shll %edx
addl $0x1, %edx
addl %edx, %eax
cltq
movswl (%rcx,%rax,2), %eax
movslq -0xa0(%rbp), %rdx
movswl (%rcx,%rdx,2), %ecx
subl %ecx, %eax
addl -0x98(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x1a0b
jmp 0x1a91
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x18b8
movq -0x68(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)