// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %r8, %r15
movq %rcx, -0x68(%rbp)
movl %edx, %r13d
movq %rsi, %r12
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 0x2140
movq %rax, 0x8(%r15)
movq %rbx, %rdi
callq 0x2140
movq %rax, -0x58(%rbp)
movq %rax, 0x8(%r12)
movq 0x8(%r15), %rax
movq %rax, -0x70(%rbp)
testq %rax, %rax
je 0x28ce
movq 0x8(%r14), %rax
movq %rax, -0x60(%rbp)
movl (%r14), %r15d
movslq 0x4(%r14), %rsi
movq %rsi, -0x38(%rbp)
leal -0x1(%r13), %eax
shrl $0x1f, %eax
movq %r13, -0x90(%rbp)
leal (%rax,%r13), %ecx
decl %ecx
movl %ecx, %ebx
sarl %ebx
movq -0x68(%rbp), %rdx
leal -0x1(%rdx), %eax
shrl $0x1f, %eax
addl %edx, %eax
decl %eax
movl %eax, %edx
sarl %edx
movl %edx, -0x2c(%rbp)
andl $-0x2, %ecx
movq %rcx, -0x50(%rbp)
leal (%r15,%rcx), %edx
andl $-0x2, %eax
movq %rax, -0x48(%rbp)
leal (%rsi,%rax), %ecx
movq %rdx, -0x80(%rbp)
leaq 0xf(,%rdx,2), %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rcx, -0x88(%rbp)
leaq 0xf(,%rcx,2), %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq 0x2a26(%rip), %r12      # 0x4fd0
leaq 0xa9f(%rip), %rsi       # 0x3050
movl $0x3, %edx
movq %r12, %rdi
callq 0x20f0
movq %r12, %rdi
movq %rbx, -0x40(%rbp)
movl %ebx, %esi
callq 0x2190
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
leaq 0xa57(%rip), %rsi       # 0x3054
movl $0x3, %edx
movq %r12, %rbx
movq %r12, %rdi
callq 0x20f0
movq %r12, %rdi
movl -0x2c(%rbp), %esi
callq 0x2190
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x20c0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
leaq 0xa0f(%rip), %rsi       # 0x3058
movl $0x3, %edx
movq %rbx, %rdi
callq 0x20f0
movq %rbx, %rdi
movl %r15d, %esi
callq 0x2190
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x20c0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
leaq 0x9ca(%rip), %rsi       # 0x305c
movl $0x3, %edx
movq %rbx, %rdi
callq 0x20f0
movq %rbx, %rdi
movq -0x38(%rbp), %rbx
movl %ebx, %esi
callq 0x2190
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x20c0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
movq -0x60(%rbp), %rsi
testq %rbx, %rbx
jle 0x27dd
movslq %r15d, %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rcx
movl %ecx, %edi
orl $0x1, %edi
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %r9d
cmovgl %ecx, %r9d
movq -0x40(%rbp), %rax
movslq %eax, %r10
movl %eax, %r11d
negl %eax
movq %rax, -0x40(%rbp)
incq %r9
movslq %ecx, %rax
leaq 0x2(,%rax,2), %r8
addq %r13, %r8
movq -0x58(%rbp), %r12
movq %rsi, %rbx
movq %rdx, -0x50(%rbp)
cmpl $0x0, -0x80(%rbp)
movq -0x90(%rbp), %rdx
jle 0x2772
xorl %eax, %eax
cmpq %r10, %rax
jge 0x2748
leal (%r11,%rax), %ecx
jmp 0x275a
cmpq -0x78(%rbp), %rax
jge 0x2754
leaq (%rbx,%rax), %rcx
jmp 0x2760
movq -0x40(%rbp), %rcx
addl %eax, %ecx
movslq %ecx, %rcx
addq %rsi, %rcx
movzbl (%rcx), %ecx
movw %cx, (%r13,%rax,2)
incq %rax
cmpq %rax, -0x80(%rbp)
jne 0x273d
testl %edx, %edx
js 0x278c
xorl %eax, %eax
xorl %ecx, %ecx
movswl (%r13,%rax,2), %edx
addl %edx, %ecx
incq %rax
cmpq %rax, %r9
jne 0x277a
jmp 0x278e
xorl %ecx, %ecx
testl %r15d, %r15d
jle 0x27b7
xorl %esi, %esi
movl %ecx, %eax
cltd
idivl %edi
movb %al, (%r12,%rsi)
movswl (%r8,%rsi,2), %eax
movswl (%r13,%rsi,2), %edx
addl %ecx, %eax
subl %edx, %eax
incq %rsi
movl %eax, %ecx
cmpq %rsi, %r15
jne 0x2795
movq -0x50(%rbp), %rdx
incq %rdx
movq -0x78(%rbp), %rax
addq %rax, %rbx
addq %r15, -0x40(%rbp)
addq %r15, %r11
addq %rax, %r12
cmpq -0x38(%rbp), %rdx
movq -0x60(%rbp), %rsi
jne 0x272a
testl %r15d, %r15d
movq -0x38(%rbp), %r12
movq -0x48(%rbp), %r13
jle 0x28bf
movl %r13d, %esi
orl $0x1, %esi
movslq %r13d, %rdi
movslq -0x2c(%rbp), %r8
xorl %r9d, %r9d
testl %edi, %edi
cmovlel %r9d, %r13d
movl %r12d, %r10d
incq %r13
leaq (%r14,%rdi,2), %r11
addq $0x2, %r11
xorl %ebx, %ebx
movq %r13, -0x48(%rbp)
cmpl $0x0, -0x88(%rbp)
jle 0x285a
movq -0x58(%rbp), %rax
addq %rbx, %rax
xorl %ecx, %ecx
cmpq %r12, %rcx
movq %rdi, %rdx
cmovlq %r9, %rdx
negq %rdx
cmpq %r8, %rcx
cmovlq %r8, %rdx
addq %rcx, %rdx
imulq %r15, %rdx
movzbl (%rax,%rdx), %edx
movw %dx, (%r14,%rcx,2)
incq %rcx
cmpq %rcx, -0x88(%rbp)
jne 0x282a
cmpl $0x0, -0x68(%rbp)
js 0x2875
xorl %eax, %eax
xorl %ecx, %ecx
movswl (%r14,%rax,2), %edx
addl %edx, %ecx
incq %rax
cmpq %rax, %r13
jne 0x2864
jmp 0x2877
xorl %ecx, %ecx
testl %r12d, %r12d
jle 0x28a7
movq -0x70(%rbp), %r12
xorl %r13d, %r13d
movl %ecx, %eax
cltd
idivl %esi
movb %al, (%r12)
movswl (%r11,%r13,2), %eax
movswl (%r14,%r13,2), %edx
addl %ecx, %eax
subl %edx, %eax
incq %r13
addq %r15, %r12
movl %eax, %ecx
cmpq %r13, %r10
jne 0x2883
incq %rbx
incq -0x70(%rbp)
cmpq %r15, %rbx
movq -0x38(%rbp), %r12
movq -0x48(%rbp), %r13
jne 0x2818
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x271b(%rip), %rdi      # 0x4ff0
leaq 0x764(%rip), %rsi       # 0x3040
callq 0x20d0
movl $0x1, %edi
callq 0x2110
nop