// File: /workspace/github_c_makefile_star2_O1/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, -0x30(%rbp)
testl %edx, %edx
je 0xba0e
leaq 0xa315(%rip), %rax      # 0x15d20
movb $0x1, (%rax)
leaq 0xf483(%rip), %rdi      # 0x1ae98
movl $0xa, %esi
xorl %edx, %edx
callq 0x42fd
movl $0xa, %edi
movl $0x8, %esi
callq 0x3120
movq %rax, 0xf479(%rip)      # 0x1aeb0
movq $0xa, 0xf476(%rip) # 0x1aeb8
movw $0x100, 0xf475(%rip)    # imm = 0x100
movq -0x30(%rbp), %rax
movq 0x38(%rax), %r13
testq %r13, %r13
je 0xbad1
leaq 0xf439(%rip), %rbx      # 0x1ae98
leaq 0xf36a(%rip), %r12      # 0x1add0
movl 0xf437(%rip), %r14d     # 0x1aea4
movq 0x40(%r13), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x4427
cmpl %r14d, 0xf41f(%rip)     # 0x1aea4
jne 0xbac8
movq 0x40(%r13), %rdx
movq %r12, %rdi
leaq 0x3eeb(%rip), %rsi      # 0xf980
xorl %eax, %eax
callq 0x31b0
movl 0xc(%r13), %ecx
movl 0x10(%r13), %r8d
leaq 0x3727(%rip), %rdi      # 0xf1d2
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
pushq %r12
leaq 0x3ee9(%rip), %rax      # 0xf9a7
pushq %rax
callq 0x94d4
addq $0x10, %rsp
movq 0x38(%r13), %r13
testq %r13, %r13
jne 0xba66
movq -0x30(%rbp), %rax
movq 0x40(%rax), %r12
testq %r12, %r12
je 0xbb85
leaq 0xf3c7(%rip), %rbx      # 0x1aeb0
leaq 0xf2e0(%rip), %r13      # 0x1add0
movq 0x50(%r12), %rsi
testq %rsi, %rsi
je 0xbb77
leaq 0xf397(%rip), %rdi      # 0x1ae98
xorl %edx, %edx
callq 0x4427
movl 0xf3ad(%rip), %r14d     # 0x1aebc
movq 0x50(%r12), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0xa3aa
cmpl %r14d, 0xf396(%rip)     # 0x1aebc
jne 0xbb77
movq 0x10(%rax), %rax
movq 0x50(%r12), %rdx
movl 0xc(%rax), %ecx
movl 0x10(%rax), %r8d
movq %r13, %rdi
leaq 0x3e73(%rip), %rsi      # 0xf9b5
xorl %eax, %eax
callq 0x31b0
movl 0xc(%r12), %ecx
movl 0x10(%r12), %r8d
leaq 0x3678(%rip), %rdi      # 0xf1d2
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
pushq %r13
leaq 0x3e80(%rip), %rax      # 0xf9ed
pushq %rax
callq 0x94d4
addq $0x10, %rsp
movq 0x48(%r12), %r12
testq %r12, %r12
jne 0xbaf0
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rbx
testq %rbx, %rbx
movl -0x34(%rbp), %r14d
je 0xbbe0
movq %r15, %rdi
movq %rbx, %rsi
callq 0xa13f
movq 0x50(%rbx), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xa7eb
movq %r15, %rdi
callq 0xa194
testb %r14b, %r14b
je 0xbbc6
movq 0x58(%rbx), %rdi
callq 0x86cc
leaq 0x20(%rbx), %rdi
callq 0xdd08
movq $0x0, 0x58(%rbx)
movq 0x48(%rbx), %rbx
testq %rbx, %rbx
jne 0xbb96
movq -0x30(%rbp), %rax
movq 0x38(%rax), %r13
testq %r13, %r13
je 0xbc5d
leaq 0xf2a4(%rip), %rbx      # 0x1ae98
leaq 0xf1d5(%rip), %r14      # 0x1add0
leaq 0x3e27(%rip), %r15      # 0xfa29
leaq 0x35c9(%rip), %r12      # 0xf1d2
movq 0x40(%r13), %rsi
movq %rbx, %rdi
callq 0x4402
andl $0x3, %eax
cmpl $0x1, %eax
jne 0xbc54
movq 0x40(%r13), %rdx
movq %r14, %rdi
leaq 0x3dcf(%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 0x94d4
addq $0x10, %rsp
movq 0x38(%r13), %r13
testq %r13, %r13
jne 0xbc09
movq -0x30(%rbp), %rax
movq 0x40(%rax), %r13
testq %r13, %r13
je 0xbcde
leaq 0xf227(%rip), %rbx      # 0x1ae98
leaq 0xf158(%rip), %r14      # 0x1add0
leaq 0x3dd8(%rip), %r15      # 0xfa57
leaq 0x354c(%rip), %r12      # 0xf1d2
movq 0x50(%r13), %rsi
testq %rsi, %rsi
je 0xbcd5
movq %rbx, %rdi
callq 0x4402
testb $0x4, %al
jne 0xbcd5
movq 0x50(%r13), %rdx
movq %r14, %rdi
leaq 0x3d8f(%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 0x94d4
addq $0x10, %rsp
movq 0x48(%r13), %r13
testq %r13, %r13
jne 0xbc86
leaq 0xf1b3(%rip), %rdi      # 0x1ae98
callq 0x433f
leaq 0xf1bf(%rip), %rdi      # 0x1aeb0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa2c3