// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/sam.c
// Function: sam_open_mode_opts
char *sam_open_mode_opts(const char *fn,
                         const char *mode,
                         const char *format)
{
    char *mode_opts = malloc((format ? strlen(format) : 1) +
                             (mode   ? strlen(mode)   : 1) + 12);
    char *opts, *cp;
    int format_len;

    if (!mode_opts)
        return NULL;

    strcpy(mode_opts, mode ? mode : "r");
    cp = mode_opts + strlen(mode_opts);

    if (format == NULL) {
        // Try to pick a format based on the filename extension
        char extension[HTS_MAX_EXT_LEN];
        if (find_file_extension(fn, extension) < 0) {
            free(mode_opts);
            return NULL;
        }
        if (sam_open_mode(cp, fn, extension) == 0) {
            return mode_opts;
        } else {
            free(mode_opts);
            return NULL;
        }
    }

    if ((opts = strchr(format, ','))) {
        format_len = opts-format;
    } else {
        opts="";
        format_len = strlen(format);
    }

    if (strncmp(format, "bam", format_len) == 0) {
        *cp++ = 'b';
    } else if (strncmp(format, "cram", format_len) == 0) {
        *cp++ = 'c';
    } else if (strncmp(format, "cram2", format_len) == 0) {
        *cp++ = 'c';
        strcpy(cp, ",VERSION=2.1");
        cp += 12;
    } else if (strncmp(format, "cram3", format_len) == 0) {
        *cp++ = 'c';
        strcpy(cp, ",VERSION=3.0");
        cp += 12;
    } else if (strncmp(format, "sam", format_len) == 0) {
        ; // format mode=""
    } else if (strncmp(format, "sam.gz", format_len) == 0) {
        *cp++ = 'z';
    } else if (strncmp(format, "fastq", format_len) == 0 ||
               strncmp(format, "fq", format_len) == 0) {
        *cp++ = 'f';
    } else if (strncmp(format, "fastq.gz", format_len) == 0 ||
               strncmp(format, "fq.gz", format_len) == 0) {
        *cp++ = 'f';
        *cp++ = 'z';
    } else if (strncmp(format, "fasta", format_len) == 0 ||
               strncmp(format, "fa", format_len) == 0) {
        *cp++ = 'F';
    } else if (strncmp(format, "fasta.gz", format_len) == 0 ||
               strncmp(format, "fa", format_len) == 0) {
        *cp++ = 'F';
        *cp++ = 'z';
    } else {
        free(mode_opts);
        return NULL;
    }

    strcpy(cp, opts);

    return mode_opts;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
testq %rdx, %rdx
je 0x2edf4
movq %r15, %rdi
callq 0x41d0
movq %rax, %rbx
addq $0xc, %rbx
jmp 0x2edf9
movl $0xd, %ebx
testq %r14, %r14
je 0x2ee08
movq %r14, %rdi
callq 0x41d0
jmp 0x2ee0d
movl $0x1, %eax
addq %rax, %rbx
movq %rbx, %rdi
callq 0x4680
testq %rax, %rax
je 0x2f0f3
movq %rax, %rbx
testq %r14, %r14
leaq 0x6979d(%rip), %rsi     # 0x985cb
cmovneq %r14, %rsi
movq %rax, %rdi
callq 0x4460
movq %rbx, %rdi
callq 0x41d0
movq %rax, %r14
addq %rbx, %r14
testq %r15, %r15
je 0x2ee6e
movq %r15, %rdi
movl $0x2c, %esi
callq 0x4190
testq %rax, %rax
je 0x2ef17
movq %rax, %r12
subq %r15, %rax
jmp 0x2ef26
testq %r12, %r12
je 0x2f0eb
leaq 0x67062(%rip), %rsi     # 0x95ee0
movq %r12, %rdi
callq 0x40e0
movq %rax, %r15
testq %rax, %rax
jne 0x2ee9c
movq %r12, %rdi
callq 0x41d0
movq %rax, %r15
addq %r12, %r15
movq %r15, %rsi
cmpq %r12, %r15
jbe 0x2eec7
xorl %eax, %eax
movq %r15, %rsi
movb (%rsi), %cl
movl %ecx, %edx
andb $-0x2, %dl
cmpb $0x2e, %dl
je 0x2f0b2
decq %rsi
incq %rax
cmpq %r12, %rsi
ja 0x2eea9
movq %r12, %rsi
movb (%rsi), %al
cmpb $0x2e, %al
jne 0x2f0eb
subq %rsi, %r15
leaq -0xa(%r15), %rax
cmpq $-0x8, %rax
jbe 0x2f0eb
incq %rsi
leaq -0x1(%r15), %rdx
leaq 0x7(%rsp), %r13
movq %r13, %rdi
callq 0x43c0
movb $0x0, 0x6(%rsp,%r15)
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2eb3b
testl %eax, %eax
jne 0x2f0eb
jmp 0x2f126
movq %r15, %rdi
callq 0x41d0
leaq 0x66e47(%rip), %r12     # 0x95d6d
movslq %eax, %r13
leaq 0x67315(%rip), %rsi     # 0x96245
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f082
leaq 0x67688(%rip), %rsi     # 0x965d2
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f088
leaq 0x68eef(%rip), %rsi     # 0x97e53
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f094
leaq 0x68ee8(%rip), %rsi     # 0x97e66
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f0f7
leaq 0x6976b(%rip), %rsi     # 0x98703
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f11b
leaq 0x6728c(%rip), %rsi     # 0x9623e
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f140
leaq 0x6727d(%rip), %rsi     # 0x96249
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f137
leaq 0x67269(%rip), %rsi     # 0x9624f
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f137
leaq 0x67252(%rip), %rsi     # 0x96252
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f180
leaq 0x67241(%rip), %rsi     # 0x9625b
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f180
leaq 0x6722d(%rip), %rsi     # 0x96261
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f18c
leaq 0x67219(%rip), %rsi     # 0x96267
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2f18c
leaq 0x67202(%rip), %rsi     # 0x9626a
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
jne 0x2f0eb
movw $0x7a46, (%r14)         # imm = 0x7A46
jmp 0x2f186
movb $0x62, (%r14)
jmp 0x2f08c
movb $0x63, (%r14)
incq %r14
jmp 0x2f11b
movb $0x63, (%r14)
movabsq $0x4e4f49535245562c, %rax # imm = 0x4E4F49535245562C
movq %rax, 0x1(%r14)
movabsq $0x312e323d4e4f49, %rax # imm = 0x312E323D4E4F49
jmp 0x2f113
cmpb $0x2e, %cl
jne 0x2f0eb
cmpq $0x3, %rax
je 0x2f149
cmpq $0x4, %rax
jne 0x2eec7
cmpb $0x62, 0x1(%rsi)
jne 0x2eec7
cmpb $0x67, 0x2(%rsi)
jne 0x2eec7
cmpb $0x7a, 0x3(%rsi)
jne 0x2eec7
jmp 0x2f15d
movq %rbx, %rdi
callq 0x45c0
xorl %ebx, %ebx
jmp 0x2f126
movb $0x63, (%r14)
movabsq $0x4e4f49535245562c, %rax # imm = 0x4E4F49535245562C
movq %rax, 0x1(%r14)
movabsq $0x302e333d4e4f49, %rax # imm = 0x302E333D4E4F49
movq %rax, 0x6(%r14)
addq $0xd, %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x4460
movq %rbx, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movb $0x66, (%r14)
jmp 0x2f08c
movb $0x7a, (%r14)
jmp 0x2f08c
cmpb $0x67, 0x1(%rsi)
jne 0x2eec7
cmpb $0x7a, 0x2(%rsi)
jne 0x2eec7
decq %rsi
cmpq %r12, %rsi
jbe 0x2eec7
movb (%rsi), %al
movl %eax, %ecx
andb $-0x2, %cl
decq %rsi
cmpb $0x2e, %cl
jne 0x2f160
incq %rsi
jmp 0x2eec9
movw $0x7a66, (%r14)         # imm = 0x7A66
addq $0x2, %r14
jmp 0x2f11b
movb $0x46, (%r14)
jmp 0x2f08c