// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
pushq $0x1
popq %rdi
pushq $0x60
popq %rsi
callq 0x4370
movq %rax, %rbx
movb $0x0, 0x64(%rsp)
testq %rax, %rax
je 0xc8e9
movq %r14, %rdi
callq 0x49a0
movq %rax, 0x28(%rbx)
pushq $0x2c
popq %rsi
movq %r13, %rdi
callq 0x41e0
testq %rax, %rax
je 0xc72a
movq %rax, %r15
movq %rax, %rbp
subq %r13, %rbp
cmpq $0x64, %rbp
pushq $0x64
popq %rdx
cmovlq %rbp, %rdx
movq %rsp, %rdi
movq %r13, %rsi
callq 0x42c0
movb $0x0, (%rsp,%rbp)
incq %r15
jmp 0xc73b
movq %rsp, %rdi
pushq $0x64
popq %rdx
movq %r13, %rsi
callq 0x42c0
xorl %r15d, %r15d
movq %rsp, %rdi
pushq $0x72
popq %rsi
callq 0x41e0
testq %rax, %rax
je 0xc768
leaq 0x40(%rbx), %rsi
movq %r12, %rdi
callq 0xbf38
testl %eax, %eax
js 0xc8e9
movl 0x44(%rbx), %r13d
jmp 0xc82b
movq %rsp, %rdi
pushq $0x77
popq %rsi
callq 0x41e0
testq %rax, %rax
jne 0xc78c
movq %rsp, %rdi
pushq $0x61
popq %rsi
callq 0x41e0
testq %rax, %rax
je 0xc8e9
movl $0x2, (%rbx)
movq %rsp, %rdi
pushq $0x62
popq %rsi
callq 0x41e0
testq %rax, %rax
je 0xc7af
pushq $0x1
popq %r13
movl $0x1, 0x44(%rbx)
jmp 0xc7d5
movq %rsp, %rdi
pushq $0x63
popq %rsi
callq 0x41e0
testq %rax, %rax
je 0xc7ca
movl $0x6, 0x44(%rbx)
pushq $0x6
jmp 0xc7d3
movl $0x2, 0x44(%rbx)
pushq $0x2
popq %r13
movq %rsp, %rdi
pushq $0x7a
popq %rsi
callq 0x41e0
testq %rax, %rax
je 0xc7e9
pushq $0x2
jmp 0xc7ff
movq %rsp, %rdi
pushq $0x67
popq %rsi
callq 0x41e0
testq %rax, %rax
je 0xc988
pushq $0x1
popq %rcx
movl %ecx, 0x4c(%rbx)
leal -0x3(%r13), %ecx
xorl %eax, %eax
cmpl $0x4, %ecx
jae 0xc81a
movl %ecx, %eax
leaq 0x37199(%rip), %rcx     # 0x439b0
movl (%rcx,%rax,4), %eax
movl %eax, 0x40(%rbx)
orl $-0x1, 0x48(%rbx)
orw $-0x1, 0x50(%rbx)
andq $0x0, 0x58(%rbx)
cmpl $0x9, %r13d
ja 0xc8e9
movl $0x10c, %eax            # imm = 0x10C
btl %r13d, %eax
jb 0xc86b
movl $0x212, %eax            # imm = 0x212
btl %r13d, %eax
jae 0xc891
movq %rsp, %rsi
movq %r12, %rdi
callq 0x3632d
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0xc8e9
orb $0x1, (%rbx)
jmp 0xc950
testb $0x2, (%rbx)
jne 0xc8cf
movq %rsp, %rsi
movq %r12, %rdi
callq 0x3632d
testq %rax, %rax
je 0xc8e9
movq %rax, %rdi
callq 0xb638
movq %rax, 0x38(%rbx)
jmp 0xc950
cmpl $0x6, %r13d
jne 0xc8e9
movq %rsp, %rdx
movq %r12, %rdi
movq %r14, %rsi
callq 0x2d49e
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0xc8e9
movl (%rbx), %ebp
testb $0x2, %bpl
jne 0xc8c5
pushq $0x1
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x2dcd2
orl $0x8, %ebp
movl %ebp, (%rbx)
jmp 0xc950
cmpl $0x0, 0x4c(%rbx)
je 0xc94c
movq %rsp, %rsi
movq %r12, %rdi
callq 0x3632d
movq %rax, 0x38(%rbx)
testq %rax, %rax
jne 0xc950
cmpl $0x2, 0x4ac58(%rip)     # 0x57548
jge 0xc928
testq %rbx, %rbx
je 0xc911
movq 0x28(%rbx), %rdi
callq 0x4600
movq 0x30(%rbx), %rdi
callq 0x4600
movq %rbx, %rdi
callq 0x4600
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4a6a1(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x37373(%rip), %rsi     # 0x43cac
leaq 0x37544(%rip), %rdx     # 0x43e84
movq %r14, %rcx
xorl %eax, %eax
callq 0x4770
jmp 0xc8f2
movq %r12, 0x38(%rbx)
testq %r15, %r15
je 0xc913
leaq 0x68(%rsp), %rdi
andq $0x0, 0x18(%rdi)
movq %r15, %rsi
callq 0xcfbb
testl %eax, %eax
jne 0xc913
movq 0x80(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xc9d5
movq %r14, %rdi
callq 0xcf8e
jmp 0xc913
movq %rsp, %rdi
pushq $0x75
popq %rsi
callq 0x41e0
xorl %ecx, %ecx
testq %rax, %rax
jne 0xc800
cmpl $0x1, %r13d
je 0xc9c3
cmpl $0x2, %r13d
je 0xc9bf
cmpl $0x6, %r13d
jne 0xc9d0
movl $0x3, 0x4c(%rbx)
pushq $0x1
popq %rax
jmp 0xc81a
xorl %eax, %eax
jmp 0xc9c6
pushq $0x2
popq %rax
movl %eax, 0x4c(%rbx)
xorl %eax, %eax
jmp 0xc81a
callq 0x42f0