// File: /workspace/github_cpp_makefile_star2_O1/brianwu02[P]ImageProcessing/convolve.cpp
// Function: openKernelFile(char*)
float* openKernelFile(char* file) {
    int array_size = 1024;
    char * array = new char[array_size];
    int position = 0;
    // this stupid implementation only works for example input files.
    ifstream f(file);
    float *matrix;
    matrix = (float *) malloc(4 * 9); // 4 bytes * 9 elements;

    if (f.is_open()) {
        
        while (!f.eof() && position < array_size) {
            f.get(array[position]);
            position++;
        }

        array[position-1] = '\0';
        
        char a, e;
        int i = 0;
        int j = 0;

        string b, d;
        while (array[i] != '\0') {
            if (isdigit(array[i]) and isdigit(array[i+1])) {
                // handles 2 digits
                int val;
                a = array[i];
                e = array[i+1];
                b = a;
                d = e;
                val += 10 * atoi(b.c_str());
                val += 1 * atoi(d.c_str());
                matrix[j] = val;
                i++;
                i++;
                j++;
            } else if (isdigit(array[i]) and (array[i-1] == '-')) {
                a = array[i];
                b = a;
                int val = atoi(b.c_str());
                matrix[j] = (-1 * val);
                i++;
                j++;
            } else if (isdigit(array[i]) and (array[i-1] != '-')) {
                a = array[i];
                b = a;
                int val = atoi(b.c_str());
                matrix[j] = val;
                i++;
                j++;
            } else {
                i++;
            }
        }
    }
    return matrix;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %rdi, %rbx
movl $0x400, %edi            # imm = 0x400
callq 0x2030
movq %rax, %r14
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x22d0
movl $0x24, %edi
callq 0x21c0
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x299b
movq 0x40(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x60(%rsp,%rax)
jne 0x2811
xorl %r12d, %r12d
leaq 0x40(%rsp), %r15
leaq (%r14,%r12), %rsi
movq %r15, %rdi
callq 0x21e0
leaq 0x1(%r12), %rax
movq 0x40(%rsp), %rcx
movq -0x18(%rcx), %rcx
testb $0x2, 0x60(%rsp,%rcx)
jne 0x2813
cmpq $0x3fe, %r12            # imm = 0x3FE
movq %rax, %r12
jbe 0x27e2
jmp 0x2813
xorl %eax, %eax
xorl %ebp, %ebp
movb %bpl, -0x1(%r14,%rax)
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rbp, -0x8(%rax)
movb %bpl, (%rax)
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rbp, -0x8(%rax)
movb %bpl, (%rax)
movb (%r14), %cl
testb %cl, %cl
je 0x2964
movq %r14, %rax
xorl %r12d, %r12d
movsbl %cl, %r8d
leal -0x30(%r8), %ecx
cmpl $0x9, %ecx
ja 0x291e
movslq %r12d, %rdx
movsbl 0x1(%r14,%rdx), %r15d
leal -0x30(%r15), %edx
cmpl $0x9, %edx
ja 0x28d3
movq 0x8(%rsp), %rdx
movl $0x1, %ecx
movq %rsp, %rdi
xorl %esi, %esi
callq 0x22e0
movq 0x28(%rsp), %rdx
movsbl %r15b, %r8d
movl $0x1, %ecx
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x22e0
movq (%rsp), %rdi
callq 0x2240
leal (%rax,%rax,4), %eax
leal (%r13,%rax,2), %r15d
movq 0x20(%rsp), %rdi
callq 0x2240
movl %eax, %r13d
addl %r15d, %r13d
xorps %xmm0, %xmm0
cvtsi2ss %r13d, %xmm0
movslq %ebp, %rax
movss %xmm0, (%rbx,%rax,4)
addl $0x2, %r12d
jmp 0x290f
cmpl $0x9, %ecx
ja 0x291e
cmpb $0x2d, -0x1(%rax)
jne 0x2913
movq 0x8(%rsp), %rdx
movl $0x1, %ecx
movq %rsp, %rdi
xorl %esi, %esi
callq 0x22e0
movq (%rsp), %rdi
callq 0x2240
negl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movslq %ebp, %rax
movss %xmm0, (%rbx,%rax,4)
incl %r12d
incl %ebp
jmp 0x2921
cmpl $0x9, %ecx
ja 0x291e
cmpb $0x2d, -0x1(%rax)
jne 0x2936
incl %r12d
movslq %r12d, %rcx
leaq (%r14,%rcx), %rax
movb (%r14,%rcx), %cl
testb %cl, %cl
jne 0x284b
jmp 0x2964
movq 0x8(%rsp), %rdx
movl $0x1, %ecx
movq %rsp, %rdi
xorl %esi, %esi
callq 0x22e0
movq (%rsp), %rdi
callq 0x2240
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movslq %ebp, %rbp
movss %xmm0, (%rbx,%rbp,4)
jmp 0x290c
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2980
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2150
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x299b
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2150
leaq 0x40(%rsp), %rdi
callq 0x2070
movq %rbx, %rax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x29bc
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x29db
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2150
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x29fb
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2150
jmp 0x29fb
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x22b0