// File: 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
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 0x53723
movzbl -0x41(%rbp), %eax
cmpl $0x2, %eax
jne 0x5372f
movzbl -0x42(%rbp), %eax
cmpl $0x0, %eax
jne 0x5372f
movl $0x3, -0x4(%rbp)
jmp 0x53833
movzbl -0x41(%rbp), %eax
cmpl $0x2, %eax
jne 0x53756
movzbl -0x42(%rbp), %eax
cmpl $0x1, %eax
jne 0x53756
leaq 0x29508(%rip), %rax     # 0x7cc50
movq %rax, -0x50(%rbp)
movq $0x1e, -0x58(%rbp)
jmp 0x53769
leaq 0x29513(%rip), %rax     # 0x7cc70
movq %rax, -0x50(%rbp)
movq $0x26, -0x58(%rbp)
jmp 0x5376b
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x53840
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 0x6abd0
cmpq $0x0, %rax
jge 0x537c2
callq 0x4070
cmpl $0x1d, (%rax)
jne 0x537b9
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x53870
movl $0x2, -0x4(%rbp)
jmp 0x53833
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x53833
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x45540
cmpq -0x58(%rbp), %rax
je 0x537e5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x53833
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x60(%rbp), %rsi
xorl %edx, %edx
callq 0x6abd0
cmpq $0x0, %rax
jge 0x53806
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x53833
movzbl -0x38(%rbp), %eax
andl $0xf, %eax
movb %al, -0x38(%rbp)
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x4240
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)