// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x1, %edi
movl $0x88, %esi
callq 0x42f0
movq %rax, -0x38(%rbp)
movb $0x0, -0x3c(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x1a79a
jmp 0x1ae96
movq -0x18(%rbp), %rdi
callq 0x4920
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x28(%rax)
callq 0x1cf00
movl %eax, %edx
movq -0x38(%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 0x4170
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x1a87d
leaq -0xa0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xa8(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
cmpq $0x64, %rax
jg 0x1a82b
movq -0xa8(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xe8(%rbp)
jmp 0x1a839
movl $0x64, %eax
movq %rax, -0xe8(%rbp)
jmp 0x1a839
movq -0xd8(%rbp), %rsi
movq -0xe0(%rbp), %rdi
movq -0xe8(%rbp), %rdx
callq 0x4240
movq -0xa8(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movb $0x0, -0xa0(%rbp,%rax)
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x1a89d
leaq -0xa0(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x64, %edx
callq 0x4240
movq $0x0, -0xb0(%rbp)
leaq -0xa0(%rbp), %rdi
movl $0x72, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1aa45
movl $0x5, -0xb4(%rbp)
movl $0x0, -0xb8(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rdx
addq $0x48, %rdx
callq 0x18450
cmpl $0x0, %eax
jge 0x1a8eb
jmp 0x1ae96
jmp 0x1a8ed
movq -0x38(%rbp), %rcx
movb $0x1, %al
cmpl $0xe, 0x4c(%rcx)
movb %al, -0xe9(%rbp)
je 0x1a910
movq -0x38(%rbp), %rax
cmpl $0x14, 0x4c(%rax)
sete %al
movb %al, -0xe9(%rbp)
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0x1a91f
jmp 0x1aa40
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
cmpl $0x5, %eax
jle 0x1a943
callq 0x4050
movl $0x28, (%rax)
jmp 0x1ae96
movq -0x38(%rbp), %rax
cmpl $0xe, 0x4c(%rax)
jne 0x1a996
movq -0x10(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2cc40
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
jne 0x1a973
jmp 0x1ae96
movq -0x10(%rbp), %rax
cmpq -0x30(%rbp), %rax
je 0x1a986
movq -0x10(%rbp), %rdi
callq 0x149d0
movq -0xc0(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x1aa1c
movq -0x38(%rbp), %rax
cmpl $0x14, 0x4c(%rax)
jne 0x1aa1a
movq -0x10(%rbp), %rax
cmpq -0x28(%rbp), %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0xc4(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x30(%rbp), %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0xc8(%rbp)
movq -0x18(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movq -0x38(%rbp), %rcx
leaq -0x10(%rbp), %rdx
callq 0x1cf20
cmpl $0x0, %eax
jge 0x1a9ee
jmp 0x1ae96
cmpl $0x0, -0xc4(%rbp)
je 0x1aa07
movq -0x28(%rbp), %rax
movb 0x30(%rax), %cl
andb $-0x9, %cl
orb $0x8, %cl
movb %cl, 0x30(%rax)
cmpl $0x0, -0xc8(%rbp)
je 0x1aa18
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x1aa1a
jmp 0x1aa1c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rdx
addq $0x48, %rdx
callq 0x18450
cmpl $0x0, %eax
jge 0x1aa3b
jmp 0x1ae96
jmp 0x1a8ed
jmp 0x1acb5
leaq -0xa0(%rbp), %rdi
movl $0x77, %esi
callq 0x4170
cmpq $0x0, %rax
jne 0x1aa77
leaq -0xa0(%rbp), %rdi
movl $0x61, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1aca3
movq -0x38(%rbp), %rax
addq $0x48, %rax
movq %rax, -0xd0(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %ecx
andl $-0x3, %ecx
orl $0x2, %ecx
movl %ecx, (%rax)
leaq -0xa0(%rbp), %rdi
movl $0x62, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1aabe
movq -0xd0(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0x1ab47
leaq -0xa0(%rbp), %rdi
movl $0x63, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1aae5
movq -0xd0(%rbp), %rax
movl $0x6, 0x4(%rax)
jmp 0x1ab45
leaq -0xa0(%rbp), %rdi
movl $0x66, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1ab0c
movq -0xd0(%rbp), %rax
movl $0x11, 0x4(%rax)
jmp 0x1ab43
leaq -0xa0(%rbp), %rdi
movl $0x46, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1ab33
movq -0xd0(%rbp), %rax
movl $0x10, 0x4(%rax)
jmp 0x1ab41
movq -0xd0(%rbp), %rax
movl $0x2, 0x4(%rax)
jmp 0x1ab43
jmp 0x1ab45
jmp 0x1ab47
leaq -0xa0(%rbp), %rdi
movl $0x7a, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1ab71
movq -0xd0(%rbp), %rax
movl $0x2, 0xc(%rax)
jmp 0x1ac51
leaq -0xa0(%rbp), %rdi
movl $0x67, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1ab9b
movq -0xd0(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0x1ac4f
leaq -0xa0(%rbp), %rdi
movl $0x75, %esi
callq 0x4170
cmpq $0x0, %rax
je 0x1abc5
movq -0xd0(%rbp), %rax
movl $0x0, 0xc(%rax)
jmp 0x1ac4d
movq -0xd0(%rbp), %rax
movl 0x4(%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xf8(%rbp)
subl $0x10, %eax
ja 0x1ac46
movq -0xf8(%rbp), %rax
leaq 0xd4daf(%rip), %rcx     # 0xef99c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xd0(%rbp), %rax
movl $0x2, 0xc(%rax)
jmp 0x1ac4b
movq -0xd0(%rbp), %rax
movl $0x3, 0xc(%rax)
jmp 0x1ac4b
movq -0xd0(%rbp), %rax
movl $0x0, 0xc(%rax)
jmp 0x1ac4b
movq -0xd0(%rbp), %rax
movl $0x0, 0xc(%rax)
jmp 0x1ac4b
movq -0xd0(%rbp), %rax
movl $0x0, 0xc(%rax)
jmp 0x1ac4b
callq 0x4260
jmp 0x1ac4d
jmp 0x1ac4f
jmp 0x1ac51
movq -0xd0(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x1d100
movl %eax, %ecx
movq -0xd0(%rbp), %rax
movl %ecx, (%rax)
movq -0xd0(%rbp), %rax
movw $0xffff, 0xa(%rax)      # imm = 0xFFFF
movq -0xd0(%rbp), %rax
movw $0xffff, 0x8(%rax)      # imm = 0xFFFF
movq -0xd0(%rbp), %rax
movw $0xffff, 0x10(%rax)     # imm = 0xFFFF
movq -0xd0(%rbp), %rax
movq $0x0, 0x18(%rax)
jmp 0x1acb3
callq 0x4050
movl $0x16, (%rax)
jmp 0x1ae96
jmp 0x1acb5
movq -0x38(%rbp), %rax
movl 0x4c(%rax), %eax
movl %eax, -0xfc(%rbp)
subl $0x1, %eax
je 0x1ad41
jmp 0x1acc9
movl -0xfc(%rbp), %eax
addl $-0x2, %eax
subl $0x2, %eax
jb 0x1adf2
jmp 0x1acdd
movl -0xfc(%rbp), %eax
subl $0x4, %eax
je 0x1ad41
jmp 0x1acea
movl -0xfc(%rbp), %eax
subl $0x6, %eax
je 0x1ad8d
jmp 0x1acfb
movl -0xfc(%rbp), %eax
subl $0x8, %eax
je 0x1adf2
jmp 0x1ad0c
movl -0xfc(%rbp), %eax
subl $0x9, %eax
je 0x1ad41
jmp 0x1ad19
movl -0xfc(%rbp), %eax
subl $0xd, %eax
je 0x1adf2
jmp 0x1ad2a
movl -0xfc(%rbp), %eax
addl $-0xf, %eax
subl $0x3, %eax
jb 0x1adf2
jmp 0x1ae42
movq -0x10(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xc880
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x1ad6c
jmp 0x1ae96
movq -0x38(%rbp), %rax
movl (%rax), %ecx
andl $-0x11, %ecx
orl $0x10, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rax
movl (%rax), %ecx
andl $-0x2, %ecx
orl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x1ae4f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0x78bd0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x1adbc
jmp 0x1ae96
movq -0x38(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x1ade2
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rdi
xorl %esi, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movb $0x0, %al
callq 0x79f60
movq -0x38(%rbp), %rax
movl (%rax), %ecx
andl $-0x9, %ecx
orl $0x8, %ecx
movl %ecx, (%rax)
jmp 0x1ae4f
movq -0x38(%rbp), %rax
cmpl $0x0, 0x54(%rax)
je 0x1ae34
movq -0x10(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xc880
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x1ae24
jmp 0x1ae96
movq -0x38(%rbp), %rax
movl (%rax), %ecx
andl $-0x11, %ecx
orl $0x10, %ecx
movl %ecx, (%rax)
jmp 0x1ae40
movq -0x10(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x1ae4f
callq 0x4050
movl $0x8, (%rax)
jmp 0x1ae96
cmpq $0x0, -0xb0(%rbp)
je 0x1ae69
movq -0x38(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x1d200
movq -0x28(%rbp), %rax
movb 0x30(%rax), %cl
andb $-0x9, %cl
orb $0x0, %cl
movb %cl, 0x30(%rax)
movq -0x10(%rbp), %rax
cmpq -0x30(%rbp), %rax
je 0x1ae8c
movq -0x30(%rbp), %rdi
callq 0x149d0
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x1af09
movq -0x18(%rbp), %rcx
movl $0x1, %edi
leaq 0xd560b(%rip), %rsi     # 0xf04b1
leaq 0xd560e(%rip), %rdx     # 0xf04bb
movb $0x0, %al
callq 0x1b030
movq -0x10(%rbp), %rax
cmpq -0x28(%rbp), %rax
je 0x1aec7
movq -0x10(%rbp), %rdi
callq 0x149d0
movq -0x28(%rbp), %rax
movb 0x30(%rax), %cl
andb $-0x9, %cl
orb $0x0, %cl
movb %cl, 0x30(%rax)
cmpq $0x0, -0x38(%rbp)
je 0x1af01
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x45a0
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x45a0
movq -0x38(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)