// File: /workspace/github_c_makefile_star2_O1/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
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x4080
movq %rax, %r15
movq %rbx, %rdi
callq 0x4070
movl %eax, %ebp
testl %eax, %eax
jne 0x7e86
leaq 0x4(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r12
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
leaq 0x8(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r13
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
movq %r13, 0x40(%rsp)
leaq 0xc(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r13
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
movq %r13, 0x38(%rsp)
leaq 0x10(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r13
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
movq %r13, 0x30(%rsp)
leaq 0x14(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r13
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
movq %r13, 0x28(%rsp)
leaq 0x18(%r14), %rdi
movl $0x40, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r13
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
movq %r13, 0x20(%rsp)
leaq 0x58(%r14), %rdi
movl $0x100, %esi            # imm = 0x100
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, %r13
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
leaq 0x158(%r14), %rdi
movl $0x240, %esi            # imm = 0x240
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
leaq 0x398(%r14), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
cmovnel %eax, %ebp
jne 0x7e86
leaq 0x39c(%r14), %rdi
movl $0xe90, %esi            # imm = 0xE90
movl $0x1, %edx
movq %rbx, %rcx
callq 0x4080
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x4070
imulq $0xe90, 0x8(%rsp), %rdx # imm = 0xE90
xorl %ecx, %ecx
testl %eax, %eax
cmoveq %rdx, %rcx
cmovnel %eax, %ebp
jne 0x7e86
addq %r15, %r12
movq 0x40(%rsp), %rdx
addq 0x38(%rsp), %rdx
addq %r12, %rdx
movq 0x30(%rsp), %rsi
addq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rax
shlq $0x6, %rax
shlq $0x8, %r13
addq %rax, %r13
movq 0x18(%rsp), %rax
leaq (%rax,%rax,8), %rax
shlq $0x6, %rax
addq %r13, %rax
addq 0x10(%rsp), %rsi
addq %rdx, %rsi
leaq (%rax,%rsi,4), %r12
addq %rcx, %r12
movl 0x4(%r14), %r15d
subl (%r14), %r15d
movq %r15, 0x1230(%r14)
movq %r15, %rdi
callq 0x4110
movq %rax, 0x1238(%r14)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x4080
movq %rax, %r15
movq %rbx, %rdi
callq 0x4070
testl %eax, %eax
je 0x7e1d
movl %eax, %ebp
jmp 0x7e28
imulq 0x1230(%r14), %r15
addq %r15, %r12
testl %eax, %eax
jne 0x7e86
movl 0x8(%r14), %r15d
subl 0x4(%r14), %r15d
movl %r15d, %eax
shrl $0x2, %eax
movq %rax, 0x1240(%r14)
movq %r15, %rdi
callq 0x4110
movq %rax, 0x1248(%r14)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
movq %rbx, %rcx
callq 0x4080
movq %rax, %r14
movq %rbx, %rdi
callq 0x4070
imull %r15d, %r14d
xorl %ecx, %ecx
testl %eax, %eax
cmovel %r14d, %ecx
addl %ecx, %r12d
testl %eax, %eax
cmovnel %eax, %r12d
movl %r12d, %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq