// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %r8, %r12
movl %ecx, %r14d
movq %rdx, -0x68(%rbp)
movq %rsi, %r13
movq %rdi, %r15
movl (%rdi), %eax
movl 0x4(%rdi), %ecx
movl %ecx, 0x4(%rsi)
imull %eax, %ecx
movl %eax, (%rsi)
movl (%rdi), %eax
movl %eax, (%r8)
movl 0x4(%rdi), %eax
movl %eax, 0x4(%r8)
movslq %ecx, %rbx
movq %rbx, %rdi
callq 0x2150
movq %rax, 0x8(%r12)
movq %rbx, %rdi
callq 0x2150
movq %rax, -0x50(%rbp)
movq %rax, 0x8(%r13)
movq 0x8(%r12), %rax
movq %rax, -0x70(%rbp)
testq %rax, %rax
je 0x2970
movslq (%r15), %r8
movslq 0x4(%r15), %rsi
movq -0x68(%rbp), %r10
leal -0x1(%r10), %eax
shrl $0x1f, %eax
leal (%r10,%rax), %edx
decl %edx
leal -0x1(%r14), %eax
shrl $0x1f, %eax
movq %r14, -0x98(%rbp)
leal (%r14,%rax), %ecx
decl %ecx
movl %edx, %eax
andl $-0x2, %eax
leal (%r8,%rax), %r9d
movl %ecx, -0x44(%rbp)
andl $-0x2, %ecx
movq %rcx, -0x38(%rbp)
leal (%rsi,%rcx), %edi
movq %r9, -0x58(%rbp)
leaq 0xf(,%r9,2), %rcx
andq $-0x10, %rcx
movq %rsp, %r12
subq %rcx, %r12
movq %r12, %rsp
movq %rdi, -0x60(%rbp)
leaq 0xf(,%rdi,2), %rcx
andq $-0x10, %rcx
movq %rsp, %rbx
subq %rcx, %rbx
movq %rbx, %rsp
movq %rsi, -0x40(%rbp)
testq %rsi, %rsi
jle 0x2870
movq %r10, %rsi
movq 0x8(%r15), %r9
sarl %edx
movl %eax, %ecx
orl $0x1, %ecx
movslq %edx, %r11
movq $0x0, -0x78(%rbp)
testl %eax, %eax
movl $0x0, %r13d
cmovgl %eax, %r13d
movl -0x58(%rbp), %edi
movl %r11d, %edx
movq %rdx, -0x30(%rbp)
movl %r11d, %edx
negl %edx
movq %rdx, -0x88(%rbp)
movl %r8d, %r10d
incq %r13
cltq
leaq (%r12,%rax,2), %r14
addq $0x2, %r14
movq -0x50(%rbp), %r15
movq %r9, -0x80(%rbp)
movq %r9, -0x90(%rbp)
cmpl $0x0, -0x58(%rbp)
jle 0x27fa
xorl %eax, %eax
cmpq %r11, %rax
jge 0x27cd
movq -0x30(%rbp), %rdx
jmp 0x27e2
cmpq %r8, %rax
jge 0x27db
movq -0x80(%rbp), %rdx
addq %rax, %rdx
jmp 0x27ea
movq -0x88(%rbp), %rdx
addl %eax, %edx
movslq %edx, %rdx
addq %r9, %rdx
movzbl (%rdx), %edx
movw %dx, (%r12,%rax,2)
incq %rax
cmpq %rax, %rdi
jne 0x27c2
testl %esi, %esi
js 0x2813
xorl %eax, %eax
xorl %esi, %esi
movswl (%r12,%rax,2), %edx
addl %edx, %esi
incq %rax
cmpq %rax, %r13
jne 0x2802
jmp 0x2815
xorl %esi, %esi
testl %r8d, %r8d
jle 0x283e
xorl %r9d, %r9d
movl %esi, %eax
cltd
idivl %ecx
movb %al, (%r15,%r9)
movswl (%r14,%r9,2), %eax
movswl (%r12,%r9,2), %edx
addl %esi, %eax
subl %edx, %eax
incq %r9
movl %eax, %esi
cmpq %r9, %r10
jne 0x281d
movq -0x78(%rbp), %rdx
incq %rdx
addq %r8, -0x80(%rbp)
addq %r10, -0x88(%rbp)
addq %r10, -0x30(%rbp)
addq %r8, %r15
movq %rdx, -0x78(%rbp)
cmpq -0x40(%rbp), %rdx
movq -0x68(%rbp), %rsi
movq -0x90(%rbp), %r9
jne 0x27ba
testl %r8d, %r8d
movq -0x40(%rbp), %r13
jle 0x2961
movl -0x44(%rbp), %edx
sarl %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
orl $0x1, %ecx
movslq %eax, %rdi
movslq %edx, %r9
xorl %r10d, %r10d
testl %edi, %edi
cmovlel %r10d, %eax
movl -0x60(%rbp), %r11d
movl %r13d, %r14d
movq %r9, %rdx
imulq %r8, %rdx
movq %rdx, -0x30(%rbp)
incq %rax
movq %rax, -0x38(%rbp)
leaq (%rbx,%rdi,2), %r12
addq $0x2, %r12
cmpl $0x0, -0x60(%rbp)
jle 0x28fa
movq -0x50(%rbp), %rax
addq %r10, %rax
movq -0x30(%rbp), %rdx
xorl %esi, %esi
movq %rdx, %r15
cmpq %r9, %rsi
jl 0x28e5
movq %rsi, %r15
cmpq %r13, %rsi
jl 0x28e1
subq %rdi, %r15
imulq %r8, %r15
movzbl (%rax,%r15), %r15d
movw %r15w, (%rbx,%rsi,2)
incq %rsi
addq %r8, %rdx
cmpq %rsi, %r11
jne 0x28ce
cmpl $0x0, -0x98(%rbp)
js 0x291b
xorl %eax, %eax
xorl %esi, %esi
movq -0x38(%rbp), %r15
movswl (%rbx,%rax,2), %edx
addl %edx, %esi
incq %rax
cmpq %rax, %r15
jne 0x290b
jmp 0x291d
xorl %esi, %esi
testl %r13d, %r13d
jle 0x294d
movq -0x70(%rbp), %r13
xorl %r15d, %r15d
movl %esi, %eax
cltd
idivl %ecx
movb %al, (%r13)
movswl (%r12,%r15,2), %eax
movswl (%rbx,%r15,2), %edx
addl %esi, %eax
subl %edx, %eax
incq %r15
addq %r8, %r13
movl %eax, %esi
cmpq %r15, %r14
jne 0x2929
incq %r10
incq -0x70(%rbp)
cmpq %r8, %r10
movq -0x40(%rbp), %r13
jne 0x28bb
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2679(%rip), %rdi      # 0x4ff0
leaq 0x6cf(%rip), %rsi       # 0x304d
callq 0x20e0
movl $0x1, %edi
callq 0x2120