// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x22b0
pushq $0x24
popq %rdi
callq 0x2190
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x2778
xorl %r12d, %r12d
leaq 0x50(%rsp), %r15
movq 0x50(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x70(%rsp,%rax)
jne 0x2662
cmpq $0x3ff, %r12            # imm = 0x3FF
ja 0x2662
leaq (%r14,%r12), %rsi
movq %r15, %rdi
callq 0x21b0
incq %r12
jmp 0x2638
movq %rbx, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %r13d, %r13d
movb %r13b, -0x1(%r14,%r12)
movb %r13b, (%rax)
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %r13b, (%rax)
leaq 0x10(%rsp), %rdi
xorl %ebp, %ebp
movslq %r13d, %r13
movsbl (%r14,%r13), %esi
testl %esi, %esi
je 0x275f
leal -0x30(%rsi), %eax
cmpl $0x9, %eax
jbe 0x26b5
incq %r13
jmp 0x269b
movsbl 0x1(%r14,%r13), %r15d
leal -0x30(%r15), %eax
movslq %ebp, %r12
cmpl $0x9, %eax
jbe 0x2710
cmpb $0x2d, -0x1(%r14,%r13)
jne 0x26e5
movq %rdi, %r15
callq 0x2260
movq 0x10(%rsp), %rdi
callq 0x2220
negl %eax
jmp 0x26f7
movq %rdi, %r15
callq 0x2260
movq 0x10(%rsp), %rdi
callq 0x2220
cvtsi2ss %eax, %xmm0
movq 0x8(%rsp), %rax
movss %xmm0, (%rax,%r12,4)
incl %ebp
incl %r13d
movq %r15, %rdi
jmp 0x2698
callq 0x2260
movsbl %r15b, %esi
leaq 0x30(%rsp), %rdi
callq 0x2260
movq 0x10(%rsp), %rdi
callq 0x2220
imull $0xa, %eax, %r15d
movq 0x30(%rsp), %rdi
callq 0x2220
addl %eax, %ebx
addl %r15d, %ebx
cvtsi2ss %ebx, %xmm0
movq 0x8(%rsp), %rax
movss %xmm0, (%rax,%r12,4)
addl $0x2, %r13d
incl %ebp
leaq 0x10(%rsp), %rdi
jmp 0x2698
leaq 0x30(%rsp), %rdi
callq 0x20b0
leaq 0x10(%rsp), %rdi
callq 0x20b0
movq 0x8(%rsp), %rbx
leaq 0x50(%rsp), %rdi
callq 0x2060
movq %rbx, %rax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2799
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x20b0
leaq 0x10(%rsp), %rdi
callq 0x20b0
jmp 0x27b5
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x2290