// File: /workspace/github_c_makefile_star2_O1/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 0x13a8d
movb $0x0, (%rax)
xorl %eax, %eax
movq %r15, %rcx
xorl %r13d, %r13d
xorl %edx, %edx
movb (%rcx), %dil
movzbl %dil, %esi
leal -0x62(%rsi), %r8d
cmpl $0x2, %r8d
jae 0x13aaf
movq %r15, %rsi
movl %edi, %edx
jmp 0x13ac9
testl %esi, %esi
je 0x13ad1
leaq 0x1(%r15), %rsi
movb %dil, (%r15)
testq %r13, %r13
jne 0x13ac9
cmpb $0x75, (%rcx)
cmovneq %rax, %r15
movq %r15, %r13
incq %rcx
movq %rsi, %r15
jmp 0x13a97
movb %dl, (%r15)
movb $0x0, 0x1(%r15)
testq %r14, %r14
je 0x13af7
movl 0x4(%r14), %eax
leal -0x1(%rax), %ecx
cmpl $0x14, %ecx
ja 0x13af7
leaq 0x84f2f(%rip), %rcx     # 0x98a20
movb (%rax,%rcx), %al
movb %al, (%r15)
testq %r13, %r13
je 0x13b2b
cmpb $0x62, (%r15)
jne 0x13b2b
movq %rsp, %rdi
movl $0x77, %esi
callq 0x4190
testq %rax, %rax
jne 0x13b26
movq %rsp, %rdi
movl $0x61, %esi
callq 0x4190
testq %rax, %rax
je 0x13b2b
movb $0x30, (%r13)
movq %r12, %rdi
movl $0x77, %esi
callq 0x4190
testq %r14, %r14
je 0x13b60
testq %rax, %rax
je 0x13b60
cmpl $0x2, 0xc(%r14)
jne 0x13b60
movl 0x4(%r14), %eax
cmpl $0x8, %eax
ja 0x13b60
movl $0x10c, %ecx            # imm = 0x10C
btl %eax, %ecx
jae 0x13b60
movb $0x7a, (%r15)
leaq 0x852d9(%rip), %rsi     # 0x98e40
movq %rbx, %rdi
callq 0x40e0
testq %rax, %rax
je 0x13b98
movq %rax, %r12
movq %rbx, %rdi
callq 0x4930
testq %rax, %rax
je 0x13c46
movq %rax, %r15
subq %rbx, %r12
movb $0x0, (%rax,%r12)
movq %rax, %rbx
jmp 0x13b9b
xorl %r15d, %r15d
movq %rsp, %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10014
testq %rax, %rax
je 0x13c41
movq %rax, %r12
movq %rsp, %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x13cce
testq %rax, %rax
je 0x13c4c
movq %rax, %r13
testq %r14, %r14
je 0x13bf3
movl (%r13), %eax
andl $0x2, %eax
je 0x13bf3
movl 0x4(%r14), %eax
cmpl $0x11, %eax
ja 0x13bf3
movl $0x32318, %ecx          # imm = 0x32318
btl %eax, %ecx
jae 0x13bf3
movl %eax, 0x4c(%r13)
testq %r14, %r14
je 0x13cbf
movq 0x18(%r14), %rsi
testq %rsi, %rsi
je 0x13cbf
movq %r13, %rdi
callq 0x142e8
testl %eax, %eax
je 0x13cbf
movq 0x18(%r14), %rax
cmpl $0x9, 0x8(%rax)
jne 0x13c4c
callq 0x4050
movl (%rax), %ecx
cmpl $0x15, %ecx
ja 0x13c4c
movl $0x202224, %edx         # imm = 0x202224
btl %ecx, %edx
jae 0x13c4c
movl $0x16, (%rax)
jmp 0x13c4c
xorl %r12d, %r12d
jmp 0x13c4c
xorl %r12d, %r12d
xorl %r15d, %r15d
callq 0x4050
movl (%rax), %edi
leaq 0x85073(%rip), %r9      # 0x98ccd
leaq 0x85214(%rip), %r14     # 0x98e75
testl %edi, %edi
cmoveq %r9, %r14
je 0x13c71
callq 0x4150
movq %rax, %r9
leaq 0x851d0(%rip), %rsi     # 0x98e48
leaq 0x851d9(%rip), %rdx     # 0x98e58
movl $0x1, %edi
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x14378
testq %r15, %r15
je 0x13c9e
movq %r15, %rdi
callq 0x45c0
testq %r12, %r12
je 0x13cab
movq %r12, %rdi
callq 0xf730
xorl %r13d, %r13d
movq %r13, %rax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
testq %r15, %r15
je 0x13cae
movq %r15, %rdi
callq 0x45c0
jmp 0x13cae