// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rdi
movl $0x8a18, %esi           # imm = 0x8A18
callq 0x4300
testq %rax, %rax
je 0x448cf
movq %rax, %rbx
movl $0x5, 0x88(%rax)
movq %r15, %rax
movb (%rax), %cl
testb %cl, %cl
je 0x446ae
addb $-0x30, %cl
incq %rax
cmpb $0x9, %cl
ja 0x44694
movzbl %cl, %eax
movl %eax, 0x88(%rbx)
movq %r12, (%rbx)
movsbl (%r15), %eax
movl %eax, 0x8(%rbx)
cmpl $0x72, %eax
jne 0x44707
movq %rbx, %rdi
callq 0x43991
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x448c7
movzwl 0x4(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x448e0
movq %rbx, %rdi
callq 0x43a42
movq %rax, 0x18(%rbx)
testq %rax, %rax
jne 0x44746
movq 0x10(%rbx), %rdi
callq 0x45c0
jmp 0x448c7
pushq $0x1
popq %rdi
pushq $0x1a
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x448cf
movq %rax, 0x10(%rbx)
movl $0x4d415243, (%rax)     # imm = 0x4D415243
addq $0x6, %rax
pushq $0x14
popq %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x4250
movl $0x301, 0xc(%rbx)       # imm = 0x301
movq %rbx, %rdi
callq 0x448e0
pushq $0x2f
popq %rsi
movq %r14, %rdi
callq 0x4530
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %r14, %rdi
callq 0x4930
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x448c7
orq $-0x1, 0x48(%rbx)
andq $0x0, 0x28(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rbx)
callq 0x421be
movq %rax, 0x50(%rbx)
testq %rax, %rax
je 0x448c7
movl $0xfffffffe, 0x68(%rbx) # imm = 0xFFFFFFFE
andq $0x0, 0x58(%rbx)
movaps 0x3837c(%rip), %xmm0    # 0x7cb20
movups %xmm0, 0x210(%rbx)
orl $-0x1, 0x220(%rbx)
andl $0x0, 0x8a10(%rbx)
xorps %xmm1, %xmm1
movups %xmm1, 0x224(%rbx)
movl 0xc(%rbx), %eax
xorl %ecx, %ecx
cmpl $0x300, %eax            # imm = 0x300
setge %cl
movl %ecx, 0x234(%rbx)
testb %al, %al
setne %al
andb %al, %cl
movzbl %cl, %eax
movl %eax, 0x240(%rbx)
andl $0x0, 0x238(%rbx)
movaps 0x3833c(%rip), %xmm0    # 0x7cb30
movups %xmm0, 0x8898(%rbx)
andq $0x0, 0x8878(%rbx)
andl $0x0, 0x88b0(%rbx)
movups %xmm1, 0x8970(%rbx)
movups %xmm1, 0x88b8(%rbx)
movsd 0x38320(%rip), %xmm0    # 0x7cb40
movups %xmm0, 0x248(%rbx)
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
pushq $0x12
popq %r14
cmpq $0x41, %r14
je 0x4487f
callq 0x407a0
movq %rax, (%rbx,%r14,8)
incq %r14
testq %rax, %rax
jne 0x44866
jmp 0x448c7
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x4300
movq %rax, 0x208(%rbx)
testq %rax, %rax
je 0x448c7
movl $0xfffffffe, 0x258(%rbx) # imm = 0xFFFFFFFE
movl $0x1, 0x8890(%rbx)
andq $0x0, 0x80(%rbx)
andq $0x0, 0x8940(%rbx)
movq %rbx, %rdi
callq 0x40af8
cmpl $-0x1, %eax
jne 0x448d1
movq %rbx, %rdi
callq 0x45c0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq