// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rsi, %r13
movl $0x122c, %eax           # imm = 0x122C
movl %eax, (%rsi)
addl 0x1230(%rsi), %eax
movq %rdi, %r15
movl %eax, 0x4(%rsi)
movl $0xffffffff, 0xc(%rsi)  # imm = 0xFFFFFFFF
movl $0x0, 0x398(%rsi)
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x4170
movq %rax, %r14
movq %r15, %rdi
callq 0x4070
movl %eax, %ebx
testl %eax, %eax
jne 0x7b53
leaq 0x4(%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x4170
movq %rax, %r12
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
leaq 0x8(%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x4170
movq %rax, %rbp
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
movq %rbp, 0x50(%rsp)
leaq 0xc(%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x4170
movq %rax, %rbp
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
movq %rbp, 0x48(%rsp)
leaq 0x10(%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x4170
movq %rax, %rbp
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
movq %rbp, 0x40(%rsp)
leaq 0x14(%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x4170
movq %rax, %rbp
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
movq %rbp, 0x38(%rsp)
leaq 0x18(%r13), %rdi
movl $0x2, %esi
movl $0x20, %edx
movq %r15, %rcx
callq 0x4170
movq %rax, %rbp
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
movq %rbp, 0x30(%rsp)
leaq 0x58(%r13), %rdi
movl $0x2, %esi
movl $0x80, %edx
movq %r15, %rcx
callq 0x4170
movq %rax, %rbp
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
leaq 0x158(%r13), %rdi
movl $0x2, %esi
movl $0x120, %edx            # imm = 0x120
movq %r15, %rcx
callq 0x4170
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
leaq 0x398(%r13), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x4170
movq %rax, 0x20(%rsp)
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
movq %rbp, 0x18(%rsp)
leaq 0x39c(%r13), %rdi
movl $0x4, %esi
movl $0x3a4, %edx            # imm = 0x3A4
movq %r15, %rcx
callq 0x4170
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
movq 0x1230(%r13), %rdx
movq 0x1238(%r13), %rdi
movl $0x1, %esi
movq %r15, %rcx
callq 0x4170
movq %rax, 0x8(%rsp)
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
movq 0x1240(%r13), %rdx
movq 0x1248(%r13), %rdi
movl $0x4, %esi
movq %r15, %rcx
callq 0x4170
movq %rax, %r13
movq %r15, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebx
jne 0x7b53
addl %r14d, %r12d
movq 0x48(%rsp), %rax
addl 0x50(%rsp), %eax
addl %r12d, %eax
movq 0x38(%rsp), %rbp
addl 0x40(%rsp), %ebp
movq 0x20(%rsp), %rcx
addl %ebp, %ecx
addl %eax, %ecx
movq 0x10(%rsp), %rax
addl %ecx, %eax
addl %eax, %r13d
movq 0x18(%rsp), %rax
addl 0x30(%rsp), %eax
movq 0x28(%rsp), %rcx
addl %eax, %ecx
movq 0x8(%rsp), %rax
leal (%rax,%rcx,2), %eax
leal (%rax,%r13,4), %ebx
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq