// File: 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 %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2097d
movq -0x20(%rbp), %rdi
callq 0x4230
movq %rax, -0x50(%rbp)
jmp 0x20988
movl $0x1, %eax
movq %rax, -0x50(%rbp)
jmp 0x20988
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x209a6
movq -0x18(%rbp), %rdi
callq 0x4230
movq %rax, -0x60(%rbp)
jmp 0x209b1
movl $0x1, %eax
movq %rax, -0x60(%rbp)
jmp 0x209b1
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rax
addq %rax, %rdi
addq $0xc, %rdi
callq 0x47a0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x209dd
movq $0x0, -0x8(%rbp)
jmp 0x20c5b
movq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x209f6
movq -0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x20a03
leaq 0x5aff2(%rip), %rax     # 0x7b9ef
movq %rax, -0x70(%rbp)
jmp 0x20a03
movq -0x68(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x4530
movq -0x28(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4230
movq %rax, %rcx
movq -0x78(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x20adf
cmpq $0x0, -0x10(%rbp)
je 0x20a55
movq -0x10(%rbp), %rdi
movl $0x2e, %esi
callq 0x4640
movq %rax, -0x80(%rbp)
jmp 0x20a5d
xorl %eax, %eax
movq %rax, -0x80(%rbp)
jmp 0x20a5d
movq -0x80(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x20a80
movq -0x48(%rbp), %rdi
movl $0x2f, %esi
callq 0x41e0
cmpq $0x0, %rax
je 0x20a96
movq -0x28(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x20c5b
movq -0x38(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq $0x1, %rdx
callq 0x20830
cmpl $0x0, %eax
je 0x20ac4
movq -0x28(%rbp), %rdi
callq 0x46a0
xorl %eax, %eax
movq %rax, -0x88(%rbp)
jmp 0x20acf
movq -0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x20c5b
movq -0x20(%rbp), %rdi
movl $0x2c, %esi
callq 0x41e0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x20b07
movq -0x30(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x3c(%rbp)
jmp 0x20b1e
leaq 0x5b9be(%rip), %rax     # 0x7c4cc
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4230
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
leaq 0x5a495(%rip), %rsi     # 0x7afc2
callq 0x4290
cmpl $0x0, %eax
jne 0x20b4e
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb $0x62, (%rax)
jmp 0x20c46
movq -0x20(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
leaq 0x5a469(%rip), %rsi     # 0x7afc6
callq 0x4290
cmpl $0x0, %eax
jne 0x20b7e
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb $0x63, (%rax)
jmp 0x20c44
movq -0x20(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
leaq 0x5ac58(%rip), %rsi     # 0x7b7e5
callq 0x4290
cmpl $0x0, %eax
jne 0x20bc7
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb $0x63, (%rax)
movq -0x38(%rbp), %rdi
leaq 0x5ac37(%rip), %rsi     # 0x7b7eb
callq 0x4530
movq -0x38(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x38(%rbp)
jmp 0x20c42
movq -0x20(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
leaq 0x5ac22(%rip), %rsi     # 0x7b7f8
callq 0x4290
cmpl $0x0, %eax
jne 0x20c10
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x38(%rbp)
movb $0x63, (%rax)
movq -0x38(%rbp), %rdi
leaq 0x5ac01(%rip), %rsi     # 0x7b7fe
callq 0x4530
movq -0x38(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x38(%rbp)
jmp 0x20c40
movq -0x20(%rbp), %rdi
movslq -0x3c(%rbp), %rdx
leaq 0x5a39f(%rip), %rsi     # 0x7afbe
callq 0x4290
cmpl $0x0, %eax
jne 0x20c2b
jmp 0x20c3e
movq -0x28(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x20c5b
jmp 0x20c40
jmp 0x20c42
jmp 0x20c44
jmp 0x20c46
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x4530
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)