// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x1, %edi
movl $0x8900, %esi           # imm = 0x8900
callq 0x4370
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x51a2b
movq $0x0, -0x8(%rbp)
jmp 0x51e71
movq -0x38(%rbp), %rax
movl $0x5, 0x78(%rax)
movl $0x0, -0x24(%rbp)
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x51a92
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x30, %eax
jl 0x51a85
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x39, %eax
jg 0x51a85
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movsbl (%rax,%rcx), %ecx
subl $0x30, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x78(%rax)
jmp 0x51a92
jmp 0x51a87
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x51a3d
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, 0x8840(%rax)
movq -0x38(%rbp), %rax
cmpl $0x72, 0x8(%rax)
jne 0x51b25
movq -0x38(%rbp), %rdi
callq 0x4fcf0
movq -0x38(%rbp), %rcx
movq %rax, 0x10(%rcx)
cmpq $0x0, %rax
jne 0x51ae0
jmp 0x51e59
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 0x4fe70
movq -0x38(%rbp), %rcx
movq %rax, 0x18(%rcx)
cmpq $0x0, %rax
jne 0x51b20
jmp 0x51e59
jmp 0x51bc7
movl $0x1, %edi
movl $0x1a, %esi
callq 0x4370
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x51b4c
movq $0x0, -0x8(%rbp)
jmp 0x51e71
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 0x42d0
movq -0x40(%rbp), %rdi
addq $0x6, %rdi
movq -0x18(%rbp), %rsi
movl $0x14, %edx
callq 0x42c0
movl 0x38bb9(%rip), %ecx     # 0x8a770
shll $0x8, %ecx
addl 0x38c6c(%rip), %ecx     # 0x8a82c
movq -0x38(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x38(%rbp), %rdi
callq 0x51e80
movq -0x18(%rbp), %rdi
movl $0x2f, %esi
callq 0x4640
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
je 0x51bf6
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x51bfe
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x4aa0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x51c22
jmp 0x51e59
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x44(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x40(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x38(%rax)
callq 0x4b240
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x51c70
jmp 0x51e59
movq -0x38(%rbp), %rax
movl $0xfffffffe, 0x60(%rax) # imm = 0xFFFFFFFE
movq -0x38(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x1f8(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x1fc(%rax)
movq -0x38(%rbp), %rax
movl $0x2710, 0x200(%rax)    # imm = 0x2710
movq -0x38(%rbp), %rax
movl $0x1, 0x204(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x208(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x20c(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x210(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x88fc(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x214(%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, 0x218(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x21c(%rax)
movq -0x38(%rbp), %rax
movl $0xffffffff, 0x8850(%rax) # imm = 0xFFFFFFFF
movq -0x38(%rbp), %rax
movl $0x0, 0x8854(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x220(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8838(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x885c(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8860(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x8868(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x88f0(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x88f8(%rax)
movq -0x38(%rbp), %rax
movl $0x7fffffff, 0x224(%rax) # imm = 0x7FFFFFFF
movl $0x0, -0x24(%rbp)
cmpl $0x2f, -0x24(%rbp)
jge 0x51df2
callq 0x48400
movq %rax, %rdx
movq -0x38(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, 0x80(%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x51dc9
movq -0x38(%rbp), %rax
movl $0xfffffffe, 0x228(%rax) # imm = 0xFFFFFFFE
movq -0x38(%rbp), %rax
movl $0x1, 0x8848(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x70(%rax)
movq -0x38(%rbp), %rax
movq $0x0, 0x88c0(%rax)
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x489e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x51e4f
jmp 0x51e59
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x51e71
cmpq $0x0, -0x38(%rbp)
je 0x51e69
movq -0x38(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)