// File: /workspace/github_c_makefile_star2_O2/tstword[P]MIPSAssemblerC/src/assembler.c
// Function: program_cfg
void program_cfg(struct assembler *assembler) {
    cfg_assembler = assembler;

    instruction_list_cfg();
    
    /* Verify undefined symbol table */
    for(struct list_node *head = assembler->decl_symlist->front; head != NULL; head = head->next) {
        struct symbol_table_entry *sym_entry = (struct symbol_table_entry *)head->value;
        symstat_t status = sym_entry->status;
        if(status == SYMBOL_UNDEFINED) {
            /* Symbol is still undefined, program cannot be assembled */
            fprintf(stderr, "Symbol Error: Undefined symbol '%s'\n", ((struct symbol_table_entry *)head->value)->key);
            struct list_node *instr_ref = sym_entry->instr_list->front;
            while(instr_ref != NULL) {
                destroy_instruction((struct instruction_node *)instr_ref->value);
                instr_ref = instr_ref->next;
            }
            assembler->status = ASSEMBLER_STATUS_FAIL;
        } else {
            struct list_node *instr_ref = sym_entry->instr_list->front;
            while(instr_ref != NULL) {
                if(((struct instruction_node *)instr_ref->value)->mnemonic->token == TOK_MNEMONIC) {
                    assembler->segment = ((struct instruction_node *)instr_ref->value)->segment;
                    assembler->segment_offset[assembler->segment] = ((struct instruction_node *)instr_ref->value)->offset;
                    assemble_instruction((struct instruction_node *)instr_ref->value); 
                    destroy_instruction((struct instruction_node *)instr_ref->value);
                }
                else if(((struct instruction_node *)instr_ref->value)->mnemonic->token == TOK_DIRECTIVE) {
                    assembler->segment = ((struct instruction_node *)instr_ref->value)->segment;
                    assembler->segment_offset[assembler->segment] = ((struct instruction_node *)instr_ref->value)->offset;
                    check_directive((struct instruction_node *)instr_ref->value); 
                    destroy_instruction((struct instruction_node *)instr_ref->value);
                }
                instr_ref = instr_ref->next;
            }
        }
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
movq %rdi, 0x741d(%rip)      # 0xc5b8
callq 0x5124
movq 0x18(%rbx), %r15
movq 0x5e4d(%rip), %r12      # 0xaff8
leaq 0x24bc(%rip), %r14      # 0x766e
movq (%r15), %r15
testq %r15, %r15
je 0x5250
movq 0x8(%r15), %r13
cmpb $0x0, 0x8(%r13)
je 0x521d
movq 0x18(%r13), %r13
movq (%r13), %r13
testq %r13, %r13
je 0x51b2
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movl 0x8(%rax), %eax
cmpl $0xc, %eax
je 0x51ff
cmpl $0x8, %eax
jne 0x51cd
movzbl 0x14(%rdi), %eax
movb %al, 0x45(%rbx)
movl 0x10(%rdi), %ecx
movl %ecx, 0x48(%rbx,%rax,4)
callq 0x4bcd
jmp 0x5212
movzbl 0x14(%rdi), %eax
movb %al, 0x45(%rbx)
movl 0x10(%rdi), %ecx
movl %ecx, 0x48(%rbx,%rax,4)
callq 0x4c83
movq 0x8(%r13), %rdi
callq 0x3d45
jmp 0x51cd
movq (%r12), %rdi
movq (%r13), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x30d0
movq 0x18(%r13), %r13
movq (%r13), %r13
testq %r13, %r13
je 0x5247
movq 0x8(%r13), %rdi
callq 0x3d45
jmp 0x5233
movb $0x2, 0x44(%rbx)
jmp 0x51b2
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq