// File: /workspace/github_c_makefile_star2_O3/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
pushq %rax
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
movl $0x1, %edi
movl $0x9d80, %esi           # imm = 0x9D80
callq 0x6470
testq %rax, %rax
je 0x5cc3c
movq %rax, %rbx
movl $0x5, 0x78(%rax)
movzbl (%r12), %eax
testl %eax, %eax
je 0x5c58d
movsbl %al, %ecx
incq %r12
addb $-0x30, %al
cmpb $0x9, %al
jbe 0x5c595
movb (%r12), %al
incq %r12
testb %al, %al
jne 0x5c57a
jmp 0x5c59b
movq %r15, (%rbx)
movl %eax, 0x8(%rbx)
jmp 0x5c5f5
movzbl %al, %eax
movl %eax, 0x78(%rbx)
movq %r15, (%rbx)
movl %ecx, 0x8(%rbx)
cmpb $0x72, %cl
jne 0x5c5f5
movq %rbx, %rdi
callq 0x5b54a
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x5cc34
movzwl 0x4(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x5b67c
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x5cc34
movq %r14, (%rsp)
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rbx), %eax
cmpl $0x100, %eax            # imm = 0x100
sete %bpl
jmp 0x5c638
movl $0x1, %edi
movl $0x1a, %esi
callq 0x6470
testq %rax, %rax
je 0x5cc3c
movq %rax, 0x10(%rbx)
movl $0x4d415243, (%rax)     # imm = 0x4D415243
addq $0x6, %rax
movl $0x14, %edx
movq %rax, %rdi
movq %r14, (%rsp)
movq %r14, %rsi
callq 0x6380
movl $0x300, 0xc(%rbx)       # imm = 0x300
xorl %ebp, %ebp
leaq 0x8234(%rbx), %rdi
movl $0x100, %edx            # imm = 0x100
movl $0x4, %esi
callq 0x63b0
xorl %r15d, %r15d
movb %r15b, 0x8275(%rbx)
movb %r15b, 0x8295(%rbx)
movb $0x1, %r12b
movb %r12b, 0x8277(%rbx)
movb %r12b, 0x8297(%rbx)
movb $0x2, %r13b
movb %r13b, 0x827b(%rbx)
movb %r13b, 0x829b(%rbx)
movb $0x3, %r14b
movb %r14b, 0x8288(%rbx)
movb %r14b, 0x82a8(%rbx)
leaq 0x8334(%rbx), %rdi
movl $0x100, %edx            # imm = 0x100
movl $0x5, %esi
callq 0x63b0
movb %r15b, 0x8375(%rbx)
movb %r15b, 0x8395(%rbx)
movb %r12b, 0x8377(%rbx)
movb %r12b, 0x8397(%rbx)
movb %r13b, 0x837b(%rbx)
movb %r13b, 0x839b(%rbx)
movb %r14b, 0x8388(%rbx)
movb %r14b, 0x83a8(%rbx)
movb $0x4, %al
movb %al, 0x8382(%rbx)
movb %al, 0x83a2(%rbx)
testb %bpl, %bpl
je 0x5c9f6
movdqa 0x31f80(%rip), %xmm10   # 0x8e680
movdqa 0x31f88(%rip), %xmm4    # 0x8e690
movdqa 0x31290(%rip), %xmm5    # 0x8d9a0
xorl %eax, %eax
movdqa 0x31296(%rip), %xmm6    # 0x8d9b0
movaps 0x3c5ff(%rip), %xmm7    # 0x98d20
movaps %xmm7, %xmm8
shufps $0xdd, %xmm7, %xmm8     # xmm8 = xmm8[1,3],xmm7[1,3]
movdqa 0x3c5fe(%rip), %xmm0    # 0x98d30
movdqa 0x32496(%rip), %xmm1    # 0x8ebd0
movdqa 0x3c5fe(%rip), %xmm2    # 0x98d40
movdqa 0x3c635(%rip), %xmm12   # 0x98d80
movdqa 0x3c63c(%rip), %xmm13   # 0x98d90
movdqa 0x31f43(%rip), %xmm14   # 0x8e6a0
movdqa %xmm4, %xmm15
pxor %xmm6, %xmm15
movdqa %xmm15, %xmm9
pcmpgtd %xmm7, %xmm9
movdqa %xmm10, %xmm3
pxor %xmm6, %xmm3
shufps $0xdd, %xmm3, %xmm15    # xmm15 = xmm15[1,3],xmm3[1,3]
pcmpgtd %xmm7, %xmm3
movdqa %xmm9, %xmm11
shufps $0x88, %xmm3, %xmm11    # xmm11 = xmm11[0,2],xmm3[0,2]
pcmpeqd %xmm8, %xmm15
pand %xmm11, %xmm15
shufps $0xdd, %xmm3, %xmm9     # xmm9 = xmm9[1,3],xmm3[1,3]
orps %xmm15, %xmm9
movdqa %xmm5, %xmm15
psrld $0x6, %xmm15
pand %xmm0, %xmm15
psubd %xmm9, %xmm15
movdqa %xmm5, %xmm3
psrld $0x4, %xmm3
pand %xmm1, %xmm3
movdqa %xmm5, %xmm9
psrld $0x1, %xmm9
pand %xmm2, %xmm9
por %xmm3, %xmm9
movdqa %xmm5, %xmm3
pslld $0x2, %xmm3
pand 0x3c568(%rip), %xmm3    # 0x98d50
por %xmm9, %xmm3
movdqa %xmm5, %xmm9
pslld $0x4, %xmm9
pand 0x3c55f(%rip), %xmm9    # 0x98d60
por %xmm3, %xmm9
movdqa %xmm5, %xmm3
pslld $0x6, %xmm3
pand 0x3c559(%rip), %xmm3    # 0x98d70
por %xmm9, %xmm3
movdqa %xmm5, %xmm9
pslld $0x8, %xmm9
pand %xmm12, %xmm9
por %xmm3, %xmm9
por %xmm15, %xmm9
movdqa %xmm5, %xmm3
pslld $0xa, %xmm3
pand %xmm13, %xmm3
por %xmm9, %xmm3
movdqu %xmm3, 0x234(%rbx,%rax,4)
addq $0x4, %rax
paddq %xmm14, %xmm4
paddq %xmm14, %xmm10
paddd %xmm1, %xmm5
cmpq $0x200, %rax            # imm = 0x200
jne 0x5c75d
movdqa 0x31127(%rip), %xmm12   # 0x8d9a0
xorl %eax, %eax
pxor %xmm5, %xmm5
movdqa 0x3c529(%rip), %xmm6    # 0x98db0
movdqa 0x3c531(%rip), %xmm7    # 0x98dc0
movdqa 0x3c538(%rip), %xmm8    # 0x98dd0
movdqa 0x3c53f(%rip), %xmm9    # 0x98de0
movdqa 0x3c546(%rip), %xmm10   # 0x98df0
movdqa 0x322fd(%rip), %xmm11   # 0x8ebb0
movdqa %xmm12, %xmm4
movq (%rsp), %r14
movdqa %xmm4, %xmm13
pand 0x3c4d6(%rip), %xmm13   # 0x98da0
movdqa %xmm13, %xmm14
punpckhbw %xmm5, %xmm14   # xmm14 = xmm14[8],xmm5[8],xmm14[9],xmm5[9],xmm14[10],xmm5[10],xmm14[11],xmm5[11],xmm14[12],xmm5[12],xmm14[13],xmm5[13],xmm14[14],xmm5[14],xmm14[15],xmm5[15]
pshuflw $0x1b, %xmm14, %xmm14   # xmm14 = xmm14[3,2,1,0,4,5,6,7]
pshufhw $0x1b, %xmm14, %xmm14   # xmm14 = xmm14[0,1,2,3,7,6,5,4]
punpcklbw %xmm5, %xmm13   # xmm13 = xmm13[0],xmm5[0],xmm13[1],xmm5[1],xmm13[2],xmm5[2],xmm13[3],xmm5[3],xmm13[4],xmm5[4],xmm13[5],xmm5[5],xmm13[6],xmm5[6],xmm13[7],xmm5[7]
pshuflw $0x1b, %xmm13, %xmm13   # xmm13 = xmm13[3,2,1,0,4,5,6,7]
pshufhw $0x1b, %xmm13, %xmm13   # xmm13 = xmm13[0,1,2,3,7,6,5,4]
packuswb %xmm14, %xmm13
movdqa %xmm13, %xmm14
psllw $0x4, %xmm14
psrlw $0x4, %xmm13
pand %xmm6, %xmm13
por %xmm14, %xmm13
movdqa %xmm13, %xmm14
psrlw $0x2, %xmm14
pand %xmm7, %xmm14
pand %xmm7, %xmm13
psllw $0x2, %xmm13
por %xmm14, %xmm13
movdqa %xmm13, %xmm14
psrlw $0x1, %xmm14
pand %xmm8, %xmm14
pand %xmm8, %xmm13
paddb %xmm13, %xmm13
por %xmm14, %xmm13
movdqa %xmm12, %xmm14
paddd %xmm12, %xmm14
pand %xmm9, %xmm14
movdqa %xmm12, %xmm15
psrld $0x2, %xmm15
pand %xmm2, %xmm15
por %xmm14, %xmm15
movdqa %xmm12, %xmm14
psrld $0x4, %xmm14
pand %xmm10, %xmm14
movdqa %xmm12, %xmm3
psrld $0x6, %xmm3
pand %xmm1, %xmm3
por %xmm14, %xmm3
por %xmm15, %xmm3
movdqa %xmm12, %xmm14
psrld $0x8, %xmm14
pand %xmm0, %xmm14
movdqa %xmm12, %xmm15
psrld $0xa, %xmm15
pand %xmm11, %xmm15
por %xmm14, %xmm15
por %xmm3, %xmm15
psrld $0x17, %xmm13
por %xmm13, %xmm15
movdqu %xmm15, 0x4234(%rbx,%rax,4)
addq $0x4, %rax
paddd %xmm1, %xmm12
paddd %xmm1, %xmm4
cmpq $0x1000, %rax           # imm = 0x1000
jne 0x5c8bc
jmp 0x5ca48
movdqa 0x30fa2(%rip), %xmm1    # 0x8d9a0
xorl %eax, %eax
movdqa 0x321c8(%rip), %xmm0    # 0x8ebd0
movdqu %xmm1, 0x234(%rbx,%rax,4)
addq $0x4, %rax
paddd %xmm0, %xmm1
cmpq $0x1000, %rax           # imm = 0x1000
jne 0x5ca08
movdqa 0x30f77(%rip), %xmm1    # 0x8d9a0
xorl %eax, %eax
movq (%rsp), %r14
movdqu %xmm1, 0x4234(%rbx,%rax,4)
addq $0x4, %rax
paddd %xmm0, %xmm1
cmpq $0x1000, %rax           # imm = 0x1000
jne 0x5ca2f
movq %rbx, %r15
addq $0x8434, %r15           # imm = 0x8434
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movl $0x4, %esi
callq 0x63b0
movl $0x8435, %eax           # imm = 0x8435
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
cmpq $0x8835, %rax           # imm = 0x8835
jne 0x5ca69
movq $-0x4, %rax
leaq 0x3ca9e(%rip), %rcx     # 0x99539
movaps 0x3c35e(%rip), %xmm0    # 0x98e00
leaq 0x3ca96(%rip), %rdx     # 0x9953f
movzbl (%rcx), %esi
andl $0x1f, %esi
shll $0x5, %esi
movups %xmm0, (%r15,%rsi)
movl $0x3030303, 0x10(%r15,%rsi) # imm = 0x3030303
movzbl 0x4(%rax,%rdx), %edi
andl $0x1f, %edi
addq %r15, %rsi
movb $0x0, (%rdi,%rsi)
movzbl 0x5(%rax,%rdx), %edi
andl $0x1f, %edi
movb $0x1, (%rdi,%rsi)
movzbl 0x6(%rax,%rdx), %edi
andl $0x1f, %edi
movb $0x2, (%rdi,%rsi)
movzbl 0x7(%rax,%rdx), %edi
andl $0x1f, %edi
movb $0x3, (%rdi,%rsi)
addq $0x4, %rax
incq %rcx
cmpq $0x10, %rax
jb 0x5caa9
movq %r14, %rdi
movl $0x2f, %esi
callq 0x67c0
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %r14, %rdi
callq 0x6dd0
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x5cc34
movq $-0x1, 0x40(%rbx)
xorl %r15d, %r15d
movq %r15, 0x28(%rbx)
movq %r15, 0x38(%rbx)
callq 0x593f8
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x5cc34
movq %rax, %r14
movl $0xfffffffe, 0x60(%rbx) # imm = 0xFFFFFFFE
movq %r15, 0x50(%rbx)
movaps 0x3c2ac(%rip), %xmm0    # 0x98e10
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)
movl %r15d, 0x21c(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x8850(%rbx)
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
movq %rax, 0x220(%rbx)
movq %r15, 0x8838(%rbx)
movq %r15, 0x88f0(%rbx)
movl %r15d, 0x88f8(%rbx)
movups %xmm0, 0x885c(%rbx)
movl %r15d, 0x886c(%rbx)
movl $0x10, %r15d
movabsq $0x3200000002, %r12     # imm = 0x3200000002
movl $0x1, %edi
movl $0x78, %esi
callq 0x6470
testq %rax, %rax
je 0x5cbf5
movq %r12, (%rax)
movq %rax, (%rbx,%r15,8)
incq %r15
cmpq $0x3f, %r15
jne 0x5cbde
movl $0xfffffffe, 0x228(%rbx) # imm = 0xFFFFFFFE
movl $0x1, 0x8848(%rbx)
xorl %eax, %eax
movq %rax, 0x70(%rbx)
movq %rax, 0x88c0(%rbx)
movq 0x18(%rbx), %rsi
movq %r14, %rdi
callq 0x57a34
cmpl $-0x1, %eax
jne 0x5cc3e
movq %rbx, %rdi
callq 0x68a0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq