// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdx, %r13
movq %rsi, 0x8(%rsp)
movq %rdi, %r15
movl $0x1, %edi
movl $0x60, %esi
callq 0x4360
movq %rax, %rbx
movb $0x0, 0x74(%rsp)
testq %rax, %rax
je 0xf561
movq 0x8(%rsp), %rdi
callq 0x49c0
movq %rax, 0x28(%rbx)
movl (%rbx), %ebp
andl $-0x5, %ebp
movl %ebp, (%rbx)
movq %r13, %rdi
movl $0x2c, %esi
callq 0x4200
testq %rax, %rax
je 0xf335
movq %rax, %r12
movq %rax, %r14
subq %r13, %r14
cmpq $0x64, %r14
movl $0x64, %edx
cmovlq %r14, %rdx
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x42b0
movb $0x0, 0x10(%rsp,%r14)
incq %r12
jmp 0xf34a
leaq 0x10(%rsp), %rdi
movl $0x64, %edx
movq %r13, %rsi
callq 0x42b0
xorl %r12d, %r12d
leaq 0x10(%rsp), %rdi
movl $0x72, %esi
callq 0x4200
testq %rax, %rax
je 0xf377
leaq 0x40(%rbx), %rsi
movq %r15, %rdi
callq 0xe35b
testl %eax, %eax
jns 0xf469
jmp 0xf561
leaq 0x10(%rsp), %rdi
movl $0x77, %esi
callq 0x4200
testq %rax, %rax
jne 0xf3a3
leaq 0x10(%rsp), %rdi
movl $0x61, %esi
callq 0x4200
testq %rax, %rax
je 0xf561
orl $0x2, %ebp
movl %ebp, (%rbx)
leaq 0x10(%rsp), %rdi
movl $0x62, %esi
callq 0x4200
testq %rax, %rax
je 0xf3c5
movl $0x1, 0x44(%rbx)
jmp 0xf3e9
leaq 0x10(%rsp), %rdi
movl $0x63, %esi
callq 0x4200
testq %rax, %rax
je 0xf3e2
movl $0x6, 0x44(%rbx)
jmp 0xf3e9
movl $0x2, 0x44(%rbx)
leaq 0x10(%rsp), %rdi
movl $0x7a, %esi
callq 0x4200
movl $0x2, %ecx
testq %rax, %rax
jne 0xf435
leaq 0x10(%rsp), %rdi
movl $0x67, %esi
callq 0x4200
movl $0x1, %ecx
testq %rax, %rax
jne 0xf435
leaq 0x10(%rsp), %rdi
movl $0x75, %esi
callq 0x4200
xorl %ecx, %ecx
testq %rax, %rax
je 0xf608
movl %ecx, 0x4c(%rbx)
movl 0x44(%rbx), %eax
addl $-0x3, %eax
xorl %ecx, %ecx
cmpl $0xa, %eax
ja 0xf451
movl %eax, %eax
leaq 0x436c2(%rip), %rcx     # 0x52b10
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x40(%rbx)
movl $0xffffffff, 0x48(%rbx) # imm = 0xFFFFFFFF
movw $0xffff, 0x50(%rbx)     # imm = 0xFFFF
movq $0x0, 0x58(%rbx)
movl 0x44(%rbx), %eax
cmpl $0x9, %eax
ja 0xf561
movl $0x10c, %ecx            # imm = 0x10C
btl %eax, %ecx
jb 0xf4ab
movl $0x212, %ecx            # imm = 0x212
btl %eax, %ecx
jae 0xf505
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x43ba6
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0xf561
orb $0x1, (%rbx)
jmp 0xf5a4
testb $0x2, (%rbx)
jne 0xf545
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x43ba6
testq %rax, %rax
je 0xf561
movq %rax, %r14
movl $0x1, %edi
movl $0x28, %esi
callq 0x4360
movq %rax, %r15
movq %r14, 0x18(%rax)
movl 0x8(%rax), %eax
andl $0x3, %eax
orl $0x40000, %eax          # imm = 0x40000
movl %eax, 0x8(%r15)
movl $0x10000, %edi          # imm = 0x10000
callq 0x4700
movq %rax, 0x20(%r15)
jmp 0xf5a0
cmpl $0x6, %eax
jne 0xf561
leaq 0x10(%rsp), %rdx
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x38c45
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0xf561
movl (%rbx), %ebp
testb $0x2, %bpl
jne 0xf53e
movq %rax, %rdi
xorl %esi, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0x3956d
orl $0x8, %ebp
movl %ebp, (%rbx)
jmp 0xf5a4
cmpl $0x0, 0x4c(%rbx)
je 0xf5a0
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x43ba6
movq %rax, 0x38(%rbx)
testq %rax, %rax
jne 0xf5a4
cmpl $0x2, 0x54fe8(%rip)     # 0x64550
jge 0xf5df
testq %rbx, %rbx
je 0xf589
movq 0x28(%rbx), %rdi
callq 0x4610
movq 0x30(%rbx), %rdi
callq 0x4610
movq %rbx, %rdi
callq 0x4610
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x38(%rbx)
testq %r12, %r12
je 0xf58b
leaq 0x78(%rsp), %rdi
movq $0x0, 0x18(%rdi)
movq %r12, %rsi
callq 0xfc1a
testl %eax, %eax
jne 0xf58b
movq 0x90(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf639
movq %r14, %rdi
callq 0xfbea
jmp 0xf58b
movq 0x549e2(%rip), %rax     # 0x63fc8
movq (%rax), %rdi
leaq 0x4364a(%rip), %rsi     # 0x52c3a
leaq 0x4381b(%rip), %rdx     # 0x52e12
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x4770
jmp 0xf56a
movl 0x44(%rbx), %eax
movl $0x2, %ecx
cmpl $0x1, %eax
je 0xf435
cmpl $0x2, %eax
je 0xf62d
cmpl $0x6, %eax
jne 0xf634
movl $0x3, %ecx
jmp 0xf435
xorl %ecx, %ecx
jmp 0xf435
callq 0x42e0