// File: /workspace/github_c_makefile_star2_O1/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 0x60d9
movq %rax, %rbx
movl %r14d, %eax
movq %rax, 0x20(%rsp)
cmpq %rax, 0x8(%r15)
jbe 0x6661
movl %ebp, %eax
movq %rax, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x20(%rsp), %r14
movl $0x0, 0x4(%rsp)
movl $0x0, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r15, 0x28(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x6c76
movq %rax, %rdi
callq 0x7be4
movq %rax, %r12
movq %rax, %rdi
callq 0x20b0
movq %rax, %rbp
movq %r14, 0x10(%rsp)
cmpq 0x20(%rsp), %r14
movl $0x0, %r13d
cmoveq 0x30(%rsp), %r13
movl %ebp, %ebx
cmpq %rbx, %r13
jae 0x6637
leaq (%r12,%r13), %r14
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x8291
testb %al, %al
je 0x6549
incq %r13
cmpq %rbx, %r13
jne 0x6527
jmp 0x6637
movzbl (%r14), %eax
cmpl $0x7d, %eax
je 0x6580
cmpl $0x7b, %eax
jne 0x653c
movslq %r13d, %r14
movb -0x1(%r12,%r14), %r15b
movsbl %r15b, %edi
callq 0x7ed1
testq %r14, %r14
jle 0x6574
decq %r14
testb %al, %al
jne 0x655a
cmpb $0x3d, %r15b
jne 0x6590
incl 0x4(%rsp)
jmp 0x653c
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0x65c1
decl %eax
movl %eax, 0x4(%rsp)
jmp 0x653c
cmpb $0x0, 0x18(%rsp)
movl (%rsp), %edx
jne 0x65ab
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 0x65e1
movl %edx, (%rsp)
jmp 0x653c
movl (%rsp), %eax
decl %eax
cmpb $0x0, 0x18(%rsp)
movl %eax, (%rsp)
je 0x65d4
testl %eax, %eax
je 0x661c
movl $0x0, 0x4(%rsp)
jmp 0x653c
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %r13d, %edx
movq 0x8(%rsp), %r14
movq %r14, %rcx
callq 0x6484
movl $0x2, (%rsp)
testq %rax, %rax
je 0x653c
movq 0x8(%r14), %rdi
movq %rax, %rsi
callq 0x8d18
jmp 0x653c
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movl %ecx, 0x28(%rax)
incl %r13d
movl %r13d, 0x2c(%rax)
movl $0x0, (%rsp)
movq %r12, %rdi
callq 0x2030
movq 0x8(%rsp), %rbx
cmpl $-0x1, 0x28(%rbx)
movq 0x28(%rsp), %r15
movq 0x10(%rsp), %r14
jne 0x6661
incq %r14
cmpq 0x8(%r15), %r14
jb 0x64e5
cmpl $-0x1, 0x28(%rbx)
je 0x6674
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6690
jmp 0x667e
movq %rbx, %rdi
callq 0x672a
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq