// File: /workspace/github_c_makefile_star2_O0/System-Glitch[P]ESGILinter-C/src/scopetree.c
// Function: parse_root_scope
scope_t *parse_root_scope(arraylist_t *file) {
	scope_t    *scope     = scope_init(NULL);
	scope_t    *child     = NULL;
	scope_t    *tmp_child = NULL;
	function_t *function  = NULL;
	node_t     *current   = NULL;
	field_t    *tmp_field = NULL;
	field_t    *field     = NULL;

	if(scope == NULL) return NULL;

	scope->from_line = 0;
	scope->to_line   = file->size - 1;
	scope->from_char = 0;
	scope->to_char   = strlen((char*) arraylist_get(file, file->size - 1));

	while((child = parse_scope(file, tmp_child != NULL ? tmp_child->to_line : 0, tmp_child != NULL ? tmp_child->to_char : 0, scope)) != NULL) {
		linkedlist_add(scope->children, child);
		tmp_child = child;
	}

	parse_scope_content(file, scope);
	parse_scope_functions(file, scope); //Parse functions only in root scope

	//Copy parameters to child variables
	current = scope->children->head;
	if(current != NULL) {
		do {
			tmp_child = current->val;
			function = find_function_from_line(scope->functions, tmp_child->from_line);
			if(function != NULL) {
				for(size_t i = 0 ; i < function->params->size ; i++) {
					tmp_field = arraylist_get(function->params, i);
					field = field_init(strduplicate(tmp_field->name), strduplicate(tmp_field->type.name), tmp_field->type.is_pointer, tmp_field->line);
					field->is_param = 1;
					arraylist_add(tmp_child->variables, field);
				}
			}
		} while ((current = current->next) != NULL);
	}

	register_sizeof(scope);
	//TODO parse inner scopes in switch

	return scope;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x7680
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x77fd
movq $0x0, -0x8(%rbp)
jmp 0x79f1
movq -0x18(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
subq $0x1, %rsi
callq 0x8890
movq %rax, %rdi
callq 0x2090
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x2c(%rax)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x7869
movq -0x28(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0x7870
xorl %eax, %eax
movl %eax, -0x5c(%rbp)
jmp 0x7870
movl -0x5c(%rbp), %eax
movl %eax, -0x60(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x7889
movq -0x28(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x64(%rbp)
jmp 0x7890
xorl %eax, %eax
movl %eax, -0x64(%rbp)
jmp 0x7890
movl -0x60(%rbp), %esi
movq -0x58(%rbp), %rdi
movl -0x64(%rbp), %edx
movq -0x18(%rbp), %rcx
callq 0x7a00
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x78c8
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0xc1a0
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x784e
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7c40
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x7ce0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x79e0
jmp 0x78ff
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movl 0x20(%rax), %esi
callq 0x7d60
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x79c6
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x20(%rcx), %rcx
cmpq 0x8(%rcx), %rax
jae 0x79c4
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0x8890
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rdi
callq 0xa550
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0xa550
movq -0x70(%rbp), %rdi
movq %rax, %rsi
movq -0x40(%rbp), %rax
movb 0x18(%rax), %al
movq -0x40(%rbp), %rcx
movl 0x20(%rcx), %ecx
movsbl %al, %edx
callq 0x7600
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movb $0x1, 0x24(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x8730
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x7935
jmp 0x79c6
jmp 0x79c8
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x78ff
jmp 0x79e0
movq -0x18(%rbp), %rdi
callq 0x7de0
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)