// File: /workspace/github_c_makefile_star2_O1/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 0x56a40
xorl $0x2, %eax
movzbl %cl, %ecx
movl %ecx, %edx
orl %eax, %edx
je 0x56a40
movq %rdi, %rbx
xorl $0x1, %ecx
xorl %r12d, %r12d
xorl %edx, %edx
orl %eax, %ecx
setne %bpl
leaq 0x46f5b(%rip), %rax     # 0x9d8b0
leaq 0x46f74(%rip), %r14     # 0x9d8d0
cmoveq %rax, %r14
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 0xf583
testq %rax, %rax
js 0x56a22
movq %r14, 0x8(%rsp)
movb %bpl, %r12b
leaq 0x1e(,%r12,8), %r12
movq (%rbx), %rbp
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %r14
subq %rsi, %r14
cmpq %r12, %r14
movq %r12, %r13
cmovbq %r14, %r13
leaq 0x10(%rsp), %rdi
movq %r13, %rdx
callq 0x43c0
addq %r13, 0x8(%rbp)
cmpq %r12, %r14
jae 0x569e6
testb $0x2, 0x30(%rbp)
je 0x569e6
leaq 0x10(%rsp), %rsi
movq %rbp, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0xf2cd
movq %rax, %r13
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %r12, %r13
jne 0x56a40
movq (%rbx), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xf583
testq %rax, %rax
js 0x56a40
leaq 0x10(%rsp), %rsi
andb $0xf, 0x8(%rsi)
movq 0x8(%rsp), %rdi
movq %r12, %rdx
callq 0x44d0
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
jmp 0x56a40
callq 0x4050
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x1d, (%rax)
jne 0x56a40
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