// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rsi, %r14
movq %rdi, %r15
movl $0x1, %edi
movl $0x8a18, %esi           # imm = 0x8A18
callq 0x4300
testq %rax, %rax
je 0x52972
movq %rax, %rbx
movl $0x5, 0x88(%rax)
movzbl (%r12), %eax
testl %eax, %eax
je 0x52711
movsbl %al, %ecx
incq %r12
addb $-0x30, %al
cmpb $0x9, %al
jbe 0x52719
movb (%r12), %al
incq %r12
testb %al, %al
jne 0x526fe
jmp 0x52722
movq %r15, (%rbx)
movl %eax, 0x8(%rbx)
jmp 0x52780
movzbl %al, %eax
movl %eax, 0x88(%rbx)
movq %r15, (%rbx)
movl %ecx, 0x8(%rbx)
cmpb $0x72, %cl
jne 0x52780
movq %rbx, %rdi
callq 0x51760
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x5296a
movzwl 0x4(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x52983
movq %rbx, %rdi
callq 0x51905
movq %rax, 0x18(%rbx)
testq %rax, %rax
jne 0x527c5
movq 0x10(%rbx), %rdi
testq %rdi, %rdi
je 0x5296a
callq 0x45c0
jmp 0x5296a
movl $0x1, %edi
movl $0x1a, %esi
callq 0x4300
testq %rax, %rax
je 0x52972
movq %rax, 0x10(%rbx)
movl $0x4d415243, (%rax)     # imm = 0x4D415243
addq $0x6, %rax
movl $0x14, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x4250
movl $0x301, 0xc(%rbx)       # imm = 0x301
movq %rbx, %rdi
callq 0x52983
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 0x5296a
movq $-0x1, 0x48(%rbx)
movq $0x0, 0x28(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%rbx)
callq 0x4fc28
movq %rax, 0x50(%rbx)
testq %rax, %rax
je 0x5296a
movl $0xfffffffe, 0x68(%rbx) # imm = 0xFFFFFFFE
xorl %eax, %eax
movq %rax, 0x58(%rbx)
movaps 0x483f4(%rip), %xmm0    # 0x9ac20
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)
movaps 0x483b2(%rip), %xmm0    # 0x9ac30
movups %xmm0, 0x8898(%rbx)
movq %rax, 0x8878(%rbx)
movl %eax, 0x88b0(%rbx)
movups %xmm1, 0x8970(%rbx)
movups %xmm1, 0x88b8(%rbx)
movsd 0x48398(%rip), %xmm0    # 0x9ac40
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
movl $0x12, %r14d
movabsq $0x2300000002, %r15     # imm = 0x2300000002
movl $0x1, %edi
movl $0x228, %esi            # imm = 0x228
callq 0x4300
testq %rax, %rax
je 0x5296a
movq %r15, (%rax)
movq %rax, (%rbx,%r14,8)
incq %r14
cmpq $0x41, %r14
jne 0x528fa
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
movq %rax, 0x208(%rbx)
testq %rax, %rax
je 0x5296a
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 0x4e1d0
cmpl $-0x1, %eax
jne 0x52974
movq %rbx, %rdi
callq 0x45c0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq