// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/bwamem.c
// Function: mem_reg2sam
void mem_reg2sam(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, bseq1_t *s, mem_alnreg_v *a, int extra_flag, const mem_aln_t *m)
{
	extern char **mem_gen_alt(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, mem_alnreg_v *a, int l_query, const char *query);
	kstring_t str;
	kvec_t(mem_aln_t) aa;
	int k, l;
	char **XA = 0;

	if (!(opt->flag & MEM_F_ALL))
		XA = mem_gen_alt(opt, bns, pac, a, s->l_seq, s->seq);
	kv_init(aa);
	str.l = str.m = 0; str.s = 0;

	for (k = l = 0; k < a->n; ++k) {
		mem_alnreg_t *p = &a->a[k];
		mem_aln_t *q;

		if (p->score < opt->T) continue;
		if (p->secondary >= 0 && (p->is_alt || !(opt->flag&MEM_F_ALL))) continue;
		if (p->secondary >= 0 && p->secondary < INT_MAX && p->score < a->a[p->secondary].score * opt->drop_ratio) continue;

		q = kv_pushp(mem_aln_t, aa);
		*q = mem_reg2aln(opt, bns, pac, s->l_seq, s->seq, p);
		assert(q->rid >= 0); // this should not happen with the new code
		q->XA = XA? XA[k] : 0;
		q->flag |= extra_flag; // flag secondary
		if (p->secondary >= 0) q->sub = -1; // don't output sub-optimal score
		if (l && p->secondary < 0) // if supplementary
			q->flag |= (opt->flag&MEM_F_NO_MULTI)? 0x10000 : 0x800;
		if (l && !p->is_alt && q->mapq > aa.a[0].mapq) q->mapq = aa.a[0].mapq;

		// Cache mapping quality
		p->mapq = q->mapq;
		++l;
	}

	// Skip alignments filtered by competing frames
	if (!(a->active)) {
		for (k = 0; k < aa.n; ++k) free(aa.a[k].cigar);
		if (aa.n > 0) free(aa.a);
	}
	else if (aa.n == 0) { // no alignments good enough; then write an unaligned record
		mem_aln_t t;
		t = mem_reg2aln(opt, bns, pac, s->l_seq, s->seq, 0);
		t.flag |= extra_flag;
		mem_aln2sam(opt, bns, &str, s, 1, &t, 0, m);
	} else {
		for (k = 0; k < aa.n; ++k) {
			mem_aln2sam(opt, bns, &str, s, aa.n, aa.a, k, m);
		}
		for (k = 0; k < aa.n; ++k) free(aa.a[k].cigar);
		free(aa.a);
	}
	s->sam = str.s;
	if (XA) {
		for (k = 0; k < a->n; ++k) free(XA[k]);
		free(XA);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r9d, 0x24(%rsp)
movq %r8, %rbx
movq %rdi, %r10
xorl %edi, %edi
testb $0x8, 0x48(%r10)
movq %rcx, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdx, 0x40(%rsp)
jne 0x1a16b
movl (%rcx), %r8d
movq 0x18(%rcx), %r9
movq %r10, %rdi
movq %rbx, %rcx
callq 0x1dac9
xorl %edi, %edi
movq 0x18(%rsp), %r10
movq %rax, 0x8(%rsp)
jmp 0x1a174
movq $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebp, %ebp
movq $0x0, 0x30(%rsp)
xorl %r12d, %r12d
movq %rbx, 0x38(%rsp)
cmpq %r15, (%rbx)
jbe 0x1a36f
movq 0x18(%rbx), %r13
movl 0x1c(%r13,%r14), %eax
cmpl 0x44(%r10), %eax
jl 0x1a363
movslq 0x3c(%r13,%r14), %rcx
testq %rcx, %rcx
js 0x1a204
cmpl $0x3fffffff, 0x48(%r13,%r14) # imm = 0x3FFFFFFF
ja 0x1a363
testb $0x8, 0x48(%r10)
je 0x1a363
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x1a204
cvtsi2ss %eax, %xmm0
imulq $0x60, %rcx, %rax
cvtsi2ssl 0x1c(%r13,%rax), %xmm1
mulss 0xa0(%r10), %xmm1
ucomiss %xmm0, %xmm1
ja 0x1a363
cmpq 0x30(%rsp), %rbp
movq %rdi, 0x48(%rsp)
jne 0x1a24e
leaq (,%rbp,2), %rcx
testq %rbp, %rbp
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x30(%rsp)
imulq $0x38, %rcx, %rsi
movq %r12, %rdi
leaq 0x961b(%rip), %rdx      # 0x23850
movl $0x43a, %ecx            # imm = 0x43A
leaq 0x9c82(%rip), %r8       # 0x23ec3
callq 0x1e0b4
movq 0x18(%rsp), %r10
movq %rax, %r12
leaq (%r14,%r13), %rax
imulq $0x38, %rbp, %rbx
movq 0x10(%rsp), %rcx
movl (%rcx), %r8d
movq 0x18(%rcx), %r9
subq $0x8, %rsp
leaq 0x78(%rsp), %rdi
movq %r10, %rsi
movq 0x30(%rsp), %rdx
movq 0x48(%rsp), %rcx
pushq %rax
callq 0x1a4cd
popq %rax
popq %rcx
movq 0xa0(%rsp), %rax
movq %rax, 0x30(%r12,%rbx)
movups 0x70(%rsp), %xmm0
movups 0x80(%rsp), %xmm1
movups 0x90(%rsp), %xmm2
movups %xmm2, 0x20(%r12,%rbx)
movups %xmm1, 0x10(%r12,%rbx)
movups %xmm0, (%r12,%rbx)
cmpl $0x0, 0x8(%r12,%rbx)
js 0x1a4ae
movq 0x8(%rsp), %rax
testq %rax, %rax
movq 0x48(%rsp), %rdi
je 0x1a2d4
movq (%rax,%r15,8), %rax
jmp 0x1a2d6
xorl %eax, %eax
movq 0x18(%rsp), %r10
addq %r12, %rbx
movq %rax, 0x20(%rbx)
movl 0xc(%rbx), %eax
orl 0x24(%rsp), %eax
movl %eax, 0xc(%rbx)
movl 0x3c(%r13,%r14), %ecx
testl %ecx, %ecx
js 0x1a2f9
orl $-0x1, 0x2c(%rbx)
testl %edi, %edi
je 0x1a34b
testl %ecx, %ecx
jns 0x1a318
testb $0x10, 0x48(%r10)
movl $0x10000, %ecx          # imm = 0x10000
movl $0x800, %edx            # imm = 0x800
cmovel %edx, %ecx
orl %eax, %ecx
movl %ecx, 0xc(%rbx)
cmpl $0x3fffffff, 0x48(%r13,%r14) # imm = 0x3FFFFFFF
ja 0x1a34b
movl 0x10(%rbx), %eax
movl %eax, %edx
shrl $0x2, %edx
movl $0xff, %esi
andl %esi, %edx
movl 0x10(%r12), %ecx
shrl $0x2, %ecx
andl %esi, %ecx
cmpl %ecx, %edx
jbe 0x1a34b
andl $0xfffffc03, %eax       # imm = 0xFFFFFC03
leal (%rax,%rcx,4), %eax
movl %eax, 0x10(%rbx)
incq %rbp
movl 0x10(%rbx), %eax
shrl $0x2, %eax
movzbl %al, %eax
movl %eax, 0x4c(%r13,%r14)
incl %edi
movq 0x38(%rsp), %rbx
incq %r15
addq $0x60, %r14
jmp 0x1a19b
cmpl $0x0, 0x10(%rbx)
je 0x1a3de
testq %rbp, %rbp
je 0x1a413
xorl %ebx, %ebx
leaq 0x50(%rsp), %r15
movq 0x8(%rsp), %r14
cmpq %rbx, %rbp
je 0x1a3c0
movq %r10, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movq 0x10(%rsp), %rcx
movl %ebp, %r8d
movq %r12, %r9
pushq 0xe0(%rsp)
pushq %rbx
callq 0x1915c
movq 0x28(%rsp), %r10
addq $0x10, %rsp
incq %rbx
jmp 0x1a38a
leaq 0x18(%r12), %rbx
movq 0x10(%rsp), %r15
subq $0x1, %rbp
jb 0x1a409
movq (%rbx), %rdi
callq 0x3390
addq $0x38, %rbx
jmp 0x1a3ca
leaq 0x18(%r12), %rbx
movq %rbp, %r14
movq 0x10(%rsp), %r15
subq $0x1, %r14
jb 0x1a3ff
movq (%rbx), %rdi
callq 0x3390
addq $0x38, %rbx
jmp 0x1a3eb
testq %rbp, %rbp
movq 0x8(%rsp), %r14
je 0x1a46c
movq %r12, %rdi
callq 0x3390
jmp 0x1a46c
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r9
movups %xmm0, 0x20(%r9)
andq $0x0, 0x30(%r9)
movups %xmm0, 0x10(%r9)
orq $-0x1, (%r9)
orl $-0x1, 0x8(%r9)
movl 0x24(%rsp), %eax
orl $0x4, %eax
movl %eax, 0xc(%r9)
leaq 0x50(%rsp), %rdx
pushq $0x1
popq %r8
movq %r10, %rdi
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %r15
movq %r15, %rcx
pushq 0xe0(%rsp)
pushq $0x0
callq 0x1915c
popq %rax
popq %rcx
movq 0x8(%rsp), %r14
movq 0x60(%rsp), %rax
movq %rax, 0x28(%r15)
testq %r14, %r14
je 0x1a49c
xorl %ebx, %ebx
movq 0x38(%rsp), %rax
cmpq %rbx, (%rax)
jbe 0x1a494
movq (%r14,%rbx,8), %rdi
callq 0x3390
incq %rbx
jmp 0x1a47c
movq %r14, %rdi
callq 0x3390
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9a1a(%rip), %rdi      # 0x23ecf
leaq 0x9394(%rip), %rsi      # 0x23850
leaq 0x9a18(%rip), %rcx      # 0x23edb
movl $0x43c, %edx            # imm = 0x43C
callq 0x31a0