// File: /workspace/github_c_makefile_star2_O3/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, 0x6aa6(%rip)      # 0xa0b8
callq 0x3158
movq 0x6a9a(%rip), %rcx      # 0xa0b8
movq %rax, (%rcx)
xorl %edi, %edi
callq 0x2dac
movq %rax, 0x6a81(%rip)      # 0xa0b0
cmpl $0x0, 0x18(%rbx)
je 0x38a3
movq %rbx, 0x10(%rsp)
movl $0x30, %edi
callq 0x10e0
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x6a57(%rip), %rdi      # 0xa0b0
callq 0x2dac
movq %rax, %r15
movq %rax, 0x20(%r14)
movq %rbx, %rdi
callq 0x2f9f
testq %rax, %rax
je 0x38e4
movl (%rax), %edi
callq 0x39a4
movl %eax, 0x8(%r14)
movq %rbx, %rdi
movl $0x3, %esi
callq 0x39e5
movq 0x8(%rax), %rax
movq %rax, (%r14)
movq %rbx, %rdi
movl $0xe, %esi
callq 0x39e5
movq %rbx, %rdi
movl $0xf, %esi
callq 0x391c
testb %al, %al
jne 0x36d5
movq %rbx, %rdi
movl $0x7, %esi
callq 0x391c
testb %al, %al
je 0x37c5
movq %rbx, %rdi
movl $0xf, %esi
callq 0x39e5
movq 0x69dc(%rip), %rax      # 0xa0b8
movq (%rax), %rdi
movq (%r14), %rsi
callq 0x3183
testq %rax, %rax
je 0x37a6
movq (%r14), %rdi
movq %rax, 0x8(%rsp)
movq (%rax), %rsi
callq 0x4ec2
testl %eax, %eax
jne 0x38b9
movl 0x8(%r14), %eax
movq 0x8(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x38b9
movq 0x18(%r14), %rax
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rcx
testq %rax, %rax
je 0x3849
testq %rcx, %rcx
je 0x38b9
movl 0x18(%rcx), %edx
cmpl %edx, 0x18(%rax)
jne 0x38b9
testl %edx, %edx
jle 0x384e
movq (%rcx), %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 0x3183
movq %rax, %r13
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq 0x3183
movl (%r13), %ecx
cmpl (%rax), %ecx
jne 0x38b9
incl %r15d
movq 0x18(%r14), %rax
cmpl 0x18(%rax), %r15d
jl 0x3756
jmp 0x384e
movq 0x690b(%rip), %rax      # 0xa0b8
movq (%rax), %rdi
movq (%r14), %rsi
movq %r14, %rdx
callq 0x31fd
movq %r14, 0x8(%rsp)
jmp 0x384e
callq 0x2ea0
movq %rax, 0x8(%rsp)
movq %rax, 0x18(%r14)
cmpl $0x0, 0x18(%rbx)
je 0x36d5
movq %rbx, %rdi
callq 0x2f9f
movq %rax, %r13
movq %rbx, %rdi
movl $0x3, %esi
callq 0x39e5
movq %rax, %rbp
movq 0x8(%rax), %r12
movl (%r13), %edi
callq 0x39a4
movq %r15, %rdi
movq %r12, %rsi
movl %eax, %edx
movl $0x1, %ecx
callq 0x2e66
movq 0x8(%rbp), %rsi
movq 0x8(%rsp), %rdi
callq 0x2f0b
movq %rbx, %rdi
movl $0xf, %esi
callq 0x391c
testb %al, %al
jne 0x36d5
movq %rbx, %rdi
movl $0x13, %esi
callq 0x39e5
jmp 0x37d3
testq %rcx, %rcx
jne 0x38b9
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl $0x12, %esi
callq 0x391c
testb %al, %al
jne 0x3899
movq 0x8(%rsp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x3900
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0x3947
movq %rax, 0x10(%r14)
movq 0x682d(%rip), %rax      # 0xa0b8
movq (%rax), %rdi
movq (%r14), %rsi
movq %r14, %rdx
callq 0x31fd
cmpl $0x0, 0x18(%rbx)
jne 0x363e
movq 0x680e(%rip), %rax      # 0xa0b8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2874(%rip), %rdi      # 0x6134
leaq 0x2f85(%rip), %rsi      # 0x684c
leaq 0x2f86(%rip), %rcx      # 0x6854
movl $0x3a3, %edx            # imm = 0x3A3
xorl %eax, %eax
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110
leaq 0x2849(%rip), %rdi      # 0x6134
leaq 0x2f5a(%rip), %rsi      # 0x684c
leaq 0x2fb8(%rip), %rcx      # 0x68b1
movl $0x33f, %edx            # imm = 0x33F
jmp 0x38d3
leaq 0x282d(%rip), %rdi      # 0x6134
leaq 0x2f3e(%rip), %rsi      # 0x684c
leaq 0x2f75(%rip), %rcx      # 0x688a
movl $0x3a9, %edx            # imm = 0x3A9
jmp 0x38d3