// File: karel-brinda[P]ococo/ext/htslib/hts.c
// Function: hts_hopen
htsFile *hts_hopen(struct hFILE *hfile, const char *fn, const char *mode)
{
    htsFile *fp = (htsFile*)calloc(1, sizeof(htsFile));
    char simple_mode[101], *cp, *opts;
    simple_mode[100] = '\0';

    if (fp == NULL) goto error;

    fp->fn = strdup(fn);
    fp->is_be = ed_is_big();

    // Split mode into simple_mode,opts strings
    if ((cp = strchr(mode, ','))) {
        strncpy(simple_mode, mode, cp-mode <= 100 ? cp-mode : 100);
        simple_mode[cp-mode] = '\0';
        opts = cp+1;
    } else {
        strncpy(simple_mode, mode, 100);
        opts = NULL;
    }

    if (strchr(simple_mode, 'r')) {
        if (hts_detect_format(hfile, &fp->format) < 0) goto error;
    }
    else if (strchr(simple_mode, 'w') || strchr(simple_mode, 'a')) {
        htsFormat *fmt = &fp->format;
        fp->is_write = 1;

        if (strchr(simple_mode, 'b')) fmt->format = binary_format;
        else if (strchr(simple_mode, 'c')) fmt->format = cram;
        else fmt->format = text_format;

        if (strchr(simple_mode, 'z')) fmt->compression = bgzf;
        else if (strchr(simple_mode, 'g')) fmt->compression = gzip;
        else if (strchr(simple_mode, 'u')) fmt->compression = no_compression;
        else {
            // No compression mode specified, set to the default for the format
            switch (fmt->format) {
            case binary_format: fmt->compression = bgzf; break;
            case cram: fmt->compression = custom; break;
            case text_format: fmt->compression = no_compression; break;
            default: abort();
            }
        }

        // Fill in category (if determinable; e.g. 'b' could be BAM or BCF)
        fmt->category = format_category(fmt->format);

        fmt->version.major = fmt->version.minor = -1;
        fmt->compression_level = -1;
        fmt->specific = NULL;
    }
    else goto error;

    switch (fp->format.format) {
    case binary_format:
    case bam:
    case bcf:
        fp->fp.bgzf = bgzf_hopen(hfile, simple_mode);
        if (fp->fp.bgzf == NULL) goto error;
        fp->is_bin = 1;
        break;

    case cram:
        fp->fp.cram = cram_dopen(hfile, fn, simple_mode);
        if (fp->fp.cram == NULL) goto error;
        if (!fp->is_write)
            cram_set_option(fp->fp.cram, CRAM_OPT_DECODE_MD, 1);
        fp->is_cram = 1;
        break;

    case text_format:
    case sam:
    case vcf:
        if (!fp->is_write) {
        #if KS_BGZF
            BGZF *gzfp = bgzf_hopen(hfile, simple_mode);
        #else
            // TODO Implement gzip hFILE adaptor
            hclose(hfile); // This won't work, especially for stdin
            gzFile gzfp = strcmp(fn, "-")? gzopen(fn, "rb") : gzdopen(fileno(stdin), "rb");
        #endif
            if (gzfp) fp->fp.voidp = ks_init(gzfp);
            else goto error;
        }
        else if (fp->format.compression != no_compression) {
            fp->fp.bgzf = bgzf_hopen(hfile, simple_mode);
            if (fp->fp.bgzf == NULL) goto error;
        }
        else
            fp->fp.hfile = hfile;
        break;

    default:
        goto error;
    }

    if (opts)
        hts_process_opts(fp, opts);

    return fp;

error:
    if (hts_verbose >= 2)
        fprintf(stderr, "[E::%s] fail to open file '%s'\n", __func__, fn);

    if (fp) {
        free(fp->fn);
        free(fp->fn_aux);
        free(fp);
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x1, %edi
movl $0x60, %esi
callq 0x4370
movq %rax, -0x28(%rbp)
movb $0x0, -0x2c(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x12c9a
jmp 0x13183
movq -0x18(%rbp), %rdi
callq 0x4aa0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
callq 0x13f80
movl %eax, %edx
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $0x1, %edx
shll $0x2, %edx
andl $-0x5, %ecx
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rdi
movl $0x2c, %esi
callq 0x41e0
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x12d7d
leaq -0x90(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x98(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
cmpq $0x64, %rax
jg 0x12d2b
movq -0x98(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xc8(%rbp)
jmp 0x12d39
movl $0x64, %eax
movq %rax, -0xc8(%rbp)
jmp 0x12d39
movq -0xb8(%rbp), %rsi
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rdx
callq 0x42c0
movq -0x98(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movb $0x0, -0x90(%rbp,%rax)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x12d9d
leaq -0x90(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x64, %edx
callq 0x42c0
movq $0x0, -0xa0(%rbp)
leaq -0x90(%rbp), %rdi
movl $0x72, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x12dd4
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x40, %rsi
callq 0x11cf0
cmpl $0x0, %eax
jge 0x12dcf
jmp 0x13183
jmp 0x12fc1
leaq -0x90(%rbp), %rdi
movl $0x77, %esi
callq 0x41e0
cmpq $0x0, %rax
jne 0x12e06
leaq -0x90(%rbp), %rdi
movl $0x61, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x12fba
movq -0x28(%rbp), %rax
addq $0x40, %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $-0x3, %ecx
orl $0x2, %ecx
movl %ecx, (%rax)
leaq -0x90(%rbp), %rdi
movl $0x62, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x12e4a
movq -0xa8(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0x12e81
leaq -0x90(%rbp), %rdi
movl $0x63, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x12e71
movq -0xa8(%rbp), %rax
movl $0x6, 0x4(%rax)
jmp 0x12e7f
movq -0xa8(%rbp), %rax
movl $0x2, 0x4(%rax)
jmp 0x12e81
leaq -0x90(%rbp), %rdi
movl $0x7a, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x12eab
movq -0xa8(%rbp), %rax
movl $0x2, 0xc(%rax)
jmp 0x12f68
leaq -0x90(%rbp), %rdi
movl $0x67, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x12ed5
movq -0xa8(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0x12f66
leaq -0x90(%rbp), %rdi
movl $0x75, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x12efc
movq -0xa8(%rbp), %rax
movl $0x0, 0xc(%rax)
jmp 0x12f64
movq -0xa8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0xcc(%rbp)
subl $0x1, %eax
je 0x12f2d
jmp 0x12f13
movl -0xcc(%rbp), %eax
subl $0x2, %eax
je 0x12f4d
jmp 0x12f20
movl -0xcc(%rbp), %eax
subl $0x6, %eax
je 0x12f3d
jmp 0x12f5d
movq -0xa8(%rbp), %rax
movl $0x2, 0xc(%rax)
jmp 0x12f62
movq -0xa8(%rbp), %rax
movl $0x3, 0xc(%rax)
jmp 0x12f62
movq -0xa8(%rbp), %rax
movl $0x0, 0xc(%rax)
jmp 0x12f62
callq 0x42e0
jmp 0x12f64
jmp 0x12f66
jmp 0x12f68
movq -0xa8(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x13fa0
movl %eax, %ecx
movq -0xa8(%rbp), %rax
movl %ecx, (%rax)
movq -0xa8(%rbp), %rax
movw $0xffff, 0xa(%rax)      # imm = 0xFFFF
movq -0xa8(%rbp), %rax
movw $0xffff, 0x8(%rax)      # imm = 0xFFFF
movq -0xa8(%rbp), %rax
movw $0xffff, 0x10(%rax)     # imm = 0xFFFF
movq -0xa8(%rbp), %rax
movq $0x0, 0x18(%rax)
jmp 0x12fbf
jmp 0x13183
jmp 0x12fc1
movq -0x28(%rbp), %rax
movl 0x44(%rax), %eax
movl %eax, -0xd0(%rbp)
subl $0x1, %eax
je 0x13025
jmp 0x12fd5
movl -0xd0(%rbp), %eax
addl $-0x2, %eax
subl $0x2, %eax
jb 0x130cb
jmp 0x12fe9
movl -0xd0(%rbp), %eax
subl $0x4, %eax
je 0x13025
jmp 0x12ff6
movl -0xd0(%rbp), %eax
subl $0x6, %eax
je 0x13063
jmp 0x13003
movl -0xd0(%rbp), %eax
subl $0x8, %eax
je 0x130cb
jmp 0x13014
movl -0xd0(%rbp), %eax
subl $0x9, %eax
jne 0x1315d
jmp 0x13025
movq -0x10(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x63520
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x13050
jmp 0x13183
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $-0x2, %ecx
orl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x1315f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x519f0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x13092
jmp 0x13183
movq -0x28(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x130b8
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rdi
xorl %esi, %esi
movl $0x1, %edx
movb $0x0, %al
callq 0x52a20
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $-0x9, %ecx
orl $0x8, %ecx
movl %ecx, (%rax)
jmp 0x1315f
movq -0x28(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x13119
movq -0x10(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x63520
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
je 0x13115
movq -0xb0(%rbp), %rdi
callq 0x10c30
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x13117
jmp 0x13183
jmp 0x1315b
movq -0x28(%rbp), %rax
cmpl $0x0, 0x4c(%rax)
je 0x1314d
movq -0x10(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x63520
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x1314b
jmp 0x13183
jmp 0x13159
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x1315b
jmp 0x1315f
jmp 0x13183
cmpq $0x0, -0xa0(%rbp)
je 0x13179
movq -0x28(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x14050
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x131e1
cmpl $0x2, 0x77456(%rip)     # 0x8a5e0
jl 0x131af
movq 0x76e3d(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x18(%rbp), %rcx
leaq 0x67c52(%rip), %rsi     # 0x7adf3
leaq 0x67e23(%rip), %rdx     # 0x7afcb
movb $0x0, %al
callq 0x4830
cmpq $0x0, -0x28(%rbp)
je 0x131d9
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x46a0
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nop