// File: /workspace/github_c_makefile_star2_O2/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 0x27d0
movq %rdx, %rbx
leaq 0xacc7(%rip), %rsi      # 0xd004
movq %rdx, %rdi
callq 0x2150
testq %rax, %rax
je 0x27d0
movq %rax, %r14
movq %rbx, 0x68(%rsp)
movq %rax, %rdi
callq 0x22bc
movl %eax, 0x44(%rsp)
movl $0x418, %edi            # imm = 0x418
callq 0x2130
movq %rax, %r13
movl $0x418, %edi            # imm = 0x418
callq 0x2130
movq %rax, %r15
movb $0x0, (%rax)
pushq $0x1
popq %rax
movq $0x0, 0x58(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq %rax, 0x48(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x34(%rsp)
movl $0x0, 0x24(%rsp)
movq %r14, 0x50(%rsp)
movq %r15, 0x38(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x44(%rsp), %eax
je 0x27df
movq %r13, %rdi
movl $0x418, %esi            # imm = 0x418
movq %r14, %rdx
callq 0x2100
testq %rax, %rax
je 0x2805
movl $0x418, %edi            # imm = 0x418
callq 0x2130
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2080
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x68b5
movq %r13, %rdi
leaq 0xac10(%rip), %rsi      # 0xd02a
callq 0x21b0
testq %rax, %rax
pushq $0x1
popq %r14
je 0x245f
movq %rax, %rbx
addq $0x3, %rbx
movq %rbx, %rdi
callq 0x20b0
movq %rax, %r15
xorl %r14d, %r14d
movl %r14d, %r12d
movsbl (%rbx,%r12), %edi
callq 0x7ace
testb %al, %al
je 0x2456
incl %r14d
cmpq %r12, %r15
ja 0x243d
pushq $0x1
popq %r14
movl %r14d, 0x8(%rsp)
movq %r13, %rdi
callq 0x20b0
movq %rax, %r12
incl 0x24(%rsp)
movl $0x0, 0x28(%rsp)
movl %r14d, %r8d
movl $0x0, 0x30(%rsp)
movl $0x0, 0x10(%rsp)
xorl %r9d, %r9d
movl 0x4(%rsp), %ebx
movl %r9d, %r15d
cmpq %r15, %r12
jbe 0x272a
movb (%r13,%r15), %al
testl %ebp, %ebp
movl %ebp, %ecx
cmovel %r14d, %ecx
cmpb $0x22, %al
cmovnel %ebp, %ecx
cmpl $0x1, %ebp
movl %ecx, %ebp
movl $0x0, %edx
cmovel %edx, %ebp
cmpb $0x22, %al
cmovnel %ecx, %ebp
addq %r13, %r15
cmpb $0x3d, %al
jne 0x252e
testl %ebx, %ebx
jne 0x252e
movq %r9, 0x18(%rsp)
movl %ebp, 0xc(%rsp)
movl %ebx, 0x4(%rsp)
movl %r8d, %ebp
movl %r8d, %ebx
movl %ebx, %r14d
movsbl (%r13,%r14), %edi
callq 0x7ace
testb %al, %al
je 0x24f7
incl %ebx
cmpq %r14, %r12
ja 0x24de
cmpb $0x7b, (%r13,%r14)
jne 0x2517
pushq $0x1
popq %r14
movl %r14d, %ebx
movl %ebp, %r8d
movl 0xc(%rsp), %ebp
movq 0x18(%rsp), %r9
jmp 0x26fa
movb (%r15), %al
pushq $0x1
popq %r14
movl 0x4(%rsp), %ebx
movl %ebp, %r8d
movl 0xc(%rsp), %ebp
movq 0x18(%rsp), %r9
cmpl $0x1, %ebx
jne 0x254b
cmpb $0x7d, %al
je 0x25b4
movzbl %al, %ecx
cmpl $0x7b, %ecx
jne 0x254b
incl 0x34(%rsp)
movl %r14d, %ebx
jmp 0x26fa
testl %ebx, %ebx
sete %cl
cmpb $0x3b, %al
sete %sil
cmpl $0x0, 0x8(%rsp)
sete %dil
cmpb $0x7b, %al
sete %dl
testb %dil, %sil
jne 0x257d
andb %cl, %dl
jne 0x257d
cmpb $0x7d, %al
jne 0x26d9
testl %ebx, %ebx
jne 0x26d9
cmpl $0x1, %ebp
je 0x26d9
movq %r9, 0x18(%rsp)
movl %r8d, 0xc(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movl %r12d, %edx
callq 0x7e2e
testb %al, %al
je 0x25c2
movb (%r15), %al
movl 0xc(%rsp), %r8d
movq 0x18(%rsp), %r9
jmp 0x26d9
xorl %ebx, %ebx
decl 0x34(%rsp)
setne %bl
jmp 0x26fa
movl %ebx, 0x4(%rsp)
pushq $0x18
popq %rdi
callq 0x2130
movq %rax, %rbx
movq 0x68(%rsp), %rdi
callq 0x794d
movq %rax, 0x8(%rbx)
movl 0x20(%rsp), %eax
movl %eax, 0x14(%rbx)
movl %eax, 0x10(%rbx)
movq 0x38(%rsp), %r14
cmpb $0x0, (%r14)
je 0x2650
movq %r14, %rdi
callq 0x794d
movq %rax, (%rbx)
movq 0x18(%rsp), %rax
leal 0x1(%rax), %edx
movl %edx, 0x20(%rsp)
movq %r13, %rdi
movl 0x10(%rsp), %esi
callq 0x79a6
movq %rax, 0x28(%rsp)
movq (%rbx), %rdi
movq %rax, %rsi
callq 0x7bab
movq %rax, 0x10(%rsp)
movq (%rbx), %rdi
callq 0x2030
movq 0x28(%rsp), %rdi
callq 0x2030
movq 0x10(%rsp), %rax
movq %rax, (%rbx)
movb $0x0, (%r14)
movl 0x20(%rsp), %eax
jmp 0x2679
movl 0x28(%rsp), %eax
testl %eax, %eax
je 0x2705
movq 0x18(%rsp), %r14
movl %r14d, %edx
subl %eax, %edx
movq %r13, %rdi
movl 0x10(%rsp), %esi
callq 0x79a6
movq %rax, (%rbx)
leal 0x1(%r14), %eax
movl %eax, 0x10(%rsp)
pushq $0x1
popq %r14
movq 0x58(%rsp), %rax
incl %eax
movl %eax, 0x20(%rsp)
movq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x68b5
incl 0x30(%rsp)
movb (%r15), %al
cmpl $0x1, 0x8(%rsp)
jne 0x26be
xorl %ecx, %ecx
cmpb $0x7b, %al
setne %cl
movl %ecx, 0x8(%rsp)
movq 0x18(%rsp), %r9
movl %r9d, 0x28(%rsp)
jmp 0x26d0
movq 0x18(%rsp), %r9
movl %r9d, 0x28(%rsp)
movl $0x0, 0x8(%rsp)
movl 0x4(%rsp), %ebx
movl 0xc(%rsp), %r8d
cmpb $0xa, %al
je 0x26f1
movzbl %al, %eax
cmpl $0xd, %eax
jne 0x26fa
leal 0x1(%r9), %eax
cmpb $0xa, (%r13,%rax)
jne 0x26fa
movl 0x24(%rsp), %eax
movq %rax, 0x58(%rsp)
incl %r9d
incl %r8d
jmp 0x2490
movq 0x18(%rsp), %rax
leal 0x1(%rax), %r14d
movq %r13, %rdi
movl 0x10(%rsp), %esi
movl %r14d, %edx
callq 0x79a6
movq %rax, (%rbx)
movl %r14d, 0x10(%rsp)
jmp 0x267d
movl %ebx, 0x4(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0x277a
movl 0x10(%rsp), %ebx
testl %ebx, %ebx
movq 0x50(%rsp), %r14
movq 0x38(%rsp), %r15
je 0x23c4
movq %r13, %rdi
callq 0x20b0
movq %r13, %rdi
movl %ebx, %esi
movl %eax, %edx
callq 0x79a6
movq %rax, %rbx
movq %r15, %rdi
movq %rax, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2030
jmp 0x23c4
movq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x20b0
movq %rax, %rbx
movq %r13, %rdi
callq 0x20b0
addq %rbx, %rax
cmpq $0x418, %rax            # imm = 0x418
movq 0x50(%rsp), %r14
jb 0x27c0
movq 0x48(%rsp), %rax
incl %eax
movq %rax, 0x48(%rsp)
imulq $0x418, %rax, %rsi      # imm = 0x418
movq %r15, %rdi
callq 0x2140
movq %rax, %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2170
jmp 0x23bf
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
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
callq 0x2050
movl (%rax), %edi
callq 0x21a0
leaq 0xa7ef(%rip), %rdi      # 0xd007
leaq 0xa7fa(%rip), %rsi      # 0xd019
leaq 0xa7f9(%rip), %rdx      # 0xd01f
leaq 0xa7f8(%rip), %r8       # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
pushq $0x1
popq %rdi
callq 0x2190