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

#define CHECKED_READ(dst, size, num) \
    do { \
        size_t ret = fread(dst, size, num, fd); \
        int err = ferror(fd); \
        if (err) { \
            return err; \
        } \
        read_size += ret * (size); \
    } while (false)

    CHECKED_READ(&bin->object_code_offset, 4, 1);
    CHECKED_READ(&bin->function_offset_table_offset, 4, 1);
    CHECKED_READ(&bin->bin_size, 4, 1);
    CHECKED_READ(&bin->xffffffff, 4, 1);
    CHECKED_READ(&bin->quest_number, 4, 1);
    CHECKED_READ(&bin->language, 4, 1);
    CHECKED_READ(&bin->quest_name, 64, 1);
    CHECKED_READ(&bin->short_description, 256, 1);
    CHECKED_READ(&bin->long_description, 576, 1);
    CHECKED_READ(&bin->padding, 4, 1);
    CHECKED_READ(&bin->items_list, 3728, 1);

    bin->object_code_len = bin->function_offset_table_offset - bin->object_code_offset;
    bin->object_code = malloc(bin->object_code_len);
    CHECKED_READ(bin->object_code, bin->object_code_len, 1);

    size_t labels_size = bin->bin_size - bin->function_offset_table_offset;
    bin->function_offset_table_len = labels_size / 4;
    bin->function_offset_table = malloc(labels_size);
    CHECKED_READ(bin->function_offset_table, labels_size, 1);

    return (int) read_size;

#undef CHECKED_READ
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x4080
movq %rax, %r15
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
leaq 0x4(%r14), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r13
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
leaq 0x8(%r14), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x4080
movq %rax, %rbp
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
leaq 0xc(%r14), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r12
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
movq %r12, 0x10(%rsp)
leaq 0x10(%r14), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r12
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
movq %r12, 0x40(%rsp)
leaq 0x14(%r14), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r12
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
movq %r12, 0x38(%rsp)
leaq 0x18(%r14), %rdi
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r12
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
movq %r12, 0x18(%rsp)
leaq 0x58(%r14), %rdi
pushq $0x1
popq %rdx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rcx
callq 0x4080
movq %rax, %r12
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
leaq 0x158(%r14), %rdi
pushq $0x1
popq %rdx
movl $0x240, %esi            # imm = 0x240
movq %rbx, %rcx
callq 0x4080
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
leaq 0x398(%r14), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x4080
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
leaq 0x39c(%r14), %rdi
pushq $0x1
popq %rdx
movl $0xe90, %esi            # imm = 0xE90
movq %rbx, %rcx
callq 0x4080
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
movl 0x4(%r14), %edi
subl (%r14), %edi
movq %rdi, 0x8(%rsp)
movq %rdi, 0x1230(%r14)
callq 0x4110
movq %rax, 0x1238(%r14)
pushq $0x1
popq %rdx
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rcx
callq 0x4080
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
jne 0x69c5
addq %r15, %r13
addq 0x10(%rsp), %rbp
addq %r13, %rbp
movq 0x40(%rsp), %r13
addq 0x38(%rsp), %r13
addq %rbp, %r13
shlq $0x6, 0x18(%rsp)
shlq $0x8, %r12
movq 0x8(%rsp), %rax
imull 0x1230(%r14), %eax
movq %rax, 0x8(%rsp)
imulq $0x240, 0x30(%rsp), %rbp # imm = 0x240
movl 0x8(%r14), %r15d
subl 0x4(%r14), %r15d
imulq $0xe90, 0x20(%rsp), %rax # imm = 0xE90
movq %rax, 0x10(%rsp)
movl %r15d, %eax
shrl $0x2, %eax
movq %rax, 0x1240(%r14)
movq %r15, %rdi
callq 0x4110
movq %rax, 0x1248(%r14)
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x4080
movq %rax, %r14
movq %rbx, %rdi
callq 0x4070
movl %eax, %ecx
imull %r15d, %r14d
addl 0x28(%rsp), %r13d
addl 0x18(%rsp), %r12d
addl %r12d, %ebp
leal (%rbp,%r13,4), %eax
movq 0x10(%rsp), %rdx
addl %eax, %edx
xorl %eax, %eax
testl %ecx, %ecx
cmovel %r14d, %eax
addl %edx, %eax
addl 0x8(%rsp), %eax
testl %ecx, %ecx
cmovnel %ecx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq