// File: /workspace/github_cpp_makefile_star2_O3/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 $0x258, %rsp            # imm = 0x258
movq %rdi, %rbx
movl $0x400, %edi            # imm = 0x400
callq 0x2030
movq %rax, %r14
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
movl $0x8, %edx
callq 0x22d0
movl $0x24, %edi
callq 0x21c0
movq %rax, %r13
leaq 0xc8(%rsp), %rdi
callq 0x2060
testb %al, %al
je 0x29b7
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x70(%rsp,%rax)
jne 0x281e
xorl %r12d, %r12d
leaq 0x50(%rsp), %r15
leaq (%r14,%r12), %rsi
movq %r15, %rdi
callq 0x21e0
leaq 0x1(%r12), %rax
movq 0x50(%rsp), %rcx
movq -0x18(%rcx), %rcx
testb $0x2, 0x70(%rsp,%rcx)
jne 0x2820
cmpq $0x3fe, %r12            # imm = 0x3FE
movq %rax, %r12
jbe 0x27ef
jmp 0x2820
xorl %eax, %eax
xorl %r12d, %r12d
movb %r12b, -0x1(%r14,%rax)
leaq 0x18(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq %r12, -0x8(%rbp)
movb %r12b, (%rbp)
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
movq %r12, -0x8(%rax)
movb %r12b, (%rax)
movb (%r14), %cl
testb %cl, %cl
je 0x29b7
movq %r14, %rax
movq %rdx, 0x28(%rsp)
xorl %r15d, %r15d
movsbl %cl, %r8d
leal -0x30(%r8), %ecx
cmpl $0x9, %ecx
ja 0x28f1
movslq %r15d, %rcx
movsbl 0x1(%r14,%rcx), %ebp
leal -0x30(%rbp), %ecx
cmpl $0x9, %ecx
ja 0x28f6
movq 0x10(%rsp), %rdx
movl $0x1, %ecx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x22e0
movq 0x38(%rsp), %rdx
movsbl %bpl, %r8d
movl $0x1, %ecx
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x22e0
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x2240
leal (%rax,%rax,4), %eax
movq 0x28(%rsp), %rcx
leal (%rcx,%rax,2), %ebx
movq 0x30(%rsp), %rdi
callq 0x2240
addl %ebx, %eax
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movslq %r12d, %rax
movss %xmm0, (%r13,%rax,4)
addl $0x2, %r15d
jmp 0x2966
incl %r15d
jmp 0x2969
cmpb $0x2d, -0x1(%rax)
movq 0x10(%rsp), %rdx
jne 0x2934
movl $0x1, %ecx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x22e0
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x2240
negl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movslq %r12d, %rax
movss %xmm0, (%r13,%rax,4)
jmp 0x2963
movl $0x1, %ecx
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x22e0
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x2240
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movslq %r12d, %r12
movss %xmm0, (%r13,%r12,4)
incl %r15d
incl %r12d
movslq %r15d, %rcx
leaq (%r14,%rcx), %rax
movb (%r14,%rcx), %cl
testb %cl, %cl
jne 0x285f
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x299d
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2150
movq 0x8(%rsp), %rbp
leaq 0x18(%rsp), %rax
cmpq %rax, %rbp
je 0x29b7
movq 0x18(%rsp), %rsi
incq %rsi
movq %rbp, %rdi
callq 0x2150
leaq 0x50(%rsp), %rdi
callq 0x2070
movq %r13, %rax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x29d8
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x29f7
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2150
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x2a18
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2150
jmp 0x2a18
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x22b0