// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/hts.c
// Function: hts_hopen
htsFile *hts_hopen(hFILE *hfile, const char *fn, const char *mode)
{
    hFILE *hfile_orig = hfile;
    hFILE *hfile_cleanup = hfile;
    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')) {
        const int max_loops = 5; // Should be plenty
        int loops = 0;
        if (hts_detect_format2(hfile, fn, &fp->format) < 0) goto error;

        // Deal with formats that re-direct an underlying file via a plug-in.
        // Loops as we may have crypt4gh served via htsget, or
        // crypt4gh-in-crypt4gh.

        while (fp->format.format == htsget ||
               fp->format.format == hts_crypt4gh_format) {
            // Ensure we don't get stuck in an endless redirect loop
            if (++loops > max_loops) {
                errno = ELOOP;
                goto error;
            }

            if (fp->format.format == htsget) {
                hFILE *hfile2 = hopen_htsget_redirect(hfile, simple_mode);
                if (hfile2 == NULL) goto error;

                if (hfile != hfile_cleanup) {
                    // Close the result of an earlier redirection
                    hclose_abruptly(hfile);
                }

                hfile = hfile2;
            }
            else if (fp->format.format == hts_crypt4gh_format) {
                int should_preserve = (hfile == hfile_orig);
                int update_cleanup = (hfile == hfile_cleanup);
                if (hts_crypt4gh_redirect(fn, simple_mode, &hfile, fp) < 0)
                    goto error;
                if (should_preserve) {
                    // The original hFILE is now contained in a crypt4gh
                    // wrapper.  Should we need to close the wrapper due
                    // to a later error, we need to prevent the wrapped
                    // handle from being closed as the caller will see
                    // this function return NULL and try to clean up itself.
                    hfile_orig->preserve = 1;
                }
                if (update_cleanup) {
                    // Update handle to close at the end if redirected by htsget
                    hfile_cleanup = hfile;
                }
            }

            // Re-detect format against the result of the redirection
            if (hts_detect_format2(hfile, fn, &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 if (strchr(simple_mode, 'f')) fmt->format = fastq_format;
        else if (strchr(simple_mode, 'F')) fmt->format = fasta_format;
        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 fastq_format: fmt->compression = no_compression; break;
            case fasta_format: fmt->compression = no_compression; 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 { errno = EINVAL; 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 = fp->is_bgzf = 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); // auto
        fp->is_cram = 1;
        break;

    case empty_format:
    case text_format:
    case bed:
    case fasta_format:
    case fastq_format:
    case sam:
    case vcf:
        if (fp->format.compression != no_compression) {
            fp->fp.bgzf = bgzf_hopen(hfile, simple_mode);
            if (fp->fp.bgzf == NULL) goto error;
            fp->is_bgzf = 1;
        }
        else
            fp->fp.hfile = hfile;
        break;

    default:
        errno = EFTYPE;
        goto error;
    }

    if (opts)
        hts_process_opts(fp, opts);

    // Allow original file to close if it was preserved earlier by crypt4gh
    hfile_orig->preserve = 0;

    // If redirecting via htsget, close the original hFILE now (pedantically
    // we would instead close it in hts_close(), but this a simplifying
    // optimisation)
    if (hfile != hfile_cleanup) hclose_abruptly(hfile_cleanup);

    return fp;

error:
    hts_log_error("Failed to open file %s", fn);

    // If redirecting, close the failed redirection hFILE that we have opened
    if (hfile != hfile_orig) hclose_abruptly(hfile);
    hfile_orig->preserve = 0; // Allow caller to close the original hfile

    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 $0x318, %rsp            # imm = 0x318
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
movl $0x1, %edi
movl $0x88, %esi
callq 0x4300
movq %rax, %rbx
movb $0x0, 0x94(%rsp)
movq %r13, %r14
testq %rax, %rax
je 0x1427e
movq %r15, %rdi
callq 0x4930
movq %rax, 0x28(%rbx)
movl (%rbx), %ebp
andl $-0x5, %ebp
movl %ebp, (%rbx)
movq %r12, %rdi
movl $0x2c, %esi
callq 0x4190
testq %rax, %rax
movq %rbx, (%rsp)
je 0x13d6d
movq %rax, %rbx
subq %r12, %rbx
cmpq $0x64, %rbx
movl $0x64, %edx
cmovlq %rbx, %rdx
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %rax, %r14
callq 0x4250
movb $0x0, 0x30(%rsp,%rbx)
movq (%rsp), %rbx
incq %r14
movq %r14, 0x20(%rsp)
jmp 0x13d88
leaq 0x30(%rsp), %rdi
movl $0x64, %edx
movq %r12, %rsi
callq 0x4250
movq $0x0, 0x20(%rsp)
leaq 0x30(%rsp), %rdi
movl $0x72, %esi
callq 0x4190
testq %rax, %rax
je 0x13f98
leaq 0x48(%rbx), %rdx
movq %r13, %rdi
movq %r15, %rsi
movq %rdx, 0x28(%rsp)
callq 0x11915
testl %eax, %eax
movq %r15, 0x10(%rsp)
js 0x13fe9
movl $0x6, %r12d
movq %r13, %rbp
movq %r13, 0x8(%rsp)
movq %r13, 0x18(%rsp)
movl 0x4c(%rbx), %eax
cmpl $0x14, %eax
je 0x13de5
cmpl $0xe, %eax
jne 0x1402c
decl %r12d
je 0x13ff9
cmpl $0x14, %eax
je 0x13e28
cmpl $0xe, %eax
jne 0x13f79
movq %rbp, %rdi
leaq 0x30(%rsp), %rsi
callq 0x1f8dc
movq %rax, %r13
testq %rax, %rax
je 0x13e5e
xorl %r15d, %r15d
cmpq 0x8(%rsp), %rbp
je 0x13e23
movq %rbp, %rdi
callq 0xf730
movq %r13, %rbp
jmp 0x13e64
movq %r15, %rdi
callq 0x41d0
movq %rax, %r15
addq $0xa, %r15
leaq 0x110(%rsp), %r13
cmpq $0x201, %r15            # imm = 0x201
jb 0x13e91
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq $0x7ffffffe, %r15       # imm = 0x7FFFFFFE
jbe 0x13e7a
movq %rbp, %r14
jmp 0x13f36
movl $0x2, %r15d
movq %rbp, %r14
testq %r13, %r13
movq 0x18(%rsp), %r13
jne 0x13f79
jmp 0x14032
movq %r15, %rdi
callq 0x4680
movq %rax, %r13
movq %rbp, %r14
testq %rax, %rax
je 0x13f36
movq %r13, %rdi
movq %r15, %rsi
leaq 0x84fd2(%rip), %rdx     # 0x98e70
leaq 0x8466f(%rip), %rcx     # 0x98514
movq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x4800
leaq 0x30(%rsp), %rdi
movl $0x3a, %esi
callq 0x4190
leaq 0x84e33(%rip), %r8      # 0x98cfa
testq %rax, %rax
je 0x13ed3
leaq 0x84dfa(%rip), %r8      # 0x98ccd
movl $0x66, %esi
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x84f86(%rip), %rdx     # 0x98e70
leaq 0x30(%rsp), %rcx
xorl %eax, %eax
callq 0x4800
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x85a8e(%rip), %rdx     # 0x99991
movq %rbp, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x10014
movq %rax, %r14
xorl %ebx, %ebx
testq %rax, %rax
sete %bl
cmoveq %rbp, %r14
negl %ebx
leaq 0x110(%rsp), %rax
cmpq %rax, %r13
je 0x13f36
movq %r13, %rdi
callq 0x45c0
testl %ebx, %ebx
js 0x13f5f
movq 0x18(%rsp), %r13
cmpq %r13, %rbp
jne 0x13f49
orb $0x8, 0x30(%r13)
movq 0x8(%rsp), %rax
cmpq %rax, %rbp
cmoveq %r14, %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
jmp 0x13f6a
movl $0x2, %r15d
movq 0x18(%rsp), %r13
movq %r14, %rbp
testl %ebx, %ebx
movq (%rsp), %rbx
js 0x14032
movq %rbp, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
movq 0x28(%rsp), %rdx
callq 0x11915
testl %eax, %eax
jns 0x13dd4
jmp 0x14004
leaq 0x30(%rsp), %rdi
movl $0x77, %esi
callq 0x4190
testq %rax, %rax
jne 0x13fc4
leaq 0x30(%rsp), %rdi
movl $0x61, %esi
callq 0x4190
testq %rax, %rax
je 0x1405a
orl $0x2, %ebp
movl %ebp, (%rbx)
leaq 0x30(%rsp), %rdi
movl $0x62, %esi
callq 0x4190
testq %rax, %rax
je 0x1400f
movl $0x1, 0x4c(%rbx)
jmp 0x140a8
movq %r13, %r14
movl $0x2, %r15d
movq %r13, 0x8(%rsp)
jmp 0x14032
callq 0x4050
movl $0x28, (%rax)
movq %rbp, %r14
movl $0x2, %r15d
jmp 0x14032
leaq 0x30(%rsp), %rdi
movl $0x63, %esi
callq 0x4190
testq %rax, %rax
je 0x14067
movl $0x6, 0x4c(%rbx)
jmp 0x140a8
movq %rbp, %r14
xorl %r15d, %r15d
testl %r15d, %r15d
je 0x1404b
cmpl $0x2, %r15d
movq 0x10(%rsp), %r15
je 0x1427e
jmp 0x142ce
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r12
jmp 0x14161
movl $0x16, %ebx
movq %r13, %r14
jmp 0x14273
leaq 0x30(%rsp), %rdi
movl $0x66, %esi
callq 0x4190
testq %rax, %rax
je 0x14084
movl $0x11, 0x4c(%rbx)
jmp 0x140a8
leaq 0x30(%rsp), %rdi
movl $0x46, %esi
callq 0x4190
testq %rax, %rax
je 0x140a1
movl $0x10, 0x4c(%rbx)
jmp 0x140a8
movl $0x2, 0x4c(%rbx)
leaq 0x30(%rsp), %rdi
movl $0x7a, %esi
callq 0x4190
movl $0x2, %ecx
testq %rax, %rax
jne 0x14127
leaq 0x30(%rsp), %rdi
movl $0x67, %esi
callq 0x4190
movl $0x1, %ecx
testq %rax, %rax
jne 0x14127
leaq 0x30(%rsp), %rdi
movl $0x75, %esi
callq 0x4190
xorl %ecx, %ecx
testq %rax, %rax
jne 0x14127
movl 0x4c(%rbx), %eax
cmpl $0x5, %eax
jle 0x14112
cmpl $0x11, %eax
je 0x14125
cmpl $0x10, %eax
je 0x14125
cmpl $0x6, %eax
jne 0x142e3
movl $0x3, %ecx
jmp 0x14127
movl $0x2, %ecx
cmpl $0x1, %eax
je 0x14127
cmpl $0x2, %eax
jne 0x142e3
xorl %ecx, %ecx
movl %ecx, 0x54(%rbx)
movl 0x4c(%rbx), %eax
addl $-0x3, %eax
xorl %ecx, %ecx
cmpl $0x12, %eax
ja 0x14143
movl %eax, %eax
leaq 0x84a10(%rip), %rcx     # 0x98b50
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x48(%rbx)
movl $0xffffffff, 0x50(%rbx) # imm = 0xFFFFFFFF
movw $0xffff, 0x58(%rbx)     # imm = 0xFFFF
movq $0x0, 0x60(%rbx)
movq %r13, %r14
movq %r13, %r12
movl 0x4c(%rbx), %eax
movl $0x8, %ebx
cmpl $0x11, %eax
ja 0x14273
movl $0x3a10c, %ecx          # imm = 0x3A10C
btl %eax, %ecx
jae 0x141a5
movq (%rsp), %rbx
cmpl $0x0, 0x54(%rbx)
je 0x141d6
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0xab5a
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x1427e
orb $0x10, (%rbx)
jmp 0x141da
movl $0x212, %ecx            # imm = 0x212
btl %eax, %ecx
jae 0x14233
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0xab5a
movq (%rsp), %rbx
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x1427e
orb $0x11, (%rbx)
jmp 0x141da
movq %r14, 0x38(%rbx)
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x14218
leaq 0x110(%rsp), %rdi
movq $0x0, 0x18(%rdi)
callq 0x15429
testl %eax, %eax
jne 0x14218
movq 0x128(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x142e8
movq %r15, %rdi
callq 0x153f9
andb $-0x9, 0x30(%r13)
cmpq %r12, %r14
je 0x142ce
movq %r12, %rdi
callq 0xf730
jmp 0x142ce
cmpl $0x6, %eax
jne 0x14273
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x55208
movq (%rsp), %rbx
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x1427e
testb $0x2, (%rbx)
jne 0x1426b
movq %rax, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x55e0e
orb $0x8, (%rbx)
jmp 0x141da
callq 0x4050
movl %ebx, (%rax)
movq (%rsp), %rbx
leaq 0x84f54(%rip), %rsi     # 0x991d9
leaq 0x84f57(%rip), %rdx     # 0x991e3
movl $0x1, %edi
movq %r15, %rcx
xorl %eax, %eax
callq 0x14378
cmpq %r13, %r14
je 0x142a8
movq %r14, %rdi
callq 0xf730
andb $-0x9, 0x30(%r13)
testq %rbx, %rbx
je 0x142cc
movq 0x28(%rbx), %rdi
callq 0x45c0
movq 0x30(%rbx), %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4270