// File: /workspace/github_c_makefile_star2_O2/System-Glitch[P]ESGILinter-C/src/scopetree.c
// Function: parse_scope
scope_t *parse_scope(arraylist_t *file, unsigned int start_line, unsigned int from_char, scope_t *parent_scope) {
	scope_t *scope              = scope_init(parent_scope);
	scope_t *child              = NULL;
	unsigned int level          = 0;
	unsigned int no_scope_level = 0;
	char found                  = 0;
	unsigned int length;
	char *line;

	for(size_t i = start_line ; i < file->size ; i++) { //Already considers end of line as bracket
		line   = str_remove_comments(arraylist_get(file, i));
		length = strlen(line);
		for(size_t j = i == start_line ? from_char : 0 ; j < length ; j++) {

			if(!check_quotes(line, line + j, length)) {

				if(line[j] == '{') {

					if(previous_char_is_equal(line, j)) {
						no_scope_level++;
					} else {
						level++;

						if(!found) {
							scope->from_line = i;
							scope->from_char = j;
						}

						found = 1;

						if(level == 2) {
							child = parse_scope(file, i, j, scope);
							if(child != NULL)
								linkedlist_add(scope->children, child);
						}
					}
				} else if(line[j] == '}') {
					if(no_scope_level != 0) {
						no_scope_level--;
					} else {
						level--;

						if(found && level == 0) {
							scope->to_line = i;
							scope->to_char = j + 1;
							break;
						}
					}
				}
			}
		}

		free(line);
		if(scope->to_line != -1) break;
	}

	if(scope->to_line == -1) {
		scope_free(scope);
		scope = NULL;
	} else {
		parse_scope_content(file, scope);
	}

	return scope;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %r15
movq %rcx, %rdi
callq 0x5f28
movq %rax, %rbx
movl %r14d, %r14d
movl %ebp, %ecx
movq %rcx, 0x38(%rsp)
movq $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movq $0x0, 0x28(%rsp)
movq %r14, 0x40(%rsp)
movq %rax, 0x18(%rsp)
movq %r15, 0x30(%rsp)
cmpq 0x8(%r15), %r14
jae 0x6416
movq %r15, %rdi
movq %r14, %rsi
callq 0x6978
movq %rax, %rdi
callq 0x77dc
movq %rax, %r12
movq %rax, %rdi
callq 0x20b0
movq %rax, %rbp
movq %r14, 0x20(%rsp)
cmpq 0x40(%rsp), %r14
movl $0x0, %r13d
cmoveq 0x38(%rsp), %r13
movl %ebp, %r15d
movl 0xc(%rsp), %ebx
cmpq %r15, %r13
jae 0x63ec
leaq (%r12,%r13), %r14
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x7e2e
testb %al, %al
je 0x62ee
incq %r13
jmp 0x62cb
movzbl (%r14), %eax
cmpl $0x7d, %eax
je 0x6329
cmpl $0x7b, %eax
jne 0x62e9
movl %ebx, 0xc(%rsp)
movslq %r13d, %r14
movb -0x1(%r12,%r14), %bl
movsbl %bl, %edi
callq 0x7ace
testq %r14, %r14
jle 0x631c
decq %r14
testb %al, %al
jne 0x6303
cmpb $0x3d, %bl
jne 0x6331
movl 0xc(%rsp), %ebx
incl %ebx
jmp 0x62e9
testl %ebx, %ebx
je 0x63a1
decl %ebx
jmp 0x62e9
cmpb $0x0, 0x28(%rsp)
movl 0xc(%rsp), %ebx
jne 0x634d
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movl %ecx, 0x20(%rax)
movl %r13d, 0x24(%rax)
movq 0x10(%rsp), %rcx
incl %ecx
movb $0x1, %al
movq %rax, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
cmpl $0x2, %ecx
jne 0x62e9
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %r13d, %edx
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
callq 0x622f
testq %rax, %rax
je 0x6390
movq 0x8(%rbx), %rdi
movq %rax, %rsi
callq 0x8828
pushq $0x2
popq %rax
movq %rax, 0x10(%rsp)
movl 0xc(%rsp), %ebx
jmp 0x62e9
cmpb $0x0, 0x28(%rsp)
setne %al
movq 0x10(%rsp), %rcx
decl %ecx
movq %rcx, 0x10(%rsp)
sete %cl
andb %al, %cl
xorl %ebx, %ebx
cmpb $0x1, %cl
jne 0x62e9
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %rax
movl %eax, 0x28(%rbx)
incl %r13d
movl %r13d, 0x2c(%rbx)
movl $0x0, 0xc(%rsp)
movq $0x0, 0x10(%rsp)
jmp 0x63f5
movl %ebx, 0xc(%rsp)
movq 0x18(%rsp), %rbx
movq %r12, %rdi
callq 0x2030
movq 0x20(%rsp), %r14
incq %r14
cmpl $-0x1, 0x28(%rbx)
movq 0x30(%rsp), %r15
je 0x6283
jmp 0x641c
cmpl $-0x1, 0x28(%rbx)
je 0x6439
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6445
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x64b5
xorl %ebx, %ebx
jmp 0x6427