// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x8
popq %rdi
callq 0x10e0
movq %rax, 0x5dfc(%rip)      # 0x90b8
callq 0x2eb0
movq 0x5df0(%rip), %rcx      # 0x90b8
movq %rax, (%rcx)
xorl %edi, %edi
callq 0x2b4c
movq %rax, 0x5dd7(%rip)      # 0x90b0
movq %rbx, 0x10(%rsp)
cmpl $0x0, 0x18(%rbx)
je 0x3531
pushq $0x30
popq %rdi
callq 0x10e0
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movq 0x5daf(%rip), %rdi      # 0x90b0
callq 0x2b4c
movq %rax, %r15
movq %rax, 0x20(%r12)
movq %rbx, %rdi
callq 0x2d0a
testq %rax, %rax
je 0x3570
movl (%rax), %edi
callq 0x362c
movl %eax, 0x8(%r12)
movq %rbx, %rdi
pushq $0x3
popq %rsi
callq 0x3666
movq 0x8(%rax), %rax
movq %rax, (%r12)
movq %rbx, %rdi
pushq $0xe
popq %rsi
callq 0x3666
movq %rbx, %rdi
pushq $0xf
popq %rsi
callq 0x35a8
testb %al, %al
movq %r12, 0x8(%rsp)
jne 0x337b
movq %rbx, %rdi
pushq $0x7
popq %rsi
callq 0x35a8
testb %al, %al
je 0x345e
movq %rbx, %rdi
pushq $0xf
popq %rsi
callq 0x3666
movq 0x5d36(%rip), %rax      # 0x90b8
movq (%rax), %rdi
movq (%r12), %rsi
callq 0x2ed9
testq %rax, %rax
je 0x3440
movq %rax, %r15
movq (%r12), %rdi
movq (%rax), %rsi
callq 0x4990
testl %eax, %eax
jne 0x3547
movl 0x8(%r12), %eax
cmpl 0x8(%r15), %eax
jne 0x3547
movq 0x18(%r12), %rcx
movq 0x18(%r15), %rax
testq %rcx, %rcx
je 0x34dc
testq %rax, %rax
je 0x3547
movl 0x18(%rcx), %edx
cmpl 0x18(%rax), %edx
jne 0x3547
movq (%rcx), %rbp
movq (%rax), %rbx
pushq $-0x1
popq %r13
movq 0x18(%r12), %rax
incl %r13d
cmpl 0x18(%rax), %r13d
jge 0x34e1
movq 0x8(%rbx), %rbx
movq 0x8(%rbp), %rbp
movq (%rbp), %rsi
movq (%rbx), %r14
movq 0x20(%r12), %rax
movq (%rax), %rdi
callq 0x2ed9
movq %rax, %r12
movq 0x20(%r15), %rax
movq (%rax), %rdi
movq %r14, %rsi
callq 0x2ed9
movl (%r12), %ecx
movq 0x8(%rsp), %r12
cmpl (%rax), %ecx
je 0x33ee
jmp 0x3547
movq 0x5c71(%rip), %rax      # 0x90b8
movq (%rax), %rdi
movq (%r12), %rsi
movq %r12, %rdx
callq 0x2f53
movq %r12, %r15
jmp 0x34e1
callq 0x2c34
movq %rax, %r13
movq %rax, 0x18(%r12)
cmpl $0x0, 0x18(%rbx)
je 0x337b
movq %rbx, %rdi
callq 0x2d0a
movq %rax, %rbp
movq %rbx, %rdi
pushq $0x3
popq %rsi
callq 0x3666
movq %rax, %r12
movq 0x8(%rax), %r14
movl (%rbp), %edi
callq 0x362c
movq %r15, %rdi
movq %r14, %rsi
movl %eax, %edx
pushq $0x1
popq %rcx
callq 0x2bfd
movq 0x8(%r12), %rsi
movq 0x8(%rsp), %r12
movq %r13, %rdi
callq 0x2c7e
movq %rbx, %rdi
pushq $0xf
popq %rsi
callq 0x35a8
testb %al, %al
jne 0x337b
movq %rbx, %rdi
pushq $0x13
popq %rsi
callq 0x3666
jmp 0x346b
testq %rax, %rax
jne 0x3547
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
pushq $0x12
popq %rsi
callq 0x35a8
testb %al, %al
jne 0x32de
cmpq $0x0, 0x10(%r15)
jne 0x358c
movq 0x20(%r12), %rsi
movq %rbx, %rdi
callq 0x35d3
movq %rax, 0x10(%r12)
movq 0x5b9b(%rip), %rax      # 0x90b8
movq (%rax), %rdi
movq (%r12), %rsi
movq %r12, %rdx
callq 0x2f53
jmp 0x32de
movq 0x5b80(%rip), %rax      # 0x90b8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1be6(%rip), %rdi      # 0x5134
leaq 0x2327(%rip), %rsi      # 0x587c
leaq 0x2328(%rip), %rcx      # 0x5884
movl $0x3a3, %edx            # imm = 0x3A3
xorl %eax, %eax
callq 0x1080
pushq $-0x1
popq %rdi
callq 0x1110
leaq 0x1bbd(%rip), %rdi      # 0x5134
leaq 0x22fe(%rip), %rsi      # 0x587c
leaq 0x235c(%rip), %rcx      # 0x58e1
movl $0x33f, %edx            # imm = 0x33F
jmp 0x3561
leaq 0x1ba1(%rip), %rdi      # 0x5134
leaq 0x22e2(%rip), %rsi      # 0x587c
leaq 0x2319(%rip), %rcx      # 0x58ba
movl $0x3a9, %edx            # imm = 0x3A9
jmp 0x3561