// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/hts.c
// Function: hts_open_format
htsFile *hts_open_format(const char *fn, const char *mode, const htsFormat *fmt)
{
    char smode[101], *cp, *cp2, *mode_c, *uncomp = NULL;
    htsFile *fp = NULL;
    hFILE *hfile = NULL;
    char fmt_code = '\0';
    // see enum htsExactFormat in htslib/hts.h
    const char format_to_mode[] = "\0g\0\0b\0c\0\0b\0g\0\0\0\0\0Ff\0\0";

    strncpy(smode, mode, 99);
    smode[99]=0;
    if ((cp = strchr(smode, ',')))
        *cp = '\0';

    // Migrate format code (b or c) to the end of the smode buffer.
    for (cp2 = cp = smode; *cp; cp++) {
        if (*cp == 'b')
            fmt_code = 'b';
        else if (*cp == 'c')
            fmt_code = 'c';
        else {
            *cp2++ = *cp;
            // Cache the uncompress flag 'u' pos if present
            if (!uncomp && (*cp == 'u')) {
                uncomp = cp2 - 1;
            }
        }
    }
    mode_c = cp2;
    *cp2++ = fmt_code;
    *cp2++ = 0;

    // Set or reset the format code if opts->format is used
    if (fmt && fmt->format > unknown_format
        && fmt->format < sizeof(format_to_mode)) {
        *mode_c = format_to_mode[fmt->format];
    }

    // Uncompressed bam/bcf is not supported, change 'u' to '0' on write
    if (uncomp && *mode_c == 'b' && (strchr(smode, 'w') || strchr(smode, 'a'))) {
        *uncomp = '0';
    }

    // If we really asked for a compressed text format then mode_c above will
    // point to nul.  We set to 'z' to enable bgzf.
    if (strchr(mode, 'w') && fmt && fmt->compression == bgzf) {
        if (fmt->format == sam || fmt->format == vcf || fmt->format == text_format)
            *mode_c = 'z';
    }

    char *rmme = NULL, *fnidx = strstr(fn, HTS_IDX_DELIM);
    if ( fnidx ) {
        rmme = strdup(fn);
        if ( !rmme ) goto error;
        rmme[fnidx-fn] = 0;
        fn = rmme;
    }

    hfile = hopen(fn, smode);
    if (hfile == NULL) goto error;

    fp = hts_hopen(hfile, fn, smode);
    if (fp == NULL) goto error;

    // Compensate for the loss of exactness in htsExactFormat.
    // hts_hopen returns generics such as binary or text, but we
    // have been given something explicit here so use that instead.
    if (fp->is_write && fmt &&
        (fmt->format == bam || fmt->format == sam ||
         fmt->format == vcf || fmt->format == bcf ||
         fmt->format == bed || fmt->format == fasta_format ||
         fmt->format == fastq_format))
        fp->format.format = fmt->format;

    if (fmt && fmt->specific) {
        if (hts_opt_apply(fp, fmt->specific) != 0) {
            if (((hts_opt*)fmt->specific)->opt == CRAM_OPT_REFERENCE &&
                (errno == ENOENT || errno == EIO || errno == EBADF ||
                  errno == EACCES || errno == EISDIR)) {
                /* error during reference file operation
                 for these specific errors, set the error as EINVAL */
                errno = EINVAL;
            }
            goto error;
        }
    }
    if ( rmme ) free(rmme);
    return fp;

error:
    hts_log_error("Failed to open file \"%s\"%s%s", fn,
                  errno ? " : " : "", errno ? strerror(errno) : "");
    if ( rmme ) free(rmme);

    if (hfile)
        hclose_abruptly(hfile);

    return NULL;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x70, %rsp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %rbx
movq %rsp, %r15
movl $0x63, %edx
movq %r15, %rdi
callq 0x4250
movb $0x0, 0x63(%r15)
movq %r15, %rdi
movl $0x2c, %esi
callq 0x4190
testq %rax, %rax
je 0x134b6
movb $0x0, (%rax)
xorl %ecx, %ecx
movq %r15, %rdx
xorl %r13d, %r13d
xorl %eax, %eax
movb (%rdx), %dil
movzbl %dil, %esi
leal -0x62(%rsi), %r8d
cmpl $0x2, %r8d
jae 0x134d8
movq %r15, %rsi
movl %edi, %eax
jmp 0x134f2
testl %esi, %esi
je 0x134fa
leaq 0x1(%r15), %rsi
movb %dil, (%r15)
testq %r13, %r13
jne 0x134f2
cmpb $0x75, (%rdx)
cmovneq %rcx, %r15
movq %r15, %r13
incq %rdx
movq %rsi, %r15
jmp 0x134c0
movb %al, (%r15)
movb $0x0, 0x1(%r15)
testq %r14, %r14
je 0x13520
movl 0x4(%r14), %ecx
leal -0x1(%rcx), %edx
cmpl $0x14, %edx
ja 0x13520
leaq 0x825b6(%rip), %rax     # 0x95ad0
movb (%rcx,%rax), %al
movb %al, (%r15)
testq %r13, %r13
je 0x13552
cmpb $0x62, %al
jne 0x13552
movq %rsp, %rdi
movl $0x77, %esi
callq 0x4190
testq %rax, %rax
jne 0x1354d
movq %rsp, %rdi
movl $0x61, %esi
callq 0x4190
testq %rax, %rax
je 0x13552
movb $0x30, (%r13)
movq %r12, %rdi
movl $0x77, %esi
callq 0x4190
testq %r14, %r14
je 0x13587
testq %rax, %rax
je 0x13587
cmpl $0x2, 0xc(%r14)
jne 0x13587
movl 0x4(%r14), %eax
cmpl $0x8, %eax
ja 0x13587
movl $0x10c, %ecx            # imm = 0x10C
btl %eax, %ecx
jae 0x13587
movb $0x7a, (%r15)
leaq 0x82952(%rip), %rsi     # 0x95ee0
movq %rbx, %rdi
callq 0x40e0
testq %rax, %rax
je 0x135bb
movq %rax, %r12
movq %rbx, %rdi
callq 0x4930
testq %rax, %rax
je 0x13628
movq %rax, %r15
subq %rbx, %r12
movb $0x0, (%rax,%r12)
movq %rax, %rbx
jmp 0x135be
xorl %r15d, %r15d
movq %rsp, %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0xfe44
testq %rax, %rax
je 0x13623
movq %rax, %r12
movq %rsp, %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x13701
testq %rax, %rax
je 0x1362e
movq %rax, %r13
testq %r14, %r14
je 0x136a1
movl (%r13), %eax
andl $0x2, %eax
je 0x136a1
movl 0x4(%r14), %eax
cmpl $0x11, %eax
ja 0x136a6
movl $0x32318, %ecx          # imm = 0x32318
btl %eax, %ecx
jae 0x136a6
movl %eax, 0x4c(%r13)
jmp 0x136a6
xorl %r12d, %r12d
jmp 0x1362e
xorl %r12d, %r12d
xorl %r15d, %r15d
callq 0x4050
movl (%rax), %edi
leaq 0x82731(%rip), %r9      # 0x95d6d
leaq 0x828d2(%rip), %r14     # 0x95f15
testl %edi, %edi
cmoveq %r9, %r14
je 0x13653
callq 0x4150
movq %rax, %r9
leaq 0x8288e(%rip), %rsi     # 0x95ee8
leaq 0x82897(%rip), %rdx     # 0x95ef8
movl $0x1, %edi
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x13de5
testq %r15, %r15
je 0x13680
movq %r15, %rdi
callq 0x45c0
testq %r12, %r12
je 0x1368d
movq %r12, %rdi
callq 0xf53f
xorl %r13d, %r13d
movq %r13, %rax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
testq %r14, %r14
je 0x136f2
movq 0x18(%r14), %rsi
testq %rsi, %rsi
je 0x136f2
movq %r13, %rdi
callq 0x13d55
testl %eax, %eax
je 0x136f2
movq 0x18(%r14), %rax
cmpl $0x9, 0x8(%rax)
jne 0x1362e
callq 0x4050
movl (%rax), %ecx
cmpl $0x15, %ecx
ja 0x1362e
movl $0x202224, %edx         # imm = 0x202224
btl %ecx, %edx
jae 0x1362e
movl $0x16, (%rax)
jmp 0x1362e
testq %r15, %r15
je 0x13690
movq %r15, %rdi
callq 0x45c0
jmp 0x13690