// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movl %ecx, -0x2c(%rbp)
movq %rsi, %r12
movq %rdi, %r15
testl %edx, %edx
je 0xaa97
leaq 0xa28c(%rip), %rax      # 0x14d20
movb $0x1, (%rax)
leaq 0xf3fa(%rip), %rdi      # 0x19e98
pushq $0xa
popq %rbx
movl %ebx, %esi
xorl %edx, %edx
callq 0x3775
leaq 0xf3ff(%rip), %rdi      # 0x19eb0
movl %ebx, %esi
xorl %edx, %edx
callq 0x952d
leaq 0x38(%r15), %r13
movq %r13, %r14
movq (%r14), %r14
testq %r14, %r14
je 0xab33
movl 0xf3d5(%rip), %ebx      # 0x19ea4
movq 0x40(%r14), %rsi
leaq 0xf3be(%rip), %rdi      # 0x19e98
pushq $0x1
popq %rdx
callq 0x38b8
cmpl %ebx, 0xf3bc(%rip)      # 0x19ea4
jne 0xab2d
movq 0x40(%r14), %rdx
leaq 0xf2db(%rip), %rbx      # 0x19dd0
movq %rbx, %rdi
leaq 0x3e81(%rip), %rsi      # 0xe980
xorl %eax, %eax
callq 0x31a0
movl 0xc(%r14), %ecx
movl 0x10(%r14), %r8d
leaq 0x36bd(%rip), %rdi      # 0xe1d2
xorl %esi, %esi
pushq $-0x1
popq %rdx
xorl %r9d, %r9d
pushq %rbx
leaq 0x3e82(%rip), %rax      # 0xe9a7
pushq %rax
callq 0x873c
popq %rax
popq %rcx
addq $0x38, %r14
jmp 0xaac1
addq $0x40, %r15
movq %r15, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xabd7
movq 0x50(%rbx), %rsi
testq %rsi, %rsi
je 0xabce
leaq 0xf342(%rip), %rdi      # 0x19e98
xorl %edx, %edx
callq 0x38b8
movl 0xf358(%rip), %r14d     # 0x19ebc
movq 0x50(%rbx), %rsi
leaq 0xf341(%rip), %rdi      # 0x19eb0
movq %rbx, %rdx
callq 0x966f
cmpl %r14d, 0xf33e(%rip)     # 0x19ebc
jne 0xabce
movq 0x10(%rax), %rax
movq 0x50(%rbx), %rdx
movl 0xc(%rax), %ecx
movl 0x10(%rax), %r8d
leaq 0xf23a(%rip), %r14      # 0x19dd0
movq %r14, %rdi
leaq 0x3e15(%rip), %rsi      # 0xe9b5
xorl %eax, %eax
callq 0x31a0
movl 0xc(%rbx), %ecx
movl 0x10(%rbx), %r8d
leaq 0x361d(%rip), %rdi      # 0xe1d2
xorl %esi, %esi
pushq $-0x1
popq %rdx
xorl %r9d, %r9d
pushq %r14
leaq 0x3e27(%rip), %rax      # 0xe9ed
pushq %rax
callq 0x873c
popq %rax
popq %rcx
addq $0x48, %rbx
jmp 0xab3a
movq %r15, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xac27
movq %r12, %rdi
movq %rbx, %rsi
callq 0x936f
movq 0x50(%rbx), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9a51
movq %r12, %rdi
callq 0x93c2
cmpb $0x0, -0x2c(%rbp)
je 0xac13
movq 0x58(%rbx), %rdi
callq 0x79c0
leaq 0x20(%rbx), %rdi
callq 0xcc8a
andq $0x0, 0x58(%rbx)
addq $0x48, %rbx
jmp 0xabda
leaq 0xf26a(%rip), %rbx      # 0x19e98
pushq $-0x1
popq %r12
movq (%r13), %r13
testq %r13, %r13
je 0xac99
movq 0x40(%r13), %rsi
movq %rbx, %rdi
callq 0x3865
andl $0x3, %eax
cmpl $0x1, %eax
jne 0xac93
movq 0x40(%r13), %rdx
leaq 0xf176(%rip), %r14      # 0x19dd0
movq %r14, %rdi
leaq 0x3d96(%rip), %rsi      # 0xe9fa
xorl %eax, %eax
callq 0x31a0
movl 0xc(%r13), %ecx
movl 0x10(%r13), %r8d
leaq 0x3558(%rip), %rdi      # 0xe1d2
xorl %esi, %esi
movl %r12d, %edx
xorl %r9d, %r9d
pushq %r14
leaq 0x3d9e(%rip), %rax      # 0xea29
pushq %rax
callq 0x873c
popq %rax
popq %rcx
addq $0x38, %r13
jmp 0xac32
leaq 0xf1f8(%rip), %rbx      # 0x19e98
pushq $0x1
popq %r12
pushq $0x4
popq %r13
movq (%r15), %r15
testq %r15, %r15
je 0xad10
movq 0x50(%r15), %rsi
testq %rsi, %rsi
je 0xad0a
movq %rbx, %rdi
callq 0x3865
testb $0x4, %al
jne 0xad0a
movq 0x50(%r15), %rdx
leaq 0xf100(%rip), %r14      # 0x19dd0
movq %r14, %rdi
leaq 0x3d5e(%rip), %rsi      # 0xea38
xorl %eax, %eax
callq 0x31a0
movl 0xc(%r15), %ecx
movl 0x10(%r15), %r8d
leaq 0x34e2(%rip), %rdi      # 0xe1d2
movl %r12d, %esi
movl %r13d, %edx
xorl %r9d, %r9d
pushq %r14
leaq 0x3d55(%rip), %rax      # 0xea57
pushq %rax
callq 0x873c
popq %rax
popq %rcx
addq $0x48, %r15
jmp 0xaca8
leaq 0xf181(%rip), %rdi      # 0x19e98
callq 0x37b2
leaq 0xf18d(%rip), %rdi      # 0x19eb0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x956a