// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rcx, -0x80(%rbp)
movl %edx, %r13d
movq %rsi, %r12
movq %rdi, %r14
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 0x2140
movq %rax, 0x8(%r15)
movq %rbx, %rdi
callq 0x2140
movq %rax, -0x60(%rbp)
movq %rax, 0x8(%r12)
movq 0x8(%r15), %rax
movq %rax, -0x88(%rbp)
testq %rax, %rax
je 0x2907
movq 0x8(%r14), %rax
movq %rax, -0x78(%rbp)
movl (%r14), %r15d
movslq 0x4(%r14), %rsi
movq %rsi, -0x38(%rbp)
leal -0x1(%r13), %eax
shrl $0x1f, %eax
movq %r13, -0xa0(%rbp)
leal (%rax,%r13), %edx
decl %edx
movl %edx, %ebx
sarl %ebx
movq -0x80(%rbp), %rcx
leal -0x1(%rcx), %eax
shrl $0x1f, %eax
addl %ecx, %eax
decl %eax
movl %eax, %ecx
sarl %ecx
movl %ecx, -0x2c(%rbp)
andl $-0x2, %edx
movq %rdx, -0x40(%rbp)
addl %r15d, %edx
andl $-0x2, %eax
movq %rax, -0x58(%rbp)
leal (%rsi,%rax), %ecx
movq %rdx, -0x68(%rbp)
leaq 0xf(,%rdx,2), %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %rcx, -0x70(%rbp)
leaq 0xf(,%rcx,2), %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq 0x2a22(%rip), %r12      # 0x4fd0
leaq 0xa9b(%rip), %rsi       # 0x3050
movl $0x3, %edx
movq %r12, %rdi
callq 0x20f0
movq %r12, %rdi
movq %rbx, -0x48(%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 0xa53(%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 0xa0b(%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 0x9c6(%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 -0x78(%rbp), %r9
testq %rbx, %rbx
jle 0x2803
movslq %r15d, %rdx
movq -0x40(%rbp), %rcx
movl %ecx, %edi
orl $0x1, %edi
movq -0x48(%rbp), %rax
movslq %eax, %r8
movq $0x0, -0x90(%rbp)
testl %ecx, %ecx
movl $0x0, %r10d
cmovgl %ecx, %r10d
movl -0x68(%rbp), %r11d
movl %eax, %esi
movq %rsi, -0x98(%rbp)
negl %eax
movq %rax, -0x48(%rbp)
movq %rdx, -0x50(%rbp)
movl %edx, %r12d
incq %r10
movslq %ecx, %rax
leaq (%r14,%rax,2), %rbx
addq $0x2, %rbx
movq -0x60(%rbp), %rsi
movq %r9, -0x40(%rbp)
cmpl $0x0, -0x68(%rbp)
movq -0xa0(%rbp), %rdx
jle 0x278a
xorl %eax, %eax
cmpq %r8, %rax
jge 0x275f
movq -0x98(%rbp), %rcx
jmp 0x2772
cmpq -0x50(%rbp), %rax
jge 0x276e
movq -0x40(%rbp), %rcx
addq %rax, %rcx
jmp 0x277a
movq -0x48(%rbp), %rcx
addl %eax, %ecx
movslq %ecx, %rcx
addq %r9, %rcx
movzbl (%rcx), %ecx
movw %cx, (%r14,%rax,2)
incq %rax
cmpq %rax, %r11
jne 0x2751
testl %edx, %edx
js 0x27a3
xorl %eax, %eax
xorl %ecx, %ecx
movswl (%r14,%rax,2), %edx
addl %edx, %ecx
incq %rax
cmpq %rax, %r10
jne 0x2792
jmp 0x27a5
xorl %ecx, %ecx
testl %r15d, %r15d
jle 0x27ce
xorl %r9d, %r9d
movl %ecx, %eax
cltd
idivl %edi
movb %al, (%rsi,%r9)
movswl (%rbx,%r9,2), %eax
movswl (%r14,%r9,2), %edx
addl %ecx, %eax
subl %edx, %eax
incq %r9
movl %eax, %ecx
cmpq %r9, %r12
jne 0x27ad
movq -0x90(%rbp), %rcx
incq %rcx
movq -0x50(%rbp), %rax
addq %rax, -0x40(%rbp)
addq %r12, -0x48(%rbp)
addq %r12, -0x98(%rbp)
addq %rax, %rsi
movq %rcx, -0x90(%rbp)
cmpq -0x38(%rbp), %rcx
movq -0x78(%rbp), %r9
jne 0x2742
testl %r15d, %r15d
movq -0x38(%rbp), %r12
jle 0x28f8
movq -0x58(%rbp), %rax
movl %eax, %esi
orl $0x1, %esi
movslq %eax, %rdi
movslq -0x2c(%rbp), %r8
xorl %r9d, %r9d
testl %edi, %edi
cmovlel %r9d, %eax
movl -0x70(%rbp), %r10d
movl %r12d, %r11d
movq %r8, %rcx
imulq %r15, %rcx
movq %rcx, -0x50(%rbp)
incq %rax
movq %rax, -0x58(%rbp)
leaq 0x2(,%rdi,2), %r14
addq %r13, %r14
cmpl $0x0, -0x70(%rbp)
jle 0x288c
movq -0x60(%rbp), %rax
addq %r9, %rax
movq -0x50(%rbp), %rcx
xorl %edx, %edx
movq %rcx, %rbx
cmpq %r8, %rdx
jl 0x2877
movq %rdx, %rbx
cmpq %r12, %rdx
jl 0x2873
subq %rdi, %rbx
imulq %r15, %rbx
movzbl (%rax,%rbx), %ebx
movw %bx, (%r13,%rdx,2)
incq %rdx
addq %r15, %rcx
cmpq %rdx, %r10
jne 0x2860
cmpl $0x0, -0x80(%rbp)
js 0x28ac
xorl %eax, %eax
xorl %ecx, %ecx
movq -0x58(%rbp), %rbx
movswl (%r13,%rax,2), %edx
addl %edx, %ecx
incq %rax
cmpq %rax, %rbx
jne 0x289a
jmp 0x28ae
xorl %ecx, %ecx
testl %r12d, %r12d
jle 0x28e1
movq -0x88(%rbp), %r12
xorl %ebx, %ebx
movl %ecx, %eax
cltd
idivl %esi
movb %al, (%r12)
movswl (%r14,%rbx,2), %eax
movswl (%r13,%rbx,2), %edx
addl %ecx, %eax
subl %edx, %eax
incq %rbx
addq %r15, %r12
movl %eax, %ecx
cmpq %rbx, %r11
jne 0x28bc
incq %r9
incq -0x88(%rbp)
cmpq %r15, %r9
movq -0x38(%rbp), %r12
jne 0x284d
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x26e2(%rip), %rdi      # 0x4ff0
leaq 0x72b(%rip), %rsi       # 0x3040
callq 0x20d0
movl $0x1, %edi
callq 0x2110