// File: /workspace/github_c_makefile_star2_O3/icebert[P]pblat/htslib/hts.c
// Function: hts_detect_format
int hts_detect_format(hFILE *hfile, htsFormat *fmt)
{
    unsigned char s[21];
    ssize_t len = hpeek(hfile, s, 18);
    if (len < 0) return -1;

    if (len >= 2 && s[0] == 0x1f && s[1] == 0x8b) {
        // The stream is either gzip-compressed or BGZF-compressed.
        // Determine which, and decompress the first few bytes.
        fmt->compression = (len >= 18 && (s[3] & 4) &&
                            memcmp(&s[12], "BC\2\0", 4) == 0)? bgzf : gzip;
        len = decompress_peek(hfile, s, sizeof s);
    }
    else {
        fmt->compression = no_compression;
        len = hpeek(hfile, s, sizeof s);
    }
    if (len < 0) return -1;

    fmt->compression_level = -1;
    fmt->specific = NULL;

    if (len >= 6 && memcmp(s,"CRAM",4) == 0 && s[4]>=1 && s[4]<=3 && s[5]<=1) {
        fmt->category = sequence_data;
        fmt->format = cram;
        fmt->version.major = s[4], fmt->version.minor = s[5];
        fmt->compression = custom;
        return 0;
    }
    else if (len >= 4 && s[3] <= '\4') {
        if (memcmp(s, "BAM\1", 4) == 0) {
            fmt->category = sequence_data;
            fmt->format = bam;
            // TODO Decompress enough to pick version from @HD-VN header
            fmt->version.major = 1, fmt->version.minor = -1;
            return 0;
        }
        else if (memcmp(s, "BAI\1", 4) == 0) {
            fmt->category = index_file;
            fmt->format = bai;
            fmt->version.major = -1, fmt->version.minor = -1;
            return 0;
        }
        else if (memcmp(s, "BCF\4", 4) == 0) {
            fmt->category = variant_data;
            fmt->format = bcf;
            fmt->version.major = 1, fmt->version.minor = -1;
            return 0;
        }
        else if (memcmp(s, "BCF\2", 4) == 0) {
            fmt->category = variant_data;
            fmt->format = bcf;
            fmt->version.major = s[3];
            fmt->version.minor = (len >= 5 && s[4] <= 2)? s[4] : 0;
            return 0;
        }
        else if (memcmp(s, "CSI\1", 4) == 0) {
            fmt->category = index_file;
            fmt->format = csi;
            fmt->version.major = 1, fmt->version.minor = -1;
            return 0;
        }
        else if (memcmp(s, "TBI\1", 4) == 0) {
            fmt->category = index_file;
            fmt->format = tbi;
            fmt->version.major = -1, fmt->version.minor = -1;
            return 0;
        }
    }
    else if (len >= 16 && memcmp(s, "##fileformat=VCF", 16) == 0) {
        fmt->category = variant_data;
        fmt->format = vcf;
        if (len >= 21 && s[16] == 'v')
            parse_version(fmt, &s[17], &s[len]);
        else
            fmt->version.major = fmt->version.minor = -1;
        return 0;
    }
    else if (len >= 4 && s[0] == '@' &&
             (memcmp(s, "@HD\t", 4) == 0 || memcmp(s, "@SQ\t", 4) == 0 ||
              memcmp(s, "@RG\t", 4) == 0 || memcmp(s, "@PG\t", 4) == 0)) {
        fmt->category = sequence_data;
        fmt->format = sam;
        // @HD-VN is not guaranteed to be the first tag, but then @HD is
        // not guaranteed to be present at all...
        if (len >= 9 && memcmp(s, "@HD\tVN:", 7) == 0)
            parse_version(fmt, &s[7], &s[len]);
        else
            fmt->version.major = 1, fmt->version.minor = -1;
        return 0;
    }
    else {
        // Various possibilities for tab-delimited text:
        // .crai   (gzipped tab-delimited six columns: seqid 5*number)
        // .bed    ([3..12] tab-delimited columns)
        // .bedpe  (>= 10 tab-delimited columns)
        // .sam    (tab-delimited >= 11 columns: seqid number seqid...)
        // FIXME For now, assume it's SAM
        fmt->category = sequence_data;
        fmt->format = sam;
        fmt->version.major = 1, fmt->version.minor = -1;
        return 0;
    }

    fmt->category = unknown_category;
    fmt->format = unknown_format;
    fmt->version.major = fmt->version.minor = -1;
    fmt->compression = no_compression;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x290, %rsp            # imm = 0x290
movq %rsi, %rbx
movq %rdi, %r15
movq %rsp, %r14
movl $0x12, %edx
movq %r14, %rsi
callq 0x6b023
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
js 0x4d177
cmpq $0x2, %rax
jb 0x4cf0a
cmpb $0x1f, (%rsp)
jne 0x4cf0a
cmpb $-0x75, 0x1(%rsp)
jne 0x4cf0a
movl $0x1, %ecx
cmpq $0x12, %rax
jb 0x4cea8
testb $0x4, 0x3(%rsp)
je 0x4cea8
xorl %ecx, %ecx
cmpl $0x24342, 0xc(%rsp)     # imm = 0x24342
sete %cl
incl %ecx
movl %ecx, 0xc(%rbx)
leaq 0x90(%rsp), %r12
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
movq %r12, %rsi
callq 0x6b023
testq %rax, %rax
js 0x4cf05
pxor %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movdqu %xmm0, 0x40(%rdi)
movq %r12, (%rdi)
movl %eax, 0x8(%rdi)
movq %r14, 0x18(%rdi)
movl $0x15, 0x20(%rdi)
leaq 0x4b96e(%rip), %rdx     # 0x9885c
movl $0x1f, %esi
movl $0x70, %ecx
callq 0x65d0
testl %eax, %eax
je 0x4d04d
xorl %r14d, %r14d
jmp 0x4cf24
movl $0x0, 0xc(%rbx)
movq %rsp, %rsi
movl $0x15, %edx
movq %r15, %rdi
callq 0x6b023
movq %rax, %r14
testq %r14, %r14
js 0x4d177
movw $0xffff, 0x10(%rbx)     # imm = 0xFFFF
movq $0x0, 0x18(%rbx)
cmpq $0x5, %r14
jbe 0x4cf99
movl (%rsp), %eax
cmpl $0x4d415243, %eax       # imm = 0x4D415243
sete %sil
movb 0x4(%rsp), %cl
leal -0x1(%rcx), %edx
cmpb $0x3, %dl
setb %dil
movb 0x5(%rsp), %dl
cmpb $0x2, %dl
setb %r8b
andb %sil, %r8b
andb %dil, %r8b
cmpb $0x1, %r8b
jne 0x4cf9f
movzbl %cl, %eax
movzbl %dl, %ecx
movabsq $0x600000001, %rdx      # imm = 0x600000001
movq %rdx, (%rbx)
movw %ax, 0x8(%rbx)
movw %cx, 0xa(%rbx)
movl $0x3, 0xc(%rbx)
jmp 0x4d175
movb 0x3(%rsp), %al
jmp 0x4cfa2
shrl $0x18, %eax
cmpq $0x4, %r14
jb 0x4cfee
cmpb $0x4, %al
ja 0x4cfee
movl (%rsp), %ecx
cmpl $0x14d4141, %ecx        # imm = 0x14D4141
jg 0x4d088
cmpl $0x1494142, %ecx        # imm = 0x1494142
je 0x4d131
cmpl $0x1494254, %ecx        # imm = 0x1494254
je 0x4d161
cmpl $0x1495343, %ecx        # imm = 0x1495343
jne 0x4d153
movabsq $0xa00000003, %rax      # imm = 0xA00000003
jmp 0x4d147
cmpq $0x10, %r14
jb 0x4d010
movdqa (%rsp), %xmm0
pcmpeqb 0x4b1df(%rip), %xmm0    # 0x981e0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x4d104
cmpq $0x4, %r14
jb 0x4d0f8
cmpb $0x40, (%rsp)
jne 0x4d0f8
movl (%rsp), %eax
cmpl $0x947523f, %eax        # imm = 0x947523F
jg 0x4d0bb
cmpl $0x9444840, %eax        # imm = 0x9444840
je 0x4d0c9
cmpl $0x9475040, %eax        # imm = 0x9475040
je 0x4d0c9
jmp 0x4d0f8
movq 0x48(%rsp), %r14
cmpq $0x14, %r14
ja 0x4d079
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movl $0x2, %esi
callq 0x6540
movq 0x48(%rsp), %r14
testl %eax, %eax
jne 0x4d079
cmpq $0x15, %r14
jb 0x4d05d
leaq 0x20(%rsp), %rdi
callq 0x6730
jmp 0x4cf24
cmpl $0x4464342, %ecx        # imm = 0x4464342
je 0x4d13d
cmpl $0x2464342, %ecx        # imm = 0x2464342
je 0x4d189
cmpl $0x14d4142, %ecx        # imm = 0x14D4142
jne 0x4d153
movabsq $0x400000001, %rax      # imm = 0x400000001
jmp 0x4d147
cmpl $0x9515340, %eax        # imm = 0x9515340
je 0x4d0c9
cmpl $0x9475240, %eax        # imm = 0x9475240
jne 0x4d0f8
movabsq $0x300000001, %rax      # imm = 0x300000001
movq %rax, (%rbx)
cmpq $0x9, %r14
jb 0x4d14a
movl $0x9444840, %eax        # imm = 0x9444840
xorl (%rsp), %eax
movl $0x3a4e5609, %ecx       # imm = 0x3A4E5609
xorl 0x3(%rsp), %ecx
orl %eax, %ecx
jne 0x4d14a
leaq 0x7(%rsp), %rsi
jmp 0x4d123
movabsq $0x300000001, %rax      # imm = 0x300000001
jmp 0x4d147
movabsq $0x800000002, %rax      # imm = 0x800000002
movq %rax, (%rbx)
cmpq $0x15, %r14
jb 0x4d16e
cmpb $0x76, 0x10(%rsp)
jne 0x4d16e
leaq 0x11(%rsp), %rsi
leaq (%rsp,%r14), %rdx
movq %rbx, %rdi
callq 0x4d1b8
jmp 0x4d175
movabsq $0x500000003, %rax      # imm = 0x500000003
jmp 0x4d16b
movabsq $0x900000002, %rax      # imm = 0x900000002
movq %rax, (%rbx)
movl $0xffff0001, 0x8(%rbx)  # imm = 0xFFFF0001
jmp 0x4d175
movdqa 0x4b095(%rip), %xmm0    # 0x981f0
movdqu %xmm0, (%rbx)
jmp 0x4d175
movabsq $0xc00000003, %rax      # imm = 0xC00000003
movq %rax, (%rbx)
movl $0xffffffff, 0x8(%rbx)  # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x290, %rsp            # imm = 0x290
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movabsq $0x900000002, %rcx      # imm = 0x900000002
movq %rcx, (%rbx)
movzbl %al, %eax
movw %ax, 0x8(%rbx)
movzbl 0x4(%rsp), %eax
xorl %ebp, %ebp
cmpw $0x3, %ax
cmovael %ebp, %eax
cmpq $0x5, %r14
cmovbl %ebp, %eax
movw %ax, 0xa(%rbx)
jmp 0x4d177