// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rsi, %r13
movq %rdi, %rbp
movl $0x1, %edi
movl $0x88, %esi
callq 0x4300
movq %rax, %rbx
movb $0x0, 0x94(%rsp)
testq %rax, %rax
je 0x13a57
movq %r13, %rdi
callq 0x4930
movq %rax, 0x28(%rbx)
movq %r14, %rdi
movl $0x2c, %esi
callq 0x4190
testq %rax, %rax
je 0x1378a
movq %rax, %r12
movq %rax, %r15
subq %r14, %r15
cmpq $0x64, %r15
movl $0x64, %edx
cmovlq %r15, %rdx
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x4250
movb $0x0, 0x30(%rsp,%r15)
incq %r12
jmp 0x1379f
leaq 0x30(%rsp), %rdi
movl $0x64, %edx
movq %r14, %rsi
callq 0x4250
xorl %r12d, %r12d
leaq 0x30(%rsp), %rdi
movl $0x72, %esi
callq 0x4190
testq %rax, %rax
je 0x1397f
leaq 0x48(%rbx), %rdx
movq %rbp, %rdi
movq %r13, %rsi
movq %rdx, 0x28(%rsp)
callq 0x116ab
testl %eax, %eax
js 0x139d7
movq %r12, 0x18(%rsp)
movq %r13, 0x8(%rsp)
movl $0x6, %r13d
movq %rbp, (%rsp)
movq %rbp, 0x10(%rsp)
movq %rbx, 0x20(%rsp)
movl 0x4c(%rbx), %r15d
cmpl $0x14, %r15d
je 0x13805
cmpl $0xe, %r15d
jne 0x13a36
decl %r13d
je 0x13a23
cmpl $0x14, %r15d
je 0x1384f
cmpl $0xe, %r15d
jne 0x1395a
movq %rbp, %rdi
leaq 0x30(%rsp), %rsi
callq 0x1f0b8
testq %rax, %rax
je 0x13a2e
movq %rax, %r14
cmpq 0x10(%rsp), %rbp
je 0x1395d
movq %rbp, %rdi
callq 0xf53f
jmp 0x1395d
movq 0x8(%rsp), %rdi
callq 0x41d0
movq %rax, %r14
addq $0xa, %r14
leaq 0x110(%rsp), %r15
cmpq $0x201, %r14            # imm = 0x201
jb 0x13892
cmpq $0x7ffffffe, %r14       # imm = 0x7FFFFFFE
ja 0x13a2e
movq %r14, %rdi
callq 0x4680
movq %rax, %r15
testq %rax, %rax
je 0x13a2e
movq %r15, %rdi
movq %r14, %rsi
leaq 0x82671(%rip), %r14     # 0x95f10
movq %r14, %rdx
leaq 0x81cab(%rip), %rcx     # 0x95554
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x4800
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x4190
testq %rax, %rax
leaq 0x8249c(%rip), %r8      # 0x95d6d
leaq 0x824c2(%rip), %rax     # 0x95d9a
cmoveq %rax, %r8
movl $0x66, %esi
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x4800
movq %r15, %rdi
movq %r12, %rsi
leaq 0x8312b(%rip), %rdx     # 0x96a31
movq %rbp, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0xfe44
movq %rax, %r14
leaq 0x110(%rsp), %rax
cmpq %rax, %r15
je 0x1392b
movq %r15, %rdi
callq 0x45c0
testq %r14, %r14
movq 0x20(%rsp), %rbx
je 0x13a2e
cmpq (%rsp), %rbp
jne 0x13947
movq (%rsp), %rax
orb $0x8, 0x30(%rax)
movq 0x10(%rsp), %rax
cmpq %rax, %rbp
cmoveq %r14, %rax
movq %rax, 0x10(%rsp)
jmp 0x1395d
movq %rbp, %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x116ab
movq %r14, %rbp
testl %eax, %eax
jns 0x137f1
jmp 0x13cb8
leaq 0x30(%rsp), %rdi
movl $0x77, %esi
callq 0x4190
testq %rax, %rax
jne 0x139ab
leaq 0x30(%rsp), %rdi
movl $0x61, %esi
callq 0x4190
testq %rax, %rax
je 0x13a4c
movl $0x2, (%rbx)
leaq 0x30(%rsp), %rdi
movl $0x62, %esi
callq 0x4190
testq %rax, %rax
je 0x139fd
movl $0x1, 0x4c(%rbx)
movl $0x1, %r15d
jmp 0x13acc
leaq 0x8289b(%rip), %rsi     # 0x96279
leaq 0x8289e(%rip), %rdx     # 0x96283
movl $0x1, %edi
movq %r13, %rcx
xorl %eax, %eax
callq 0x13de5
andb $-0x9, 0x30(%rbp)
jmp 0x13cf1
leaq 0x30(%rsp), %rdi
movl $0x63, %esi
callq 0x4190
testq %rax, %rax
je 0x13a79
movl $0x6, 0x4c(%rbx)
movl $0x6, %r15d
jmp 0x13acc
callq 0x4050
movl $0x28, (%rax)
movq %rbp, %r14
jmp 0x13cb8
movq %rbp, %r14
movq (%rsp), %rbp
movq 0x8(%rsp), %r13
movq 0x18(%rsp), %r12
jmp 0x13b52
callq 0x4050
movl $0x16, (%rax)
leaq 0x8281b(%rip), %rsi     # 0x96279
leaq 0x8281e(%rip), %rdx     # 0x96283
movl $0x1, %edi
movq %r13, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0x13ce8
leaq 0x30(%rsp), %rdi
movl $0x66, %esi
callq 0x4190
testq %rax, %rax
je 0x13a9c
movl $0x11, 0x4c(%rbx)
movl $0x11, %r15d
jmp 0x13acc
leaq 0x30(%rsp), %rdi
movl $0x46, %esi
callq 0x4190
testq %rax, %rax
je 0x13abf
movl $0x10, 0x4c(%rbx)
movl $0x10, %r15d
jmp 0x13acc
movl $0x2, 0x4c(%rbx)
movl $0x2, %r15d
leaq 0x30(%rsp), %rdi
movl $0x7a, %esi
callq 0x4190
movl $0x2, %ecx
testq %rax, %rax
jne 0x13b18
leaq 0x30(%rsp), %rdi
movl $0x67, %esi
callq 0x4190
movl $0x1, %ecx
testq %rax, %rax
jne 0x13b18
leaq 0x30(%rsp), %rdi
movl $0x75, %esi
callq 0x4190
xorl %ecx, %ecx
testq %rax, %rax
je 0x13c7f
movl %ecx, 0x54(%rbx)
leal -0x3(%r15), %ecx
xorl %eax, %eax
cmpl $0xf, %ecx
jae 0x13b32
movl %ecx, %eax
leaq 0x820d1(%rip), %rcx     # 0x95c00
movl (%rcx,%rax,4), %eax
movl %eax, 0x48(%rbx)
movl $0xffffffff, 0x50(%rbx) # imm = 0xFFFFFFFF
movw $0xffff, 0x58(%rbx)     # imm = 0xFFFF
movq $0x0, 0x60(%rbx)
movq %rbp, %r14
movq %rbp, 0x10(%rsp)
cmpl $0x11, %r15d
ja 0x13c69
movl $0x3a10c, %eax          # imm = 0x3A10C
btl %r15d, %eax
jae 0x13b88
cmpl $0x0, 0x54(%rbx)
je 0x13bc0
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0xab6c
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x13bb2
orb $0x10, (%rbx)
jmp 0x13bc4
movl $0x212, %eax            # imm = 0x212
btl %r15d, %eax
jae 0x13c1f
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0xab6c
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x13bb2
orb $0x11, (%rbx)
jmp 0x13bc4
movq %r13, 0x8(%rsp)
movq %rbp, (%rsp)
jmp 0x13cb8
movq %r14, 0x38(%rbx)
movq 0x10(%rsp), %r13
testq %r12, %r12
je 0x13c05
leaq 0x110(%rsp), %rdi
movq $0x0, 0x18(%rdi)
movq %r12, %rsi
callq 0x14e8c
testl %eax, %eax
jne 0x13c05
movq 0x128(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x13d55
movq %r15, %rdi
callq 0x14e5c
andb $-0x9, 0x30(%rbp)
cmpq %r13, %r14
je 0x13d0d
movq %r13, %rdi
callq 0xf53f
jmp 0x13d0d
cmpl $0x6, %r15d
jne 0x13c69
movq %rbp, (%rsp)
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x526b9
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x13cb3
movl (%rbx), %ebp
testb $0x2, %bpl
jne 0x13c5b
movq %rax, %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %eax, %eax
callq 0x5348c
orl $0x8, %ebp
movl %ebp, (%rbx)
movq (%rsp), %rbp
jmp 0x13bc4
movq %r13, 0x8(%rsp)
movq %rbp, (%rsp)
callq 0x4050
movl $0x8, (%rax)
jmp 0x13cb8
cmpl $0x5, %r15d
jle 0x13d22
cmpl $0x11, %r15d
je 0x13d3f
cmpl $0x10, %r15d
je 0x13d3f
cmpl $0x6, %r15d
jne 0x13d50
movl $0x3, 0x54(%rbx)
jmp 0x13d46
movq %r13, 0x8(%rsp)
leaq 0x825ba(%rip), %rsi     # 0x96279
leaq 0x825bd(%rip), %rdx     # 0x96283
movl $0x1, %edi
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x13de5
movq (%rsp), %rbp
cmpq %rbp, %r14
je 0x13ce8
movq %r14, %rdi
callq 0xf53f
andb $-0x9, 0x30(%rbp)
testq %rbx, %rbx
je 0x13d0b
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
movl $0x2, %eax
cmpl $0x1, %r15d
je 0x13d35
cmpl $0x2, %r15d
jne 0x13d50
xorl %eax, %eax
movl %eax, 0x54(%rbx)
xorl %eax, %eax
jmp 0x13b32
movl $0x0, 0x54(%rbx)
movl $0x1, %eax
jmp 0x13b32
callq 0x4270