// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/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 = 5;
    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;

    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;

	if (!(fd->header = cram_read_SAM_hdr(fd)))
	    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;

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

    cram_init_tables(fd);

    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->refs  = refs_create();
    if (!fd->refs)
	goto err;
    fd->ref_id = -2;
    fd->ref = NULL;

    fd->decode_md = 0;
    fd->verbose = 0;
    fd->seqs_per_slice = SEQS_PER_SLICE;
    fd->slices_per_container = SLICE_PER_CNT;
    fd->embed_ref = 0;
    fd->no_ref = 0;
    fd->ignore_md5 = 0;
    fd->use_bz2 = 0;
    fd->use_rans = (CRAM_MAJOR_VERS(fd->version) >= 3);
    fd->use_lzma = 0;
    fd->multi_seq = -1;
    fd->unsorted   = 0;
    fd->shared_ref = 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;

    for (i = 0; i < DS_END; i++)
	fd->m[i] = cram_new_metrics();

    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->refs, fd, fd->header))
	goto err;

    return fd;

 err:
    if (fd)
	free(fd);

    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r13
pushq $0x1
popq %rdi
movl $0x9d80, %esi           # imm = 0x9D80
callq 0x6470
testq %rax, %rax
je 0x5395a
movq %rax, %r15
movq %rax, %rbx
movl $0x5, 0x78(%r15)
movq %r12, %rax
movb (%rax), %cl
testb %cl, %cl
je 0x53476
addb $-0x30, %cl
incq %rax
cmpb $0x9, %cl
ja 0x5345f
movzbl %cl, %eax
movl %eax, 0x78(%rbx)
movq %r13, (%rbx)
movsbl (%r12), %eax
movl %eax, 0x8(%rbx)
cmpl $0x72, %eax
jne 0x534d9
movq %rbx, %rdi
callq 0x525e1
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x53952
movzwl 0x4(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x52682
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x53952
movq %r14, 0x10(%rsp)
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rbx), %eax
cmpl $0x100, %eax            # imm = 0x100
sete %al
movl %eax, 0xc(%rsp)
jmp 0x5351d
pushq $0x1
popq %rdi
pushq $0x1a
popq %rsi
callq 0x6470
testq %rax, %rax
je 0x5395a
movq %rax, 0x10(%rbx)
movl $0x4d415243, (%rax)     # imm = 0x4D415243
addq $0x6, %rax
pushq $0x14
popq %rdx
movq %rax, %rdi
movq %r14, 0x10(%rsp)
movq %r14, %rsi
callq 0x6380
movl $0x300, 0xc(%rbx)       # imm = 0x300
movl $0x0, 0xc(%rsp)
leaq 0x8234(%r15), %rdi
pushq $0x4
popq %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x63b0
xorl %r12d, %r12d
movb %r12b, 0x8275(%rbx)
movb %r12b, 0x8295(%rbx)
movb $0x1, %r13b
movb %r13b, 0x8277(%rbx)
movb %r13b, 0x8297(%rbx)
movb $0x2, %r14b
movb %r14b, 0x827b(%rbx)
movb %r14b, 0x829b(%rbx)
movb $0x3, %bpl
movb %bpl, 0x8288(%rbx)
movb %bpl, 0x82a8(%rbx)
leaq 0x8334(%r15), %rdi
pushq $0x5
popq %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x63b0
movb %r12b, 0x8375(%rbx)
movb %r12b, 0x8395(%rbx)
movb %r13b, 0x8377(%rbx)
movb %r13b, 0x8397(%rbx)
movb %r14b, 0x837b(%rbx)
movb %r14b, 0x839b(%rbx)
movb %bpl, 0x8388(%rbx)
movb %bpl, 0x83a8(%rbx)
movb $0x4, %al
movb %al, 0x8382(%rbx)
movb %al, 0x83a2(%rbx)
cmpb $0x0, 0xc(%rsp)
je 0x5368d
movl $0x100, %eax            # imm = 0x100
movl $0x400, %ecx            # imm = 0x400
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0x10(%rsp), %r14
cmpq $0x200, %r9             # imm = 0x200
je 0x536ac
xorl %r10d, %r10d
cmpq $0x100, %r9             # imm = 0x100
setae %r10b
movl %r9d, %r11d
shrl $0x6, %r11d
andl $0x2, %r11d
orl %r10d, %r11d
movl %r9d, %r10d
shrl $0x4, %r10d
andl $0x4, %r10d
orl %r11d, %r10d
movl %r9d, %r11d
shrl %r11d
andl $0x10, %r11d
orl %r10d, %r11d
movl %r12d, %r10d
andl $0x40, %r10d
orl %r11d, %r10d
movl %edx, %r11d
andl $0x80, %r11d
orl %r10d, %r11d
movl %esi, %r10d
andl %eax, %r10d
orl %r11d, %r10d
movl %edi, %r11d
andl $0x200, %r11d           # imm = 0x200
movl %r8d, %ebp
andl %ecx, %ebp
orl %r11d, %ebp
orl %r10d, %ebp
movl %ebp, 0x234(%rbx,%r9,4)
incq %r9
addl %ecx, %r8d
addl %eax, %edi
addl $0x40, %esi
addl $0x10, %edx
addl $0x4, %r12d
jmp 0x535f5
xorl %eax, %eax
movq 0x10(%rsp), %r14
cmpq $0x1000, %rax           # imm = 0x1000
je 0x5376d
movl %eax, 0x234(%rbx,%rax,4)
incq %rax
jmp 0x53694
xorl %eax, %eax
movl $0xf0f, %ecx            # imm = 0xF0F
movl $0x3333, %edx           # imm = 0x3333
xorl %esi, %esi
cmpq $0x1000, %rsi           # imm = 0x1000
je 0x53783
movl %esi, %edi
andl $0x7, %edi
rolw $0x8, %di
movl %edi, %r8d
shrl $0x4, %r8d
andl %ecx, %r8d
andl %ecx, %edi
shll $0x4, %edi
orl %r8d, %edi
movl %edi, %r8d
andl %edx, %r8d
shrl $0x2, %edi
andl %edx, %edi
leal (%rdi,%r8,4), %edi
movl %edi, %r8d
andl $0x5540, %r8d           # imm = 0x5540
shrl %edi
andl $0x5500, %edi           # imm = 0x5500
leal (%rdi,%r8,2), %edi
shrl $0x7, %edi
movl %eax, %r8d
andl $0x20, %r8d
movl %esi, %r9d
shrl $0x2, %r9d
andl $0x10, %r9d
orl %r8d, %r9d
movl %esi, %r8d
shrl $0x4, %r8d
andl $0x8, %r8d
orl %r9d, %r8d
movl %esi, %r9d
shrl $0x6, %r9d
andl $0x4, %r9d
orl %r8d, %r9d
movl %esi, %r8d
shrl $0x8, %r8d
andl $0x2, %r8d
orl %r9d, %r8d
movl %esi, %r9d
shrl $0xa, %r9d
andl $0x1, %r9d
orl %r8d, %r9d
orl %edi, %r9d
movl %r9d, 0x4234(%rbx,%rsi,4)
incq %rsi
addl $0x2, %eax
jmp 0x536ba
xorl %eax, %eax
cmpq $0x1000, %rax           # imm = 0x1000
je 0x53783
movl %eax, 0x4234(%rbx,%rax,4)
incq %rax
jmp 0x5376f
addq $0x8434, %r15           # imm = 0x8434
pushq $0x4
popq %rsi
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
callq 0x63b0
movl $0x8435, %eax           # imm = 0x8435
cmpq $0x8835, %rax           # imm = 0x8835
je 0x537c5
movb $0x0, (%rbx,%rax)
movb $0x1, 0x2(%rbx,%rax)
movb $0x2, 0x6(%rbx,%rax)
movb $0x3, 0x13(%rbx,%rax)
movb $0x4, 0xd(%rbx,%rax)
addq $0x20, %rax
jmp 0x5379f
xorl %eax, %eax
leaq 0x34b16(%rip), %rcx     # 0x882e4
leaq 0x34b15(%rip), %rdx     # 0x882ea
cmpq $0x13, %rax
ja 0x5383f
movq %rax, %rsi
shrq $0x2, %rsi
movzbl (%rsi,%rcx), %esi
andl $0x1f, %esi
movl %esi, %edi
shll $0x5, %edi
addq %r15, %rdi
xorl %r8d, %r8d
cmpq $0x14, %r8
je 0x53804
movb $0x3, (%rdi,%r8)
incq %r8
jmp 0x537f4
movzbl (%rax,%rdx), %edi
andl $0x1f, %edi
shll $0x5, %esi
addq %r15, %rsi
movb $0x0, (%rdi,%rsi)
movzbl 0x1(%rax,%rdx), %edi
andl $0x1f, %edi
movb $0x1, (%rdi,%rsi)
movzbl 0x2(%rax,%rdx), %edi
andl $0x1f, %edi
movb $0x2, (%rdi,%rsi)
movzbl 0x3(%rax,%rdx), %edi
andl $0x1f, %edi
movb $0x3, (%rdi,%rsi)
addq $0x4, %rax
jmp 0x537d5
pushq $0x2f
popq %rsi
movq %r14, %rdi
callq 0x67c0
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %r14, %rdi
callq 0x6dd0
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x53952
orq $-0x1, 0x40(%rbx)
andq $0x0, 0x28(%rbx)
andq $0x0, 0x38(%rbx)
callq 0x50796
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x53952
movl $0xfffffffe, 0x60(%rbx) # imm = 0xFFFFFFFE
andq $0x0, 0x50(%rbx)
movaps 0x34325(%rip), %xmm0    # 0x87bc0
movups %xmm0, 0x1f8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x208(%rbx)
xorl %eax, %eax
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge %al
movl %eax, 0x218(%rbx)
andl $0x0, 0x21c(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x8850(%rbx)
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
movq %rax, 0x220(%rbx)
andq $0x0, 0x8838(%rbx)
andq $0x0, 0x88f0(%rbx)
andl $0x0, 0x88f8(%rbx)
movups %xmm0, 0x885c(%rbx)
andl $0x0, 0x886c(%rbx)
pushq $0x10
popq %r14
cmpq $0x3f, %r14
je 0x5391f
callq 0x4edbe
movq %rax, (%rbx,%r14,8)
incq %r14
jmp 0x5390b
movl $0xfffffffe, 0x228(%rbx) # imm = 0xFFFFFFFE
movl $0x1, 0x8848(%rbx)
andq $0x0, 0x70(%rbx)
andq $0x0, 0x88c0(%rbx)
movq 0x18(%rbx), %rsi
movq 0x48(%rbx), %rdi
callq 0x4f127
cmpl $-0x1, %eax
jne 0x5395c
movq %rbx, %rdi
callq 0x68a0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq