// File: /workspace/github_c_makefile_star2_O1/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, %r12
movq %rsi, %r14
movq %rdi, %r15
testq %rdx, %rdx
je 0x2fb4e
movq %r12, %rdi
callq 0x41d0
movq %rax, %rbx
addq $0xc, %rbx
jmp 0x2fb53
movl $0xd, %ebx
testq %r14, %r14
je 0x2fb62
movq %r14, %rdi
callq 0x41d0
jmp 0x2fb67
movl $0x1, %eax
addq %rax, %rbx
movq %rbx, %rdi
callq 0x4680
testq %rax, %rax
je 0x2feb8
movq %rax, %rbx
testq %r14, %r14
leaq 0x6ba3c(%rip), %rsi     # 0x9b5c4
cmovneq %r14, %rsi
movq %rax, %rdi
callq 0x4460
movq %rbx, %rdi
callq 0x41d0
movq %rax, %r14
addq %rbx, %r14
testq %r12, %r12
je 0x2fbc1
movq %r12, %rdi
movl $0x2c, %esi
callq 0x4190
testq %rax, %rax
je 0x2fbe1
movq %rax, %r15
subq %r12, %rax
jmp 0x2fbf0
testq %r15, %r15
je 0x2fd62
leaq 0x6926f(%rip), %rsi     # 0x98e40
movq %r15, %rdi
callq 0x40e0
movq %rax, %r12
jmp 0x2fd65
movq %r12, %rdi
callq 0x41d0
leaq 0x690dd(%rip), %r15     # 0x98ccd
movslq %eax, %r13
leaq 0x695ab(%rip), %rsi     # 0x991a5
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fd50
leaq 0x6991e(%rip), %rsi     # 0x99532
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fd56
leaq 0x6b212(%rip), %rsi     # 0x9ae40
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fda1
leaq 0x6b20b(%rip), %rsi     # 0x9ae53
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fdde
leaq 0x6ba9a(%rip), %rsi     # 0x9b6fc
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fe02
leaq 0x69522(%rip), %rsi     # 0x9919e
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fed4
leaq 0x69513(%rip), %rsi     # 0x991a9
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fecb
leaq 0x694ff(%rip), %rsi     # 0x991af
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fecb
leaq 0x694e8(%rip), %rsi     # 0x991b2
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fedd
leaq 0x694d7(%rip), %rsi     # 0x991bb
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2fedd
leaq 0x694c3(%rip), %rsi     # 0x991c1
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2feec
leaq 0x694af(%rip), %rsi     # 0x991c7
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
je 0x2feec
leaq 0x69498(%rip), %rsi     # 0x991ca
movq %r12, %rdi
movq %r13, %rdx
callq 0x4210
testl %eax, %eax
jne 0x2feb0
movw $0x7a46, (%r14)         # imm = 0x7A46
jmp 0x2fee3
movb $0x62, (%r14)
jmp 0x2fd5a
movb $0x63, (%r14)
incq %r14
jmp 0x2fe02
xorl %r12d, %r12d
testq %r15, %r15
je 0x2feb0
testq %r12, %r12
jne 0x2fd81
movq %r15, %rdi
callq 0x41d0
movq %rax, %r12
addq %r15, %r12
movq %r12, %rsi
cmpq %r15, %r12
setbe %al
jbe 0x2fe15
movb (%r12), %al
andb $-0x2, %al
cmpb $0x2e, %al
jne 0x2fdbf
xorl %eax, %eax
movq %r12, %rsi
jmp 0x2fe15
movb $0x63, (%r14)
movabsq $0x4e4f49535245562c, %rax # imm = 0x4E4F49535245562C
movq %rax, 0x1(%r14)
movabsq $0x312e323d4e4f49, %rax # imm = 0x312E323D4E4F49
jmp 0x2fdfa
leaq -0x1(%r12), %rsi
cmpq %r15, %rsi
setbe %al
jbe 0x2fe12
movb (%rsi), %cl
andb $-0x2, %cl
decq %rsi
cmpb $0x2e, %cl
jne 0x2fdc4
incq %rsi
jmp 0x2fe15
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 %r15, %rsi
callq 0x4460
jmp 0x2feba
movq %r15, %rsi
testb %al, %al
jne 0x2fe70
cmpb $0x2e, (%rsi)
jne 0x2fe70
movq %r12, %rax
subq %rsi, %rax
cmpq $0x4, %rax
je 0x2fe42
cmpq $0x3, %rax
jne 0x2fe70
cmpb $0x67, 0x1(%rsi)
jne 0x2fe3c
cmpb $0x7a, 0x2(%rsi)
je 0x2fe54
cmpq $0x4, %rax
jne 0x2fe70
cmpb $0x62, 0x1(%rsi)
jne 0x2fe70
cmpb $0x67, 0x2(%rsi)
jne 0x2fe70
cmpb $0x7a, 0x3(%rsi)
jne 0x2fe70
decq %rsi
movq %rsi, %rax
movq %rax, %rsi
cmpq %r15, %rax
jbe 0x2fe70
movb (%rsi), %cl
andb $-0x2, %cl
leaq -0x1(%rsi), %rax
cmpb $0x2e, %cl
jne 0x2fe5a
cmpb $0x2e, (%rsi)
jne 0x2feb0
subq %rsi, %r12
leaq -0xa(%r12), %rax
cmpq $-0x7, %rax
jb 0x2feb0
incq %rsi
leaq -0x1(%r12), %rdx
leaq 0x7(%rsp), %r13
movq %r13, %rdi
callq 0x43c0
movb $0x0, 0x6(%rsp,%r12)
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2f87e
testl %eax, %eax
je 0x2feba
movq %rbx, %rdi
callq 0x45c0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movb $0x66, (%r14)
jmp 0x2fd5a
movb $0x7a, (%r14)
jmp 0x2fd5a
movw $0x7a66, (%r14)         # imm = 0x7A66
addq $0x2, %r14
jmp 0x2fe02
movb $0x46, (%r14)
jmp 0x2fd5a