// File: /workspace/github_c_makefile_star2_O1/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, %r15
movl 0xa0(%rsp), %ecx
movl 0x78(%rsp), %ebx
movq (%rdi), %rbp
leaq 0x1(%r15), %r12
leal 0x1(%rcx), %eax
shrl $0x1f, %eax
addl %ecx, %eax
incl %eax
sarl %eax
addl %r12d, %esi
leal (%rsi,%rbx,4), %r13d
addl %ecx, %r13d
addl %eax, %r13d
cmpl %r13d, 0x24(%rbp)
jge 0x86a57
leal -0x1(%r13), %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
movq %r12, %r14
movq %r15, %r12
movl %r9d, %r15d
movq %rdx, %rbx
callq 0x6c80
movq %rbx, %rdx
movl 0x78(%rsp), %ebx
movl %r15d, %r9d
movq %r12, %r15
movq %r14, %r12
movq %rax, 0x28(%rbp)
testq %rax, %rax
je 0x86c0b
movq %rdx, 0x20(%rsp)
movq 0xa8(%rsp), %r14
movl 0x90(%rsp), %edi
movl 0x68(%rsp), %eax
movl 0x60(%rsp), %ecx
leal (,%rbx,4), %edx
movq %rdx, 0x18(%rsp)
movl 0xa0(%rsp), %edx
movslq %edx, %rdx
movq %rdx, 0x10(%rsp)
movl %r13d, 0x20(%rbp)
movl %r9d, (%rbp)
leal -0x1(%rcx), %edx
movl %edx, 0x4(%rbp)
movslq %ecx, %r8
movslq %eax, %r9
decq %r9
movl $0x1249, %r10d          # imm = 0x1249
movl $0xe, %eax
movl $0x6, %esi
movl $0xc, %edx
movq %r8, %r11
movl %eax, %ecx
sarq %cl, %r11
movq %r9, %r13
sarq %cl, %r13
cmpq %r13, %r11
je 0x86aed
addq $0x3, %rax
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %edx, %ecx
shll %cl, %r11d
addl %r11d, %r10d
decl %esi
addl $-0x3, %edx
cmpl $0x1, %esi
ja 0x86aba
xorl %eax, %eax
jmp 0x86af4
addl %r11d, %r10d
movzwl %r10w, %eax
movl %r12d, %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
shlq $0x30, %rbx
orq %rax, %rbx
movq %rbx, 0x8(%rbp)
movl 0xa0(%rsp), %ebx
movl %ebx, 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), %rbp
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x6380
movb $0x0, (%rbp,%r15)
addq %r12, %rbp
movq %rbp, %rdi
movq 0x80(%rsp), %rsi
movq 0x18(%rsp), %r15
movq %r15, %rdx
callq 0x6570
addq %r15, %rbp
xorl %ecx, %ecx
leaq 0x149ad(%rip), %rax     # 0x9b530
cmpl $0x2, %ebx
jl 0x86bb8
movl %ebx, %edx
movl $0x1, %ecx
movzbl -0x1(%r14,%rcx), %esi
movb (%rsi,%rax), %sil
shlb $0x4, %sil
movzbl (%r14,%rcx), %edi
addb (%rdi,%rax), %sil
movb %sil, (%rbp)
incq %rbp
addq $0x2, %rcx
cmpq %rdx, %rcx
jb 0x86b8f
decl %ecx
cmpl %ebx, %ecx
movq 0xb0(%rsp), %rsi
jge 0x86bd7
movl %ecx, %ecx
movzbl (%r14,%rcx), %ecx
movb (%rcx,%rax), %al
shlb $0x4, %al
movb %al, (%rbp)
incq %rbp
movq %rbp, %rdi
testq %rsi, %rsi
je 0x86beb
movq 0x10(%rsp), %rdx
callq 0x6570
jmp 0x86bfa
movl $0xff, %esi
movq 0x10(%rsp), %rdx
callq 0x63b0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x86bfc