// File: /workspace/github_cpp_makefile_star2_O1/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), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x18(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq 0x3b1c(%rip), %r14      # 0x5fd0
leaq 0x1b49(%rip), %rsi      # 0x4004
movl $0x8, %edx
movq %r14, %rdi
callq 0x2170
movq 0x8(%rbx), %r15
testq %r15, %r15
je 0x24ed
movq %r15, %rdi
callq 0x20b0
movq 0x3af0(%rip), %rdi      # 0x5fd0
movq %r15, %rsi
movq %rax, %rdx
callq 0x2170
jmp 0x2505
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2280
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq 0x3aad(%rip), %rdi      # 0x5fd0
callq 0x2040
movq %rax, %rdi
callq 0x20e0
movq 0x3a99(%rip), %r14      # 0x5fd0
leaq 0x1acf(%rip), %rsi      # 0x400d
movl $0xd, %edx
movq %r14, %rdi
callq 0x2170
movq 0x10(%rbx), %r15
testq %r15, %r15
je 0x2570
movq %r15, %rdi
callq 0x20b0
movq 0x3a6d(%rip), %rdi      # 0x5fd0
movq %r15, %rsi
movq %rax, %rdx
callq 0x2170
jmp 0x2588
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2280
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq 0x3a2a(%rip), %rdi      # 0x5fd0
callq 0x2040
movq %rax, %rdi
callq 0x20e0
movq 0x3a16(%rip), %r14      # 0x5fd0
leaq 0x1a5a(%rip), %rsi      # 0x401b
movl $0x9, %edx
movq %r14, %rdi
callq 0x2170
movq 0x18(%rbx), %r15
testq %r15, %r15
je 0x25f3
movq %r15, %rdi
callq 0x20b0
movq 0x39ea(%rip), %rdi      # 0x5fd0
movq %r15, %rsi
movq %rax, %rdx
callq 0x2170
jmp 0x260b
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2280
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq 0x39a7(%rip), %rdi      # 0x5fd0
callq 0x2040
movq %rax, %rdi
callq 0x20e0
movq 0x10(%rbx), %rdi
callq 0x2775
movq %rax, %r12
movq 0x8(%rbx), %rdi
callq 0x32e8
movq %rax, %r15
movl $0x10, %edi
callq 0x21c0
movq %rax, %r14
movl $0x10, %edi
callq 0x21c0
movq %rax, %r13
movl $0x10, %edi
callq 0x21c0
movq %rax, %rbp
cvttss2si (%r12), %eax
movl %eax, 0x4(%rbp)
cvttss2si 0x4(%r12), %ecx
movl %ecx, (%rbp)
imull %ecx, %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x21c0
movq %rax, 0x8(%rbp)
movups 0x8(%r12), %xmm0
movups 0x18(%r12), %xmm1
movups %xmm0, (%rax)
movups %xmm1, 0x10(%rax)
movl 0x28(%r12), %ecx
movl %ecx, 0x20(%rax)
movq %r15, %rdi
movl $0x3, %esi
movq %r13, %rdx
callq 0x36d8
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x2a0d
movq 0x18(%rbx), %rsi
movq %r14, %rdi
callq 0x350e
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
leaq 0x38(%rsp), %rbx
je 0x2703
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2150
movq 0x28(%rsp), %rdi
cmpq %rbx, %rdi
je 0x271a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2150
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2732
jmp 0x2732
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2751
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2150
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x276d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2150
movq %rbx, %rdi
callq 0x22b0