// File: /workspace/github_c_makefile_star2_O3/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, 0x14(%rsp)
movq %r8, %r13
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %rsi
testb $0x8, 0x48(%rdi)
movq %rdi, 0x8(%rsp)
jne 0x1e648
movl (%r15), %r8d
movq 0x18(%r15), %r9
movq %rsi, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x22875
movq 0x8(%rsp), %rsi
jmp 0x1e64a
xorl %eax, %eax
movq 0xe0(%rsp), %r10
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
movq (%r13), %rdi
testq %rdi, %rdi
movq %rax, 0x38(%rsp)
je 0x1e90a
movq %r14, 0x48(%rsp)
movq %rbx, 0x30(%rsp)
movq %r15, 0x18(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %r12d, %r12d
movq %r13, 0x40(%rsp)
movq 0x18(%r13), %rbx
movl 0x1c(%rbx,%r15), %ecx
cmpl 0x44(%rsi), %ecx
jl 0x1e87a
movslq 0x3c(%rbx,%r15), %rdx
testq %rdx, %rdx
js 0x1e700
cmpl $0x3fffffff, 0x48(%rbx,%r15) # imm = 0x3FFFFFFF
ja 0x1e87a
testb $0x8, 0x48(%rsi)
je 0x1e87a
cmpl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
je 0x1e700
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
leaq (%rdx,%rdx,2), %rcx
shlq $0x5, %rcx
xorps %xmm1, %xmm1
cvtsi2ssl 0x1c(%rbx,%rcx), %xmm1
mulss 0xa0(%rsi), %xmm1
ucomiss %xmm0, %xmm1
ja 0x1e87a
cmpq %r9, %r12
movl %r8d, 0x2c(%rsp)
jne 0x1e748
leaq (%r9,%r9), %r13
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %r13
imulq $0x38, %r13, %rsi
movq %rbp, %rdi
leaq 0xa198(%rip), %rdx      # 0x288c0
movl $0x43a, %ecx            # imm = 0x43A
leaq 0xa7ff(%rip), %r8       # 0x28f33
callq 0x2324c
movq 0x8(%rsp), %rsi
movq %rax, %rbp
movq %r13, 0x20(%rsp)
jmp 0x1e74d
movq %r9, 0x20(%rsp)
leaq (%rbx,%r15), %rax
imulq $0x38, %r12, %r13
movq 0x18(%rsp), %rcx
movl (%rcx), %r8d
movq 0x18(%rcx), %r9
subq $0x8, %rsp
leaq 0x78(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x50(%rsp), %rcx
pushq %rax
callq 0x1e9f1
addq $0x10, %rsp
movq 0xa0(%rsp), %rax
movq %rax, 0x30(%rbp,%r13)
movups 0x70(%rsp), %xmm0
movups 0x80(%rsp), %xmm1
movups 0x90(%rsp), %xmm2
movups %xmm2, 0x20(%rbp,%r13)
movups %xmm1, 0x10(%rbp,%r13)
movups %xmm0, (%rbp,%r13)
cmpl $0x0, 0x8(%rbp,%r13)
js 0x1e9d2
movq %rbp, %rcx
movq 0x38(%rsp), %rax
testq %rax, %rax
movl 0x2c(%rsp), %r8d
je 0x1e7d6
movq (%rax,%r14,8), %rax
jmp 0x1e7d8
xorl %eax, %eax
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %r9
movq %rcx, %rbp
addq %rcx, %r13
movq %rax, 0x20(%r13)
movl 0xc(%r13), %eax
orl 0x14(%rsp), %eax
movl %eax, 0xc(%r13)
movl 0x3c(%rbx,%r15), %ecx
testl %ecx, %ecx
js 0x1e809
movl $0xffffffff, 0x2c(%r13) # imm = 0xFFFFFFFF
testl %r8d, %r8d
je 0x1e85c
testl %ecx, %ecx
jns 0x1e829
testb $0x10, 0x48(%rsi)
movl $0x10000, %ecx          # imm = 0x10000
movl $0x800, %edx            # imm = 0x800
cmovel %edx, %ecx
orl %eax, %ecx
movl %ecx, 0xc(%r13)
cmpl $0x3fffffff, 0x48(%rbx,%r15) # imm = 0x3FFFFFFF
ja 0x1e85c
movl 0x10(%r13), %eax
movl %eax, %edx
shrl $0x2, %edx
movl $0xff, %edi
andl %edi, %edx
movl 0x10(%rbp), %ecx
shrl $0x2, %ecx
andl %edi, %ecx
cmpl %ecx, %edx
jbe 0x1e85c
andl $0xfffffc03, %eax       # imm = 0xFFFFFC03
leal (%rax,%rcx,4), %eax
movl %eax, 0x10(%r13)
incq %r12
movl 0x10(%r13), %eax
shrl $0x2, %eax
movzbl %al, %eax
movl %eax, 0x4c(%rbx,%r15)
incl %r8d
movq 0x40(%rsp), %r13
movq (%r13), %rdi
incq %r14
addq $0x60, %r15
cmpq %r14, %rdi
ja 0x1e69a
cmpl $0x0, 0x10(%r13)
movq 0x18(%rsp), %r15
je 0x1e968
testq %r12, %r12
movq 0x30(%rsp), %rbx
movq 0xe0(%rsp), %r10
je 0x1e911
movq %rbx, %r14
xorl %ebx, %ebx
movq %rsi, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
movq %r15, %rcx
movl %r12d, %r8d
movq %rbp, %r9
pushq 0xe0(%rsp)
pushq %rbx
callq 0x1bc0b
movq 0x18(%rsp), %rsi
addq $0x10, %rsp
incq %rbx
cmpq %rbx, %r12
jne 0x1e8b1
leaq 0x18(%rbp), %rbx
movq (%rbx), %rdi
callq 0x3390
addq $0x38, %rbx
decq %r12
jne 0x1e8e7
movq %rbp, %rdi
callq 0x3390
movq 0x18(%rsp), %r15
jmp 0x1e98a
cmpl $0x0, 0x10(%r13)
je 0x1e98a
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %r9
movups %xmm0, 0x20(%r9)
movups %xmm0, 0x10(%r9)
movq $0x0, 0x30(%r9)
movq $-0x1, (%r9)
movl $0xffffffff, 0x8(%r9)   # imm = 0xFFFFFFFF
movl 0x14(%rsp), %eax
orl $0x4, %eax
movl %eax, 0xc(%r9)
leaq 0x50(%rsp), %rdx
movq %rsi, %rdi
movq %rbx, %rsi
movq %r15, %rcx
movl $0x1, %r8d
pushq %r10
pushq $0x0
callq 0x1bc0b
addq $0x10, %rsp
jmp 0x1e98a
testq %r12, %r12
je 0x1e98a
leaq 0x18(%rbp), %rbx
movq (%rbx), %rdi
callq 0x3390
addq $0x38, %rbx
decq %r12
jne 0x1e971
movq %rbp, %rdi
callq 0x3390
movq 0x60(%rsp), %rax
movq %rax, 0x28(%r15)
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x1e9c0
cmpq $0x0, (%r13)
je 0x1e9b8
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x3390
incq %rbx
cmpq %rbx, (%r13)
ja 0x1e9a6
movq %r14, %rdi
callq 0x3390
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa566(%rip), %rdi      # 0x28f3f
leaq 0x9ee0(%rip), %rsi      # 0x288c0
leaq 0xa564(%rip), %rcx      # 0x28f4b
movl $0x43c, %edx            # imm = 0x43C
callq 0x31a0