// File: /workspace/github_cpp_makefile_star2_O2/brianwu02[P]ImageProcessing/convolve.cpp
// Function: main
int main(int argc, char** argv) {
    std::string kernelString;
    std::string str;
    imageP I1, I2, paddedImg;
    kernelP k;

    cout << "infile: " << argv[1] << endl;
    cout << "kernel File: " << argv[2] << endl;
    cout << "outfile: " << argv[3] << endl;
    float *kernelValues;
    kernelValues = openKernelFile(argv[2]);

    I1 = IP_readImage(argv[1]);
    I2 = NEWIMAGE;
    paddedImg = NEWIMAGE;
    k = NEWKERNEL;

    
    k->height = kernelValues[0];
    k->width = kernelValues[1];
    int ksize = k->height * k->width;
    k->kernel = (float *) malloc(4 * ksize); // stupid. have to multiply by 4 bytes. man why does c++ suck.
    float *kimg = k->kernel;
    
    // copy kernel values to another pointer, yes inefficient but it works ok!
    for (int i=0; i<9; i++) {
        kimg[i] = kernelValues[i+2];
    }

    // hard code some sample values
    // weighted average
    /*kimg[0] = 1;
    kimg[1] = 2;
    kimg[2] = 1;
    kimg[3] = 2;
    kimg[4] = 4;
    kimg[5] = 2;
    kimg[6] = 1;
    kimg[7] = 2;
    kimg[8] = 1;
    */
    
    /*kimg[0] = 0;
    kimg[1] = -1;
    kimg[2] = 0;
    kimg[3] =  -1;
    kimg[4] = 5;
    kimg[5] = -1;
    kimg[6] = 0;
    kimg[7] = -1;
    kimg[8] = 0;
    */
    
    /*
    kimg[0] = -1.0;
    kimg[1] = -1.0;
    kimg[2] = -1.0;
    kimg[3] = -1.0;
    kimg[4] = 8.0;
    kimg[5] = -1.0;
    kimg[6] = -1.0;
    kimg[7] = -1.0;
    kimg[8] = -1.0;
    */

    /*
    kimg[0] = 1;
    kimg[1] = 1;
    kimg[2] = 1;
    kimg[3] = 1;
    kimg[4] = 1;
    kimg[5] = 1;
    kimg[6] = 1;
    kimg[7] = 1;
    kimg[8] = 1;
    */
    /*
    kimg[0] = -10;
    kimg[1] = -10;
    kimg[2] = -10;
    kimg[3] = 0;
    kimg[4] = 0;
    kimg[5] = 0;
    kimg[6] = 10;
    kimg[7] = 10;
    kimg[8] = 10;
    */

    /*
    kimg[0] = -10;
    kimg[1] = 0;
    kimg[2] = 10;
    kimg[3] = -10;
    kimg[4] = 0;
    kimg[5] = 10;
    kimg[6] = -10;
    kimg[7] = 0;
    kimg[8] = 10;
    */

    // fix the size.
    int sz = 3;
    padImage(I1, sz, paddedImg);

    convolve(I1, paddedImg, k, I2);
    IP_saveImage(I2, argv[3]);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
movq 0x3b5a(%rip), %rdi      # 0x5fd0
leaq 0x1b87(%rip), %rsi      # 0x4004
callq 0x2120
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x20c0
movq 0x3b33(%rip), %rdi      # 0x5fd0
leaq 0x1b69(%rip), %rsi      # 0x400d
callq 0x2120
movq 0x10(%rbx), %rsi
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x20c0
movq 0x3b0c(%rip), %rdi      # 0x5fd0
leaq 0x1b50(%rip), %rsi      # 0x401b
callq 0x2120
movq 0x18(%rbx), %rsi
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x20c0
movq 0x10(%rbx), %rdi
callq 0x25df
movq %rax, %r12
movq 0x8(%rbx), %rdi
callq 0x2e24
movq %rax, %r15
pushq $0x10
popq %rbp
movq %rbp, %rdi
callq 0x2190
movq %rax, %r14
movq %rbp, %rdi
callq 0x2190
movq %rax, %r13
movq %rbp, %rdi
callq 0x2190
movq %rax, %rbp
movsd (%r12), %xmm0
cvttps2dq %xmm0, %xmm0
pshufd $0xe1, %xmm0, %xmm1     # xmm1 = xmm0[1,0,2,3]
movq %xmm1, (%rax)
movd %xmm0, %eax
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ecx
imull %eax, %ecx
shll $0x2, %ecx
movslq %ecx, %rdi
callq 0x2190
movq %rax, 0x8(%rbp)
xorl %ecx, %ecx
cmpq $0x9, %rcx
je 0x256b
movd 0x8(%r12,%rcx,4), %xmm0
movd %xmm0, (%rax,%rcx,4)
incq %rcx
jmp 0x2554
pushq $0x3
popq %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x3108
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x27c7
movq 0x18(%rbx), %rsi
movq %r14, %rdi
callq 0x2fee
leaq 0x8(%rsp), %rdi
callq 0x20b0
leaq 0x28(%rsp), %rdi
callq 0x20b0
pushq $0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x25c0
jmp 0x25c0
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x20b0
leaq 0x28(%rsp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x2290