// File: /workspace/github_c_makefile_star2_O1/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 0x27fc
movq %rdx, %rbx
leaq 0xacb5(%rip), %rsi      # 0xd004
movq %rdx, %rdi
callq 0x2160
testq %rax, %rax
je 0x27fc
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, 0x38(%rsp)
testl %ebx, %ebx
je 0x27d6
movl $0x1, %r12d
movq $0x0, 0x30(%rsp)
movl $0x0, 0x24(%rsp)
movl $0x0, (%rsp)
movl $0x1, %eax
movq %rax, 0x40(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x28(%rsp)
movq $0x0, 0x50(%rsp)
movq %r14, 0x58(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, %rdi
movl $0x418, %esi            # imm = 0x418
movq %r14, %rdx
callq 0x2110
testq %rax, %rax
je 0x280b
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 0x6b96
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 0x7ed1
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 0x272f
movq %rax, %rbp
movq 0x30(%rsp), %rax
incl %eax
movl %eax, 0x3c(%rsp)
xorl %r15d, %r15d
movl $0x1, %r9d
xorl %ebx, %ebx
movl $0x0, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x4(%rsp), %r14d
movl (%rsp), %r8d
testl %r14d, %r14d
movl %r14d, %eax
cmovel %r12d, %eax
movb (%r13,%r15), %cl
cmpb $0x22, %cl
cmovnel %r14d, %eax
cmpl $0x1, %r14d
movl %eax, %r14d
movl $0x0, %edx
cmovel %edx, %r14d
cmpb $0x22, %cl
cmovnel %eax, %r14d
cmpb $0x3d, %cl
jne 0x2542
testl %r8d, %r8d
jne 0x2542
movl %r8d, (%rsp)
movl %r14d, 0x4(%rsp)
movl %r9d, 0xc(%rsp)
movl %r9d, %r12d
movl %r12d, %r14d
movsbl (%r13,%r14), %edi
callq 0x7ed1
testb %al, %al
je 0x2526
incl %r12d
cmpq %r14, %rbp
ja 0x250c
cmpb $0x7b, (%r13,%r14)
movl $0x1, %r12d
movl 0x4(%rsp), %r14d
movl (%rsp), %r8d
movl 0xc(%rsp), %r9d
je 0x2561
addq %r13, %r15
cmpl $0x1, %r8d
jne 0x256c
movzbl (%r15), %eax
cmpl $0x7d, %eax
je 0x265b
cmpl $0x7b, %eax
jne 0x256c
incl 0x24(%rsp)
movl $0x1, %r8d
jmp 0x270b
testl %r8d, %r8d
sete %cl
movb (%r15), %al
cmpb $0x3b, %al
sete %sil
cmpl $0x0, 0x8(%rsp)
sete %dil
cmpb $0x7b, %al
sete %dl
testb %dil, %sil
jne 0x25a3
andb %cl, %dl
jne 0x25a3
cmpb $0x7d, %al
jne 0x26e9
testl %r8d, %r8d
jne 0x26e9
cmpl $0x1, %r14d
je 0x26e9
movl %r9d, 0xc(%rsp)
movl %r8d, (%rsp)
movq %r13, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x8291
testb %al, %al
jne 0x26da
movl %r14d, 0x4(%rsp)
movl $0x18, %edi
callq 0x2140
movq %rax, %r12
movq 0x68(%rsp), %rdi
callq 0x7d58
movq %rax, 0x8(%r12)
movl 0x28(%rsp), %eax
movl %eax, 0x14(%r12)
movl %eax, 0x10(%r12)
movq 0x48(%rsp), %r14
cmpb $0x0, (%r14)
je 0x266b
movq %r14, %rdi
callq 0x7d58
movq %rax, (%r12)
leal 0x1(%rbx), %edx
movq %r13, %rdi
movl 0x10(%rsp), %esi
callq 0x7db1
movq %rax, 0x28(%rsp)
movq (%r12), %rdi
movq %rax, %rsi
callq 0x7fbd
movq %rax, 0x10(%rsp)
movq (%r12), %rdi
callq 0x2030
movq 0x28(%rsp), %rdi
callq 0x2030
movq %r12, %rsi
movq 0x10(%rsp), %rax
movq %rax, (%r12)
movb $0x0, (%r14)
jmp 0x268f
xorl %r8d, %r8d
decl 0x24(%rsp)
setne %r8b
jmp 0x270b
movl 0x1c(%rsp), %eax
testl %eax, %eax
je 0x2679
movl %ebx, %edx
subl %eax, %edx
jmp 0x267c
leal 0x1(%rbx), %edx
movq %r13, %rdi
movl 0x10(%rsp), %esi
callq 0x7db1
movq %r12, %rsi
movq %rax, (%r12)
movl 0x4(%rsp), %r14d
movq 0x50(%rsp), %rax
incl %eax
movl %eax, 0x28(%rsp)
movq 0x70(%rsp), %rdi
callq 0x6b96
leal 0x1(%rbx), %eax
movl %eax, 0x10(%rsp)
incl 0x20(%rsp)
cmpl $0x1, 0x8(%rsp)
jne 0x26ce
xorl %eax, %eax
cmpb $0x7b, (%r15)
setne %al
movl %eax, 0x8(%rsp)
movl %ebx, 0x1c(%rsp)
jmp 0x26da
movl %ebx, 0x1c(%rsp)
movl $0x0, 0x8(%rsp)
movl (%rsp), %r8d
movl $0x1, %r12d
movl 0xc(%rsp), %r9d
movzbl (%r15), %eax
cmpl $0xa, %eax
je 0x2702
cmpl $0xd, %eax
jne 0x270b
leal 0x1(%rbx), %eax
cmpb $0xa, (%r13,%rax)
jne 0x270b
movl 0x3c(%rsp), %eax
movq %rax, 0x50(%rsp)
incl %ebx
incl %r9d
movq %rbx, %r15
cmpq %rbx, %rbp
ja 0x24c4
movl %r14d, 0x4(%rsp)
cmpl $0x0, 0x20(%rsp)
sete %al
movl 0x10(%rsp), %ebx
jmp 0x2737
xorl %ebx, %ebx
movb $0x1, %al
movl (%rsp), %r8d
testb %al, %al
movq 0x58(%rsp), %r14
movq 0x48(%rsp), %r15
movl %r8d, (%rsp)
je 0x2792
movq %r15, %rdi
callq 0x20b0
movq %rax, %rbx
movq %r13, %rdi
callq 0x20b0
addq %rbx, %rax
cmpq $0x418, %rax            # imm = 0x418
jb 0x2785
movq 0x40(%rsp), %rax
incl %eax
movq %rax, 0x40(%rsp)
imulq $0x418, %rax, %rsi      # imm = 0x418
movq %r15, %rdi
callq 0x2150
movq %rax, %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2180
jmp 0x27c0
testl %ebx, %ebx
je 0x27c0
movq %r13, %rdi
callq 0x20b0
movq %r13, %rdi
movl %ebx, %esi
movl %eax, %edx
callq 0x7db1
movq %rax, %rbx
movq %r15, %rdi
movq %rax, %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x2030
movq 0x30(%rsp), %rax
incl %eax
movq %rax, 0x30(%rsp)
cmpl 0x38(%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 0xa7e9(%rip), %rdi      # 0xd007
leaq 0xa7f4(%rip), %rsi      # 0xd019
leaq 0xa7f3(%rip), %rdx      # 0xd01f
leaq 0xa7f2(%rip), %r8       # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
movl $0x1, %edi
callq 0x21a0