// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rax
movl 0x48(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x2b136
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movl (%rax), %r8d
movq -0x20(%rbp), %rax
movq 0x18(%rax), %r9
callq 0x313e0
movq %rax, -0x70(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x2b44b
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x64(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x44(%rcx), %eax
jge 0x2b1b1
jmp 0x2b43d
movq -0x78(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jl 0x2b1de
movq -0x78(%rbp), %rax
movl 0x48(%rax), %eax
sarl $0x1e, %eax
cmpl $0x0, %eax
jne 0x2b1d9
movq -0x8(%rbp), %rax
movl 0x48(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x2b1de
jmp 0x2b43d
movq -0x78(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jl 0x2b230
movq -0x78(%rbp), %rax
cmpl $0x7fffffff, 0x3c(%rax) # imm = 0x7FFFFFFF
jge 0x2b230
movq -0x78(%rbp), %rax
cvtsi2ssl 0x1c(%rax), %xmm1
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x78(%rbp), %rcx
movslq 0x3c(%rcx), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
cvtsi2ssl 0x1c(%rax), %xmm0
movq -0x8(%rbp), %rax
mulss 0xa0(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x2b230
jmp 0x2b43d
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x2b291
cmpq $0x0, -0x58(%rbp)
je 0x2b251
movq -0x58(%rbp), %rax
shlq %rax
movq %rax, -0x130(%rbp)
jmp 0x2b25f
movl $0x2, %eax
movq %rax, -0x130(%rbp)
jmp 0x2b25f
movq -0x130(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
imulq $0x38, -0x58(%rbp), %rsi
leaq 0xf43e(%rip), %rdx      # 0x3a6b8
movl $0x43a, %ecx            # imm = 0x43A
leaq 0xfaa5(%rip), %r8       # 0x3ad2b
callq 0x31d40
movq %rax, -0x50(%rbp)
jmp 0x2b293
jmp 0x2b293
movq -0x50(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x60(%rbp)
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movl (%rax), %r8d
movq -0x20(%rbp), %rax
movq 0x18(%rax), %r9
movq -0x78(%rbp), %rax
leaq -0xb8(%rbp), %rdi
movq %rax, (%rsp)
callq 0x2b620
movq -0x138(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
movl $0x38, %edx
callq 0x3250
movq -0x80(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jl 0x2b30f
jmp 0x2b32e
leaq 0xfa21(%rip), %rdi      # 0x3ad37
leaq 0xf39b(%rip), %rsi      # 0x3a6b8
movl $0x43c, %edx            # imm = 0x43C
leaq 0xfa1a(%rip), %rcx      # 0x3ad43
callq 0x3190
cmpq $0x0, -0x70(%rbp)
je 0x2b34a
movq -0x70(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x140(%rbp)
jmp 0x2b355
xorl %eax, %eax
movq %rax, -0x140(%rbp)
jmp 0x2b355
movq -0x140(%rbp), %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x20(%rax)
movl -0x2c(%rbp), %ecx
movq -0x80(%rbp), %rax
orl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
movq -0x78(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jl 0x2b386
movq -0x80(%rbp), %rax
movl $0xffffffff, 0x2c(%rax) # imm = 0xFFFFFFFF
cmpl $0x0, -0x68(%rbp)
je 0x2b3ba
movq -0x78(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jge 0x2b3ba
movq -0x8(%rbp), %rax
movl 0x48(%rax), %edx
andl $0x10, %edx
movl $0x800, %ecx            # imm = 0x800
movl $0x10000, %eax          # imm = 0x10000
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x80(%rbp), %rax
orl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
cmpl $0x0, -0x68(%rbp)
je 0x2b41d
movq -0x78(%rbp), %rax
movl 0x48(%rax), %eax
sarl $0x1e, %eax
cmpl $0x0, %eax
jne 0x2b41d
movq -0x80(%rbp), %rax
movl 0x10(%rax), %eax
shrl $0x2, %eax
andl $0xff, %eax
movq -0x50(%rbp), %rcx
movl 0x10(%rcx), %ecx
shrl $0x2, %ecx
andl $0xff, %ecx
cmpl %ecx, %eax
jle 0x2b41d
movq -0x50(%rbp), %rax
movl 0x10(%rax), %edx
shrl $0x2, %edx
andl $0xff, %edx
movq -0x80(%rbp), %rax
movl 0x10(%rax), %ecx
andl $0xff, %edx
shll $0x2, %edx
andl $0xfffffc03, %ecx       # imm = 0xFFFFFC03
orl %edx, %ecx
movl %ecx, 0x10(%rax)
movq -0x80(%rbp), %rax
movl 0x10(%rax), %ecx
shrl $0x2, %ecx
andl $0xff, %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x4c(%rax)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2b174
movq -0x28(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x2b49e
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x2b489
movq -0x50(%rbp), %rax
movslq -0x64(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x3380
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2b45c
cmpq $0x0, -0x60(%rbp)
jbe 0x2b499
movq -0x50(%rbp), %rdi
callq 0x3380
jmp 0x2b5c7
cmpq $0x0, -0x60(%rbp)
jne 0x2b53b
movq -0x8(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movl (%rax), %r8d
movq -0x20(%rbp), %rax
movq 0x18(%rax), %r9
leaq -0x128(%rbp), %rdi
xorl %eax, %eax
movq $0x0, (%rsp)
callq 0x2b620
leaq -0xf0(%rbp), %rdi
leaq -0x128(%rbp), %rsi
movl $0x38, %edx
callq 0x3250
movl -0x2c(%rbp), %eax
orl -0xe4(%rbp), %eax
movl %eax, -0xe4(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rcx
movq 0x10(%rbp), %rax
leaq -0x48(%rbp), %rdx
movl $0x1, %r8d
leaq -0xf0(%rbp), %r9
xorl %r10d, %r10d
movl $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x29440
jmp 0x2b5c5
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x2b588
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rcx
movq -0x60(%rbp), %rax
movl %eax, %r8d
movq -0x50(%rbp), %r9
movl -0x64(%rbp), %r10d
movq 0x10(%rbp), %rax
leaq -0x48(%rbp), %rdx
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x29440
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2b542
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x2b5bc
movq -0x50(%rbp), %rax
movslq -0x64(%rbp), %rcx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x3380
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2b58f
movq -0x50(%rbp), %rdi
callq 0x3380
jmp 0x2b5c7
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
cmpq $0x0, -0x70(%rbp)
je 0x2b613
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x2b60a
movq -0x70(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3380
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2b5e1
movq -0x70(%rbp), %rdi
callq 0x3380
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopl (%rax)