// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %r15
movq %rcx, %rdi
callq 0x61a5
movq %rax, %rbx
movl %r14d, %r14d
cmpq %r14, 0x8(%r15)
jbe 0x6726
movl %ebp, %eax
movq %rax, 0x28(%rsp)
movq $0x0, 0x18(%rsp)
movq %r14, %r13
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x30(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x6d0c
movq %rax, %rdi
callq 0x7c54
movq %rax, %r12
movq %rax, %rdi
callq 0x20b0
movq %rax, %rbp
movq %r13, 0x10(%rsp)
cmpq %r14, %r13
movl $0x0, %r13d
cmoveq 0x28(%rsp), %r13
movl %ebp, %ebx
cmpq %rbx, %r13
jae 0x66f5
leaq (%r12,%r13), %r14
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x82dc
testb %al, %al
je 0x65ff
incq %r13
cmpq %rbx, %r13
jne 0x65dd
jmp 0x66f5
movzbl (%r14), %eax
cmpl $0x7d, %eax
je 0x6636
cmpl $0x7b, %eax
jne 0x65f2
movslq %r13d, %r14
movb -0x1(%r12,%r14), %r15b
movsbl %r15b, %edi
callq 0x7f3a
testq %r14, %r14
jle 0x662a
decq %r14
testb %al, %al
jne 0x6610
cmpb $0x3d, %r15b
jne 0x6646
incl 0x4(%rsp)
jmp 0x65f2
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0x6677
decl %eax
movl %eax, 0x4(%rsp)
jmp 0x65f2
cmpb $0x0, 0x18(%rsp)
movl (%rsp), %edx
jne 0x6661
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movl %ecx, 0x20(%rax)
movl %r13d, 0x24(%rax)
incl %edx
movb $0x1, %al
movq %rax, 0x18(%rsp)
cmpl $0x2, %edx
je 0x6697
movl %edx, (%rsp)
jmp 0x65f2
movl (%rsp), %eax
decl %eax
cmpb $0x0, 0x18(%rsp)
movl %eax, (%rsp)
je 0x668a
testl %eax, %eax
je 0x66d2
movl $0x0, 0x4(%rsp)
jmp 0x65f2
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %r13d, %edx
movq 0x8(%rsp), %r14
movq %r14, %rcx
callq 0x653e
movl $0x2, (%rsp)
testq %rax, %rax
je 0x65f2
movq 0x8(%r14), %rdi
movq %rax, %rsi
callq 0x8df0
jmp 0x65f2
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movl %ecx, 0x28(%rax)
incl %r13d
movl %r13d, 0x2c(%rax)
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
movq %r12, %rdi
callq 0x2030
movq 0x8(%rsp), %rbx
cmpl $-0x1, 0x28(%rbx)
movq 0x20(%rsp), %r15
movq 0x30(%rsp), %r14
movq 0x10(%rsp), %r13
jne 0x672c
incq %r13
cmpq 0x8(%r15), %r13
jb 0x659d
jmp 0x6739
cmpl $-0x1, 0x28(%rbx)
je 0x6739
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6755
jmp 0x6743
movq %rbx, %rdi
callq 0x67e1
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq