// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x1, %edi
movl $0x8a18, %esi           # imm = 0x8A18
callq 0x42f0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x78c0b
movq $0x0, -0x8(%rbp)
jmp 0x79189
movq -0x38(%rbp), %rax
movl $0x5, 0x88(%rax)
movl $0x0, -0x24(%rbp)
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x78c78
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x30, %eax
jl 0x78c6b
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x39, %eax
jg 0x78c6b
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
subl $0x30, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x88(%rax)
jmp 0x78c78
jmp 0x78c6d
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x78c20
movq -0x10(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movsbl (%rax), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8880(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8888(%rax)
movq -0x38(%rbp), %rax
cmpl $0x72, 0x8(%rax)
jne 0x78d30
movq -0x38(%rbp), %rdi
callq 0x775c0
movq -0x38(%rbp), %rcx
movq %rax, 0x10(%rcx)
cmpq $0x0, %rax
jne 0x78cd5
jmp 0x79171
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movzbl 0x4(%rax), %ecx
shll $0x8, %ecx
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movzbl 0x5(%rax), %eax
addl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x38(%rbp), %rdi
callq 0x791a0
movq -0x38(%rbp), %rdi
callq 0x77760
movq -0x38(%rbp), %rcx
movq %rax, 0x18(%rcx)
cmpq $0x0, %rax
jne 0x78d2b
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x77730
jmp 0x79171
jmp 0x78ddb
movl $0x1, %edi
movl $0x1a, %esi
callq 0x42f0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x78d57
movq $0x0, -0x8(%rbp)
jmp 0x79189
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rax
movb $0x43, (%rax)
movq -0x40(%rbp), %rax
movb $0x52, 0x1(%rax)
movq -0x40(%rbp), %rax
movb $0x41, 0x2(%rax)
movq -0x40(%rbp), %rax
movb $0x4d, 0x3(%rax)
movq -0x40(%rbp), %rax
movb $0x0, 0x4(%rax)
movq -0x40(%rbp), %rax
movb $0x0, 0x5(%rax)
movq -0x40(%rbp), %rdi
addq $0x6, %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x4250
movq -0x40(%rbp), %rdi
addq $0x6, %rdi
movq -0x18(%rbp), %rsi
movl $0x14, %edx
callq 0x4240
movl 0x94cbe(%rip), %ecx     # 0x10da80
shll $0x8, %ecx
addl 0x94cb9(%rip), %ecx     # 0x10da84
movq -0x38(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x38(%rbp), %rdi
callq 0x791a0
movq -0x18(%rbp), %rdi
movl $0x2f, %esi
callq 0x4510
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x78e01
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x78e09
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x4920
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x78e2d
jmp 0x79171
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x4c(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x48(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x40(%rax)
callq 0x74660
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jne 0x78e87
jmp 0x79171
movq -0x38(%rbp), %rax
movl $0xfffffffe, 0x68(%rax) # imm = 0xFFFFFFFE
movq -0x38(%rbp), %rax
movq $0x0, 0x58(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x210(%rax)
movq -0x38(%rbp), %rax
movl $0x2710, 0x214(%rax)    # imm = 0x2710
movq -0x38(%rbp), %rax
movl $0x4c4b40, 0x218(%rax)  # imm = 0x4C4B40
movq -0x38(%rbp), %rax
movl $0x1, 0x21c(%rax)
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x220(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movl $0x0, 0x224(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x228(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x8a10(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x22c(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x897c(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x230(%rax)
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x3, %eax
setge %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x234(%rax)
movq -0x38(%rbp), %rax
movl 0xc(%rax), %ecx
sarl $0x8, %ecx
xorl %eax, %eax
cmpl $0x3, %ecx
movb %al, -0x49(%rbp)
jl 0x78f80
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xff, %eax
cmpl $0x1, %eax
setge %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x240(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x238(%rax)
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x889c(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x88a0(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movl $0x0, 0x88a4(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x248(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x250(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x254(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x8898(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8878(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x88b0(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x88b8(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x88c0(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8970(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x8978(%rax)
movq -0x38(%rbp), %rax
movl $0x7fffffff, 0x24c(%rax) # imm = 0x7FFFFFFF
movq -0x38(%rbp), %rdi
addq $0x88c8, %rdi           # imm = 0x88C8
xorl %eax, %eax
movl %eax, %esi
callq 0x4760
movq -0x38(%rbp), %rdi
addq $0x88f0, %rdi           # imm = 0x88F0
xorl %eax, %eax
movl %eax, %esi
callq 0x4760
movq -0x38(%rbp), %rdi
addq $0x8918, %rdi           # imm = 0x8918
xorl %eax, %eax
movl %eax, %esi
callq 0x4760
movq -0x38(%rbp), %rdi
addq $0x8948, %rdi           # imm = 0x8948
xorl %eax, %eax
movl %eax, %esi
callq 0x4760
movl $0x0, -0x24(%rbp)
cmpl $0x2f, -0x24(%rbp)
jge 0x790ff
callq 0x714a0
movq %rax, %rdx
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, 0x90(%rax,%rcx,8)
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, 0x90(%rax,%rcx,8)
jne 0x790f2
jmp 0x79171
jmp 0x790f4
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x790bf
callq 0x79820
movq -0x38(%rbp), %rcx
movq %rax, 0x208(%rcx)
cmpq $0x0, %rax
jne 0x79117
jmp 0x79171
movq -0x38(%rbp), %rax
movl $0xfffffffe, 0x258(%rax) # imm = 0xFFFFFFFE
movq -0x38(%rbp), %rax
movl $0x1, 0x8890(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8940(%rax)
movq -0x38(%rbp), %rdi
callq 0x71cf0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x79167
jmp 0x79171
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x79189
cmpq $0x0, -0x38(%rbp)
je 0x79181
movq -0x38(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)