// File: /workspace/github_cpp_makefile_star2_O3/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 (%rdi), %r13d
movl 0x4(%rdi), %ecx
movl %r13d, %eax
imull %ecx, %eax
movq %rcx, 0x10(%rsp)
movl %ecx, 0x4(%r15)
movl %r13d, (%r15)
movslq %eax, %rdi
callq 0x21c0
movq %rax, %r14
movq %rax, 0x8(%r15)
movq %rbx, 0x18(%rsp)
movslq (%rbx), %r15
movq 0x8(%r12), %rbx
leal (,%r15,4), %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x21c0
movq %rax, %rcx
shlq $0x2, %r15
xorl %eax, %eax
movq %rcx, 0x8(%rsp)
movq %rcx, 0x20(%rsp,%rax,8)
incq %rax
addq %r15, %rcx
cmpq $0x3, %rax
jne 0x2a96
movq 0x18(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
movl $0x3, %edx
movq 0x8(%rsp), %r12
movq %r12, %rcx
callq 0x3a6e
movq %r15, %rdi
movl $0x1, %esi
movl $0x3, %edx
movq %r12, %rcx
callq 0x3a6e
cmpl $0x0, 0x10(%rsp)
jle 0x2bdb
xorl %eax, %eax
movl $0xaaaaaaab, %ebp       # imm = 0xAAAAAAAB
movq %rax, %r15
leal 0x2(%rax), %r12d
movq 0x18(%rsp), %rdi
movl %r12d, %esi
movl $0x3, %edx
movq 0x8(%rsp), %rcx
callq 0x3a6e
leaq 0x1(%r15), %rax
testl %r13d, %r13d
jle 0x2bcd
movl %r12d, %ecx
imulq %rbp, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %r12d
movq 0x20(%rsp,%r12,8), %rcx
movl %eax, %edx
imulq %rbp, %rdx
shrq $0x21, %rdx
leal (%rdx,%rdx,2), %edx
movl %eax, %esi
subl %edx, %esi
movq 0x20(%rsp,%rsi,8), %rdx
movl %r15d, %esi
imulq %rbp, %rsi
shrq $0x21, %rsi
leal (%rsi,%rsi,2), %esi
subl %esi, %r15d
movq 0x20(%rsp,%r15,8), %rsi
xorl %edi, %edi
movsd 0x4(%rsi,%rdi,4), %xmm0
movups (%rbx), %xmm1
movups 0x10(%rbx), %xmm2
movss (%rsi,%rdi,4), %xmm3
movlhps %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
movss (%rdx,%rdi,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(%rdx,%rdi,4), %xmm0
movhps (%rcx,%rdi,4), %xmm0    # xmm0 = xmm0[0,1],mem[0,1]
mulps %xmm2, %xmm0
cvttps2dq %xmm3, %xmm1
cvttps2dq %xmm0, %xmm0
movss 0x8(%rcx,%rdi,4), %xmm2
mulss 0x20(%rbx), %xmm2
paddd %xmm1, %xmm0
cvttss2si %xmm2, %r8d
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, %r9d
addl %r8d, %r9d
movb %r9b, (%r14,%rdi)
incq %rdi
cmpq %rdi, %r13
jne 0x2b59
addq %r13, %r14
cmpq 0x10(%rsp), %rax
jne 0x2aea
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq