// File: /workspace/github_c_makefile_star2_O0/tstword[P]MIPSAssemblerC/src/mipsfhdr.c
// Function: write_object_file
void write_object_file(struct assembler *assembler, const char *file) {
    struct MIPS_file_header file_hdr;
    size_t nbytes;
    FILE *fp;

    if((fp = fopen_wrap(file, "wb+")) == NULL) {
        fprintf(stderr, "Failed to open output file '%s: Error: ", file);
        perror(NULL);
        destroy_assembler(&assembler);
        exit(EXIT_FAILURE);
    }

    memset((void *)&file_hdr, 0, sizeof(file_hdr));

    /* Magic number */
    memcpy((void *)file_hdr.m_magic, "mips", 4);

    /* Endianness check */
    uint16_t endian = 0x0201; /* If little endian result is 1, big endian result is 2 */
    file_hdr.m_endianness = *((uint8_t *)&endian);

    /* Version */
    file_hdr.m_version = 0x1;

    /* Section header count */
    file_hdr.m_shnum = 0;
    for(segment_t segment = 0; segment < MAX_SEGMENTS; ++segment) {
        if(assembler->segment_memory_offset[segment] > 0) {
            ++file_hdr.m_shnum;
        }
    }

    /* Write header to object file */
    nbytes = fwrite((void *)&file_hdr, 0x1, sizeof(file_hdr), fp);
    if(nbytes != sizeof(file_hdr)) {
        perror("Object Write Error: Failed to write file header: ");
        destroy_assembler(&assembler);
        exit(EXIT_FAILURE);
    }

    offset_t file_offset = sizeof(file_hdr);

    /* Write segment headers and their corresponding data */
    struct MIPS_sect_header section_hdr;

    /* Zero memory */
    memset((void *)&section_hdr, 0, sizeof(section_hdr));

    for(segment_t segment = 0; segment < MAX_SEGMENTS; ++segment) {
        if(assembler->segment_memory_offset[segment] > 0) {
            section_hdr.sh_segment = segment;
            section_hdr.sh_offset = file_offset;
            section_hdr.sh_size = assembler->segment_memory_offset[segment];

            nbytes = fwrite((void *)&section_hdr, 0x1, sizeof(section_hdr), fp);
            if(nbytes != sizeof(section_hdr)) {
                perror("Object Write Error: Failed to write section header header: ");
                destroy_assembler(&assembler);
                exit(EXIT_FAILURE);
            }

            nbytes = fwrite(assembler->segment_memory[segment], 0x1, assembler->segment_memory_offset[segment], fp);
            if(nbytes != assembler->segment_memory_offset[segment]) {
                perror("Object Write Error: Failed to write memory to file: ");
                destroy_assembler(&assembler);
                exit(EXIT_FAILURE);
            }

            file_offset += sizeof(section_hdr) + assembler->segment_memory_offset[segment];
        }
    }

    fclose(fp);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x337c(%rip), %rsi      # 0xabe7
callq 0x7170
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x78b2
movq 0x6777(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x335c(%rip), %rsi      # 0xabeb
movb $0x0, %al
callq 0x30c0
xorl %eax, %eax
movl %eax, %edi
callq 0x3120
leaq -0x8(%rbp), %rdi
callq 0x7110
movl $0x1, %edi
callq 0x3140
leaq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x3070
movl 0x334b(%rip), %eax      # 0xac13
movl %eax, -0x18(%rbp)
movw $0x201, -0x2a(%rbp)     # imm = 0x201
movb -0x2a(%rbp), %al
movb %al, -0x14(%rbp)
movb $0x1, -0x13(%rbp)
movb $0x0, -0x12(%rbp)
movb $0x0, -0x2b(%rbp)
movzbl -0x2b(%rbp), %eax
cmpl $0x4, %eax
jge 0x7910
movq -0x8(%rbp), %rax
movzbl -0x2b(%rbp), %ecx
cmpq $0x0, 0x58(%rax,%rcx,8)
jbe 0x7904
movb -0x12(%rbp), %al
addb $0x1, %al
movb %al, -0x12(%rbp)
jmp 0x7906
movb -0x2b(%rbp), %al
addb $0x1, %al
movb %al, -0x2b(%rbp)
jmp 0x78e3
movq -0x28(%rbp), %rcx
leaq -0x18(%rbp), %rdi
movl $0x1, %esi
movl $0x8, %edx
callq 0x3150
movq %rax, -0x20(%rbp)
cmpq $0x8, -0x20(%rbp)
je 0x7951
leaq 0x32df(%rip), %rdi      # 0xac18
callq 0x3120
leaq -0x8(%rbp), %rdi
callq 0x7110
movl $0x1, %edi
callq 0x3140
movl $0x8, -0x30(%rbp)
leaq -0x3c(%rbp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x3070
movb $0x0, -0x3d(%rbp)
movzbl -0x3d(%rbp), %eax
cmpl $0x4, %eax
jge 0x7a71
movq -0x8(%rbp), %rax
movzbl -0x3d(%rbp), %ecx
cmpq $0x0, 0x58(%rax,%rcx,8)
jbe 0x7a62
movb -0x3d(%rbp), %al
movb %al, -0x3c(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movzbl -0x3d(%rbp), %ecx
movq 0x58(%rax,%rcx,8), %rax
movl %eax, -0x34(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x3c(%rbp), %rdi
movl $0x1, %esi
movl $0xc, %edx
callq 0x3150
movq %rax, -0x20(%rbp)
cmpq $0xc, -0x20(%rbp)
je 0x79ea
leaq 0x3278(%rip), %rdi      # 0xac4a
callq 0x3120
leaq -0x8(%rbp), %rdi
callq 0x7110
movl $0x1, %edi
callq 0x3140
movq -0x8(%rbp), %rax
movzbl -0x3d(%rbp), %ecx
movq 0x20(%rax,%rcx,8), %rdi
movq -0x8(%rbp), %rax
movzbl -0x3d(%rbp), %ecx
movq 0x58(%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x3150
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movzbl -0x3d(%rbp), %edx
cmpq 0x58(%rcx,%rdx,8), %rax
je 0x7a48
leaq 0x3256(%rip), %rdi      # 0xac86
callq 0x3120
leaq -0x8(%rbp), %rdi
callq 0x7110
movl $0x1, %edi
callq 0x3140
movq -0x8(%rbp), %rax
movzbl -0x3d(%rbp), %ecx
movq 0x58(%rax,%rcx,8), %rcx
addq $0xc, %rcx
movl -0x30(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x30(%rbp)
jmp 0x7a64
movb -0x3d(%rbp), %al
addb $0x1, %al
movb %al, -0x3d(%rbp)
jmp 0x796c
movq -0x28(%rbp), %rdi
callq 0x3050
addq $0x40, %rsp
popq %rbp
retq