// File: /workspace/github_cpp_makefile_star2_O2/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, -0x38(%rbp)
movl %edx, %r14d
movq %rsi, %r13
movq %rdi, %r15
movl (%rdi), %eax
movl 0x4(%rdi), %ecx
movl %ecx, 0x4(%rsi)
movl %eax, (%rsi)
movl %eax, (%r8)
movl %ecx, 0x4(%r8)
imull %eax, %ecx
movslq %ecx, %rbx
movq %rbx, %rdi
callq 0x10f0
movq %rax, 0x8(%r12)
movq %rbx, %rdi
callq 0x10f0
movq %rax, %rdi
movq %rax, 0x8(%r13)
movq 0x8(%r12), %rax
movq %rax, -0x58(%rbp)
testq %rax, %rax
je 0x173f
movq 0x8(%r15), %r11
movl (%r15), %ebx
movslq 0x4(%r15), %r8
decl %r14d
pushq $0x2
popq %rcx
movl %r14d, %eax
cltd
idivl %ecx
movl %eax, %r10d
movl -0x38(%rbp), %eax
decl %eax
cltd
idivl %ecx
movl %eax, %edx
leal (%r10,%r10), %eax
leal (%rbx,%r10,2), %r9d
leal (%rdx,%rdx), %ecx
movl %ecx, -0x44(%rbp)
movq %rdx, -0x68(%rbp)
leal (%r8,%rdx,2), %esi
leaq 0xf(,%r9,2), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, %rsp
movq %rsi, -0x40(%rbp)
leaq 0xf(,%rsi,2), %rdx
andq $-0x10, %rdx
movq %rsp, %r14
subq %rdx, %r14
movq %r14, %rsp
leal 0x1(,%r10,2), %esi
movq %r10, -0x30(%rbp)
movslq %r10d, %r10
movq %rbx, -0x70(%rbp)
movslq %ebx, %r15
xorl %r12d, %r12d
testl %r9d, %r9d
cmovlel %r12d, %r9d
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
movq %rdx, -0x38(%rbp)
testl %r8d, %r8d
movl $0x0, %r13d
movq %r8, -0x88(%rbp)
cmovgl %r8d, %r13d
movq %r11, %r8
movl %r10d, %edx
negl %edx
movq %rdx, -0x50(%rbp)
testl %esi, %esi
movl $0x0, %r11d
cmovgl %esi, %r11d
cltq
leaq (%rcx,%rax,2), %rbx
addq $0x2, %rbx
movq %rdi, -0x80(%rbp)
movq %r8, -0x60(%rbp)
movq %r8, -0x78(%rbp)
cmpq %r13, %r12
je 0x165a
movq %r12, -0x90(%rbp)
xorl %eax, %eax
movq -0x50(%rbp), %r12
cmpq %rax, %r9
je 0x15f4
cmpq %r10, %rax
jge 0x15d0
movq -0x30(%rbp), %rdx
addl %eax, %edx
jmp 0x15e2
cmpq %r15, %rax
jge 0x15de
movq -0x60(%rbp), %rdx
addq %rax, %rdx
jmp 0x15e8
leal (%r12,%rax), %edx
movslq %edx, %rdx
addq %r8, %rdx
movzbl (%rdx), %edx
movw %dx, (%rcx,%rax,2)
incq %rax
jmp 0x15be
xorl %eax, %eax
xorl %r8d, %r8d
cmpq %rax, %r11
je 0x160a
movswl (%rcx,%rax,2), %edx
addl %edx, %r8d
incq %rax
jmp 0x15f9
xorl %r12d, %r12d
cmpq %r12, -0x38(%rbp)
je 0x1634
movl %r8d, %eax
cltd
idivl %esi
movb %al, (%rdi,%r12)
movswl (%rbx,%r12,2), %eax
movswl (%rcx,%r12,2), %edx
addl %r8d, %eax
subl %edx, %eax
incq %r12
movl %eax, %r8d
jmp 0x160d
movq -0x90(%rbp), %r12
incq %r12
addq %r15, -0x60(%rbp)
movq -0x70(%rbp), %rax
addq %rax, -0x30(%rbp)
addq %rax, -0x50(%rbp)
addq %r15, %rdi
movq -0x78(%rbp), %r8
jmp 0x15a8
movslq -0x68(%rbp), %rcx
movl -0x44(%rbp), %r12d
movslq %r12d, %rdi
orl $0x1, %r12d
xorl %r8d, %r8d
movq -0x40(%rbp), %rbx
testl %ebx, %ebx
cmovlel %r8d, %ebx
leal 0x1(%rdi), %r9d
testl %r9d, %r9d
cmovlel %r8d, %r9d
leaq (%r14,%rdi,2), %r10
addq $0x2, %r10
xorl %edx, %edx
movq %rbx, -0x40(%rbp)
cmpq -0x38(%rbp), %rdx
movq -0x88(%rbp), %r11
je 0x1730
movq -0x80(%rbp), %rax
movq %rdx, -0x30(%rbp)
addq %rdx, %rax
xorl %edx, %edx
cmpq %rdx, %rbx
je 0x16db
cmpq %r11, %rdx
movq %rdi, %rsi
cmovlq %r8, %rsi
negq %rsi
cmpq %rcx, %rdx
cmovlq %rcx, %rsi
addq %rdx, %rsi
imulq %r15, %rsi
movzbl (%rax,%rsi), %esi
movw %si, (%r14,%rdx,2)
incq %rdx
jmp 0x16ad
xorl %eax, %eax
xorl %esi, %esi
cmpq %rax, %r9
je 0x16f0
movswl (%r14,%rax,2), %edx
addl %edx, %esi
incq %rax
jmp 0x16df
movq -0x58(%rbp), %rbx
xorl %r11d, %r11d
cmpq %r11, %r13
je 0x171c
movl %esi, %eax
cltd
idivl %r12d
movb %al, (%rbx)
movswl (%r10,%r11,2), %eax
movswl (%r14,%r11,2), %edx
addl %esi, %eax
subl %edx, %eax
incq %r11
addq %r15, %rbx
movl %eax, %esi
jmp 0x16f7
movq -0x30(%rbp), %rdx
incq %rdx
incq -0x58(%rbp)
movq -0x40(%rbp), %rbx
jmp 0x168f
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28aa(%rip), %rdi      # 0x3ff0
leaq 0x900(%rip), %rsi       # 0x204d
callq 0x1090
pushq $0x1
popq %rdi
callq 0x10c0