// File: 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
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x8(%rbp)
movl $0x400, -0xc(%rbp)      # imm = 0x400
movslq -0xc(%rbp), %rdi
callq 0x2030
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x228(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
movl $0x8, %edx
callq 0x23e0
movl $0x24, %edi
callq 0x2280
movq -0x2a0(%rbp), %rdi
movq %rax, -0x230(%rbp)
callq 0x2340
movb %al, -0x295(%rbp)
jmp 0x290b
movb -0x295(%rbp), %al
testb $0x1, %al
jne 0x291a
jmp 0x2cf5
jmp 0x291c
movq -0x228(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x228(%rbp,%rax), %rdi
callq 0x23f0
movb %al, -0x2a1(%rbp)
jmp 0x293c
movb -0x2a1(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2a2(%rbp)
jne 0x2960
jmp 0x2951
movl -0x1c(%rbp), %eax
cmpl -0xc(%rbp), %eax
setl %al
movb %al, -0x2a2(%rbp)
movb -0x2a2(%rbp), %al
testb $0x1, %al
jne 0x296c
jmp 0x29a7
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rax
addq %rax, %rsi
leaq -0x228(%rbp), %rdi
callq 0x22a0
jmp 0x2985
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x291c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x2d1f
movq -0x18(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl $0x0, -0x244(%rbp)
movl $0x0, -0x248(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x22b0
leaq -0x288(%rbp), %rdi
callq 0x22b0
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x2cdd
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x21b0
cmpl $0x0, %eax
je 0x2b50
movq -0x18(%rbp), %rax
movl -0x244(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %edi
callq 0x21b0
cmpl $0x0, %eax
je 0x2b50
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x23d(%rbp)
movq -0x18(%rbp), %rcx
movslq -0x244(%rbp), %rax
movb 0x1(%rax,%rcx), %al
movb %al, -0x23e(%rbp)
movsbl -0x23d(%rbp), %esi
leaq -0x268(%rbp), %rdi
callq 0x2370
jmp 0x2a79
movsbl -0x23e(%rbp), %esi
leaq -0x288(%rbp), %rdi
callq 0x2370
jmp 0x2a8e
leaq -0x268(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2320
imull $0xa, %eax, %eax
addl -0x28c(%rbp), %eax
movl %eax, -0x28c(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2320
shll $0x0, %eax
addl -0x28c(%rbp), %eax
movl %eax, -0x28c(%rbp)
cvtsi2ssl -0x28c(%rbp), %xmm0
movq -0x230(%rbp), %rax
movslq -0x248(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
movl -0x248(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x248(%rbp)
jmp 0x2cd8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x2130
leaq -0x268(%rbp), %rdi
callq 0x2130
jmp 0x2d1f
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x21b0
cmpl $0x0, %eax
je 0x2c11
movq -0x18(%rbp), %rax
movl -0x244(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2d, %eax
jne 0x2c11
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x23d(%rbp)
movsbl -0x23d(%rbp), %esi
leaq -0x268(%rbp), %rdi
callq 0x2370
jmp 0x2bb3
leaq -0x268(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2320
movl %eax, -0x290(%rbp)
imull $0xffffffff, -0x290(%rbp), %eax # imm = 0xFFFFFFFF
cvtsi2ss %eax, %xmm0
movq -0x230(%rbp), %rax
movslq -0x248(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
movl -0x248(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x248(%rbp)
jmp 0x2cd6
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
movsbl (%rax,%rcx), %edi
callq 0x21b0
cmpl $0x0, %eax
je 0x2cc5
movq -0x18(%rbp), %rax
movl -0x244(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x2d, %eax
je 0x2cc5
movq -0x18(%rbp), %rax
movslq -0x244(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x23d(%rbp)
movsbl -0x23d(%rbp), %esi
leaq -0x268(%rbp), %rdi
callq 0x2370
jmp 0x2c70
leaq -0x268(%rbp), %rdi
callq 0x20b0
movq %rax, %rdi
callq 0x2320
movl %eax, -0x294(%rbp)
cvtsi2ssl -0x294(%rbp), %xmm0
movq -0x230(%rbp), %rax
movslq -0x248(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
movl -0x248(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x248(%rbp)
jmp 0x2cd4
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x2cd6
jmp 0x2cd8
jmp 0x29e4
leaq -0x288(%rbp), %rdi
callq 0x2130
leaq -0x268(%rbp), %rdi
callq 0x2130
movq -0x230(%rbp), %rax
movq %rax, -0x2b0(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x2060
movq -0x2b0(%rbp), %rax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
leaq -0x228(%rbp), %rdi
callq 0x2060
movq -0x238(%rbp), %rdi
callq 0x23b0
nopw (%rax,%rax)