// File: karel-brinda[P]ococo/ext/htslib/cram/cram_samtools.c
// Function: bam_construct_seq
int bam_construct_seq(bam_seq_t **bp, size_t extra_len,
		      const char *qname, size_t qname_len,
		      int flag,
		      int rname,      // Ref ID
		      int pos,
		      int end,        // aligned start/end coords
		      int mapq,
		      uint32_t ncigar, const uint32_t *cigar,
		      int mrnm,       // Mate Ref ID
		      int mpos,
		      int isize,
		      int len,
		      const char *seq,
		      const char *qual) {
    static const char L[256] = {
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15, 0,15,15,
	15, 1,14, 2,13,15,15, 4,11,15,15,12,15, 3,15,15,
	15,15, 5, 6, 8,15, 7, 9,15,10,15,15,15,15,15,15,
	15, 1,14, 2,13,15,15, 4,11,15,15,12,15, 3,15,15,
	15,15, 5, 6, 8,15, 7, 9,15,10,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
	15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
    };
    bam1_t *b = (bam1_t *)*bp;
    uint8_t *cp;
    int i, bam_len;

    //b->l_aux = extra_len; // we fill this out later

    bam_len = qname_len + 1 + ncigar*4 + (len+1)/2 + len + extra_len;
    if (b->m_data < bam_len) {
	b->m_data = bam_len;
	kroundup32(b->m_data);
	b->data = (uint8_t*)realloc(b->data, b->m_data);
	if (!b->data)
	    return -1;
    }
    b->l_data = bam_len;

    b->core.tid     = rname;
    b->core.pos     = pos-1;
    b->core.bin     = bam_reg2bin(pos-1, end);
    b->core.qual    = mapq;
    b->core.l_qname = qname_len+1;
    b->core.flag    = flag;
    b->core.n_cigar = ncigar;
    b->core.l_qseq  = len;
    b->core.mtid    = mrnm;
    b->core.mpos    = mpos-1;
    b->core.isize   = isize;

    cp = b->data;

    strncpy((char *)cp, qname, qname_len);
    cp[qname_len] = 0;
    cp += qname_len+1;
    memcpy(cp, cigar, ncigar*4);
    cp += ncigar*4;

    for (i = 0; i+1 < len; i+=2) {
	*cp++ = (L[(uc)seq[i]]<<4) + L[(uc)seq[i+1]];
    }
    if (i < len)
	*cp++ = L[(uc)seq[i]]<<4;

    if (qual)
	memcpy(cp, qual, len);
    else
	memset(cp, '\xff', len);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
movl 0x50(%rbp), %eax
movl 0x48(%rbp), %eax
movl 0x40(%rbp), %eax
movl 0x38(%rbp), %eax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movl 0x28(%rbp), %ecx
shll $0x2, %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl 0x50(%rbp), %eax
addl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x50(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movslq 0x50(%rbp), %rcx
addq %rcx, %rax
addq -0x18(%rbp), %rax
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movl 0x24(%rax), %eax
cmpl -0x48(%rbp), %eax
jge 0x546fe
movl -0x48(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movl 0x24(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movl 0x24(%rax), %ecx
sarl %ecx
movq -0x38(%rbp), %rax
orl 0x24(%rax), %ecx
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movl 0x24(%rax), %ecx
sarl $0x2, %ecx
movq -0x38(%rbp), %rax
orl 0x24(%rax), %ecx
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movl 0x24(%rax), %ecx
sarl $0x4, %ecx
movq -0x38(%rbp), %rax
orl 0x24(%rax), %ecx
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movl 0x24(%rax), %ecx
sarl $0x8, %ecx
movq -0x38(%rbp), %rax
orl 0x24(%rax), %ecx
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movl 0x24(%rax), %ecx
sarl $0x10, %ecx
movq -0x38(%rbp), %rax
orl 0x24(%rax), %ecx
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movl 0x24(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x24(%rax)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x38(%rbp), %rax
movslq 0x24(%rax), %rsi
callq 0x4990
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x546fc
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5497a
jmp 0x546fe
movl -0x48(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x20(%rax)
movl -0x30(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movl 0x10(%rbp), %ecx
subl $0x1, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x4(%rax)
movl 0x10(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdi
movslq 0x18(%rbp), %rsi
movl $0xe, %edx
movl $0x5, %ecx
callq 0x54990
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, %ecx
movl %ecx, %edx
movq 0x8(%rax), %rcx
andq $0xffff, %rdx           # imm = 0xFFFF
andq $-0x10000, %rcx         # imm = 0xFFFF0000
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movl 0x20(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, %ecx
movl %ecx, %edx
movq 0x8(%rax), %rcx
andq $0xff, %rdx
shlq $0x10, %rdx
andq $-0xff0001, %rcx        # imm = 0xFF00FFFF
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %ecx, %ecx
movl %ecx, %edx
movq 0x8(%rax), %rcx
andq $0xff, %rdx
shlq $0x18, %rdx
movabsq $-0xff000001, %rsi      # imm = 0xFFFFFFFF00FFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movl -0x2c(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, %ecx
movl %ecx, %edx
movq 0x8(%rax), %rcx
andq $0xffff, %rdx           # imm = 0xFFFF
shlq $0x20, %rdx
movabsq $-0xffff00000001, %rsi  # imm = 0xFFFF0000FFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movl 0x28(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, %ecx
movl %ecx, %edx
movq 0x8(%rax), %rcx
andq $0xffff, %rdx           # imm = 0xFFFF
shlq $0x30, %rdx
movabsq $0xffffffffffff, %rsi   # imm = 0xFFFFFFFFFFFF
andq %rsi, %rcx
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movl 0x50(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x10(%rax)
movl 0x38(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x14(%rax)
movl 0x40(%rbp), %ecx
subl $0x1, %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x18(%rax)
movl 0x48(%rbp), %ecx
movq -0x38(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x42c0
movq -0x40(%rbp), %rax
movq -0x28(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x28(%rbp), %rax
addq $0x1, %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movq 0x30(%rbp), %rsi
movl 0x28(%rbp), %eax
shll $0x2, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x4460
movl 0x28(%rbp), %ecx
shll $0x2, %ecx
movq -0x40(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
cmpl 0x50(%rbp), %eax
jge 0x54910
movq 0x58(%rbp), %rax
movslq -0x44(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x28bf5(%rip), %rax     # 0x7d4c0
movsbl (%rax,%rcx), %eax
shll $0x4, %eax
movq 0x58(%rbp), %rcx
movl -0x44(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
movl %ecx, %edx
leaq 0x28bd4(%rip), %rcx     # 0x7d4c0
movsbl (%rcx,%rdx), %ecx
addl %ecx, %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x40(%rbp)
movb %cl, (%rax)
movl -0x44(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x44(%rbp)
jmp 0x548ab
movl -0x44(%rbp), %eax
cmpl 0x50(%rbp), %eax
jge 0x54947
movq 0x58(%rbp), %rax
movslq -0x44(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, %ecx
leaq 0x28b93(%rip), %rax     # 0x7d4c0
movsbl (%rax,%rcx), %eax
shll $0x4, %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x40(%rbp)
movb %cl, (%rax)
cmpq $0x0, 0x60(%rbp)
je 0x54961
movq -0x40(%rbp), %rdi
movq 0x60(%rbp), %rsi
movslq 0x50(%rbp), %rdx
callq 0x4460
jmp 0x54973
movq -0x40(%rbp), %rdi
movslq 0x50(%rbp), %rdx
movl $0xff, %esi
callq 0x42d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)