// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rbx
movl 0x4(%rdi), %ecx
movslq (%rdi), %rax
movl %ecx, 0x4(%r15)
movl %eax, (%r15)
movq %rax, (%rsp)
movq %rcx, %r13
imull %ecx, %eax
movslq %eax, %rdi
callq 0x2190
movq %rax, %r14
movq %rax, 0x8(%r15)
movq %rbx, 0x10(%rsp)
movslq (%rbx), %r15
movq 0x8(%r12), %rbx
imull $0xc, %r15d, %eax
movslq %eax, %rdi
callq 0x2190
movq %rax, %rcx
shlq $0x2, %r15
xorl %eax, %eax
movq %rcx, 0x8(%rsp)
cmpq $0x3, %rax
je 0x283e
movq %rcx, 0x20(%rsp,%rax,8)
incq %rax
addq %r15, %rcx
jmp 0x282b
xorl %r12d, %r12d
pushq $0x3
popq %rbp
cmpl $0x2, %r12d
je 0x2863
movq 0x10(%rsp), %rdi
movl %r12d, %esi
movl %ebp, %edx
movq 0x8(%rsp), %rcx
callq 0x3421
incl %r12d
jmp 0x2844
xorl %r12d, %r12d
movq (%rsp), %rax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
testl %r13d, %r13d
cmovlel %r12d, %r13d
movq %r13, 0x18(%rsp)
cmpq 0x18(%rsp), %r12
je 0x294d
leal 0x2(%r12), %r13d
movq 0x10(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
movq 0x8(%rsp), %rcx
callq 0x3421
movl %r12d, %eax
xorl %edx, %edx
divl %ebp
movq 0x20(%rsp,%rdx,8), %rcx
incq %r12
movl %r12d, %eax
xorl %edx, %edx
divl %ebp
movq 0x20(%rsp,%rdx,8), %rsi
movl %r13d, %eax
xorl %edx, %edx
divl %ebp
movq 0x20(%rsp,%rdx,8), %rax
xorl %edx, %edx
cmpq %rdx, %r15
je 0x2944
movsd 0x4(%rcx,%rdx,4), %xmm0
movups (%rbx), %xmm1
movups 0x10(%rbx), %xmm2
movss (%rcx,%rdx,4), %xmm3
movlhps %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
movss (%rsi,%rdx,4), %xmm4
shufps $0xd4, %xmm0, %xmm4     # xmm4 = xmm4[0,1],xmm0[1,3]
shufps $0x28, %xmm4, %xmm3     # xmm3 = xmm3[0,2],xmm4[2,0]
mulps %xmm1, %xmm3
movsd 0x4(%rsi,%rdx,4), %xmm0
movhps (%rax,%rdx,4), %xmm0    # xmm0 = xmm0[0,1],mem[0,1]
mulps %xmm2, %xmm0
cvttps2dq %xmm3, %xmm1
cvttps2dq %xmm0, %xmm0
paddd %xmm1, %xmm0
movss 0x8(%rax,%rdx,4), %xmm1
mulss 0x20(%rbx), %xmm1
cvttss2si %xmm1, %edi
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %r8d
addl %edi, %r8d
movb %r8b, (%r14,%rdx)
incq %rdx
jmp 0x28cf
addq (%rsp), %r14
jmp 0x2882
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq