// File: /workspace/github_c_makefile_star2_O1/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 0x6ac8
movq %rax, %r15
movl $0xa, %edi
callq 0x6ac8
testq %r15, %r15
sete %dl
movq %rax, -0x30(%rbp)
testq %rax, %rax
sete %cl
orb %dl, %cl
cmpb $0x1, %cl
jne 0xca71
callq 0x2050
movl (%rax), %edi
callq 0x21b0
leaq 0x5b7(%rip), %rdi       # 0xd007
leaq 0x5c2(%rip), %rsi       # 0xd019
leaq 0x5c1(%rip), %rdx       # 0xd01f
leaq 0x5c0(%rip), %r8        # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
jmp 0xcae8
movl $0xff, %edi
callq 0x2140
movq %rax, %r12
cmpl $0x2, %r14d
jl 0xca92
movq 0x8(%rbx), %rsi
movq %r12, %rdi
callq 0x2080
jmp 0xca99
movw $0x2e, (%r12)
leaq 0x2223(%rip), %rdi      # 0xecc3
movq %r15, %rsi
callq 0x583c
testb %al, %al
je 0xcafc
callq 0x2050
movl (%rax), %edi
callq 0x21b0
leaq 0x2210(%rip), %rdi      # 0xeccf
leaq 0x553(%rip), %rsi       # 0xd019
leaq 0x552(%rip), %rdx       # 0xd01f
leaq 0x551(%rip), %r8        # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
movq %r15, %rdi
xorl %esi, %esi
callq 0x6d0a
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2847
cmpq $0x0, 0x8(%rbx)
setne %bl
je 0xcb82
movq %r12, -0x38(%rbp)
movl $0xa, %edi
callq 0x6ac8
movq %rax, %r12
movl $0xa, %edi
callq 0x6ac8
movq %rax, %r13
testq %r12, %r12
sete %al
testq %r13, %r13
sete %cl
orb %al, %cl
je 0xcb89
callq 0x2050
movl (%rax), %edi
callq 0x21b0
leaq 0x4ae(%rip), %rdi       # 0xd007
leaq 0x4b9(%rip), %rsi       # 0xd019
leaq 0x4b8(%rip), %rdx       # 0xd01f
leaq 0x4b7(%rip), %r8        # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
movl $0x1, %eax
jmp 0xce17
xorl %eax, %eax
jmp 0xce1b
xorl %r14d, %r14d
movq %r15, -0x40(%rbp)
movq -0x30(%rbp), %rdi
movq %r14, -0x48(%rbp)
movq %r14, %rsi
callq 0x6c76
movq %rax, %r14
leaq 0x214e(%rip), %rdi      # 0xecf8
movq %rax, %rsi
xorl %eax, %eax
callq 0x20d0
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2324
movl 0x8(%r12), %edi
callq 0x6ac8
movq %rax, %rbx
cmpq $0x0, 0x8(%r12)
je 0xcc02
xorl %r15d, %r15d
movq %r12, %rdi
movq %r15, %rsi
callq 0x6c76
movq (%rax), %rdi
callq 0x7d58
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6b96
incq %r15
cmpq 0x8(%r12), %r15
jb 0xcbda
movq %rbx, %rdi
callq 0x6153
movq %rbx, %rdi
movq %rax, %rbx
movl $0x1, %esi
callq 0x6d0a
testq %rbx, %rbx
je 0xcd91
movq %rbx, -0x50(%rbp)
movq -0x40(%rbp), %r15
movq %r15, %rdi
leaq 0x20d6(%rip), %rbx      # 0xed0b
movq %rbx, %rsi
callq 0x6076
testb %al, %al
je 0xcc5a
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5f44
movl 0xc(%rax), %esi
movq %r13, %rdi
movq %r14, %rdx
callq 0x91ec
movq %r15, %rdi
leaq 0x20bd(%rip), %rbx      # 0xed21
movq %rbx, %rsi
callq 0x6076
testb %al, %al
je 0xcc89
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5f44
movl 0xc(%rax), %esi
movq %r13, %rdi
movq %r14, %rdx
callq 0x9bcc
movq %r15, %rdi
leaq 0x209f(%rip), %rsi      # 0xed32
callq 0x6076
testb %al, %al
je 0xcca7
movq %r13, %rdi
movq %r14, %rsi
callq 0xaa34
movq %r15, %rdi
leaq 0x2091(%rip), %rbx      # 0xed42
movq %rbx, %rsi
callq 0x6076
testb %al, %al
je 0xccd6
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5f44
movl 0xc(%rax), %esi
movq %r13, %rdi
movq %r14, %rdx
callq 0x94b8
movq %r15, %rdi
leaq 0x2069(%rip), %rsi      # 0xed49
callq 0x6076
testb %al, %al
je 0xccf4
movq %r13, %rdi
movq %r14, %rsi
callq 0x8f4c
movq %r15, %rdi
leaq 0x205d(%rip), %rsi      # 0xed5b
callq 0x6076
testb %al, %al
movq -0x50(%rbp), %rbx
je 0xcd16
movq %r12, %rdi
movq %r13, %rsi
callq 0x9010
movq %r15, %rdi
leaq 0x204d(%rip), %rsi      # 0xed6d
callq 0x6076
testb %al, %al
movq -0x48(%rbp), %r14
je 0xcd38
movq %r12, %rdi
movq %r13, %rsi
callq 0x989c
movq %r15, %rdi
leaq 0x2039(%rip), %rsi      # 0xed7b
callq 0x6076
testb %al, %al
je 0xcd59
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x99bc
movq %r15, %rdi
leaq 0x2025(%rip), %rsi      # 0xed88
callq 0x6076
testb %al, %al
je 0xcd7a
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x92ac
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
movq %r13, %r9
callq 0x9d18
jmp 0xcdbf
leaq 0x2005(%rip), %rdi      # 0xed9d
leaq 0x27a(%rip), %rsi       # 0xd019
leaq 0x279(%rip), %rdx       # 0xd01f
leaq 0x278(%rip), %rcx       # 0xd025
movq %r14, %r8
xorl %eax, %eax
callq 0x20d0
movq -0x40(%rbp), %r15
movq -0x48(%rbp), %r14
movq %r13, %rdi
movl $0x1, %esi
callq 0x6d0a
movq %r12, %rdi
callq 0x2a8d
incq %r14
movq -0x30(%rbp), %rax
cmpq %r14, 0x8(%rax)
seta %bl
jbe 0xce15
movl $0xa, %edi
callq 0x6ac8
movq %rax, %r12
movl $0xa, %edi
callq 0x6ac8
testq %r12, %r12
je 0xcb46
movq %rax, %r13
testq %rax, %rax
jne 0xcb90
jmp 0xcb46
xorl %eax, %eax
movq -0x38(%rbp), %r12
testb %bl, %bl
jne 0xcaed
movq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x6d0a
movq %r15, %rdi
callq 0x6026
movq %r12, %rdi
callq 0x2030
xorl %eax, %eax
jmp 0xcaed