// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/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, 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, 0xc(%rsp)
movq %rcx, %rbx
movq %rdx, 0x20(%rsp)
movl 0xa0(%rsp), %r13d
movl 0x78(%rsp), %r8d
movq (%rdi), %rbp
leaq 0x1(%rcx), %r15
leal 0x1(%r13), %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
addl %r15d, %esi
leal (%rsi,%r8,4), %r14d
addl %r13d, %r14d
addl %eax, %r14d
pushq $-0x1
popq %r12
cmpl %r14d, 0x24(%rbp)
jge 0x7563a
leal -0x1(%r14), %eax
movl %eax, %ecx
sarl %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x10, %ecx
orl %eax, %ecx
incl %ecx
movl %ecx, 0x24(%rbp)
movq 0x28(%rbp), %rdi
movslq %ecx, %rsi
movl %r9d, %r13d
callq 0x6c80
movl %r13d, %r9d
movl 0xa0(%rsp), %r13d
movq %rax, 0x28(%rbp)
testq %rax, %rax
je 0x757db
movq %r15, 0x10(%rsp)
movq 0xa8(%rsp), %r15
movl 0x90(%rsp), %edi
movl 0x68(%rsp), %eax
movl 0x60(%rsp), %ecx
movl 0x78(%rsp), %edx
leal (,%rdx,4), %edx
movq %rdx, 0x18(%rsp)
movslq %r13d, %r13
movl %r14d, 0x20(%rbp)
movl %r9d, (%rbp)
leal -0x1(%rcx), %edx
movl %edx, 0x4(%rbp)
movslq %ecx, %rsi
movslq %eax, %r8
decq %r8
movl $0x1249, %r10d          # imm = 0x1249
pushq $0xe
popq %rax
pushq $0xc
popq %rdx
cmpl $-0x3, %edx
je 0x756b8
movq %rsi, %r11
movl %eax, %ecx
sarq %cl, %r11
movq %r8, %r14
sarq %cl, %r14
cmpq %r14, %r11
je 0x756bc
movl %r12d, %r11d
movl %edx, %ecx
shll %cl, %r11d
addq $0x3, %rax
addl %r11d, %r10d
addl $-0x3, %edx
jmp 0x7568c
xorl %eax, %eax
jmp 0x756c3
addl %r11d, %r10d
movzwl %r10w, %eax
movq 0x10(%rsp), %r14
movl %r14d, %ecx
shll $0x18, %ecx
movl 0x70(%rsp), %edx
movzbl %dl, %edx
shll $0x10, %edx
orl %eax, %edx
orl %ecx, %edx
movzwl 0xc(%rsp), %eax
shlq $0x20, %rax
orq %rdx, %rax
movl 0x78(%rsp), %ecx
shlq $0x30, %rcx
orq %rax, %rcx
movq %rcx, 0x8(%rbp)
movl 0xa0(%rsp), %eax
movl %eax, 0x10(%rbp)
movl 0x88(%rsp), %eax
movl %eax, 0x14(%rbp)
decl %edi
movl %edi, 0x18(%rbp)
movl 0x98(%rsp), %eax
movl %eax, 0x1c(%rbp)
movq 0x28(%rbp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0x6380
movb $0x0, (%r12,%rbx)
addq %r12, %r14
movq %r14, %rdi
movq 0x80(%rsp), %rsi
movq 0x18(%rsp), %r14
movq %r14, %rdx
callq 0x6570
addq %r14, %rbx
addq %r12, %rbx
xorl %eax, %eax
leaq 0x14d83(%rip), %rcx     # 0x8a4e0
xorl %edx, %edx
leaq 0x1(%rdx), %rsi
cmpq %r13, %rsi
jge 0x7578d
movzbl (%r15,%rax,2), %esi
movb (%rsi,%rcx), %sil
shlb $0x4, %sil
movzbl 0x1(%r15,%rax,2), %edi
addb (%rdi,%rcx), %sil
movb %sil, 0x1(%rbx,%rax)
addq $0x2, %rdx
incq %rax
jmp 0x7575f
leaq (%rax,%rbx), %rdi
incq %rdi
movl 0xa0(%rsp), %esi
cmpl %esi, %edx
jge 0x757b4
movzbl (%r15,%rax,2), %edx
movb (%rdx,%rcx), %cl
shlb $0x4, %cl
movb %cl, (%rdi)
leaq (%rax,%rbx), %rdi
addq $0x2, %rdi
movq 0xb0(%rsp), %rsi
testq %rsi, %rsi
je 0x757cb
movq %r13, %rdx
callq 0x6570
jmp 0x757d8
movl $0xff, %esi
movq %r13, %rdx
callq 0x63b0
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq