// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
movb %al, -0x41(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xff, %eax
movb %al, -0x42(%rbp)
movzbl -0x41(%rbp), %eax
cmpl $0x2, %eax
jl 0x7b373
movzbl -0x41(%rbp), %eax
cmpl $0x2, %eax
jne 0x7b37f
movzbl -0x42(%rbp), %eax
cmpl $0x0, %eax
jne 0x7b37f
movl $0x3, -0x4(%rbp)
jmp 0x7b483
movzbl -0x41(%rbp), %eax
cmpl $0x2, %eax
jne 0x7b3a6
movzbl -0x42(%rbp), %eax
cmpl $0x1, %eax
jne 0x7b3a6
leaq 0x79fc8(%rip), %rax     # 0xf5360
movq %rax, -0x50(%rbp)
movq $0x1e, -0x58(%rbp)
jmp 0x7b3b9
leaq 0x79fd3(%rip), %rax     # 0xf5380
movq %rax, -0x50(%rbp)
movq $0x26, -0x58(%rbp)
jmp 0x7b3bb
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7b490
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
subq -0x58(%rbp), %rsi
movl $0x2, %edx
callq 0x146a0
cmpq $0x0, %rax
jge 0x7b412
callq 0x4050
cmpl $0x1d, (%rax)
jne 0x7b409
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7b4c0
movl $0x2, -0x4(%rbp)
jmp 0x7b483
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b483
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x6cee0
cmpq -0x58(%rbp), %rax
je 0x7b435
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b483
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x60(%rbp), %rsi
xorl %edx, %edx
callq 0x146a0
cmpq $0x0, %rax
jge 0x7b456
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x7b483
movzbl -0x38(%rbp), %eax
andl $0xf, %eax
movb %al, -0x38(%rbp)
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x41c0
movl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)