// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/htslib/cram/cram_io.c
// Function: cram_check_EOF
int cram_check_EOF(cram_fd *fd)
{
    // Byte 9 in these templates is & with 0x0f to resolve differences
    // between ITF-8 interpretations between early Java and C
    // implementations of CRAM
    static const unsigned char TEMPLATE_2_1[30] = {
        0x0b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xe0,
        0x45, 0x4f, 0x46, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
        0x01, 0x00, 0x06, 0x06, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00
    };
    static const unsigned char TEMPLATE_3[38] = {
        0x0f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xe0,
        0x45, 0x4f, 0x46, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x05,
        0xbd, 0xd9, 0x4f, 0x00, 0x01, 0x00, 0x06, 0x06, 0x01, 0x00,
        0x01, 0x00, 0x01, 0x00, 0xee, 0x63, 0x01, 0x4b
    };

    unsigned char buf[38]; // max(sizeof TEMPLATE_*)

    uint8_t major = CRAM_MAJOR_VERS(fd->version);
    uint8_t minor = CRAM_MINOR_VERS(fd->version);

    const unsigned char *template;
    ssize_t template_len;
    if ((major < 2) ||
        (major == 2 && minor == 0)) {
        return 3; // No EOF support in cram versions less than 2.1
    } else if (major == 2 && minor == 1) {
        template = TEMPLATE_2_1;
        template_len = sizeof TEMPLATE_2_1;
    } else {
        template = TEMPLATE_3;
        template_len = sizeof TEMPLATE_3;
    }

    off_t offset = htell(fd->fp);
    if (hseek(fd->fp, -template_len, SEEK_END) < 0) {
        if (errno == ESPIPE) {
            hclearerr(fd->fp);
            return 2;
        }
        else {
            return -1;
        }
    }
    if (hread(fd->fp, buf, template_len) != template_len) return -1;
    if (hseek(fd->fp, offset, SEEK_SET) < 0) return -1;
    buf[8] &= 0x0f;
    return (memcmp(template, buf, template_len) == 0)? 1 : 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl 0xc(%rdi), %ecx
movzbl %ch, %eax
movl $0x3, %ebp
cmpl $0x2, %eax
jb 0x3913c
xorl $0x2, %eax
movzbl %cl, %ecx
movl %ecx, %edx
orl %eax, %edx
je 0x3913c
movq %rdi, %rbx
xorl $0x1, %ecx
xorl %r14d, %r14d
xorl %edx, %edx
orl %eax, %ecx
setne %bpl
leaq 0x198a4(%rip), %rax     # 0x528f0
leaq 0x198bd(%rip), %r12     # 0x52910
cmoveq %rax, %r12
sete %dl
leaq -0x26(,%rdx,8), %rsi
movq (%rdi), %rdi
movq 0x28(%rdi), %r15
addq 0x8(%rdi), %r15
subq (%rdi), %r15
movl $0x2, %edx
callq 0x473bc
testq %rax, %rax
js 0x3911e
movq %r12, (%rsp)
movb %bpl, %r14b
leaq 0x1e(,%r14,8), %r13
movq %r13, 0x8(%rsp)
movq (%rbx), %rbp
movq 0x8(%rbp), %r14
movq 0x10(%rbp), %r12
subq %r14, %r12
cmpq %r13, %r12
cmovbq %r12, %r13
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x4420
addq %r13, %r14
movq %r14, 0x8(%rbp)
movq 0x8(%rsp), %r14
cmpq %r14, %r12
jae 0x390e3
leaq 0x10(%rsp), %rsi
movq %rbp, %rdi
movq %r14, %rdx
movq %r13, %rcx
callq 0x4716b
movq %rax, %r13
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %r14, %r13
jne 0x3913c
movq (%rbx), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x473bc
testq %rax, %rax
js 0x3913c
leaq 0x10(%rsp), %rsi
andb $0xf, 0x8(%rsi)
movq (%rsp), %rdi
movq %r14, %rdx
callq 0x4550
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
jmp 0x3913c
callq 0x4070
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x1d, (%rax)
jne 0x3913c
movq (%rbx), %rax
movl $0x0, 0x34(%rax)
movl $0x2, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq