// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r12
movl %ecx, %ebx
movl %edx, %r14d
movq %rsi, %r13
movl (%rdi), %eax
movq %rdi, -0x30(%rbp)
movl 0x4(%rdi), %ecx
movl %ecx, 0x4(%rsi)
movl %eax, (%rsi)
movl %eax, (%r8)
movl %ecx, 0x4(%r8)
imull %eax, %ecx
movslq %ecx, %r15
movq %r15, %rdi
callq 0x2150
movq %rax, 0x8(%r12)
movq %r15, %rdi
callq 0x2150
movq %rax, -0x50(%rbp)
movq %rax, 0x8(%r13)
movq 0x8(%r12), %rax
movq %rax, -0x58(%rbp)
testq %rax, %rax
je 0x294d
movq -0x30(%rbp), %r10
movslq (%r10), %rsi
movl %esi, %r8d
movslq 0x4(%r10), %rdi
leal -0x1(%r14), %eax
shrl $0x1f, %eax
movq %r14, -0x88(%rbp)
leal (%r14,%rax), %edx
decl %edx
leal -0x1(%rbx), %eax
shrl $0x1f, %eax
movq %rbx, -0x90(%rbp)
leal (%rbx,%rax), %ecx
decl %ecx
movq %rsi, %rbx
movl %edx, %eax
andl $-0x2, %eax
leal (%rsi,%rax), %r13d
movl %ecx, -0x44(%rbp)
andl $-0x2, %ecx
movq %rcx, -0x38(%rbp)
leal (%rdi,%rcx), %r9d
leaq 0xf(,%r13,2), %rsi
andq $-0x10, %rsi
movq %rsp, %rcx
subq %rsi, %rcx
movq %rcx, %rsp
movq %r9, -0x70(%rbp)
leaq 0xf(,%r9,2), %rsi
andq $-0x10, %rsi
movq %rsp, %r15
subq %rsi, %r15
movq %r15, %rsp
movq %rdi, -0x40(%rbp)
testq %rdi, %rdi
jle 0x285a
movq 0x8(%r10), %rsi
sarl %edx
movl %eax, %r14d
orl $0x1, %r14d
movslq %edx, %rdi
movq $0x0, -0x60(%rbp)
testl %eax, %eax
movl $0x0, %r12d
cmovgl %eax, %r12d
movl %edi, %r10d
movl %edi, %edx
negl %edx
movq %rdx, -0x30(%rbp)
incq %r12
cltq
leaq (%rcx,%rax,2), %r11
addq $0x2, %r11
movq -0x50(%rbp), %r9
movq %rsi, -0x68(%rbp)
movq %rbx, -0x80(%rbp)
movq %rsi, -0x78(%rbp)
testl %r13d, %r13d
jle 0x27e9
xorl %eax, %eax
cmpq %rdi, %rax
jge 0x27c0
leal (%r10,%rax), %edx
jmp 0x27d4
cmpq %rbx, %rax
jge 0x27ce
movq -0x68(%rbp), %rdx
addq %rax, %rdx
jmp 0x27da
movq -0x30(%rbp), %rdx
addl %eax, %edx
movslq %edx, %rdx
addq %rsi, %rdx
movzbl (%rdx), %edx
movw %dx, (%rcx,%rax,2)
incq %rax
cmpq %rax, %r13
jne 0x27b5
cmpl $0x0, -0x88(%rbp)
js 0x2806
xorl %eax, %eax
xorl %esi, %esi
movswl (%rcx,%rax,2), %edx
addl %edx, %esi
incq %rax
cmpq %rax, %r12
jne 0x27f6
jmp 0x2808
xorl %esi, %esi
testl %ebx, %ebx
jle 0x282f
xorl %ebx, %ebx
movl %esi, %eax
cltd
idivl %r14d
movb %al, (%r9,%rbx)
movswl (%r11,%rbx,2), %eax
movswl (%rcx,%rbx,2), %edx
addl %esi, %eax
subl %edx, %eax
incq %rbx
movl %eax, %esi
cmpq %rbx, %r8
jne 0x280e
movq -0x60(%rbp), %rdx
incq %rdx
movq -0x80(%rbp), %rbx
addq %rbx, -0x68(%rbp)
addq %r8, -0x30(%rbp)
addq %r8, %r10
addq %rbx, %r9
movq %rdx, -0x60(%rbp)
cmpq -0x40(%rbp), %rdx
movq -0x78(%rbp), %rsi
jne 0x27ae
testl %r8d, %r8d
movq -0x40(%rbp), %r12
movq -0x38(%rbp), %r13
jle 0x293e
movl -0x44(%rbp), %eax
sarl %eax
movl %r13d, %ecx
orl $0x1, %ecx
movslq %r13d, %rdi
movslq %eax, %r9
xorl %r10d, %r10d
testl %edi, %edi
cmovlel %r10d, %r13d
movl %r12d, %r11d
incq %r13
leaq (%r15,%rdi,2), %rbx
addq $0x2, %rbx
xorl %r14d, %r14d
movq %r13, -0x38(%rbp)
cmpl $0x0, -0x70(%rbp)
jle 0x28d6
movq -0x50(%rbp), %rax
addq %r14, %rax
xorl %edx, %edx
cmpq %r12, %rdx
movq %rdi, %rsi
cmovlq %r10, %rsi
negq %rsi
cmpq %r9, %rdx
cmovlq %r9, %rsi
addq %rdx, %rsi
imulq %r8, %rsi
movzbl (%rax,%rsi), %esi
movw %si, (%r15,%rdx,2)
incq %rdx
cmpq %rdx, -0x70(%rbp)
jne 0x28a9
cmpl $0x0, -0x90(%rbp)
js 0x28f4
xorl %eax, %eax
xorl %esi, %esi
movswl (%r15,%rax,2), %edx
addl %edx, %esi
incq %rax
cmpq %rax, %r13
jne 0x28e3
jmp 0x28f6
xorl %esi, %esi
testl %r12d, %r12d
jle 0x2926
movq -0x58(%rbp), %r12
xorl %r13d, %r13d
movl %esi, %eax
cltd
idivl %ecx
movb %al, (%r12)
movswl (%rbx,%r13,2), %eax
movswl (%r15,%r13,2), %edx
addl %esi, %eax
subl %edx, %eax
incq %r13
addq %r8, %r12
movl %eax, %esi
cmpq %r13, %r11
jne 0x2902
incq %r14
incq -0x58(%rbp)
cmpq %r8, %r14
movq -0x40(%rbp), %r12
movq -0x38(%rbp), %r13
jne 0x289a
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x269c(%rip), %rdi      # 0x4ff0
leaq 0x6f2(%rip), %rsi       # 0x304d
callq 0x20e0
movl $0x1, %edi
callq 0x2120