// File: /workspace/github_c_makefile_star2_O3/samtools[P]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 0x540e8
xorl $0x2, %eax
movzbl %cl, %ecx
movl %ecx, %edx
orl %eax, %edx
je 0x540e8
movq %rdi, %rbx
xorl $0x1, %ecx
xorl %r14d, %r14d
xorl %edx, %edx
orl %eax, %ecx
setne %bpl
leaq 0x4691e(%rip), %rax     # 0x9a910
leaq 0x46937(%rip), %r12     # 0x9a930
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 0xf3b3
testq %rax, %rax
js 0x540ca
movq %r15, 0x8(%rsp)
movq %r12, (%rsp)
movb %bpl, %r14b
leaq 0x1e(,%r14,8), %r15
movq (%rbx), %rbp
movq 0x8(%rbp), %r14
movq 0x10(%rbp), %r12
subq %r14, %r12
cmpq %r15, %r12
movq %r15, %r13
cmovbq %r12, %r13
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x43c0
addq %r13, %r14
movq %r14, 0x8(%rbp)
cmpq %r15, %r12
jae 0x5408d
testb $0x2, 0x30(%rbp)
je 0x5408d
leaq 0x10(%rsp), %rsi
movq %rbp, %rdi
movq %r15, %rdx
movq %r13, %rcx
callq 0xf129
movq %rax, %r13
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %r15, %r13
jne 0x540e8
movq (%rbx), %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0xf3b3
testq %rax, %rax
js 0x540e8
leaq 0x10(%rsp), %rsi
andb $0xf, 0x8(%rsi)
movq (%rsp), %rdi
movq %r15, %rdx
callq 0x44d0
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
jmp 0x540e8
callq 0x4050
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x1d, (%rax)
jne 0x540e8
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