// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x63
popq %rdx
movq %r15, %rdi
callq 0x4250
movb $0x0, 0x63(%r15)
pushq $0x2c
popq %rsi
movq %r15, %rdi
callq 0x4190
testq %rax, %rax
je 0x10830
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 0x10852
movq %r15, %rsi
movl %edi, %eax
jmp 0x1086c
testl %esi, %esi
je 0x10874
leaq 0x1(%r15), %rsi
movb %dil, (%r15)
testq %r13, %r13
jne 0x1086c
cmpb $0x75, (%rdx)
cmovneq %rcx, %r15
movq %r15, %r13
incq %rdx
movq %rsi, %r15
jmp 0x1083a
movb %al, (%r15)
movb $0x0, 0x1(%r15)
testq %r14, %r14
je 0x1089a
movl 0x4(%r14), %ecx
leal -0x1(%rcx), %edx
cmpl $0x14, %edx
ja 0x1089a
leaq 0x671ec(%rip), %rax     # 0x77a80
movb (%rcx,%rax), %al
movb %al, (%r15)
testq %r13, %r13
je 0x108c8
cmpb $0x62, %al
jne 0x108c8
movq %rsp, %rdi
pushq $0x77
popq %rsi
callq 0x4190
testq %rax, %rax
jne 0x108c3
movq %rsp, %rdi
pushq $0x61
popq %rsi
callq 0x4190
testq %rax, %rax
je 0x108c8
movb $0x30, (%r13)
pushq $0x77
popq %rsi
movq %r12, %rdi
callq 0x4190
testq %r14, %r14
je 0x108fb
testq %rax, %rax
je 0x108fb
cmpl $0x2, 0xc(%r14)
jne 0x108fb
movl 0x4(%r14), %eax
cmpl $0x8, %eax
ja 0x108fb
movl $0x10c, %ecx            # imm = 0x10C
btl %eax, %ecx
jae 0x108fb
movb $0x7a, (%r15)
leaq 0x675cf(%rip), %rsi     # 0x77ed1
movq %rbx, %rdi
callq 0x40e0
testq %rax, %rax
je 0x1092f
movq %rax, %r12
movq %rbx, %rdi
callq 0x4930
testq %rax, %rax
je 0x10995
movq %rax, %r15
subq %rbx, %r12
movb $0x0, (%rax,%r12)
movq %rax, %rbx
jmp 0x10932
xorl %r15d, %r15d
movq %rsp, %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe19b
testq %rax, %rax
je 0x10990
movq %rax, %r12
movq %rsp, %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10a65
testq %rax, %rax
je 0x1099b
movq %rax, %r13
testq %r14, %r14
je 0x109fb
movl (%r13), %eax
andl $0x2, %eax
je 0x109fb
movl 0x4(%r14), %eax
cmpl $0x11, %eax
ja 0x10a00
movl $0x32318, %ecx          # imm = 0x32318
btl %eax, %ecx
jae 0x10a00
movl %eax, 0x4c(%r13)
jmp 0x10a00
xorl %r12d, %r12d
jmp 0x1099b
xorl %r12d, %r12d
xorl %r15d, %r15d
callq 0x4050
movl (%rax), %edi
leaq 0x673ad(%rip), %r9      # 0x77d56
leaq 0x67556(%rip), %r14     # 0x77f06
testl %edi, %edi
cmoveq %r9, %r14
je 0x109c0
callq 0x4150
movq %rax, %r9
leaq 0x67512(%rip), %rsi     # 0x77ed9
leaq 0x6751b(%rip), %rdx     # 0x77ee9
xorl %r13d, %r13d
pushq $0x1
popq %rdi
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x110c6
movq %r15, %rdi
callq 0x45c0
testq %r12, %r12
je 0x10a54
movq %r12, %rdi
callq 0xd94c
xorl %r13d, %r13d
jmp 0x10a54
testq %r14, %r14
je 0x10a4c
movq 0x18(%r14), %rsi
testq %rsi, %rsi
je 0x10a4c
movq %r13, %rdi
callq 0x11039
testl %eax, %eax
je 0x10a4c
movq 0x18(%r14), %rax
cmpl $0x9, 0x8(%rax)
jne 0x1099b
callq 0x4050
movl (%rax), %ecx
cmpl $0x15, %ecx
ja 0x1099b
movl $0x202224, %edx         # imm = 0x202224
btl %ecx, %edx
jae 0x1099b
movl $0x16, (%rax)
jmp 0x1099b
movq %r15, %rdi
callq 0x45c0
movq %r13, %rax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq