// File: karel-brinda[P]ococo/ext/htslib/cram/cram_decode.c
// Function: cram_to_bam
static int cram_to_bam(SAM_hdr *bfd, cram_fd *fd, cram_slice *s,
		       cram_record *cr, int rec, bam_seq_t **bam) {
    int bam_idx, rg_len;
    char name_a[1024], *name;
    int name_len;
    char *aux, *aux_orig;
    char *seq, *qual;

    /* Assign names if not explicitly set */
    if (fd->required_fields & SAM_QNAME) {
	if (cr->name_len) {
	    name = (char *)BLOCK_DATA(s->name_blk) + cr->name;
	    name_len = cr->name_len;
	} else {
	    name = name_a;
	    name_len = strlen(fd->prefix);
	    memcpy(name, fd->prefix, name_len);
	    name += name_len;
	    *name++ = ':';
	    if (cr->mate_line >= 0 && cr->mate_line < rec)
		name = (char *)append_uint64((unsigned char *)name,
					     s->hdr->record_counter +
					     cr->mate_line + 1);
	    else
		name = (char *)append_uint64((unsigned char *)name,
					     s->hdr->record_counter +
					     rec + 1);
	    name_len = name - name_a;
	    name = name_a;
	}
    } else {
	name = "?";
	name_len = 1;
    }

    /* Generate BAM record */
    if (cr->rg < -1 || cr->rg >= bfd->nrg)
	return -1;
    rg_len = (cr->rg != -1) ? bfd->rg[cr->rg].name_len + 4 : 0;

    if (fd->required_fields & (SAM_SEQ | SAM_QUAL)) {
	if (!BLOCK_DATA(s->seqs_blk))
	    return -1;
	seq = (char *)BLOCK_DATA(s->seqs_blk) + cr->seq;
    } else {
	seq = "*";
	cr->len = 0;
    }


    if (fd->required_fields & SAM_QUAL) {
	if (!BLOCK_DATA(s->qual_blk))
	    return -1;
	qual = (char *)BLOCK_DATA(s->qual_blk) + cr->qual;
    } else {
	qual = NULL;
    }

    bam_idx = bam_construct_seq(bam, cr->aux_size + rg_len,
				name, name_len,
				cr->flags,
				cr->ref_id,
				cr->apos,
				cr->aend,
				cr->mqual,
				cr->ncigar, &s->cigar[cr->cigar],
				cr->mate_ref_id,
				cr->mate_pos,
				cr->tlen,
				cr->len,
				seq,
				qual);
    if (bam_idx == -1)
	return -1;

    aux = aux_orig = (char *)bam_aux(*bam);

    /* Auxiliary strings */
    if (cr->aux_size != 0) {
	memcpy(aux, BLOCK_DATA(s->aux_blk) + cr->aux, cr->aux_size);
	aux += cr->aux_size;
    }

    /* RG:Z: */
    if (cr->rg != -1) {
	int len = bfd->rg[cr->rg].name_len;
	*aux++ = 'R'; *aux++ = 'G'; *aux++ = 'Z';
	memcpy(aux, bfd->rg[cr->rg].name, len);
	aux += len;
	*aux++ = 0;
    }
    
    return bam_idx + (aux - aux_orig);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4f8, %rsp            # imm = 0x4F8
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movl %r8d, -0x54(%rbp)
movq %r9, -0x60(%rbp)
movq -0x40(%rbp), %rax
movl 0x224(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2ebef
movq -0x50(%rbp), %rax
cmpl $0x0, 0x24(%rax)
je 0x2eaec
movq -0x48(%rbp), %rax
movq 0x60(%rax), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x20(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x478(%rbp)
movq -0x50(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x47c(%rbp)
jmp 0x2ebed
leaq -0x470(%rbp), %rax
movq %rax, -0x478(%rbp)
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x4230
movl %eax, -0x47c(%rbp)
movq -0x478(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rsi
movslq -0x47c(%rbp), %rdx
callq 0x4460
movl -0x47c(%rbp), %ecx
movq -0x478(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x478(%rbp)
movq -0x478(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x478(%rbp)
movb $0x3a, (%rax)
movq -0x50(%rbp), %rax
cmpl $0x0, 0x28(%rax)
jl 0x2eb9f
movq -0x50(%rbp), %rax
movl 0x28(%rax), %eax
cmpl -0x54(%rbp), %eax
jge 0x2eb9f
movq -0x478(%rbp), %rdi
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
movq -0x50(%rbp), %rax
movslq 0x28(%rax), %rax
addq %rax, %rsi
addq $0x1, %rsi
callq 0x2fd00
movq %rax, -0x478(%rbp)
jmp 0x2ebc8
movq -0x478(%rbp), %rdi
movq -0x48(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
movslq -0x54(%rbp), %rax
addq %rax, %rsi
addq $0x1, %rsi
callq 0x2fd00
movq %rax, -0x478(%rbp)
movq -0x478(%rbp), %rax
leaq -0x470(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x47c(%rbp)
leaq -0x470(%rbp), %rax
movq %rax, -0x478(%rbp)
jmp 0x2ec07
leaq 0x4c429(%rip), %rax     # 0x7b01f
movq %rax, -0x478(%rbp)
movl $0x1, -0x47c(%rbp)
movq -0x50(%rbp), %rax
cmpl $-0x1, 0x1c(%rax)
jl 0x2ec21
movq -0x50(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x38(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jl 0x2ec2d
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x2eff2
movq -0x50(%rbp), %rax
cmpl $-0x1, 0x1c(%rax)
je 0x2ec5c
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x1c(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
addl $0x4, %eax
movl %eax, -0x4a8(%rbp)
jmp 0x2ec66
xorl %eax, %eax
movl %eax, -0x4a8(%rbp)
jmp 0x2ec66
movl -0x4a8(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movl 0x224(%rax), %eax
andl $0x600, %eax            # imm = 0x600
cmpl $0x0, %eax
je 0x2ecbe
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x2ec9e
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x2eff2
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x4c(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x498(%rbp)
jmp 0x2ecd7
leaq 0x4cad1(%rip), %rax     # 0x7b796
movq %rax, -0x498(%rbp)
movq -0x50(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x40(%rbp), %rax
movl 0x224(%rax), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
je 0x2ed26
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x2ed06
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x2eff2
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x20(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x50(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x4a0(%rbp)
jmp 0x2ed31
movq $0x0, -0x4a0(%rbp)
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rax
movl 0x40(%rax), %eax
addl -0x68(%rbp), %eax
movslq %eax, %rsi
movq -0x478(%rbp), %rdx
movslq -0x47c(%rbp), %rcx
movq -0x50(%rbp), %rax
movl 0xc(%rax), %r8d
movq -0x50(%rbp), %rax
movl 0x8(%rax), %r9d
movq -0x50(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x4bc(%rbp)
movq -0x50(%rbp), %rax
movl 0x5c(%rax), %eax
movl %eax, -0x4b8(%rbp)
movq -0x50(%rbp), %rax
movl 0x60(%rax), %eax
movl %eax, -0x4b4(%rbp)
movq -0x50(%rbp), %rax
movl 0x58(%rax), %r13d
movq -0x48(%rbp), %rax
movq 0x30(%rax), %r12
movq -0x50(%rbp), %rax
movslq 0x54(%rax), %rax
shlq $0x2, %rax
addq %rax, %r12
movq -0x50(%rbp), %rax
movl 0x2c(%rax), %r15d
movq -0x50(%rbp), %rax
movl 0x30(%rax), %r14d
movq -0x50(%rbp), %rax
movl 0x34(%rax), %ebx
movq -0x50(%rbp), %rax
movl 0x14(%rax), %r11d
movq -0x498(%rbp), %r10
movq -0x4a0(%rbp), %rax
movq %rax, -0x4b0(%rbp)
movl -0x4bc(%rbp), %eax
movl %eax, (%rsp)
movl -0x4b8(%rbp), %eax
movl %eax, 0x8(%rsp)
movl -0x4b4(%rbp), %eax
movl %eax, 0x10(%rsp)
movq -0x4b0(%rbp), %rax
movl %r13d, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movl %r15d, 0x28(%rsp)
movl %r14d, 0x30(%rsp)
movl %ebx, 0x38(%rsp)
movl %r11d, 0x40(%rsp)
movq %r10, 0x48(%rsp)
movq %rax, 0x50(%rsp)
callq 0x545a0
movl %eax, -0x64(%rbp)
cmpl $-0x1, -0x64(%rbp)
jne 0x2ee3f
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
jmp 0x2eff2
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq 0x28(%rax), %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
shrq $0x30, %rcx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movq 0x8(%rcx), %rcx
shrq $0x18, %rcx
andq $0xff, %rcx
movslq %ecx, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movl 0x10(%rcx), %ecx
addl $0x1, %ecx
sarl %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
movslq 0x10(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x490(%rbp)
movq %rax, -0x488(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x0, 0x40(%rax)
je 0x2ef02
movq -0x488(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x88(%rax), %rax
movq 0x20(%rax), %rsi
movq -0x50(%rbp), %rax
movslq 0x3c(%rax), %rax
addq %rax, %rsi
movq -0x50(%rbp), %rax
movslq 0x40(%rax), %rdx
callq 0x4460
movq -0x50(%rbp), %rax
movl 0x40(%rax), %ecx
movq -0x488(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x488(%rbp)
movq -0x50(%rbp), %rax
cmpl $-0x1, 0x1c(%rax)
je 0x2efd7
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x1c(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movl %eax, -0x4a4(%rbp)
movq -0x488(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x488(%rbp)
movb $0x52, (%rax)
movq -0x488(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x488(%rbp)
movb $0x47, (%rax)
movq -0x488(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x488(%rbp)
movb $0x5a, (%rax)
movq -0x488(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x50(%rbp), %rcx
movslq 0x1c(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movslq -0x4a4(%rbp), %rdx
callq 0x4460
movl -0x4a4(%rbp), %ecx
movq -0x488(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x488(%rbp)
movq -0x488(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x488(%rbp)
movb $0x0, (%rax)
movslq -0x64(%rbp), %rax
movq -0x488(%rbp), %rcx
movq -0x490(%rbp), %rdx
subq %rdx, %rcx
addq %rcx, %rax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)