// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
xorl %edi, %edi
callq 0x5f28
movq %rax, %rbx
testq %rax, %rax
je 0x621d
andl $0x0, 0x20(%rbx)
movq 0x8(%r14), %rsi
leal -0x1(%rsi), %eax
movl %eax, 0x28(%rbx)
andl $0x0, 0x24(%rbx)
decq %rsi
movq %r14, %rdi
callq 0x6978
movq %rax, %rdi
callq 0x20b0
movl %eax, 0x2c(%rbx)
xorl %r15d, %r15d
movl $0x0, %esi
movl $0x0, %edx
testq %r15, %r15
je 0x6000
movl 0x28(%r15), %esi
movl 0x2c(%r15), %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x622f
testq %rax, %rax
je 0x6021
movq %rax, %r15
movq 0x8(%rbx), %rdi
movq %rax, %rsi
callq 0x8828
jmp 0x5fe9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6445
movslq 0x20(%rbx), %r15
movslq 0x28(%rbx), %rax
cmpq %rax, %r15
jge 0x6065
movq %r14, %rdi
movq %r15, %rsi
callq 0x6978
movl %r15d, %edi
movq %rax, %rsi
callq 0xa4ba
testq %rax, %rax
je 0x6060
movq 0x10(%rbx), %rdi
movq %rax, %rsi
callq 0x68b5
incq %r15
jmp 0x6030
movq %rbx, (%rsp)
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rbp
testq %rbp, %rbp
je 0x610b
movq (%rbp), %rbx
movq (%rsp), %rax
movq 0x10(%rax), %r15
movl 0x20(%rbx), %r13d
xorl %r12d, %r12d
cmpq 0x8(%r15), %r12
jae 0x6102
movq %r15, %rdi
movq %r12, %rsi
callq 0x6978
incq %r12
cmpl %r13d, 0x28(%rax)
jne 0x608d
movq %rax, %r14
xorl %r15d, %r15d
movq 0x20(%r14), %rdi
cmpq 0x8(%rdi), %r15
jae 0x6102
movq %r15, %rsi
callq 0x6978
movq %rax, %r12
movq (%rax), %rdi
callq 0x794d
movq %rax, %r13
movq 0x10(%r12), %rdi
callq 0x794d
movsbl 0x18(%r12), %edx
movl 0x20(%r12), %ecx
movq %r13, %rdi
movq %rax, %rsi
callq 0x5ee4
movb $0x1, 0x24(%rax)
movq 0x18(%rbx), %rdi
movq %rax, %rsi
callq 0x68b5
incq %r15
jmp 0x60ad
movq 0x8(%rbp), %rbp
jmp 0x6071
pushq $0x1
popq %r13
movl %r13d, %edi
callq 0x67e4
movq %rax, %rbx
movq %rax, 0x8(%rsp)
movl %r13d, %edi
callq 0x67e4
movq %rax, %r12
leaq 0x8705(%rip), %r15      # 0xe836
movq %r15, %rdi
callq 0x794d
movq %rax, %r14
leaq 0x86fa(%rip), %rbp      # 0xe83d
movq %rbp, %rdi
callq 0x794d
pushq $-0x1
popq %r9
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rdx
xorl %ecx, %ecx
movq %rbx, %r8
movl %r9d, %ebx
callq 0xa468
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x794d
movq %rax, %r14
movq %rbp, %rdi
callq 0x794d
movq %r14, %rdi
movl %r13d, %esi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r12, %r8
movl %ebx, %r9d
callq 0xa468
movq %rax, %r13
leaq 0x86b3(%rip), %r14      # 0xe84f
movq %r14, %rdi
callq 0x794d
movq %rax, %r15
leaq 0x85d8(%rip), %rbp      # 0xe786
movq %rbp, %rdi
callq 0x794d
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
movl %ebx, %ecx
callq 0x5ee4
movq %r12, %rdi
movq %rax, %rsi
callq 0x68b5
movq %r14, %rdi
callq 0x794d
movq %rax, %r14
movq %rbp, %rdi
callq 0x794d
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
movl %ebx, %ecx
callq 0x5ee4
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x68b5
movq (%rsp), %rbx
movq 0x10(%rbx), %rdi
movq %r13, %rsi
callq 0x68b5
movq 0x10(%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0x68b5
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq