// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rcx
movl (%rcx), %eax
movl 0x4(%rcx), %ecx
imull %ecx, %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rcx
movl (%rcx), %eax
movl 0x4(%rcx), %ecx
imull %ecx, %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rax
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movslq -0x38(%rbp), %rdi
callq 0x2280
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x3, -0x6c(%rbp)
movl -0x54(%rbp), %eax
movl -0x6c(%rbp), %ecx
imull %ecx, %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x2280
movq %rax, -0x60(%rbp)
movl -0x6c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x78(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xe8(%rbp)
movq %rcx, %rsp
movq %rax, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x2e9d
movq -0xe8(%rbp), %rax
movq -0x60(%rbp), %rdx
movl -0x84(%rbp), %ecx
imull -0x54(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rdx
movslq -0x84(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x2e57
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
movl -0x6c(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x2ede
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %esi
movl -0x6c(%rbp), %edx
movq -0x60(%rbp), %rcx
callq 0x4b70
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x2ea7
movl $0x0, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x3178
movq -0x10(%rbp), %rdi
movl -0xd4(%rbp), %esi
movl -0x6c(%rbp), %eax
subl $0x1, %eax
addl %eax, %esi
movl -0x6c(%rbp), %edx
movq -0x60(%rbp), %rcx
callq 0x4b70
movl -0xd4(%rbp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movq -0xe8(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
addq $0x4, %rax
movq %rax, -0x90(%rbp)
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movq -0xe8(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
addq $0x4, %rax
movq %rax, -0x98(%rbp)
movl -0xd4(%rbp), %eax
addl $0x2, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movq -0xe8(%rbp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
addq $0x4, %rax
movq %rax, -0xa0(%rbp)
movl $0x0, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x3162
movq -0x90(%rbp), %rax
movss -0x4(%rax), %xmm0
movq -0x68(%rbp), %rax
mulss (%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xd0(%rbp)
movq -0x90(%rbp), %rax
movss (%rax), %xmm0
movq -0x68(%rbp), %rax
mulss 0x4(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xcc(%rbp)
movq -0x90(%rbp), %rax
movss 0x4(%rax), %xmm0
movq -0x68(%rbp), %rax
mulss 0x8(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xc8(%rbp)
movq -0x98(%rbp), %rax
movss -0x4(%rax), %xmm0
movq -0x68(%rbp), %rax
mulss 0xc(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xc4(%rbp)
movq -0x98(%rbp), %rax
movss (%rax), %xmm0
movq -0x68(%rbp), %rax
mulss 0x10(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xc0(%rbp)
movq -0x98(%rbp), %rax
movss 0x4(%rax), %xmm0
movq -0x68(%rbp), %rax
mulss 0x14(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xbc(%rbp)
movq -0xa0(%rbp), %rax
movss -0x4(%rax), %xmm0
movq -0x68(%rbp), %rax
mulss 0x18(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xb8(%rbp)
movq -0xa0(%rbp), %rax
movss (%rax), %xmm0
movq -0x68(%rbp), %rax
mulss 0x1c(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xb4(%rbp)
movq -0xa0(%rbp), %rax
movss 0x4(%rax), %xmm0
movq -0x68(%rbp), %rax
mulss 0x20(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0xb0(%rbp)
movl -0xd0(%rbp), %eax
addl -0xcc(%rbp), %eax
addl -0xc8(%rbp), %eax
addl -0xc4(%rbp), %eax
addl -0xc0(%rbp), %eax
addl -0xbc(%rbp), %eax
addl -0xb8(%rbp), %eax
addl -0xb4(%rbp), %eax
addl -0xb0(%rbp), %eax
movb %al, -0xd5(%rbp)
movb -0xd5(%rbp), %dl
movq -0x48(%rbp), %rax
movl -0xd4(%rbp), %ecx
imull -0x34(%rbp), %ecx
addl -0xdc(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x90(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x90(%rbp)
movq -0x98(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x98(%rbp)
movq -0xa0(%rbp), %rax
addq $0x4, %rax
movq %rax, -0xa0(%rbp)
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x2f9a
jmp 0x3164
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x2ee8
movq -0x78(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)