// File: /workspace/github_c_makefile_star2_O2/jtuu[P]quesler/src/qst.c
// Function: write_bin
int write_bin(FILE* fd, QstBin* bin) {
    size_t write_size = 0;

#define CHECKED_WRITE(value, size, num) \
    do { \
        size_t ret = fwrite(value, size, num, fd); \
        int err = ferror(fd); \
        if (err) { \
            return err; \
        } \
        write_size += ret * (size); \
    } while (false)
#define WRITE4(value) CHECKED_WRITE(&(value), 4, 1)

    bin->object_code_offset = BIN_MINIMUM_SIZE;
    bin->function_offset_table_offset = (uint32_t) (bin->object_code_offset +
                                                    bin->object_code_len);
    bin->xffffffff = 0xffffffff;
    bin->padding = 0;

    WRITE4(bin->object_code_offset);
    WRITE4(bin->function_offset_table_offset);
    WRITE4(bin->bin_size);
    WRITE4(bin->xffffffff);
    WRITE4(bin->quest_number);
    WRITE4(bin->language);
    
    CHECKED_WRITE(bin->quest_name, 2, 32);
    CHECKED_WRITE(bin->short_description, 2, 128);
    CHECKED_WRITE(bin->long_description, 2, 288);
    
    WRITE4(bin->padding);

    CHECKED_WRITE(bin->items_list, 4, 932);
    CHECKED_WRITE(bin->object_code, 1, bin->object_code_len);
    CHECKED_WRITE(bin->function_offset_table, 4, bin->function_offset_table_len);

    return (int) write_size;

#undef CHECKED_WRITE
#undef WRITE4
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r12
movl $0x122c, %eax           # imm = 0x122C
movl %eax, (%rsi)
addl 0x1230(%rsi), %eax
movl %eax, 0x4(%rsi)
orl $-0x1, 0xc(%rsi)
movq %rdi, %r14
andl $0x0, 0x398(%rsi)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x4170
movq %rax, %rbx
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
leaq 0x4(%r12), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4170
movq %rax, %r15
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
leaq 0x8(%r12), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4170
movq %rax, %r13
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
leaq 0xc(%r12), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4170
movq %rax, %rbp
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
movq %rbp, 0x40(%rsp)
leaq 0x10(%r12), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4170
movq %rax, %rbp
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
movq %rbp, 0x38(%rsp)
leaq 0x14(%r12), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4170
movq %rax, %rbp
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
movq %rbp, 0x30(%rsp)
leaq 0x18(%r12), %rdi
pushq $0x2
popq %rsi
pushq $0x20
popq %rdx
movq %r14, %rcx
callq 0x4170
movq %rax, %rbp
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
movq %rbp, 0x28(%rsp)
leaq 0x58(%r12), %rdi
pushq $0x2
popq %rsi
movl $0x80, %edx
movq %r14, %rcx
callq 0x4170
movq %rax, %rbp
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
leaq 0x158(%r12), %rdi
pushq $0x2
popq %rsi
movl $0x120, %edx            # imm = 0x120
movq %r14, %rcx
callq 0x4170
movq %rax, 0x20(%rsp)
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
leaq 0x398(%r12), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x4170
movq %rax, 0x18(%rsp)
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
movq %rbp, 0x10(%rsp)
leaq 0x39c(%r12), %rdi
pushq $0x4
popq %rsi
movl $0x3a4, %edx            # imm = 0x3A4
movq %r14, %rcx
callq 0x4170
movq %rax, 0x8(%rsp)
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
movq 0x1230(%r12), %rdx
movq 0x1238(%r12), %rdi
pushq $0x1
popq %rsi
movq %r14, %rcx
callq 0x4170
movq %rax, (%rsp)
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
movq 0x1240(%r12), %rdx
movq 0x1248(%r12), %rdi
pushq $0x4
popq %rsi
movq %r14, %rcx
callq 0x4170
movq %rax, %r12
movq %r14, %rdi
callq 0x4070
testl %eax, %eax
jne 0x66df
addl %ebx, %r15d
movq 0x40(%rsp), %rax
addl %r13d, %eax
addl %r15d, %eax
movq 0x30(%rsp), %rbp
addl 0x38(%rsp), %ebp
movq 0x18(%rsp), %rcx
addl %ebp, %ecx
addl %eax, %ecx
movq 0x8(%rsp), %rax
addl %ecx, %eax
addl %eax, %r12d
movq 0x10(%rsp), %rax
addl 0x28(%rsp), %eax
movq 0x20(%rsp), %rcx
addl %eax, %ecx
movq (%rsp), %rax
leal (%rax,%rcx,2), %eax
leal (%rax,%r12,4), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq