// File: /workspace/github_c_makefile_star2_O2/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 0x2656f
movq %r15, %rdi
callq 0x41d0
movq %rax, %rbx
addq $0xc, %rbx
jmp 0x26572
pushq $0xd
popq %rbx
testq %r14, %r14
je 0x26581
movq %r14, %rdi
callq 0x41d0
jmp 0x26584
pushq $0x1
popq %rax
addq %rax, %rbx
movq %rbx, %rdi
callq 0x4680
testq %rax, %rax
je 0x26609
movq %rax, %rbx
testq %r14, %r14
leaq 0x53f85(%rip), %rsi     # 0x7a526
cmovneq %r14, %rsi
movq %rax, %rdi
callq 0x4460
movq %rbx, %rdi
callq 0x41d0
movq %rax, %r14
addq %rbx, %r14
testq %r15, %r15
je 0x265d8
pushq $0x2c
popq %rsi
movq %r15, %rdi
callq 0x4190
testq %rax, %rax
je 0x26610
movq %rax, %r12
subq %r15, %rax
jmp 0x2661f
leaq 0x7(%rsp), %rsi
movq %r12, %rdi
callq 0x2645d
testl %eax, %eax
js 0x26601
leaq 0x7(%rsp), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x262c6
testl %eax, %eax
je 0x267d8
movq %rbx, %rdi
callq 0x45c0
xorl %ebx, %ebx
jmp 0x267d8
movq %r15, %rdi
callq 0x41d0
leaq 0x51737(%rip), %r12     # 0x77d56
movslq %eax, %r13
leaq 0x51c0d(%rip), %rsi     # 0x78236
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2677c
leaq 0x51f80(%rip), %rsi     # 0x785c3
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x26782
leaq 0x53751(%rip), %rsi     # 0x79dae
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2678b
leaq 0x5374a(%rip), %rsi     # 0x79dc1
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x267a9
leaq 0x53fcd(%rip), %rsi     # 0x7a65e
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x267cd
leaq 0x51b84(%rip), %rsi     # 0x7822f
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x267ef
leaq 0x51b75(%rip), %rsi     # 0x7823a
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x267e9
leaq 0x51b61(%rip), %rsi     # 0x78240
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x267e9
leaq 0x51b4a(%rip), %rsi     # 0x78243
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x267f5
leaq 0x51b39(%rip), %rsi     # 0x7824c
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x267f5
leaq 0x51b25(%rip), %rsi     # 0x78252
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x26801
leaq 0x51b11(%rip), %rsi     # 0x78258
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x26801
leaq 0x51afa(%rip), %rsi     # 0x7825b
movq %r15, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
jne 0x26601
movw $0x7a46, (%r14)         # imm = 0x7A46
jmp 0x267fb
movb $0x62, (%r14)
jmp 0x26786
movb $0x63, (%r14)
incq %r14
jmp 0x267cd
movb $0x63, (%r14)
movabsq $0x4e4f49535245562c, %rax # imm = 0x4E4F49535245562C
movq %rax, 0x1(%r14)
movabsq $0x312e323d4e4f49, %rax # imm = 0x312E323D4E4F49
jmp 0x267c5
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 0x26786
movb $0x7a, (%r14)
jmp 0x26786
movw $0x7a66, (%r14)         # imm = 0x7A66
addq $0x2, %r14
jmp 0x267cd
movb $0x46, (%r14)
jmp 0x26786