// File: /workspace/github_cpp_makefile_star2_O1/karel-brinda[P]ococo/ext/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
        const char *ext = fn? strrchr(fn, '.') : NULL;
        if (ext == NULL || strchr(ext, '/')) {
            free(mode_opts);
            return NULL;
        }
        return sam_open_mode(cp, fn, ext+1)
            ? (free(mode_opts), NULL)
            : mode_opts;
    }

    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 {
        free(mode_opts);
        return NULL;
    }

    strcpy(cp, opts);

    return mode_opts;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
testq %rdx, %rdx
je 0x1a4d4
movq %r15, %rdi
callq 0x4230
movq %rax, %rbx
addq $0xc, %rbx
jmp 0x1a4d9
movl $0xd, %ebx
testq %r14, %r14
je 0x1a4e8
movq %r14, %rdi
callq 0x4230
jmp 0x1a4ed
movl $0x1, %eax
addq %rax, %rbx
movq %rbx, %rdi
callq 0x4700
testq %rax, %rax
je 0x1a634
movq %rax, %rbx
testq %r14, %r14
leaq 0x3940b(%rip), %rsi     # 0x53919
cmovneq %r14, %rsi
movq %rax, %rdi
callq 0x44e0
movq %rbx, %rdi
callq 0x4230
movq %rax, %r14
addq %rbx, %r14
testq %r15, %r15
je 0x1a547
movq %r15, %rdi
movl $0x2c, %esi
callq 0x4200
testq %rax, %rax
je 0x1a565
movq %rax, %r12
subq %r15, %rax
jmp 0x1a574
testq %r12, %r12
je 0x1a5fd
movq %r12, %rdi
movl $0x2e, %esi
callq 0x45a0
movq %rax, %r15
jmp 0x1a600
movq %r15, %rdi
callq 0x4230
leaq 0x3860b(%rip), %r12     # 0x52b7f
movslq %eax, %r13
leaq 0x3888b(%rip), %rsi     # 0x52e09
movq %r15, %rdi
movq %r13, %rdx
callq 0x4280
testl %eax, %eax
je 0x1a5ee
leaq 0x38879(%rip), %rsi     # 0x52e0d
movq %r15, %rdi
movq %r13, %rdx
callq 0x4280
testl %eax, %eax
je 0x1a5f4
leaq 0x39165(%rip), %rsi     # 0x5370f
movq %r15, %rdi
movq %r13, %rdx
callq 0x4280
testl %eax, %eax
je 0x1a638
leaq 0x39162(%rip), %rsi     # 0x53722
movq %r15, %rdi
movq %r13, %rdx
callq 0x4280
testl %eax, %eax
je 0x1a656
leaq 0x3882b(%rip), %rsi     # 0x52e05
movq %r15, %rdi
movq %r13, %rdx
callq 0x4280
testl %eax, %eax
jne 0x1a62c
jmp 0x1a67a
movb $0x62, (%r14)
jmp 0x1a5f8
movb $0x63, (%r14)
incq %r14
jmp 0x1a67a
xorl %r15d, %r15d
testq %r15, %r15
je 0x1a62c
movq %r15, %rdi
movl $0x2f, %esi
callq 0x4200
testq %rax, %rax
jne 0x1a62c
incq %r15
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1a3fe
testl %eax, %eax
je 0x1a685
movq %rbx, %rdi
callq 0x4610
xorl %ebx, %ebx
jmp 0x1a685
movb $0x63, (%r14)
movabsq $0x4e4f49535245562c, %rax # imm = 0x4E4F49535245562C
movq %rax, 0x1(%r14)
movabsq $0x312e323d4e4f49, %rax # imm = 0x312E323D4E4F49
jmp 0x1a672
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 0x44e0
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq