// File: /workspace/github_c_makefile_star2_O1/johnvtan[P]ccccc/parse.c
// Function: parse
program_t *parse(list_t *tokens) {
    program = malloc(sizeof(program_t));
    program->fn_defs = map_new();
    global_env = env_new(NULL);

    while (tokens->len) {
        fn_def_t *next_fn = parse_fn_declaration(tokens);
        fn_def_t *prev_decl = map_get(program->fn_defs, next_fn->name);

        if (!prev_decl) {
            // This is the first declaration, which we need to put in the map
            map_set(program->fn_defs, next_fn->name, next_fn);
            prev_decl = next_fn; 
        } else if (!fn_def_is_equal(next_fn, prev_decl)) {
            UNREACHABLE("Compilation error: Function declarations don't match\n");
        }

        if (!match(tokens, TOK_SEMICOLON)) {
            // This means that we have a body for the function declaration
            if (prev_decl->stmts != NULL) {
                UNREACHABLE("Compilation error: Function redefined\n");
            }

            // We have a definition with statements, so parse the statements and update
            // definition in the map.
            next_fn->stmts = parse_stmt_list(tokens, next_fn->env);
            map_set(program->fn_defs, next_fn->name, next_fn);
        }
    }
    return program;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0x8, %edi
callq 0x10e0
movq %rax, 0x6a2a(%rip)      # 0xa0b8
callq 0x31b4
movq 0x6a1e(%rip), %rcx      # 0xa0b8
movq %rax, (%rcx)
xorl %edi, %edi
callq 0x2e04
movq %rax, 0x6a05(%rip)      # 0xa0b0
cmpl $0x0, 0x18(%rbx)
je 0x3932
movq %rbx, 0x10(%rsp)
movl $0x30, %edi
callq 0x10e0
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x69db(%rip), %rdi      # 0xa0b0
callq 0x2e04
movq %rax, %r15
movq %rax, 0x20(%r14)
movq %rbx, %rdi
callq 0x2ffb
testq %rax, %rax
je 0x3973
movl (%rax), %edi
callq 0x3a33
movl %eax, 0x8(%r14)
movq %rbx, %rdi
movl $0x3, %esi
callq 0x3a74
movq 0x8(%rax), %rax
movq %rax, (%r14)
movq %rbx, %rdi
movl $0xe, %esi
callq 0x3a74
movq %rbx, %rdi
movl $0xf, %esi
callq 0x39ab
testb %al, %al
jne 0x3751
movq %rbx, %rdi
movl $0x7, %esi
callq 0x39ab
testb %al, %al
je 0x3854
movq %rbx, %rdi
movl $0xf, %esi
callq 0x3a74
movq 0x6960(%rip), %rax      # 0xa0b8
movq (%rax), %rdi
movq (%r14), %rsi
callq 0x31df
testq %rax, %rax
je 0x3835
movq (%r14), %rdi
movq %rax, 0x8(%rsp)
movq (%rax), %rsi
callq 0x4f6b
testl %eax, %eax
jne 0x3948
movl 0x8(%r14), %eax
movq 0x8(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x3948
movq 0x18(%r14), %rax
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rcx
testq %rax, %rax
je 0x38d8
testq %rcx, %rcx
je 0x3948
movl 0x18(%rax), %ecx
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
cmpl 0x18(%rdx), %ecx
jne 0x3948
testl %ecx, %ecx
setle %cl
jle 0x3828
movq (%rdx), %rbp
movq (%rax), %rbx
xorl %r15d, %r15d
movq 0x8(%rbx), %rbx
movq 0x8(%rbp), %rbp
movq (%rbx), %rsi
movq (%rbp), %r12
movq 0x20(%r14), %rax
movq (%rax), %rdi
callq 0x31df
movq %rax, %r13
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq 0x31df
movl (%r13), %ecx
cmpl (%rax), %ecx
jne 0x3948
incl %r15d
movq 0x18(%r14), %rax
cmpl 0x18(%rax), %r15d
setge %cl
jl 0x37da
testb %cl, %cl
jne 0x38dd
jmp 0x3948
movq 0x687c(%rip), %rax      # 0xa0b8
movq (%rax), %rdi
movq (%r14), %rsi
movq %r14, %rdx
callq 0x3259
movq %r14, 0x8(%rsp)
jmp 0x38dd
callq 0x2efc
movq %rax, 0x8(%rsp)
movq %rax, 0x18(%r14)
cmpl $0x0, 0x18(%rbx)
je 0x3751
movq %rbx, %rdi
callq 0x2ffb
movq %rax, %r13
movq %rbx, %rdi
movl $0x3, %esi
callq 0x3a74
movq %rax, %rbp
movq 0x8(%rax), %r12
movl (%r13), %edi
callq 0x3a33
movq %r15, %rdi
movq %r12, %rsi
movl %eax, %edx
movl $0x1, %ecx
callq 0x2ec2
movq 0x8(%rbp), %rsi
movq 0x8(%rsp), %rdi
callq 0x2f67
movq %rbx, %rdi
movl $0xf, %esi
callq 0x39ab
testb %al, %al
jne 0x3751
movq %rbx, %rdi
movl $0x13, %esi
callq 0x3a74
jmp 0x3862
testq %rcx, %rcx
jne 0x3948
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl $0x12, %esi
callq 0x39ab
testb %al, %al
jne 0x3928
movq 0x8(%rsp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x398f
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0x39d6
movq %rax, 0x10(%r14)
movq 0x679e(%rip), %rax      # 0xa0b8
movq (%rax), %rdi
movq (%r14), %rsi
movq %r14, %rdx
callq 0x3259
cmpl $0x0, 0x18(%rbx)
jne 0x36ba
movq 0x677f(%rip), %rax      # 0xa0b8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27e5(%rip), %rdi      # 0x6134
leaq 0x2f46(%rip), %rsi      # 0x689c
leaq 0x2f47(%rip), %rcx      # 0x68a4
movl $0x3a3, %edx            # imm = 0x3A3
xorl %eax, %eax
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
leaq 0x27ba(%rip), %rdi      # 0x6134
leaq 0x2f1b(%rip), %rsi      # 0x689c
leaq 0x2f79(%rip), %rcx      # 0x6901
movl $0x33f, %edx            # imm = 0x33F
jmp 0x3962
leaq 0x279e(%rip), %rdi      # 0x6134
leaq 0x2eff(%rip), %rsi      # 0x689c
leaq 0x2f36(%rip), %rcx      # 0x68da
movl $0x3a9, %edx            # imm = 0x3A9
jmp 0x3962