// File: /workspace/github_cpp_makefile_star2_O3/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
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), %r14d
movl 0x78(%rsp), %r13d
movq (%rdi), %rbp
leaq 0x1(%rcx), %r12
leal 0x1(%r14), %eax
shrl $0x1f, %eax
addl %r14d, %eax
incl %eax
sarl %eax
addl %r12d, %esi
leal (%rsi,%r13,4), %r15d
addl %r14d, %r15d
addl %eax, %r15d
cmpl %r15d, 0x24(%rbp)
jge 0x396a0
leal -0x1(%r15), %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
movl %r9d, %r12d
callq 0x48d0
movl %r12d, %r9d
movq %r14, %r12
movl 0xa0(%rsp), %r14d
movq %rax, 0x28(%rbp)
testq %rax, %rax
je 0x3984c
movq %rbx, 0x18(%rsp)
movq 0xa8(%rsp), %rbx
movl 0x90(%rsp), %edi
movl 0x68(%rsp), %eax
movl 0x60(%rsp), %ecx
leal (,%r13,4), %edx
movq %rdx, 0x10(%rsp)
movslq %r14d, %r14
movl %r15d, 0x20(%rbp)
movl %r9d, (%rbp)
decl %ecx
movl %ecx, 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, %r15
sarq %cl, %r15
cmpq %r15, %r11
je 0x3972a
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 0x396f7
xorl %eax, %eax
jmp 0x39731
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, %r13
orq %rax, %r13
movq %r13, 0x8(%rbp)
movl 0xa0(%rsp), %eax
movq %rax, %r15
movl %r15d, 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 0x18(%rsp), %r13
movq %r13, %rdx
callq 0x42b0
movb $0x0, (%rbp,%r13)
addq %r12, %rbp
movq %rbp, %rdi
movq 0x80(%rsp), %rsi
movq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x4420
addq %r12, %rbp
xorl %ecx, %ecx
leaq 0x19bb7(%rip), %rax     # 0x53380
cmpl $0x2, %r15d
jl 0x397f7
movl $0x1, %ecx
movzbl -0x1(%rbx,%rcx), %edx
movb (%rdx,%rax), %dl
shlb $0x4, %dl
movzbl (%rbx,%rcx), %esi
addb (%rsi,%rax), %dl
movb %dl, (%rbp)
incq %rbp
addq $0x2, %rcx
cmpq %r14, %rcx
jl 0x397d4
decl %ecx
movl 0xa0(%rsp), %edx
cmpl %edx, %ecx
movq 0xb0(%rsp), %rsi
jge 0x3981c
movl %ecx, %ecx
movzbl (%rbx,%rcx), %ecx
movb (%rcx,%rax), %al
shlb $0x4, %al
movb %al, (%rbp)
incq %rbp
movq %rbp, %rdi
testq %rsi, %rsi
je 0x3982e
movq %r14, %rdx
callq 0x4420
jmp 0x3983b
movl $0xff, %esi
movq %r14, %rdx
callq 0x42d0
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 0x3983d