// File: /workspace/github_cpp_makefile_star2_O1/brianwu02[P]ImageProcessing/convolve.cpp
// Function: convolve(imageS*, imageS*, kernelS*, imageS*)
void convolve(imageP I1, imageP paddedImg, kernelP k, imageP I2) {
    /* I1 => original Image
     * paddedImg => the pixel replicated Image
     * k => the kernel 
     * I2 => outfile
     *
     */
    int kw = k->width;
    int kh = k->height;
    int kt = k->width * k->height;

    int height = I1->height;
    int width = I1->width;
    int total = I1->width * I1->height;

    I2->height = I1->height;
    I2->width = I1->width;
    I2->image = (unsigned char *) malloc(total);

    unsigned char *in, *out, *paddedIn;
    out = I2->image;
    paddedIn = paddedImg->image;
    int paddedWidth = paddedImg->width;
    int paddedHeight = paddedImg->height;
    
    float *buf;
    float *kernelP = k->kernel;
    /*for (int i=0; i<9; i++) {
        cout << kernelP[i] << endl;
    }*/

    // do a lot of hard coded values because im tired and I just want it to work for 3*3 kernel....
    int bufRowsRequired = 3;

    // initialize buffer for kernel
    buf = (float *) malloc(paddedWidth * bufRowsRequired * 4);

    // allocate memory for an array that will hold all the pointers to circlar buffer
    float *arrayOfPointers[bufRowsRequired];

    // store points in array of pointers
    for (int i=0; i<bufRowsRequired; i++) {
        arrayOfPointers[i] = &buf[i*paddedWidth];
    }

    // initialize and store the pointers to buffer in arrayOfPointers
    for (int y=0; y<bufRowsRequired-1; y++) {
        floatCopyToBuffer(paddedImg, y, bufRowsRequired, buf);
    }
    float *r0, *r1, *r2;

    // kernelP is pointer to kernel
    int kr[9]; // temp to hold calculations

    //cout << (int) kernelP[0] << endl;

    for (int y=0; y<height; y++) {
        // copy next row to buffer
        floatCopyToBuffer(paddedImg, y+(bufRowsRequired-1), bufRowsRequired, buf);
        r0 = arrayOfPointers[y % 3] + 1;
        r1 = arrayOfPointers[(y+1) % 3] + 1;
        r2 = arrayOfPointers[(y+2) % 3] + 1;
        
        unsigned char sum;
        for (int x=0; x<width; x++) {


            kr[0] = r0[-1] * kernelP[0];
            kr[1] = r0[0] * kernelP[1];
            kr[2] = r0[1] * kernelP[2];
            kr[3] = r1[-1] * kernelP[3];
            kr[4] = r1[0] * kernelP[4];
            kr[5] = r1[1] * kernelP[5];
            kr[6] = r2[-1] * kernelP[6];
            kr[7] = r2[0] * kernelP[7];
            kr[8] = r2[1] * kernelP[8];

            //cout << (int) r1[0] << " * " << (float) kernelP[4] << " = " << r1[0] * kernelP[4] << endl;
            //cout << (int) r1[1] << " * " << (float) kernelP[5] << endl;
            
            //sum = (kr[0] + kr[1] + kr[2] + kr[3] + kr[4] + kr[5] + kr[6] + kr[7] + kr[8]) / 9;
            sum = (kr[0] + kr[1] + kr[2] + kr[3] + kr[4] + kr[5] + kr[6] + kr[7] + kr[8]);
            out[y*width+x] = sum;
            r0++;
            r1++;
            r2++;

        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rbx
movl (%rdi), %r13d
movl 0x4(%rdi), %ecx
movl %r13d, %eax
imull %ecx, %eax
movq %rcx, (%rsp)
movl %ecx, 0x4(%r15)
movl %r13d, (%r15)
movslq %eax, %rdi
callq 0x21c0
movq %rax, %r14
movq %rax, 0x8(%r15)
movq %rbx, 0x8(%rsp)
movslq (%rbx), %rbp
movq 0x8(%r12), %rbx
leal (,%rbp,4), %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x21c0
movq %rax, %r15
shlq $0x2, %rbp
xorl %eax, %eax
movq %r15, %rcx
movq %rcx, 0x10(%rsp,%rax,8)
incq %rax
addq %rbp, %rcx
cmpq $0x3, %rax
jne 0x2a75
xorl %ebp, %ebp
movq 0x8(%rsp), %r12
movq %r12, %rdi
movl %ebp, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x3a1c
incl %ebp
cmpl $0x1, %ebp
je 0x2a8d
cmpl $0x0, (%rsp)
jle 0x2be3
xorl %eax, %eax
movq %rax, %rbp
leaq 0x2(%rax), %r12
movq 0x8(%rsp), %rdi
movl %r12d, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0x3a1c
leaq 0x1(%rbp), %rax
testl %r13d, %r13d
jle 0x2bd6
movl %r12d, %ecx
movl $0xaaaaaaab, %edi       # imm = 0xAAAAAAAB
imulq %rdi, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %r12d
movq 0x10(%rsp,%r12,8), %rcx
movl %eax, %edx
imulq %rdi, %rdx
shrq $0x21, %rdx
leal (%rdx,%rdx,2), %edx
movl %eax, %esi
subl %edx, %esi
movq 0x10(%rsp,%rsi,8), %rdx
movl %ebp, %esi
imulq %rdi, %rsi
shrq $0x21, %rsi
leal (%rsi,%rsi,2), %esi
subl %esi, %ebp
movq 0x10(%rsp,%rbp,8), %rsi
xorl %edi, %edi
movss (%rsi,%rdi,4), %xmm0
movss 0x4(%rsi,%rdi,4), %xmm1
mulss (%rbx), %xmm0
cvttss2si %xmm0, %r8d
mulss 0x4(%rbx), %xmm1
cvttss2si %xmm1, %r9d
addl %r8d, %r9d
movss 0x8(%rsi,%rdi,4), %xmm0
mulss 0x8(%rbx), %xmm0
cvttss2si %xmm0, %r10d
movss (%rdx,%rdi,4), %xmm0
movss 0x4(%rdx,%rdi,4), %xmm1
mulss 0xc(%rbx), %xmm0
cvttss2si %xmm0, %r8d
mulss 0x10(%rbx), %xmm1
addl %r10d, %r8d
cvttss2si %xmm1, %r10d
movss 0x8(%rdx,%rdi,4), %xmm0
mulss 0x14(%rbx), %xmm0
cvttss2si %xmm0, %r11d
addl %r9d, %r8d
addl %r10d, %r11d
movss (%rcx,%rdi,4), %xmm0
movss 0x4(%rcx,%rdi,4), %xmm1
mulss 0x18(%rbx), %xmm0
cvttss2si %xmm0, %r9d
mulss 0x1c(%rbx), %xmm1
cvttss2si %xmm1, %r10d
addl %r11d, %r9d
movss 0x8(%rcx,%rdi,4), %xmm0
mulss 0x20(%rbx), %xmm0
addl %r8d, %r9d
cvttss2si %xmm0, %r8d
addl %r10d, %r8d
addl %r9d, %r8d
movb %r8b, (%r14,%rdi)
incq %rdi
cmpq %rdi, %r13
jne 0x2b22
addq %r13, %r14
cmpq (%rsp), %rax
jne 0x2ab2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq