// File: /workspace/github_c_makefile_star2_O1/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 0x60d9
movl $0x0, %ebx
testq %rax, %rax
je 0x6472
movq %rax, %rbx
movl %r15d, 0x20(%rax)
movq 0x8(%r14), %rsi
leal -0x1(%rsi), %eax
movl %eax, 0x28(%rbx)
movl %r15d, 0x24(%rbx)
decq %rsi
movq %r14, %rdi
callq 0x6c76
movq %rax, %rdi
callq 0x20b0
movl %eax, 0x2c(%rbx)
xorl %edx, %edx
movl $0x0, %esi
testq %r15, %r15
je 0x61b7
movl 0x28(%r15), %esi
testq %r15, %r15
je 0x61c0
movl 0x2c(%r15), %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x6484
testq %rax, %rax
je 0x61e1
movq %rax, %r15
movq 0x8(%rbx), %rdi
movq %rax, %rsi
callq 0x8d18
jmp 0x61a7
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6690
movslq 0x20(%rbx), %r15
cmpl 0x28(%rbx), %r15d
jge 0x6229
movq %r14, %rdi
movq %r15, %rsi
callq 0x6c76
movl %r15d, %edi
movq %rax, %rsi
callq 0xab6f
testq %rax, %rax
je 0x621d
movq 0x10(%rbx), %rdi
movq %rax, %rsi
callq 0x6b96
incq %r15
movslq 0x28(%rbx), %rax
cmpq %rax, %r15
jl 0x61f6
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x6321
movq %rbx, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq (%rax), %r13
movq 0x10(%rbx), %r15
cmpq $0x0, 0x8(%r15)
je 0x6272
movl 0x20(%r13), %ebp
xorl %r12d, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0x6c76
cmpl %ebp, 0x28(%rax)
je 0x6277
incq %r12
cmpq 0x8(%r15), %r12
jb 0x6259
xorl %r14d, %r14d
jmp 0x627a
movq %rax, %r14
testq %r14, %r14
je 0x630a
movq 0x20(%r14), %rdi
cmpq $0x0, 0x8(%rdi)
je 0x630a
xorl %r15d, %r15d
movq %r13, (%rsp)
movq %r15, %rsi
callq 0x6c76
movq %rax, %rbp
movq (%rax), %rdi
callq 0x7d58
movq %rax, %r12
movq 0x10(%rbp), %rdi
callq 0x7d58
movq %rax, %r13
movb 0x18(%rbp), %bl
movl 0x20(%rbp), %ebp
movl $0x28, %edi
callq 0x2140
testq %rax, %rax
je 0x62e9
movq %r12, (%rax)
movq %r13, 0x10(%rax)
movb %bl, 0x18(%rax)
xorl %ecx, %ecx
movb %cl, 0x19(%rax)
movl %ebp, 0x20(%rax)
movb %cl, 0x24(%rax)
movq $0x0, 0x8(%rax)
movb $0x1, 0x24(%rax)
movq (%rsp), %r13
movq 0x18(%r13), %rdi
movq %rax, %rsi
callq 0x6b96
incq %r15
movq 0x20(%r14), %rdi
cmpq 0x8(%rdi), %r15
jb 0x6295
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
movq 0x8(%rsp), %rbx
jne 0x623f
movl $0x1, %edi
callq 0x6ac8
movq %rax, %r14
movl $0x1, %edi
callq 0x6ac8
movq %rax, %r12
leaq 0x84f7(%rip), %r15      # 0xe839
movq %r15, %rdi
callq 0x7d58
movq %rax, %r13
leaq 0x84ec(%rip), %rbp      # 0xe840
movq %rbp, %rdi
callq 0x7d58
movq %r13, %rdi
xorl %esi, %esi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r14, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xab18
movq %rax, (%rsp)
movq %r15, %rdi
callq 0x7d58
movq %rax, %r15
movq %rbp, %rdi
callq 0x7d58
movq %r15, %rdi
movl $0x1, %esi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r12, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xab18
movq %rax, %r13
leaq 0x84a2(%rip), %rdi      # 0xe852
callq 0x7d58
movq %rax, %rbp
leaq 0x83ca(%rip), %rdi      # 0xe789
callq 0x7d58
movq %rax, %r15
movl $0x28, %edi
callq 0x2140
testq %rax, %rax
je 0x63f6
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 0x6b96
leaq 0x844a(%rip), %rdi      # 0xe852
callq 0x7d58
movq %rax, %r12
leaq 0x8372(%rip), %rdi      # 0xe789
callq 0x7d58
movq %rax, %r15
movl $0x28, %edi
callq 0x2140
testq %rax, %rax
je 0x644e
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 0x6b96
movq 0x10(%rbx), %rdi
movq %r13, %rsi
callq 0x6b96
movq 0x10(%rbx), %rdi
movq (%rsp), %rsi
callq 0x6b96
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq