// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0xa, %edi
callq 0x8580
movq %rax, -0x28(%rbp)
movl $0xa, %edi
callq 0x8580
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x12018
cmpq $0x0, -0x30(%rbp)
jne 0x12056
callq 0x2040
movl (%rax), %edi
callq 0x2180
movq %rax, %rcx
leaq 0xfd9(%rip), %rdi       # 0x13007
leaq 0xfe4(%rip), %rsi       # 0x13019
leaq 0xfe3(%rip), %rdx       # 0x1301f
leaq 0xfe2(%rip), %r8        # 0x13025
movb $0x0, %al
callq 0x20b0
movl $0x1, -0x4(%rbp)
jmp 0x123f6
movl $0xff, %edi
callq 0x2110
movq %rax, -0x18(%rbp)
cmpl $0x2, -0x8(%rbp)
jl 0x1207d
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x2060
jmp 0x1208d
movq -0x18(%rbp), %rdi
leaq 0x23f4(%rip), %rsi      # 0x1447c
callq 0x2060
movq -0x28(%rbp), %rsi
leaq 0x2cc9(%rip), %rdi      # 0x14d61
callq 0x6990
cmpb $0x0, %al
je 0x120ea
callq 0x2040
movl (%rax), %edi
callq 0x2180
movq %rax, %rcx
leaq 0x2cb6(%rip), %rdi      # 0x14d6d
leaq 0xf5b(%rip), %rsi       # 0x13019
leaq 0xf5a(%rip), %rdx       # 0x1301f
leaq 0xf59(%rip), %r8        # 0x13025
movb $0x0, %al
callq 0x20b0
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0x8a00
movl $0x1, -0x4(%rbp)
jmp 0x123f6
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x2a30
movl $0x0, -0x5c(%rbp)
movslq -0x5c(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x123cf
movl $0xa, %edi
callq 0x8580
movq %rax, -0x38(%rbp)
movl $0xa, %edi
callq 0x8580
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x1213e
cmpq $0x0, -0x40(%rbp)
jne 0x1217c
callq 0x2040
movl (%rax), %edi
callq 0x2180
movq %rax, %rcx
leaq 0xeb3(%rip), %rdi       # 0x13007
leaq 0xebe(%rip), %rsi       # 0x13019
leaq 0xebd(%rip), %rdx       # 0x1301f
leaq 0xebc(%rip), %r8        # 0x13025
movb $0x0, %al
callq 0x20b0
movl $0x1, -0x4(%rbp)
jmp 0x123f6
movq -0x30(%rbp), %rdi
movslq -0x5c(%rbp), %rsi
callq 0x8890
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rsi
leaq 0x2bfe(%rip), %rdi      # 0x14d96
movb $0x0, %al
callq 0x20b0
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x2320
movq -0x38(%rbp), %rdi
callq 0x12400
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x77a0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0x8a00
cmpq $0x0, -0x50(%rbp)
je 0x12383
movq -0x28(%rbp), %rdi
leaq 0x2bbb(%rip), %rsi      # 0x14da9
callq 0x75b0
cmpb $0x0, %al
je 0x1221f
movq -0x28(%rbp), %rdi
leaq 0x2ba7(%rip), %rsi      # 0x14da9
callq 0x73b0
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0xc(%rax), %esi
movq -0x20(%rbp), %rdx
callq 0xcaa0
movq -0x28(%rbp), %rdi
leaq 0x2b95(%rip), %rsi      # 0x14dbf
callq 0x75b0
cmpb $0x0, %al
je 0x1225b
movq -0x28(%rbp), %rdi
leaq 0x2b81(%rip), %rsi      # 0x14dbf
callq 0x73b0
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0xc(%rax), %esi
movq -0x20(%rbp), %rdx
callq 0xd8b0
movq -0x28(%rbp), %rdi
leaq 0x2b6a(%rip), %rsi      # 0x14dd0
callq 0x75b0
cmpb $0x0, %al
je 0x1227c
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xecc0
movq -0x28(%rbp), %rdi
leaq 0x2b59(%rip), %rsi      # 0x14de0
callq 0x75b0
cmpb $0x0, %al
je 0x122b8
movq -0x28(%rbp), %rdi
leaq 0x2b45(%rip), %rsi      # 0x14de0
callq 0x73b0
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0xc(%rax), %esi
movq -0x20(%rbp), %rdx
callq 0xce30
movq -0x28(%rbp), %rdi
leaq 0x2b24(%rip), %rsi      # 0x14de7
callq 0x75b0
cmpb $0x0, %al
je 0x122d9
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xc5f0
movq -0x28(%rbp), %rdi
leaq 0x2b15(%rip), %rsi      # 0x14df9
callq 0x75b0
cmpb $0x0, %al
je 0x122fa
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xc740
movq -0x28(%rbp), %rdi
leaq 0x2b06(%rip), %rsi      # 0x14e0b
callq 0x75b0
cmpb $0x0, %al
je 0x1231b
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xd420
movq -0x28(%rbp), %rdi
leaq 0x2af3(%rip), %rsi      # 0x14e19
callq 0x75b0
cmpb $0x0, %al
je 0x12340
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0xd5b0
movq -0x28(%rbp), %rdi
leaq 0x2adb(%rip), %rsi      # 0x14e26
callq 0x75b0
cmpb $0x0, %al
je 0x12365
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0xcb80
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %r8
movq -0x40(%rbp), %r9
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0xda60
jmp 0x123aa
movq -0x20(%rbp), %r8
leaq 0x2aad(%rip), %rdi      # 0x14e3b
leaq 0xc84(%rip), %rsi       # 0x13019
leaq 0xc83(%rip), %rdx       # 0x1301f
leaq 0xc82(%rip), %rcx       # 0x13025
movb $0x0, %al
callq 0x20b0
movq -0x40(%rbp), %rdi
movl $0x1, %esi
callq 0x8a00
movq -0x38(%rbp), %rdi
callq 0x2d00
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x12102
movq -0x30(%rbp), %rdi
movl $0x1, %esi
callq 0x8a00
movq -0x28(%rbp), %rdi
callq 0x7530
movq -0x18(%rbp), %rdi
callq 0x2030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop