// File: /workspace/github_c_makefile_star2_O3/System-Glitch[P]ESGILinter-C/src/fileloader.c
// Function: file_loader
void file_loader(arraylist_t *e/*, arraylist_t *files*/, arraylist_t *real_file, char *filename){

    if(strlen(filename) <= 0) return;
    unsigned int length;
    unsigned int i;
    unsigned int j;
    unsigned int index;
    int tempo;
    int init;
    char *line;
    char *tmp = NULL;
    char *tmp2 = NULL;
    char *tmp_line;
    char *res;
    char *for_loop = NULL;
    char *real;
    char c;
    int real_line;
    int last_real_line;
    line_t *l;
    int line_counter;
    int counter;
    int start_for;
    int literal;
    //int include_fold;
    unsigned int realloc_count = 1;
    size_t start_buffer;
    size_t file_length;
    size_t for_loop_length;
    int start_array;
    int array_length;
    FILE *src;

    src = fopen(filename,"rb");

    if(src == NULL){
        return;
    }

    length = file_row_number(src);
    /*
     * Memory allocation
     */

    line = malloc(sizeof(char) * 1048);
    tmp_line = malloc(sizeof(char) * 1048);
    tmp_line[0] = '\0';

    /*
     * Load the array
     */

    line_counter = 0;
    real_line = 0;
    last_real_line = 0;
    start_for = 0;
    literal = 0;
    start_array = 0;
    array_length = 0;
    start_buffer = 0;
    //include_fold = 0;

    for(i = 0; i < length; i++){
        tempo = 0;
        counter = 0;
        init = 0;
        line_counter++;
        if(!fgets(line, 1048, src)) {
            printf("%s[ERROR]%s %s%s\n", COLOR_RED, COLOR_YELLOW, strerror(errno), FORMAT_RESET);
            exit(EXIT_FAILURE);
        }

        /*if(strstr(line, "#include \"") == line){
            int first_index = strindexof(line, '"');
            int last_index = strlastindexof(line, '"');
            tmp2 = strsubstr(line, first_index+1, last_index-first_index-1);
            last_index = strlastindexof(tmp2, '/');
            if(last_index != -1){
                tmp = strsubstr(tmp2, last_index+1, strlen(tmp2) - last_index);
                include_fold = 1;
                free(tmp2);
                tmp2 = NULL;
            }
            for(size_t k = 0; k < files->size; k++){
                if(include_fold){
                    if(strstr(files->array[k], tmp) != NULL){
                        file_loader(e, files, real_file, files->array[k]);
                        start_buffer = real_file->size;
                    }
                }else{
                    if(strstr(files->array[k], tmp2) != NULL){
                        file_loader(e, files, real_file, files->array[k]);
                        start_buffer = real_file->size;
                    }
                }
            }
            if(tmp != NULL) {
                free(tmp);
                tmp = NULL;
            }else{
                free(tmp2);
                tmp2 = NULL;
            }
            continue;
        }*/

        real = malloc(sizeof(char) * 1048);
        strcpy(real, line);
        arraylist_add(real_file, real);

        for_loop = strstr(line, "for");
        if(for_loop != NULL){
            for_loop += 3;
            index = 0;
            start_for = 1;
            for_loop_length = strlen(for_loop);
            while(is_whitespace(c = for_loop[index]) && index < for_loop_length) { index++; };
            if(for_loop[index] == '(') start_for = 1;
        }

        file_length = strlen(line);
        for(j = 0; j < file_length ; j++){
            if(line[j] == '"' && literal == 1){
                literal = 0;
            }else if(line[j] == '"' && literal == 0){
                literal = 1;
            }
            if(line[j] == '=' && start_array == 0){
                index = j+1;
                while(is_whitespace(c = line[index]) && index < file_length) { index++; };
                if(line[index] == '{'){
                    start_array = 1;
                    continue;
                }
            }
            if(start_array == 1 && line[j] == '{'){
                array_length++;
                continue;
            }
            if(start_array == 1 && line[j] == '}'){
                array_length--;
                if(array_length == 0) start_array = 0;
                continue;
            }


            if(((line[j] == ';' && start_for == 0) || (line[j] == '{' && start_array == 0) || (line[j] == '}' && start_array == 0)) && literal != 1 ){
                if(!check_quotes(line, line + j, file_length)) {
                    l = malloc(sizeof(line_t));
                    l->source = strduplicate(filename);
                    l->start_line_in_buffer = start_buffer+last_real_line;
                    l->start_real_line = last_real_line;
                    last_real_line = real_line + 1;

                    if(strlen(tmp_line) != 0){
                        l->line = strduplicate(tmp_line);
                        tmp = strsubstr(line, tempo, j+1);
                        tmp2 = strconcat(l->line, tmp);
                        free(l->line);
                        free(tmp);
                        l->line = tmp2;
                        tmp_line[0] = '\0';
                    }else{
                        if(!init){
                            l->line = strsubstr(line, tempo, j+1);
                        }else{
                            l->line = strsubstr(line, tempo, j-init);
                        }
                    }
                    arraylist_add(e, l);
                    tempo = j+1;
                    counter++;
                    init = j;
                    if(start_for == 1 && line[j] == '{')
                        start_for = 0;
                }
            }
            if(line[j] == '\n' || (j < file_length && line[j] == '\r' && line[j+1] == '\n')){
                real_line = i + 1;
            }
        }

        if(!counter){
            if(strlen(tmp_line) + strlen(line) >= 1048)
                tmp_line = realloc(tmp_line, sizeof(char) * 1048 * ++realloc_count);
            strcat(tmp_line, line);
        }else if(counter && tempo){
            res = strsubstr(line, tempo, strlen(line));
            strcat(tmp_line, res);
            free(res);
        }
    }


    /*
     * Free the memory
     */
    fclose(src);
    free(line);
    free(tmp_line);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x60(%rsp)
movq %rdi, 0x70(%rsp)
cmpb $0x0, (%rdx)
je 0x2841
movq %rdx, %rbx
leaq 0xacb5(%rip), %rsi      # 0xd004
movq %rdx, %rdi
callq 0x2160
testq %rax, %rax
je 0x2841
movq %rax, %r14
movq %rbx, 0x68(%rsp)
movq %rax, %rdi
callq 0x22cc
movl %eax, %ebx
movl $0x418, %edi            # imm = 0x418
callq 0x2140
movq %rax, %r13
movl $0x418, %edi            # imm = 0x418
callq 0x2140
movq %rax, %r15
movb $0x0, (%rax)
movl %ebx, 0x48(%rsp)
testl %ebx, %ebx
je 0x281b
movl $0x1, %r12d
movq $0x0, 0x38(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x1, %eax
movq %rax, 0x50(%rsp)
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x1c(%rsp)
movq $0x0, 0x58(%rsp)
movq %r14, 0x30(%rsp)
movq %r13, %rdi
movl $0x418, %esi            # imm = 0x418
movq %r14, %rdx
callq 0x2110
testq %rax, %rax
je 0x2850
movq %r15, 0x40(%rsp)
movl $0x418, %edi            # imm = 0x418
callq 0x2140
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2080
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x6c26
movq %r13, %rdi
leaq 0xabf4(%rip), %rsi      # 0xd02a
callq 0x21c0
testq %rax, %rax
movl 0x8(%rsp), %ecx
je 0x2475
movq %rax, %rbx
addq $0x3, %rbx
movq %rbx, %rdi
callq 0x20b0
movq %rax, %r15
xorl %ebp, %ebp
movl %ebp, %r14d
movsbl (%rbx,%r14), %edi
callq 0x7f3a
movl $0x1, %ecx
testb %al, %al
je 0x2475
incl %ebp
cmpq %r14, %r15
ja 0x2458
movl %ecx, 0x8(%rsp)
movq %r13, %rdi
callq 0x20b0
testq %rax, %rax
je 0x27ac
movq %rax, %rbp
movq 0x38(%rsp), %rax
incl %eax
movl %eax, 0x4c(%rsp)
xorl %r15d, %r15d
movl $0x1, %r9d
xorl %ebx, %ebx
movl $0x0, 0x10(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x20(%rsp)
movl (%rsp), %r14d
movl 0x4(%rsp), %r8d
movb (%r13,%r15), %al
testl %r14d, %r14d
movl %r14d, %ecx
cmovel %r12d, %ecx
cmpb $0x22, %al
cmovnel %r14d, %ecx
cmpl $0x1, %r14d
movl %ecx, %r14d
movl $0x0, %edx
cmovel %edx, %r14d
cmpb $0x22, %al
cmovnel %ecx, %r14d
addq %r13, %r15
cmpb $0x3d, %al
jne 0x255f
testl %r8d, %r8d
jne 0x255f
movl %r8d, 0x4(%rsp)
movl %r14d, (%rsp)
movl %r9d, 0xc(%rsp)
movl %r9d, %r14d
movl %r14d, %r12d
movsbl (%r13,%r12), %edi
callq 0x7f3a
testb %al, %al
je 0x2526
incl %r14d
cmpq %r12, %rbp
ja 0x250c
cmpb $0x7b, (%r13,%r12)
jne 0x2548
movl $0x1, %r8d
movl $0x1, %r12d
movl (%rsp), %r14d
movl 0xc(%rsp), %r9d
jmp 0x2728
movb (%r15), %al
movl $0x1, %r12d
movl (%rsp), %r14d
movl 0x4(%rsp), %r8d
movl 0xc(%rsp), %r9d
cmpl $0x1, %r8d
jne 0x2580
cmpb $0x7d, %al
je 0x25e1
movzbl %al, %ecx
cmpl $0x7b, %ecx
jne 0x2580
incl 0x2c(%rsp)
movl $0x1, %r8d
jmp 0x2728
testl %r8d, %r8d
sete %cl
cmpb $0x3b, %al
sete %sil
cmpl $0x0, 0x8(%rsp)
sete %dil
cmpb $0x7b, %al
sete %dl
testb %dil, %sil
jne 0x25b4
andb %cl, %dl
jne 0x25b4
cmpb $0x7d, %al
jne 0x2708
testl %r8d, %r8d
jne 0x2708
cmpl $0x1, %r14d
je 0x2708
movl %r9d, 0xc(%rsp)
movl %r8d, 0x4(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x82dc
testb %al, %al
je 0x25f1
movb (%r15), %al
jmp 0x26f8
xorl %r8d, %r8d
decl 0x2c(%rsp)
setne %r8b
jmp 0x2728
movl %r14d, (%rsp)
movl $0x18, %edi
callq 0x2140
movq %rax, %r12
movq 0x68(%rsp), %rdi
callq 0x7dc1
movq %rax, 0x8(%r12)
movl 0x1c(%rsp), %eax
movl %eax, 0x14(%r12)
movl %eax, 0x10(%r12)
movq 0x40(%rsp), %r14
cmpb $0x0, (%r14)
je 0x2688
movq %r14, %rdi
callq 0x7dc1
movq %rax, (%r12)
leal 0x1(%rbx), %edx
movl %edx, 0x1c(%rsp)
movq %r13, %rdi
movl 0x10(%rsp), %esi
callq 0x7e1a
movq %rax, 0x20(%rsp)
movq (%r12), %rdi
movq %rax, %rsi
callq 0x8026
movq %rax, 0x10(%rsp)
movq (%r12), %rdi
callq 0x2030
movq 0x20(%rsp), %rdi
callq 0x2030
movq %r12, %rsi
movq 0x10(%rsp), %rax
movq %rax, (%r12)
movb $0x0, (%r14)
movl 0x1c(%rsp), %eax
jmp 0x26b0
movq %r12, %r14
movl 0x20(%rsp), %eax
testl %eax, %eax
je 0x273b
movl %ebx, %edx
subl %eax, %edx
movq %r13, %rdi
movl 0x10(%rsp), %esi
callq 0x7e1a
movq %r14, %rsi
movq %rax, (%r14)
leal 0x1(%rbx), %eax
movl %eax, 0x10(%rsp)
movl (%rsp), %r14d
movq 0x58(%rsp), %rax
incl %eax
movl %eax, 0x1c(%rsp)
movq 0x70(%rsp), %rdi
callq 0x6c26
incl 0x28(%rsp)
movb (%r15), %al
cmpl $0x1, 0x8(%rsp)
jne 0x26ec
xorl %ecx, %ecx
cmpb $0x7b, %al
setne %cl
movl %ecx, 0x8(%rsp)
movl %ebx, 0x20(%rsp)
jmp 0x26f8
movl %ebx, 0x20(%rsp)
movl $0x0, 0x8(%rsp)
movl 0x4(%rsp), %r8d
movl $0x1, %r12d
movl 0xc(%rsp), %r9d
cmpb $0xa, %al
je 0x271f
movzbl %al, %eax
cmpl $0xd, %eax
jne 0x2728
leal 0x1(%rbx), %eax
cmpb $0xa, (%r13,%rax)
jne 0x2728
movl 0x4c(%rsp), %eax
movq %rax, 0x58(%rsp)
incl %ebx
incl %r9d
movq %rbx, %r15
cmpq %rbx, %rbp
ja 0x24c4
jmp 0x275e
leal 0x1(%rbx), %r12d
movq %r13, %rdi
movl 0x10(%rsp), %esi
movl %r12d, %edx
callq 0x7e1a
movq %r14, %rsi
movq %rax, (%r14)
movl %r12d, 0x10(%rsp)
jmp 0x26b4
cmpl $0x0, 0x28(%rsp)
movq 0x40(%rsp), %r15
movl %r8d, 0x4(%rsp)
je 0x27b5
movl %r14d, (%rsp)
movl 0x10(%rsp), %ebx
testl %ebx, %ebx
je 0x27a5
movq %r13, %rdi
callq 0x20b0
movq %r13, %rdi
movl %ebx, %esi
movl %eax, %edx
callq 0x7e1a
movq %rax, %rbx
movq %r15, %rdi
movq %rax, %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x2030
movq 0x30(%rsp), %r14
jmp 0x2805
movq 0x40(%rsp), %r15
movl (%rsp), %r14d
movl %r14d, (%rsp)
movq %r15, %rdi
callq 0x20b0
movq %rax, %rbx
movq %r13, %rdi
callq 0x20b0
addq %rbx, %rax
cmpq $0x418, %rax            # imm = 0x418
jb 0x27f5
movq 0x50(%rsp), %rax
incl %eax
movq %rax, 0x50(%rsp)
imulq $0x418, %rax, %rsi      # imm = 0x418
movq %r15, %rdi
callq 0x2150
movq %rax, %r15
movq 0x30(%rsp), %r14
movq %r15, %rdi
movq %r13, %rsi
callq 0x2180
movq 0x38(%rsp), %rax
incl %eax
movq %rax, 0x38(%rsp)
cmpl 0x48(%rsp), %eax
jne 0x23e9
movq %r14, %rdi
callq 0x2090
movq %r13, %rdi
callq 0x2030
movq %r15, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2050
movl (%rax), %edi
callq 0x21b0
leaq 0xa7a4(%rip), %rdi      # 0xd007
leaq 0xa7af(%rip), %rsi      # 0xd019
leaq 0xa7ae(%rip), %rdx      # 0xd01f
leaq 0xa7ad(%rip), %r8       # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
movl $0x1, %edi
callq 0x21a0