// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, %r15
movl %ecx, %r12d
movl %edx, -0x40(%rbp)
movq %rsi, %r13
movq %rdi, %r14
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(%r15)
movq %rbx, %rdi
callq 0x10f0
movq %rax, -0x68(%rbp)
movq %rax, 0x8(%r13)
movq 0x8(%r15), %rax
movq %rax, -0x90(%rbp)
testq %rax, %rax
je 0x1797
movq 0x8(%r14), %rax
movq %rax, -0x60(%rbp)
movl (%r14), %esi
movq %rsi, -0x58(%rbp)
movl 0x4(%r14), %eax
movslq %eax, %rdi
movq %rdi, -0x70(%rbp)
movl -0x40(%rbp), %eax
decl %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
movl %eax, %ebx
decl %r12d
movl %r12d, %eax
cltd
idivl %ecx
movl %eax, %r15d
leal (%rbx,%rbx), %eax
movl %eax, -0x38(%rbp)
leal (%rsi,%rbx,2), %r14d
movq %rbx, -0x30(%rbp)
leal (%r15,%r15), %eax
movl %eax, -0x4c(%rbp)
leal (%rdi,%r15,2), %ecx
leaq 0xf(,%r14,2), %rax
andq $-0x10, %rax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq %rcx, -0x48(%rbp)
leaq 0xf(,%rcx,2), %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq 0x2ae1(%rip), %rdi      # 0x3fd0
leaq 0xb5a(%rip), %rsi       # 0x2050
callq 0x1090
movq %rax, %rdi
movl %ebx, %esi
callq 0x1140
movq %rax, %rdi
callq 0x1060
leaq 0xb40(%rip), %rsi       # 0x2054
movq 0x2ab5(%rip), %rdi      # 0x3fd0
movq %rdi, %rbx
callq 0x1090
movq %rax, %rdi
movq %r15, -0x88(%rbp)
movl %r15d, %esi
callq 0x1140
movq %rax, %rdi
callq 0x1060
leaq 0xb14(%rip), %rsi       # 0x2058
movq %rbx, %rdi
movq %rbx, %r15
callq 0x1090
movq %rax, %rdi
movq -0x58(%rbp), %rbx
movl %ebx, %esi
callq 0x1140
movq %rax, %rdi
callq 0x1060
leaq 0xaf0(%rip), %rsi       # 0x205c
movq %r15, %rdi
callq 0x1090
movq %rax, %rdi
movq -0x70(%rbp), %r15
movl %r15d, %esi
callq 0x1140
movq %rax, %rdi
callq 0x1060
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rax
leal 0x1(,%rax,2), %r9d
movslq %eax, %r10
movslq %ebx, %rsi
xorl %ecx, %ecx
testl %r14d, %r14d
cmovlel %ecx, %r14d
testl %esi, %esi
movl $0x0, %eax
cmovgl %esi, %eax
movq %rax, -0x40(%rbp)
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
movq %rdx, -0x80(%rbp)
movl %r10d, %eax
negl %eax
movq %rax, -0x78(%rbp)
testl %r9d, %r9d
movl $0x0, %r11d
cmovgl %r9d, %r11d
movslq -0x38(%rbp), %rax
leaq (%r12,%rax,2), %rbx
addq $0x2, %rbx
movq -0x68(%rbp), %r15
movq %rdi, -0x38(%rbp)
cmpq -0x80(%rbp), %rcx
je 0x169d
movq %rcx, -0x98(%rbp)
xorl %eax, %eax
cmpq %rax, %r14
je 0x163c
cmpq %r10, %rax
jge 0x1615
movq -0x30(%rbp), %rcx
jmp 0x1627
cmpq %rsi, %rax
jge 0x1623
movq -0x38(%rbp), %rcx
addq %rax, %rcx
jmp 0x162f
movq -0x78(%rbp), %rcx
addl %eax, %ecx
movslq %ecx, %rcx
addq %rdi, %rcx
movzbl (%rcx), %ecx
movw %cx, (%r12,%rax,2)
incq %rax
jmp 0x1605
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rax, %r11
je 0x1651
movswl (%r12,%rax,2), %edx
addl %edx, %ecx
incq %rax
jmp 0x1640
xorl %edi, %edi
cmpq %rdi, -0x40(%rbp)
je 0x1677
movl %ecx, %eax
cltd
idivl %r9d
movb %al, (%r15,%rdi)
movswl (%rbx,%rdi,2), %eax
movswl (%r12,%rdi,2), %edx
addl %ecx, %eax
subl %edx, %eax
incq %rdi
movl %eax, %ecx
jmp 0x1653
movq -0x98(%rbp), %rcx
incq %rcx
addq %rsi, -0x38(%rbp)
movq -0x58(%rbp), %rax
addq %rax, -0x30(%rbp)
addq %rax, -0x78(%rbp)
addq %rsi, %r15
movq -0x60(%rbp), %rdi
jmp 0x15f2
movslq -0x88(%rbp), %r9
movl -0x4c(%rbp), %r11d
movslq %r11d, %r10
orl $0x1, %r11d
xorl %eax, %eax
movq -0x48(%rbp), %r15
testl %r15d, %r15d
cmovlel %eax, %r15d
leal 0x1(%r10), %ebx
testl %ebx, %ebx
movl $0x0, %r8d
cmovlel %eax, %ebx
leaq 0x2(,%r10,2), %r14
addq %r13, %r14
xorl %ecx, %ecx
movq -0x90(%rbp), %r12
movq %r15, -0x48(%rbp)
cmpq -0x40(%rbp), %rcx
movq -0x70(%rbp), %rdi
je 0x1788
movq -0x68(%rbp), %rax
movq %rcx, -0x30(%rbp)
addq %rcx, %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x172d
cmpq %rdi, %rcx
movq %r10, %rdx
cmovlq %r8, %rdx
negq %rdx
cmpq %r9, %rcx
cmovlq %r9, %rdx
addq %rcx, %rdx
imulq %rsi, %rdx
movzbl (%rax,%rdx), %edx
movw %dx, (%r13,%rcx,2)
incq %rcx
jmp 0x16fe
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rax, %rbx
je 0x1743
movswl (%r13,%rax,2), %edx
addl %edx, %ecx
incq %rax
jmp 0x1731
movq %r12, %rdi
xorl %r15d, %r15d
cmpq %r15, -0x80(%rbp)
je 0x1772
movl %ecx, %eax
cltd
idivl %r11d
movb %al, (%r12)
movswl (%r14,%r15,2), %eax
movswl (%r13,%r15,2), %edx
addl %ecx, %eax
subl %edx, %eax
incq %r15
addq %rsi, %r12
movl %eax, %ecx
jmp 0x1749
movq -0x30(%rbp), %rcx
incq %rcx
movq %rdi, %r12
incq %r12
movq -0x48(%rbp), %r15
jmp 0x16e3
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2852(%rip), %rdi      # 0x3ff0
leaq 0x89b(%rip), %rsi       # 0x2040
callq 0x1090
pushq $0x1
popq %rdi
callq 0x10c0
nop