// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/cram/cram_io.c
// Function: cram_dopen
cram_fd *cram_dopen(hFILE *fp, const char *filename, const char *mode) {
    int i;
    char *cp;
    cram_fd *fd = calloc(1, sizeof(*fd));
    if (!fd)
        return NULL;

    fd->level = CRAM_DEFAULT_LEVEL;
    for (i = 0; mode[i]; i++) {
        if (mode[i] >= '0' && mode[i] <= '9') {
            fd->level = mode[i] - '0';
            break;
        }
    }

    fd->fp = fp;
    fd->mode = *mode;
    fd->first_container = 0;
    fd->curr_position = 0;

    if (fd->mode == 'r') {
        /* Reader */

        if (!(fd->file_def = cram_read_file_def(fd)))
            goto err;

        fd->version = fd->file_def->major_version * 256 +
            fd->file_def->minor_version;

        cram_init_tables(fd);

        if (!(fd->header = cram_read_SAM_hdr(fd))) {
            cram_free_file_def(fd->file_def);
            goto err;
        }

    } else {
        /* Writer */
        cram_file_def *def = calloc(1, sizeof(*def));
        if (!def)
            return NULL;

        fd->file_def = def;

        def->magic[0] = 'C';
        def->magic[1] = 'R';
        def->magic[2] = 'A';
        def->magic[3] = 'M';
        def->major_version = 0; // Indicator to write file def later.
        def->minor_version = 0;
        memset(def->file_id, 0, 20);
        strncpy(def->file_id, filename, 20);

        fd->version = major_version * 256 + minor_version;
        cram_init_tables(fd);

        /* SAM header written later along with this file_def */
    }

    fd->prefix = strdup((cp = strrchr(filename, '/')) ? cp+1 : filename);
    if (!fd->prefix)
        goto err;
    fd->first_base = fd->last_base = -1;
    fd->record_counter = 0;

    fd->ctr = NULL;
    fd->ctr_mt = NULL;
    fd->refs  = refs_create();
    if (!fd->refs)
        goto err;
    fd->ref_id = -2;
    fd->ref = NULL;

    fd->decode_md = 0;
    fd->seqs_per_slice = SEQS_PER_SLICE;
    fd->bases_per_slice = BASES_PER_SLICE;
    fd->slices_per_container = SLICE_PER_CNT;
    fd->embed_ref = -1; // automatic selection
    fd->no_ref = 0;
    fd->no_ref_counter = 0;
    fd->ap_delta = 0;
    fd->ignore_md5 = 0;
    fd->lossy_read_names = 0;
    fd->use_bz2 = 0;
    fd->use_rans = (CRAM_MAJOR_VERS(fd->version) >= 3);
    fd->use_tok = (CRAM_MAJOR_VERS(fd->version) >= 3) && (CRAM_MINOR_VERS(fd->version) >= 1);
    fd->use_lzma = 0;
    fd->multi_seq = -1;
    fd->multi_seq_user = -1;
    fd->unsorted   = 0;
    fd->shared_ref = 0;
    fd->store_md = 0;
    fd->store_nm = 0;
    fd->last_RI_count = 0;

    fd->index       = NULL;
    fd->own_pool    = 0;
    fd->pool        = NULL;
    fd->rqueue      = NULL;
    fd->job_pending = NULL;
    fd->ooc         = 0;
    fd->required_fields = INT_MAX;

    pthread_mutex_init(&fd->metrics_lock, NULL);
    pthread_mutex_init(&fd->ref_lock, NULL);
    pthread_mutex_init(&fd->range_lock, NULL);
    pthread_mutex_init(&fd->bam_list_lock, NULL);

    for (i = 0; i < DS_END; i++) {
        fd->m[i] = cram_new_metrics();
        if (!fd->m[i])
            goto err;
    }

    if (!(fd->tags_used = kh_init(m_metrics)))
        goto err;

    fd->range.refid = -2; // no ref.
    fd->eof = 1;          // See samtools issue #150
    fd->ref_fn = NULL;

    fd->bl = NULL;

    /* Initialise dummy refs from the @SQ headers */
    if (-1 == refs_from_header(fd))
        goto err;

    return fd;

 err:
    if (fd)
        free(fd);

    return NULL;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %r12
movl $0x1, %edi
movl $0x8a18, %esi           # imm = 0x8A18
callq 0x4300
testq %rax, %rax
je 0x554c1
movq %rax, %rbx
movl $0x5, 0x88(%rax)
movb (%r15), %al
testb %al, %al
je 0x55263
leaq 0x1(%r15), %rcx
addb $-0x30, %al
cmpb $0x9, %al
jbe 0x5525a
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x55249
jmp 0x55263
movzbl %al, %eax
movl %eax, 0x88(%rbx)
movq %r12, (%rbx)
movsbl (%r15), %eax
movl %eax, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8880(%rbx)
cmpl $0x72, %eax
jne 0x552cf
movq %rbx, %rdi
callq 0x542a2
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x554b9
movzwl 0x4(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x554d2
movq %rbx, %rdi
callq 0x54445
movq %rax, 0x18(%rbx)
testq %rax, %rax
jne 0x55323
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x554b9
callq 0x45c0
jmp 0x554b9
movl $0x1, %edi
movl $0x1a, %esi
callq 0x4300
testq %rax, %rax
je 0x554c1
movq %rax, 0x10(%rbx)
movl $0x4d415243, (%rax)     # imm = 0x4D415243
movq %rax, %rdi
addq $0x6, %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax)
movq $0x0, 0x12(%rax)
movl $0x14, %edx
movq %r14, %rsi
callq 0x4250
movl $0x301, 0xc(%rbx)       # imm = 0x301
movq %rbx, %rdi
callq 0x554d2
movq %r14, %rdi
movl $0x2f, %esi
callq 0x4530
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %r14, %rdi
callq 0x4930
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x554b9
movq $-0x1, 0x48(%rbx)
movq $0x0, 0x28(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rbx)
callq 0x52713
movq %rax, 0x50(%rbx)
testq %rax, %rax
je 0x554b9
movl $0xfffffffe, 0x68(%rbx) # imm = 0xFFFFFFFE
xorl %eax, %eax
movq %rax, 0x58(%rbx)
movaps 0x48796(%rip), %xmm0    # 0x9db20
movups %xmm0, 0x210(%rbx)
movl $0xffffffff, 0x220(%rbx) # imm = 0xFFFFFFFF
movl %eax, 0x8a10(%rbx)
xorps %xmm1, %xmm1
movups %xmm1, 0x224(%rbx)
movl 0xc(%rbx), %ecx
xorl %edx, %edx
cmpl $0x300, %ecx            # imm = 0x300
setge %dl
movl %edx, 0x234(%rbx)
testb %cl, %cl
setne %cl
andb %cl, %dl
movzbl %dl, %ecx
movl %ecx, 0x240(%rbx)
movl %eax, 0x238(%rbx)
movl %eax, 0x248(%rbx)
movq %rax, 0x250(%rbx)
movaps 0x48747(%rip), %xmm0    # 0x9db30
movups %xmm0, 0x8898(%rbx)
movq %rax, 0x8878(%rbx)
movl %eax, 0x88b0(%rbx)
movups %xmm1, 0x8970(%rbx)
movups %xmm1, 0x88b8(%rbx)
movl $0x7fffffff, 0x24c(%rbx) # imm = 0x7FFFFFFF
leaq 0x88c8(%rbx), %rdi
xorl %esi, %esi
callq 0x4780
leaq 0x88f0(%rbx), %rdi
xorl %esi, %esi
callq 0x4780
leaq 0x8918(%rbx), %rdi
xorl %esi, %esi
callq 0x4780
movq %rbx, %rdi
addq $0x8948, %rdi           # imm = 0x8948
xorl %esi, %esi
callq 0x4780
movl $0x12, %r14d
callq 0x5076f
movq %rax, (%rbx,%r14,8)
testq %rax, %rax
je 0x554b9
incq %r14
cmpq $0x41, %r14
jne 0x55456
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
movq %rax, 0x208(%rbx)
testq %rax, %rax
je 0x554b9
movl $0xfffffffe, 0x258(%rbx) # imm = 0xFFFFFFFE
movl $0x1, 0x8890(%rbx)
xorl %eax, %eax
movq %rax, 0x80(%rbx)
movq %rax, 0x8940(%rbx)
movq %rbx, %rdi
callq 0x50b2f
cmpl $-0x1, %eax
jne 0x554c3
movq %rbx, %rdi
callq 0x45c0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq