// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/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->lossy_read_names = 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 $0x8900, %esi           # imm = 0x8900
callq 0x4360
testq %rax, %rax
je 0x38532
movq %rax, %rbx
movl $0x5, 0x78(%rax)
movzbl (%r12), %eax
testl %eax, %eax
je 0x37e8a
movsbl %al, %ecx
incq %r12
addb $-0x30, %al
cmpb $0x9, %al
jbe 0x37e92
movb (%r12), %al
incq %r12
testb %al, %al
jne 0x37e77
jmp 0x37e98
movq %r15, (%rbx)
movl %eax, 0x8(%rbx)
jmp 0x37ef2
movzbl %al, %eax
movl %eax, 0x78(%rbx)
movq %r15, (%rbx)
movl %ecx, 0x8(%rbx)
cmpb $0x72, %cl
jne 0x37ef2
movq %rbx, %rdi
callq 0x36e47
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x3852a
movzwl 0x4(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x36f79
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x3852a
movq %r14, (%rsp)
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rbx), %eax
cmpl $0x100, %eax            # imm = 0x100
sete %bpl
jmp 0x37f35
movl $0x1, %edi
movl $0x1a, %esi
callq 0x4360
testq %rax, %rax
je 0x38532
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 0x42b0
movl $0x300, 0xc(%rbx)       # imm = 0x300
xorl %ebp, %ebp
leaq 0x8234(%rbx), %rdi
movl $0x100, %edx            # imm = 0x100
movl $0x4, %esi
callq 0x42d0
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 0x42d0
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 0x382f3
movdqa 0x19e23(%rip), %xmm10   # 0x51e20
movdqa 0x19e2b(%rip), %xmm4    # 0x51e30
movdqa 0x1aa23(%rip), %xmm5    # 0x52a30
xorl %eax, %eax
movdqa 0x19e29(%rip), %xmm6    # 0x51e40
movaps 0x1aa32(%rip), %xmm7    # 0x52a50
movaps %xmm7, %xmm8
shufps $0xdd, %xmm7, %xmm8     # xmm8 = xmm8[1,3],xmm7[1,3]
movdqa 0x1aa31(%rip), %xmm0    # 0x52a60
movdqa 0x1aa09(%rip), %xmm1    # 0x52a40
movdqa 0x1aa31(%rip), %xmm2    # 0x52a70
movdqa 0x1aa68(%rip), %xmm12   # 0x52ab0
movdqa 0x1aa6f(%rip), %xmm13   # 0x52ac0
movdqa 0x19df6(%rip), %xmm14   # 0x51e50
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 0x1a99b(%rip), %xmm3    # 0x52a80
por %xmm9, %xmm3
movdqa %xmm5, %xmm9
pslld $0x4, %xmm9
pand 0x1a992(%rip), %xmm9    # 0x52a90
por %xmm3, %xmm9
movdqa %xmm5, %xmm3
pslld $0x6, %xmm3
pand 0x1a98c(%rip), %xmm3    # 0x52aa0
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 0x3805a
movdqa 0x1a8ba(%rip), %xmm12   # 0x52a30
xorl %eax, %eax
pxor %xmm5, %xmm5
movdqa 0x1a95c(%rip), %xmm6    # 0x52ae0
movdqa 0x1a964(%rip), %xmm7    # 0x52af0
movdqa 0x1a96b(%rip), %xmm8    # 0x52b00
movdqa 0x1a972(%rip), %xmm9    # 0x52b10
movdqa 0x1a979(%rip), %xmm10   # 0x52b20
movdqa 0x1a980(%rip), %xmm11   # 0x52b30
movdqa %xmm12, %xmm4
movq (%rsp), %r14
movdqa %xmm4, %xmm13
pand 0x1a909(%rip), %xmm13   # 0x52ad0
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 0x381b9
jmp 0x38345
movdqa 0x1a735(%rip), %xmm1    # 0x52a30
xorl %eax, %eax
movdqa 0x1a73b(%rip), %xmm0    # 0x52a40
movdqu %xmm1, 0x234(%rbx,%rax,4)
addq $0x4, %rax
paddd %xmm0, %xmm1
cmpq $0x1000, %rax           # imm = 0x1000
jne 0x38305
movdqa 0x1a70a(%rip), %xmm1    # 0x52a30
xorl %eax, %eax
movq (%rsp), %r14
movdqu %xmm1, 0x4234(%rbx,%rax,4)
addq $0x4, %rax
paddd %xmm0, %xmm1
cmpq $0x1000, %rax           # imm = 0x1000
jne 0x3832c
movq %rbx, %r15
addq $0x8434, %r15           # imm = 0x8434
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
movl $0x4, %esi
callq 0x42d0
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 0x38366
movq $-0x4, %rax
leaq 0x16989(%rip), %rcx     # 0x4ed21
movaps 0x1a7a1(%rip), %xmm0    # 0x52b40
leaq 0x197eb(%rip), %rdx     # 0x51b91
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 0x383a6
movq %r14, %rdi
movl $0x2f, %esi
callq 0x45a0
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %r14, %rdi
callq 0x49c0
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x3852a
movq $-0x1, 0x40(%rbx)
xorl %r15d, %r15d
movq %r15, 0x28(%rbx)
movq %r15, 0x38(%rbx)
callq 0x34cf5
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x3852a
movq %rax, %r14
movl $0xfffffffe, 0x60(%rbx) # imm = 0xFFFFFFFE
movq %r15, 0x50(%rbx)
movaps 0x1a6ef(%rip), %xmm0    # 0x52b50
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)
movups %xmm0, 0x88f0(%rbx)
movups %xmm0, 0x885c(%rbx)
movl %r15d, 0x886c(%rbx)
movl $0x10, %r15d
movabsq $0x3200000002, %r12     # imm = 0x3200000002
movl $0x1, %edi
movl $0x78, %esi
callq 0x4360
testq %rax, %rax
je 0x384eb
movq %r12, (%rax)
movq %rax, (%rbx,%r15,8)
incq %r15
cmpq $0x3f, %r15
jne 0x384d4
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 0x33138
cmpl $-0x1, %eax
jne 0x38534
movq %rbx, %rdi
callq 0x4610
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq