// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rsi, %r14
movq %rdi, %r12
movl $0x1, %edi
movl $0x8900, %esi           # imm = 0x8900
callq 0x4360
testq %rax, %rax
je 0x3918f
movq %rax, %rbx
movl $0x5, 0x78(%rax)
movb (%r15), %al
testb %al, %al
je 0x38c9d
leaq 0x1(%r15), %rcx
addb $-0x30, %al
cmpb $0x9, %al
jbe 0x38c97
movb (%rcx), %al
incq %rcx
testb %al, %al
jne 0x38c86
jmp 0x38c9d
movzbl %al, %eax
movl %eax, 0x78(%rbx)
movq %r12, (%rbx)
movsbl (%r15), %eax
movl %eax, 0x8(%rbx)
movq $0x0, 0x8840(%rbx)
cmpl $0x72, %eax
jne 0x38cf0
movq %rbx, %rdi
callq 0x37c5d
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x39187
movzwl 0x4(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x37d82
movq %rax, 0x18(%rbx)
testq %rax, %rax
jne 0x38d3c
jmp 0x39187
movl $0x1, %edi
movl $0x1a, %esi
callq 0x4360
testq %rax, %rax
je 0x3918f
movq %rax, 0x10(%rbx)
movl $0x4d415243, (%rax)     # imm = 0x4D415243
movq %rax, %rdi
addq $0x6, %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax)
movq $0x0, 0x12(%rax)
movl $0x14, %edx
movq %r14, %rsi
callq 0x42b0
movl $0x300, 0xc(%rbx)       # imm = 0x300
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, %bpl
movb %bpl, 0x8277(%rbx)
movb %bpl, 0x8297(%rbx)
movb $0x2, %r12b
movb %r12b, 0x827b(%rbx)
movb %r12b, 0x829b(%rbx)
movb $0x3, %r13b
movb %r13b, 0x8288(%rbx)
movb %r13b, 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 %bpl, 0x8377(%rbx)
movb %bpl, 0x8397(%rbx)
movb %r12b, 0x837b(%rbx)
movb %r12b, 0x839b(%rbx)
movb %r13b, 0x8388(%rbx)
movb %r13b, 0x83a8(%rbx)
movb $0x4, %al
movb %al, 0x8382(%rbx)
movb %al, 0x83a2(%rbx)
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rbx), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x38f6c
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
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 %r15d, %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, %r15d
cmpq $0x200, %r9             # imm = 0x200
jne 0x38e1b
xorl %eax, %eax
movl $0xf0f, %ecx            # imm = 0xF0F
movl $0x3333, %edx           # imm = 0x3333
xorl %esi, %esi
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
cmpq $0x1000, %rsi           # imm = 0x1000
jne 0x38ebc
jmp 0x38f94
xorl %eax, %eax
movl %eax, 0x234(%rbx,%rax,4)
incq %rax
cmpq $0x1000, %rax           # imm = 0x1000
jne 0x38f6e
xorl %eax, %eax
movl %eax, 0x4234(%rbx,%rax,4)
incq %rax
cmpq $0x1000, %rax           # imm = 0x1000
jne 0x38f82
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 0x38fb5
movq $-0x4, %rax
leaq 0x17d3a(%rip), %rcx     # 0x50d21
movaps 0x1b892(%rip), %xmm0    # 0x54880
leaq 0x1aaec(%rip), %rdx     # 0x53ae1
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 0x38ff5
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 0x39187
movq $-0x1, 0x40(%rbx)
xorl %eax, %eax
movq %rax, 0x28(%rbx)
movq %rax, 0x38(%rbx)
callq 0x35a93
movq %rax, 0x48(%rbx)
testq %rax, %rax
je 0x39187
movl $0xfffffffe, 0x60(%rbx) # imm = 0xFFFFFFFE
xorl %eax, %eax
movq %rax, 0x50(%rbx)
movaps 0x1b7e2(%rip), %xmm0    # 0x54890
movups %xmm0, 0x1f8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x208(%rbx)
xorl %ecx, %ecx
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge %cl
movl %ecx, 0x218(%rbx)
movq %rax, 0x21c(%rbx)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x8850(%rbx)
movq %rax, 0x8838(%rbx)
movups %xmm0, 0x88f0(%rbx)
movups %xmm0, 0x885c(%rbx)
movl $0x0, 0x886c(%rbx)
movl $0x7fffffff, 0x224(%rbx) # imm = 0x7FFFFFFF
movl $0x10, %r14d
movabsq $0x3200000002, %r15     # imm = 0x3200000002
movl $0x1, %edi
movl $0x78, %esi
callq 0x4360
testq %rax, %rax
je 0x39145
movq %r15, (%rax)
movq $0x0, 0x20(%rax)
movl $0x0, 0x40(%rax)
jmp 0x39147
xorl %eax, %eax
movq %rax, (%rbx,%r14,8)
incq %r14
cmpq $0x3f, %r14
jne 0x3911d
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 0x48(%rbx), %rdi
callq 0x33e56
cmpl $-0x1, %eax
jne 0x39191
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