// File: /workspace/github_c_makefile_star2_O0/icebert[P]pblat/htslib/hts.c
// Function: hts_hopen
htsFile *hts_hopen(struct hFILE *hfile, const char *fn, const char *mode)
{
    htsFile *fp = (htsFile*)calloc(1, sizeof(htsFile));
    char simple_mode[101], *cp, *opts;
    simple_mode[100] = '\0';

    if (fp == NULL) goto error;

    fp->fn = strdup(fn);
    fp->is_be = ed_is_big();

    // Split mode into simple_mode,opts strings
    if ((cp = strchr(mode, ','))) {
        strncpy(simple_mode, mode, cp-mode <= 100 ? cp-mode : 100);
        simple_mode[cp-mode] = '\0';
        opts = cp+1;
    } else {
        strncpy(simple_mode, mode, 100);
        opts = NULL;
    }

    if (strchr(simple_mode, 'r')) {
        if (hts_detect_format(hfile, &fp->format) < 0) goto error;
    }
    else if (strchr(simple_mode, 'w') || strchr(simple_mode, 'a')) {
        htsFormat *fmt = &fp->format;
        fp->is_write = 1;

        if (strchr(simple_mode, 'b')) fmt->format = binary_format;
        else if (strchr(simple_mode, 'c')) fmt->format = cram;
        else fmt->format = text_format;

        if (strchr(simple_mode, 'z')) fmt->compression = bgzf;
        else if (strchr(simple_mode, 'g')) fmt->compression = gzip;
        else if (strchr(simple_mode, 'u')) fmt->compression = no_compression;
        else {
            // No compression mode specified, set to the default for the format
            switch (fmt->format) {
            case binary_format: fmt->compression = bgzf; break;
            case cram: fmt->compression = custom; break;
            case text_format: fmt->compression = no_compression; break;
            default: abort();
            }
        }

        // Fill in category (if determinable; e.g. 'b' could be BAM or BCF)
        fmt->category = format_category(fmt->format);

        fmt->version.major = fmt->version.minor = -1;
        fmt->compression_level = -1;
        fmt->specific = NULL;
    }
    else goto error;

    switch (fp->format.format) {
    case binary_format:
    case bam:
    case bcf:
        fp->fp.bgzf = bgzf_hopen(hfile, simple_mode);
        if (fp->fp.bgzf == NULL) goto error;
        fp->is_bin = 1;
        break;

    case cram:
        fp->fp.cram = cram_dopen(hfile, fn, simple_mode);
        if (fp->fp.cram == NULL) goto error;
        if (!fp->is_write)
            cram_set_option(fp->fp.cram, CRAM_OPT_DECODE_MD, 1);
        fp->is_cram = 1;
        break;

    case text_format:
    case sam:
    case vcf:
        if (!fp->is_write) {
        #if KS_BGZF
            BGZF *gzfp = bgzf_hopen(hfile, simple_mode);
        #else
            // TODO Implement gzip hFILE adaptor
            hclose(hfile); // This won't work, especially for stdin
            gzFile gzfp = strcmp(fn, "-")? gzopen(fn, "rb") : gzdopen(fileno(stdin), "rb");
        #endif
            if (gzfp) fp->fp.voidp = ks_init(gzfp);
            else goto error;
        }
        else if (fp->format.compression != no_compression) {
            fp->fp.bgzf = bgzf_hopen(hfile, simple_mode);
            if (fp->fp.bgzf == NULL) goto error;
        }
        else
            fp->fp.hfile = hfile;
        break;

    default:
        goto error;
    }

    if (opts)
        hts_process_opts(fp, opts);

    return fp;

error:
    if (hts_verbose >= 2)
        fprintf(stderr, "[E::%s] fail to open file '%s'\n", __func__, fn);

    if (fp) {
        free(fp->fn);
        free(fp->fn_aux);
        free(fp);
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x1, %edi
movl $0x60, %esi
callq 0x6460
movq %rax, -0x28(%rbp)
movb $0x0, -0x2c(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x710ba
jmp 0x715a3
movq -0x18(%rbp), %rdi
callq 0x6dd0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
callq 0x72270
movl %eax, %edx
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $0x1, %edx
shll $0x2, %edx
andl $-0x5, %ecx
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rdi
movl $0x2c, %esi
callq 0x6260
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x7119d
leaq -0x90(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x98(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
cmpq $0x64, %rax
jg 0x7114b
movq -0x98(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0xc8(%rbp)
jmp 0x71159
movl $0x64, %eax
movq %rax, -0xc8(%rbp)
jmp 0x71159
movq -0xb8(%rbp), %rsi
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rdx
callq 0x6370
movq -0x98(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movb $0x0, -0x90(%rbp,%rax)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x711bd
leaq -0x90(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x64, %edx
callq 0x6370
movq $0x0, -0xa0(%rbp)
leaq -0x90(%rbp), %rdi
movl $0x72, %esi
callq 0x6260
cmpq $0x0, %rax
je 0x711f4
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x40, %rsi
callq 0x6ff80
cmpl $0x0, %eax
jge 0x711ef
jmp 0x715a3
jmp 0x713e1
leaq -0x90(%rbp), %rdi
movl $0x77, %esi
callq 0x6260
cmpq $0x0, %rax
jne 0x71226
leaq -0x90(%rbp), %rdi
movl $0x61, %esi
callq 0x6260
cmpq $0x0, %rax
je 0x713da
movq -0x28(%rbp), %rax
addq $0x40, %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $-0x3, %ecx
orl $0x2, %ecx
movl %ecx, (%rax)
leaq -0x90(%rbp), %rdi
movl $0x62, %esi
callq 0x6260
cmpq $0x0, %rax
je 0x7126a
movq -0xa8(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0x712a1
leaq -0x90(%rbp), %rdi
movl $0x63, %esi
callq 0x6260
cmpq $0x0, %rax
je 0x71291
movq -0xa8(%rbp), %rax
movl $0x6, 0x4(%rax)
jmp 0x7129f
movq -0xa8(%rbp), %rax
movl $0x2, 0x4(%rax)
jmp 0x712a1
leaq -0x90(%rbp), %rdi
movl $0x7a, %esi
callq 0x6260
cmpq $0x0, %rax
je 0x712cb
movq -0xa8(%rbp), %rax
movl $0x2, 0xc(%rax)
jmp 0x71388
leaq -0x90(%rbp), %rdi
movl $0x67, %esi
callq 0x6260
cmpq $0x0, %rax
je 0x712f5
movq -0xa8(%rbp), %rax
movl $0x1, 0xc(%rax)
jmp 0x71386
leaq -0x90(%rbp), %rdi
movl $0x75, %esi
callq 0x6260
cmpq $0x0, %rax
je 0x7131c
movq -0xa8(%rbp), %rax
movl $0x0, 0xc(%rax)
jmp 0x71384
movq -0xa8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0xcc(%rbp)
subl $0x1, %eax
je 0x7134d
jmp 0x71333
movl -0xcc(%rbp), %eax
subl $0x2, %eax
je 0x7136d
jmp 0x71340
movl -0xcc(%rbp), %eax
subl $0x6, %eax
je 0x7135d
jmp 0x7137d
movq -0xa8(%rbp), %rax
movl $0x2, 0xc(%rax)
jmp 0x71382
movq -0xa8(%rbp), %rax
movl $0x3, 0xc(%rax)
jmp 0x71382
movq -0xa8(%rbp), %rax
movl $0x0, 0xc(%rax)
jmp 0x71382
callq 0x63b0
jmp 0x71384
jmp 0x71386
jmp 0x71388
movq -0xa8(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x72290
movl %eax, %ecx
movq -0xa8(%rbp), %rax
movl %ecx, (%rax)
movq -0xa8(%rbp), %rax
movw $0xffff, 0xa(%rax)      # imm = 0xFFFF
movq -0xa8(%rbp), %rax
movw $0xffff, 0x8(%rax)      # imm = 0xFFFF
movq -0xa8(%rbp), %rax
movw $0xffff, 0x10(%rax)     # imm = 0xFFFF
movq -0xa8(%rbp), %rax
movq $0x0, 0x18(%rax)
jmp 0x713df
jmp 0x715a3
jmp 0x713e1
movq -0x28(%rbp), %rax
movl 0x44(%rax), %eax
movl %eax, -0xd0(%rbp)
subl $0x1, %eax
je 0x71445
jmp 0x713f5
movl -0xd0(%rbp), %eax
addl $-0x2, %eax
subl $0x2, %eax
jb 0x714eb
jmp 0x71409
movl -0xd0(%rbp), %eax
subl $0x4, %eax
je 0x71445
jmp 0x71416
movl -0xd0(%rbp), %eax
subl $0x6, %eax
je 0x71483
jmp 0x71423
movl -0xd0(%rbp), %eax
subl $0x8, %eax
je 0x714eb
jmp 0x71434
movl -0xd0(%rbp), %eax
subl $0x9, %eax
jne 0x7157d
jmp 0x71445
movq -0x10(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x9b640
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x71470
jmp 0x715a3
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $-0x2, %ecx
orl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x7157f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x89f80
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x714b2
jmp 0x715a3
movq -0x28(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x714d8
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rdi
xorl %esi, %esi
movl $0x1, %edx
movb $0x0, %al
callq 0x8afa0
movq -0x28(%rbp), %rax
movl (%rax), %ecx
andl $-0x9, %ecx
orl $0x8, %ecx
movl %ecx, (%rax)
jmp 0x7157f
movq -0x28(%rbp), %rax
movl (%rax), %eax
shrl %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x71539
movq -0x10(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x9b640
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
je 0x71535
movq -0xb0(%rbp), %rdi
callq 0x6eec0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x71537
jmp 0x715a3
jmp 0x7157b
movq -0x28(%rbp), %rax
cmpl $0x0, 0x4c(%rax)
je 0x7156d
movq -0x10(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x9b640
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x7156b
jmp 0x715a3
jmp 0x71579
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
jmp 0x7157b
jmp 0x7157f
jmp 0x715a3
cmpq $0x0, -0xa0(%rbp)
je 0x71599
movq -0x28(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x72340
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x71601
cmpl $0x2, 0x95a36(%rip)     # 0x106fe0
jl 0x715cf
movq 0x93a1d(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
movq -0x18(%rbp), %rcx
leaq 0x70d88(%rip), %rsi     # 0xe2349
leaq 0x70f29(%rip), %rdx     # 0xe24f1
movb $0x0, %al
callq 0x6a80
cmpq $0x0, -0x28(%rbp)
je 0x715f9
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x6880
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x6880
movq -0x28(%rbp), %rdi
callq 0x6880
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nop