// File: /workspace/github_c_makefile_star2_O3/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 %r15d, %r15d
xorl %edi, %edi
callq 0x61a5
movq %rax, %rbx
testq %rax, %rax
je 0x652c
movl %r15d, 0x20(%rbx)
movq 0x8(%r14), %rsi
leal -0x1(%rsi), %eax
movl %eax, 0x28(%rbx)
movl %r15d, 0x24(%rbx)
decq %rsi
movq %r14, %rdi
callq 0x6d0c
movq %rax, %rdi
callq 0x20b0
movl %eax, 0x2c(%rbx)
testq %r15, %r15
je 0x627d
movl 0x28(%r15), %esi
movl 0x2c(%r15), %edx
jmp 0x6281
xorl %esi, %esi
xorl %edx, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x653e
testq %rax, %rax
je 0x62a2
movq %rax, %r15
movq 0x8(%rbx), %rdi
movq %rax, %rsi
callq 0x8df0
jmp 0x626e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6755
movslq 0x20(%rbx), %r15
cmpl 0x28(%rbx), %r15d
jge 0x62ea
movq %r14, %rdi
movq %r15, %rsi
callq 0x6d0c
movl %r15d, %edi
movq %rax, %rsi
callq 0xac6b
testq %rax, %rax
je 0x62de
movq 0x10(%rbx), %rdi
movq %rax, %rsi
callq 0x6c26
incq %r15
movslq 0x28(%rbx), %rax
cmpq %rax, %r15
jl 0x62b7
movq %rbx, 0x10(%rsp)
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rax
movq %rax, (%rsp)
testq %rax, %rax
je 0x63d4
movq 0x10(%rsp), %rax
movq 0x10(%rax), %r15
cmpq $0x0, 0x8(%r15)
je 0x63cb
movq (%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movl 0x20(%rax), %ebp
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0x6d0c
cmpl %ebp, 0x28(%rax)
je 0x6348
incq %r12
cmpq 0x8(%r15), %r12
jb 0x632a
jmp 0x63cb
movq %rax, %r14
movq 0x20(%rax), %rdi
cmpq $0x0, 0x8(%rdi)
je 0x63cb
xorl %r15d, %r15d
movq %r15, %rsi
callq 0x6d0c
movq %rax, %rbp
movq (%rax), %rdi
callq 0x7dc1
movq %rax, %r12
movq 0x10(%rbp), %rdi
callq 0x7dc1
movq %rax, %r13
movb 0x18(%rbp), %bl
movl 0x20(%rbp), %ebp
movl $0x28, %edi
callq 0x2140
testq %rax, %rax
je 0x63a9
movq %r12, (%rax)
movq %r13, 0x10(%rax)
movb %bl, 0x18(%rax)
movb $0x0, 0x19(%rax)
movl %ebp, 0x20(%rax)
movq $0x0, 0x8(%rax)
movb $0x1, 0x24(%rax)
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rdi
movq %rax, %rsi
callq 0x6c26
incq %r15
movq 0x20(%r14), %rdi
cmpq 0x8(%rdi), %r15
jb 0x6359
movq (%rsp), %rax
jmp 0x62f3
movl $0x1, %edi
callq 0x6b58
movq %rax, %r14
movl $0x1, %edi
callq 0x6b58
movq %rax, %r12
leaq 0x8441(%rip), %r15      # 0xe836
movq %r15, %rdi
callq 0x7dc1
movq %rax, %r13
leaq 0x8436(%rip), %rbp      # 0xe83d
movq %rbp, %rdi
callq 0x7dc1
movq %r13, %rdi
xorl %esi, %esi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r14, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xac14
movq %rax, 0x8(%rsp)
movq %r15, %rdi
callq 0x7dc1
movq %rax, %r15
movq %rbp, %rdi
callq 0x7dc1
movq %r15, %rdi
movl $0x1, %esi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r12, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xac14
movq %rax, %r13
leaq 0x83eb(%rip), %rdi      # 0xe84f
callq 0x7dc1
movq %rax, %rbp
leaq 0x8313(%rip), %rdi      # 0xe786
callq 0x7dc1
movq %rax, %r15
movl $0x28, %edi
callq 0x2140
testq %rax, %rax
je 0x64aa
movq %rbp, (%rax)
movq %r15, 0x10(%rax)
movw $0x0, 0x18(%rax)
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movb $0x0, 0x24(%rax)
movq $0x0, 0x8(%rax)
movq %r12, %rdi
movq %rax, %rsi
callq 0x6c26
leaq 0x8393(%rip), %rdi      # 0xe84f
callq 0x7dc1
movq %rax, %r12
leaq 0x82bb(%rip), %rdi      # 0xe786
callq 0x7dc1
movq %rax, %r15
movl $0x28, %edi
callq 0x2140
testq %rax, %rax
je 0x6502
movq %r12, (%rax)
movq %r15, 0x10(%rax)
movw $0x0, 0x18(%rax)
movl $0xffffffff, 0x20(%rax) # imm = 0xFFFFFFFF
movb $0x0, 0x24(%rax)
movq $0x0, 0x8(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x6c26
movq 0x10(%rsp), %rbx
movq 0x10(%rbx), %rdi
movq %r13, %rsi
callq 0x6c26
movq 0x10(%rbx), %rdi
movq 0x8(%rsp), %rsi
callq 0x6c26
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq