// File: brianwu02[P]ImageProcessing/sharpen.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;

    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.
    
    for (int y=0; y<h; y++) {
        for (int x=0; x<bufx_size; x++) {
            if (x < s) {
                bufx[x] = in[y*w+(x+s)];
            } else if (x >= w) {
                bufx[x] = in[y*w+(x-s)];
            } else {
                bufx[x] = in[y*w+x];
            }
        }

        int sum = 0;
        for (int i=0; i<(s*2 + 1); i++) {
            sum += bufx[i];
        }
        for (int x=0; x<w; x++) {
            tmp[y*w+x] = sum / (s*2+1);
            sum += (bufx[x+(s*2+1)] - bufx[x]);
        }
    }

    for (int x=0; x<w; x++) {
        for (int y=0; y<bufy_size; y++) {
            if (y < t) {
                bufy[y] = tmp[(y+t)*w+x];
            } else if (y >= h) {
                bufy[y] = tmp[(y-(t*2)) * w+x];
            } else {
                bufy[y] = tmp[y*w+x];
            }
        }

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

        for (int y=0; y<h; y++) {
            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 0x1606
movq 0x3a00(%rip), %rdi      # 0x4ff0
leaq 0x1a56(%rip), %rsi      # 0x304d
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)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x187a
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x178d
movl -0x80(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x171e
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 0x177d
movl -0x80(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jl 0x1754
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 0x177b
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 0x177d
jmp 0x177f
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x16dc
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 0x17e2
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 0x17a1
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x186a
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 0x17ec
jmp 0x186c
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x16c9
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x1a71
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x1978
movl -0x94(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x18ec
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 0x1962
movl -0x94(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x1930
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 0x1960
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 0x1962
jmp 0x1964
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x189d
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 0x19cd
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 0x198c
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x1a5b
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 0x19d7
jmp 0x1a5d
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x1884
movq -0x68(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)