// File: /workspace/github_c_makefile_star2_O1/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 %rdx, %rbx
movq %rdi, %r10
testb $0x8, 0x48(%rdi)
movq %rcx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
jne 0x1df5b
movl (%rcx), %r8d
movq 0x18(%rcx), %r9
movq %r10, %rdi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x22334
movq 0x8(%rsp), %r10
jmp 0x1df5d
xorl %eax, %eax
movq %rax, 0x38(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
xorl %esi, %esi
cmpq $0x0, (%r13)
movq %r13, 0x30(%rsp)
je 0x1e186
movq %rbx, 0x40(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %r8d, %r8d
xorl %ebp, %ebp
movq 0x18(%r13), %rbx
movl 0x1c(%rbx,%r12), %eax
cmpl 0x44(%r10), %eax
jl 0x1e173
movslq 0x3c(%rbx,%r12), %rcx
testq %rcx, %rcx
js 0x1dfcf
cmpl $0x3fffffff, 0x48(%rbx,%r12) # imm = 0x3FFFFFFF
ja 0x1e173
testb $0x8, 0x48(%r10)
je 0x1e173
cmpl $0x7ffffffe, %ecx       # imm = 0x7FFFFFFE
ja 0x1e001
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
leaq (%rcx,%rcx,2), %rax
shlq $0x5, %rax
xorps %xmm1, %xmm1
cvtsi2ssl 0x1c(%rbx,%rax), %xmm1
mulss 0xa0(%r10), %xmm1
ucomiss %xmm0, %xmm1
ja 0x1e173
cmpq %r8, %rbp
movq %rsi, 0x48(%rsp)
jne 0x1e049
leaq (%r8,%r8), %r13
testq %r8, %r8
movl $0x2, %eax
cmoveq %rax, %r13
imulq $0x38, %r13, %rsi
movq %r14, %rdi
leaq 0xa7a7(%rip), %rdx      # 0x287d0
movl $0x43a, %ecx            # imm = 0x43A
leaq 0xae0e(%rip), %r8       # 0x28e43
callq 0x22d64
movq 0x8(%rsp), %r10
movq %rax, %r14
movq %r13, 0x28(%rsp)
jmp 0x1e04e
movq %r8, 0x28(%rsp)
movq 0x18(%rsp), %rdx
leaq (%rbx,%r12), %rax
imulq $0x38, %rbp, %r13
movq 0x20(%rsp), %rcx
movl (%rcx), %r8d
movq 0x18(%rcx), %r9
subq $0x8, %rsp
leaq 0x78(%rsp), %rdi
movq %r10, %rsi
movq 0x48(%rsp), %rcx
pushq %rax
callq 0x1e2f7
addq $0x10, %rsp
movq 0xa0(%rsp), %rax
movq %rax, 0x30(%r14,%r13)
movups 0x70(%rsp), %xmm0
movups 0x80(%rsp), %xmm1
movups 0x90(%rsp), %xmm2
movups %xmm2, 0x20(%r14,%r13)
movups %xmm1, 0x10(%r14,%r13)
movups %xmm0, (%r14,%r13)
cmpl $0x0, 0x8(%r14,%r13)
js 0x1e2d8
movq 0x38(%rsp), %rax
testq %rax, %rax
movq 0x48(%rsp), %rsi
je 0x1e0d6
movq (%rax,%r15,8), %rax
jmp 0x1e0d8
xorl %eax, %eax
movq 0x8(%rsp), %r10
movq 0x28(%rsp), %r8
addq %r14, %r13
movq %rax, 0x20(%r13)
movl 0xc(%r13), %eax
orl 0x14(%rsp), %eax
movl %eax, 0xc(%r13)
movl 0x3c(%rbx,%r12), %ecx
testl %ecx, %ecx
js 0x1e106
movl $0xffffffff, 0x2c(%r13) # imm = 0xFFFFFFFF
testl %esi, %esi
je 0x1e15a
testl %ecx, %ecx
jns 0x1e126
testb $0x10, 0x48(%r10)
movl $0x10000, %ecx          # imm = 0x10000
movl $0x800, %edx            # imm = 0x800
cmovel %edx, %ecx
orl %eax, %ecx
movl %ecx, 0xc(%r13)
cmpl $0x3fffffff, 0x48(%rbx,%r12) # imm = 0x3FFFFFFF
ja 0x1e15a
movl 0x10(%r13), %eax
movl %eax, %edx
shrl $0x2, %edx
movl $0xff, %edi
andl %edi, %edx
movl 0x10(%r14), %ecx
shrl $0x2, %ecx
andl %edi, %ecx
cmpl %ecx, %edx
jbe 0x1e15a
andl $0xfffffc03, %eax       # imm = 0xFFFFFC03
leal (%rax,%rcx,4), %eax
movl %eax, 0x10(%r13)
incq %rbp
movl 0x10(%r13), %eax
shrl $0x2, %eax
movzbl %al, %eax
movl %eax, 0x4c(%rbx,%r12)
incl %esi
movq 0x30(%rsp), %r13
incq %r15
addq $0x60, %r12
cmpq %r15, (%r13)
ja 0x1df98
jmp 0x1e18b
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpl $0x0, 0x10(%r13)
je 0x1e20e
movq 0xe0(%rsp), %r11
testq %rbp, %rbp
je 0x1e232
xorl %r15d, %r15d
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r12
movq %r10, %rdi
movq %r12, %rsi
leaq 0x50(%rsp), %rdx
movq %rbx, %rcx
movl %ebp, %r8d
movq %r14, %r9
pushq %r11
pushq %r15
movq %r14, %r13
movq %r11, %r14
callq 0x1b6bb
movq %r14, %r11
movq %r13, %r14
movq 0x18(%rsp), %r10
addq $0x10, %rsp
incq %r15
cmpq %r15, %rbp
jne 0x1e1b0
leaq 0x18(%r14), %rbx
movq (%rbx), %rdi
callq 0x3390
addq $0x38, %rbx
decq %rbp
jne 0x1e1ee
movq %r14, %rdi
callq 0x3390
movq 0x30(%rsp), %r13
jmp 0x1e28b
testq %rbp, %rbp
je 0x1e28b
leaq 0x18(%r14), %rbx
movq (%rbx), %rdi
callq 0x3390
addq $0x38, %rbx
decq %rbp
jne 0x1e217
movq %r14, %rdi
callq 0x3390
jmp 0x1e28b
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 %r10, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rcx
movl $0x1, %r8d
pushq %r11
pushq $0x0
callq 0x1b6bb
addq $0x10, %rsp
movq 0x60(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rax, 0x28(%rcx)
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x1e2c6
cmpq $0x0, (%r13)
je 0x1e2be
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x3390
incq %rbx
cmpq %rbx, (%r13)
ja 0x1e2ac
movq %r14, %rdi
callq 0x3390
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xab70(%rip), %rdi      # 0x28e4f
leaq 0xa4ea(%rip), %rsi      # 0x287d0
leaq 0xab6e(%rip), %rcx      # 0x28e5b
movl $0x43c, %edx            # imm = 0x43C
callq 0x31a0