// File: /workspace/github_c_makefile_star2_O3/System-Glitch[P]ESGILinter-C/src/main.c
// Function: main
int main(int argc, char **argv) {
	char *path = NULL;
	char *file = NULL;
	arraylist_t *conf = arraylist_init(ARRAYLIST_DEFAULT_CAPACITY);
	arraylist_t *files = arraylist_init(ARRAYLIST_DEFAULT_CAPACITY);
	arraylist_t *buffer = NULL;
	arraylist_t *real_file = NULL;
	arraylist_t *lines = NULL;
	scope_t *scope = NULL;
	rule_t *rule = NULL;

	if(conf == NULL || files == NULL) {
		printf("%s[ERROR]%s %s%s\n", COLOR_RED, COLOR_YELLOW, strerror(errno), FORMAT_RESET);
		return EXIT_FAILURE;
	}

	path = malloc(sizeof(char) * 255);
	if(argc >= 2) {
		strcpy(path, argv[1]);
	} else {
		strcpy(path, ".");
	}

	if(load_configuration("linter.conf", conf)) { //replace with argument
		printf("%s[ERROR]%s Failed to load config: %s%s\n", COLOR_RED, COLOR_YELLOW, strerror(errno), FORMAT_RESET);
		arraylist_free(conf, 0);
		return EXIT_FAILURE;
	}

	search_files(conf, files, path);

	for(int i = 0; i < files->size; i++){
		buffer = arraylist_init(ARRAYLIST_DEFAULT_CAPACITY);
		real_file = arraylist_init(ARRAYLIST_DEFAULT_CAPACITY);

		if(buffer == NULL || real_file == NULL) {
			printf("%s[ERROR]%s %s%s\n", COLOR_RED, COLOR_YELLOW, strerror(errno), FORMAT_RESET);
			return EXIT_FAILURE;
		}

		file = arraylist_get(files, i);
		printf("Checking file: %s\n", file);
		file_loader(buffer/*, files*/, real_file, file);

		lines = get_lines_list(buffer);
		scope = parse_root_scope(lines);
		arraylist_free(lines, 1);

		if(scope != NULL) {
			if(check_rule(conf, "max-file-line-numbers")) {
				rule = get_rule(conf, "max-file-line-numbers");
				max_file_line_numbers(real_file, rule->value, file);
			}
			if(check_rule(conf, "max-line-numbers")) {
				rule = get_rule(conf, "max-line-numbers");
				check_max_line_length(real_file, rule->value, file);
			}
			if(check_rule(conf, "comments-header")) {
				check_comments_header(real_file, file);
			}
			if(check_rule(conf, "indent")) {
				rule = get_rule(conf, "indent");
				check_indent(real_file, rule->value, file);
			}
			if(check_rule(conf, "no-trailing-space")) {
				no_trailing_spaces(real_file, file);
			}
			if(check_rule(conf, "array-bracket-eol")) {
				array_bracket_eol(buffer, real_file);
			}
			if(check_rule(conf, "comma-spacing")) {
				comma_spacing(buffer, real_file);
			}
			if(check_rule(conf, "no-prototype")) {
				check_no_prototype(scope, buffer, real_file);
			}
			if(check_rule(conf, "no-multi-declaration")) {
				check_no_multi_declaration(scope, buffer, real_file);
			}
			parse_and_check(scope, buffer, NULL, NULL, conf, real_file);
		} else {
			printf("%s[WARNING]%s Scope parsing failed for %s%s\n", COLOR_RED, COLOR_YELLOW, FORMAT_RESET, file);
		}

		/*
		 *
		 * TREAT DATA FROM HERE
		 *
		 * APPLY RULES
		 *
		 */
		arraylist_free(real_file, 1);
		free_buffer(buffer);
	}
	arraylist_free(files, 1);

	free_conf(conf);
    free(path);
	return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movl %edi, %r14d
movl $0xa, %edi
callq 0x6b58
movq %rax, %r12
movl $0xa, %edi
callq 0x6b58
testq %r12, %r12
sete %dl
movq %rax, -0x30(%rbp)
testq %rax, %rax
sete %cl
orb %dl, %cl
cmpb $0x1, %cl
jne 0xcc31
callq 0x2050
movl (%rax), %edi
callq 0x21b0
leaq 0x3f7(%rip), %rdi       # 0xd007
leaq 0x402(%rip), %rsi       # 0xd019
leaq 0x401(%rip), %rdx       # 0xd01f
leaq 0x400(%rip), %r8        # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
jmp 0xcca7
movl $0xff, %edi
callq 0x2140
movq %rax, %r15
cmpl $0x2, %r14d
jl 0xcc52
movq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x2080
jmp 0xcc58
movw $0x2e, (%r15)
leaq 0x2054(%rip), %rdi      # 0xecb3
movq %r12, %rsi
callq 0x58f0
testb %al, %al
je 0xccbb
callq 0x2050
movl (%rax), %edi
callq 0x21b0
leaq 0x2041(%rip), %rdi      # 0xecbf
leaq 0x394(%rip), %rsi       # 0xd019
leaq 0x393(%rip), %rdx       # 0xd01f
leaq 0x392(%rip), %r8        # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
movq %r12, %rdi
xorl %esi, %esi
callq 0x6d97
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
movq %r15, -0x48(%rbp)
movq %r15, %rdx
callq 0x288c
cmpq $0x0, 0x8(%rbx)
je 0xcf60
xorl %ebx, %ebx
movq %r12, -0x38(%rbp)
movl $0xa, %edi
callq 0x6b58
movq %rax, %r13
movl $0xa, %edi
callq 0x6b58
testq %r13, %r13
je 0xcbfd
movq %rax, %r15
testq %rax, %rax
je 0xcbfd
movq -0x30(%rbp), %rdi
movq %rbx, -0x40(%rbp)
movq %rbx, %rsi
callq 0x6d0c
movq %rax, %r14
leaq 0x1fc0(%rip), %rdi      # 0xece8
movq %rax, %rsi
xorl %eax, %eax
callq 0x20d0
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2324
movl 0x8(%r13), %edi
callq 0x6b58
movq %rax, %rbx
cmpq $0x0, 0x8(%r13)
je 0xcd7d
xorl %r12d, %r12d
movq %r13, %rdi
movq %r12, %rsi
callq 0x6d0c
movq (%rax), %rdi
callq 0x7dc1
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6c26
incq %r12
cmpq 0x8(%r13), %r12
jb 0xcd56
movq %rbx, %rdi
callq 0x621f
movq %rax, %r12
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6d97
movq %r12, -0x50(%rbp)
testq %r12, %r12
je 0xcf0c
movq -0x38(%rbp), %r12
movq %r12, %rdi
leaq 0x1f4b(%rip), %rbx      # 0xecfb
movq %rbx, %rsi
callq 0x6142
testb %al, %al
je 0xcdd5
movq %r12, %rdi
movq %rbx, %rsi
callq 0x6010
movl 0xc(%rax), %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x9378
movq %r12, %rdi
leaq 0x1f32(%rip), %rbx      # 0xed11
movq %rbx, %rsi
callq 0x6142
testb %al, %al
je 0xce04
movq %r12, %rdi
movq %rbx, %rsi
callq 0x6010
movl 0xc(%rax), %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x9d08
movq %r12, %rdi
leaq 0x1f14(%rip), %rsi      # 0xed22
callq 0x6142
testb %al, %al
je 0xce22
movq %r15, %rdi
movq %r14, %rsi
callq 0xab5c
movq %r12, %rdi
leaq 0x1f06(%rip), %rbx      # 0xed32
movq %rbx, %rsi
callq 0x6142
testb %al, %al
je 0xce51
movq %r12, %rdi
movq %rbx, %rsi
callq 0x6010
movl 0xc(%rax), %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x962c
movq %r12, %rdi
leaq 0x1ede(%rip), %rsi      # 0xed39
callq 0x6142
testb %al, %al
je 0xce6f
movq %r15, %rdi
movq %r14, %rsi
callq 0x902c
movq %r12, %rdi
leaq 0x1ed2(%rip), %rsi      # 0xed4b
callq 0x6142
testb %al, %al
movq -0x40(%rbp), %rbx
je 0xce91
movq %r13, %rdi
movq %r15, %rsi
callq 0x9104
movq %r12, %rdi
leaq 0x1ec2(%rip), %rsi      # 0xed5d
callq 0x6142
testb %al, %al
movq -0x50(%rbp), %r14
je 0xceb3
movq %r13, %rdi
movq %r15, %rsi
callq 0x9a18
movq %r12, %rdi
leaq 0x1eae(%rip), %rsi      # 0xed6b
callq 0x6142
testb %al, %al
je 0xced4
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x9b38
movq %r12, %rdi
leaq 0x1e9a(%rip), %rsi      # 0xed78
callq 0x6142
testb %al, %al
je 0xcef5
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x9438
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r12, %r8
movq %r15, %r9
callq 0x9e5c
jmp 0xcf3a
leaq 0x1e7a(%rip), %rdi      # 0xed8d
leaq 0xff(%rip), %rsi        # 0xd019
leaq 0xfe(%rip), %rdx        # 0xd01f
leaq 0xfd(%rip), %rcx        # 0xd025
movq %r14, %r8
xorl %eax, %eax
callq 0x20d0
movq -0x38(%rbp), %r12
movq -0x40(%rbp), %rbx
movq %r15, %rdi
movl $0x1, %esi
callq 0x6d97
movq %r13, %rdi
callq 0x2a91
incq %rbx
movq -0x30(%rbp), %rax
cmpq %rbx, 0x8(%rax)
ja 0xcce2
movq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x6d97
movq %r12, %rdi
callq 0x60f2
movq -0x48(%rbp), %rdi
callq 0x2030
xorl %eax, %eax
jmp 0xccac