// File: /workspace/github_c_makefile_star2_O3/sharmaeklavya2[P]cerplag/compiler.c
// Function: compile_program
void compile_program(ProgramNode* root, pSD psd, bool code_gen, bool destroy_module_bodies) {
    if(code_gen) {
        allow_codegen = true;
    }

    vptr_int_hmap_init(&module_status, 10, false);
    vptr_pMN_hmap_init(&module_node_map, 10, false);

    IDListNode* decl_node = NULL;
    ModuleNode* module_node = NULL;

    decl_node = root->decls;
    while(decl_node != NULL) {
        int old_size = module_status.size;
        vptr_int_hmap_insert(&module_status, decl_node->varname, MODULE_DECLARED);
        if(module_status.size == old_size) {
            sprintf(msg, "Module '%s' has already been declared.", decl_node->varname);
            print_error("semantic", ERROR, -1, decl_node->base.line, decl_node->base.col, NULL, "MODULE_REDECL", msg);
        }
        decl_node = decl_node->next;
    }

    module_node = root->modules;
    while(module_node != NULL) {
        if(module_node->name != NULL) {
            vptr_int_hmap_insert(&module_status, module_node->name, 0);
            int old_size = module_node_map.size;
            ModuleNode* n2 = vptr_pMN_hmap_insert(&module_node_map, module_node->name, module_node)->value;
            if(module_node_map.size == old_size) {
                sprintf(msg, "Module '%s' has already been defined on line %d col %d.",
                    module_node->name, n2->base.line, n2->base.col);
                print_error("semantic", ERROR, -1, module_node->base.line, module_node->base.col, NULL, "MODULE_REDEF", msg);
            }
        }
        module_node = module_node->next;
    }

    module_node = root->modules;
    while(module_node != NULL) {
        SD_add_scope(psd, (pAstNode)module_node);
        compile_node((pAstNode)module_node, psd, module_node->name);
        SD_remove_scope(psd);
        if(destroy_module_bodies) {
            destroy_ast(module_node->body);
        }
        optimize_ircode(&(module_node->base.ircode));
        module_node->body = NULL;
        module_node = module_node->next;
    }

    //vptr_pMN_hmap_print(&module_node_map, stderr);
    //vptr_int_hmap_print(&module_status, stderr);

    decl_node = root->decls;
    while(decl_node != NULL) {
        int status = vptr_int_hmap_get(&module_status, decl_node->varname);
        if((status & MODULE_DECLARED) && (~status & MODULE_DEFINED)) {
            // module is declared but not defined
            sprintf(msg, "Module '%s' has been declared but not defined.", decl_node->varname);
            print_error("semantic", ERROR, -1, decl_node->base.line, decl_node->base.col, NULL, "MODULE_NOTDECL", msg);
        }
        decl_node = decl_node->next;
    }

    module_node = root->modules;
    while(module_node != NULL) {
        if(module_node->name != NULL) {
            int status = vptr_int_hmap_get(&module_status, module_node->name);
            if(~status & MODULE_USED) {
                sprintf(msg, "Module '%s' has not been used.", module_node->name);
                print_error("semantic", WARNING, 4, module_node->base.line, module_node->base.col, NULL, "MODULE_NOTUSED", msg);
            }
        }
        module_node = module_node->next;
    }

    vptr_int_hmap_clear(&module_status);
    vptr_pMN_hmap_clear(&module_node_map);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, -0x34(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
testl %edx, %edx
je 0xb944
leaq 0xa3df(%rip), %rax      # 0x15d20
movb $0x1, (%rax)
leaq 0xf54d(%rip), %rdi      # 0x1ae98
movl $0xa, %esi
xorl %edx, %edx
callq 0x4309
movl $0xa, %edi
movl $0x8, %esi
callq 0x3120
movq %rax, 0xf543(%rip)      # 0x1aeb0
movq $0xa, 0xf540(%rip) # 0x1aeb8
movw $0x100, 0xf53f(%rip)    # imm = 0x100
movq %rbx, -0x30(%rbp)
movq 0x38(%rbx), %r12
testq %r12, %r12
je 0xba1b
movl 0xf50b(%rip), %r13d     # 0x1aea4
leaq 0xf4f8(%rip), %rbx      # 0x1ae98
leaq 0xf429(%rip), %r14      # 0x1add0
movq 0x40(%r12), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x4433
movl 0xf4e5(%rip), %eax      # 0x1aea4
cmpl %r13d, %eax
jne 0xba0e
movq 0x40(%r12), %rdx
movq %r14, %rdi
leaq 0x3fad(%rip), %rsi      # 0xf980
xorl %eax, %eax
callq 0x31b0
movl 0xc(%r12), %ecx
movl 0x10(%r12), %r8d
leaq 0x37e7(%rip), %rdi      # 0xf1d2
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
pushq %r14
leaq 0x3fa9(%rip), %rax      # 0xf9a7
pushq %rax
callq 0x93bc
addq $0x10, %rsp
movl 0xf496(%rip), %eax      # 0x1aea4
movq 0x38(%r12), %r12
movl %eax, %r13d
testq %r12, %r12
jne 0xb9a7
movq -0x30(%rbp), %r14
movq 0x40(%r14), %r12
testq %r12, %r12
je 0xbb24
leaq 0xf47d(%rip), %rbx      # 0x1aeb0
leaq 0xf396(%rip), %r14      # 0x1add0
movq 0x50(%r12), %rsi
testq %rsi, %rsi
je 0xbac1
leaq 0xf44d(%rip), %rdi      # 0x1ae98
xorl %edx, %edx
callq 0x4433
movl 0xf463(%rip), %r13d     # 0x1aebc
movq 0x50(%r12), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0xa2be
cmpl %r13d, 0xf44c(%rip)     # 0x1aebc
jne 0xbac1
movq 0x10(%rax), %rax
movq 0x50(%r12), %rdx
movl 0xc(%rax), %ecx
movl 0x10(%rax), %r8d
movq %r14, %rdi
leaq 0x3f29(%rip), %rsi      # 0xf9b5
xorl %eax, %eax
callq 0x31b0
movl 0xc(%r12), %ecx
movl 0x10(%r12), %r8d
leaq 0x372e(%rip), %rdi      # 0xf1d2
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
pushq %r14
leaq 0x3f36(%rip), %rax      # 0xf9ed
pushq %rax
callq 0x93bc
addq $0x10, %rsp
movq 0x48(%r12), %r12
testq %r12, %r12
jne 0xba3a
movq -0x30(%rbp), %r14
movq 0x40(%r14), %rbx
testq %rbx, %rbx
je 0xbb24
movq %r15, %rdi
movq %rbx, %rsi
callq 0xa030
movq 0x50(%rbx), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa701
movq %r15, %rdi
callq 0xa0a9
cmpb $0x0, -0x34(%rbp)
je 0xbb0d
movq 0x58(%rbx), %rdi
callq 0x85c7
leaq 0x20(%rbx), %rdi
callq 0xdc82
movq $0x0, 0x58(%rbx)
movq 0x48(%rbx), %rbx
jmp 0xbad7
movq 0x38(%r14), %r13
testq %r13, %r13
je 0xbb9d
leaq 0xf364(%rip), %rbx      # 0x1ae98
leaq 0xf295(%rip), %r14      # 0x1add0
leaq 0x3ee7(%rip), %r15      # 0xfa29
leaq 0x3689(%rip), %r12      # 0xf1d2
movq 0x40(%r13), %rsi
movq %rbx, %rdi
callq 0x440e
andl $0x3, %eax
cmpl $0x1, %eax
jne 0xbb94
movq 0x40(%r13), %rdx
movq %r14, %rdi
leaq 0x3e8f(%rip), %rsi      # 0xf9fa
xorl %eax, %eax
callq 0x31b0
movl 0xc(%r13), %ecx
movl 0x10(%r13), %r8d
movq %r12, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
pushq %r14
pushq %r15
callq 0x93bc
addq $0x10, %rsp
movq 0x38(%r13), %r13
testq %r13, %r13
jne 0xbb49
movq -0x30(%rbp), %rax
movq 0x40(%rax), %r13
testq %r13, %r13
je 0xbc1e
leaq 0xf2e7(%rip), %rbx      # 0x1ae98
leaq 0xf218(%rip), %r14      # 0x1add0
leaq 0x3e98(%rip), %r15      # 0xfa57
leaq 0x360c(%rip), %r12      # 0xf1d2
movq 0x50(%r13), %rsi
testq %rsi, %rsi
je 0xbc15
movq %rbx, %rdi
callq 0x440e
testb $0x4, %al
jne 0xbc15
movq 0x50(%r13), %rdx
movq %r14, %rdi
leaq 0x3e4f(%rip), %rsi      # 0xfa38
xorl %eax, %eax
callq 0x31b0
movl 0xc(%r13), %ecx
movl 0x10(%r13), %r8d
movq %r12, %rdi
movl $0x1, %esi
movl $0x4, %edx
xorl %r9d, %r9d
pushq %r14
pushq %r15
callq 0x93bc
addq $0x10, %rsp
movq 0x48(%r13), %r13
testq %r13, %r13
jne 0xbbc6
leaq 0xf273(%rip), %rdi      # 0x1ae98
callq 0x434b
leaq 0xf27f(%rip), %rdi      # 0x1aeb0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa1d7