// File: /workspace/github_c_makefile_star2_O3/lh3[P]minimap2/pe.c
// Function: mm_select_sub_multi
void mm_select_sub_multi(void *km, float pri_ratio, float pri1, float pri2, int max_gap_ref, int min_diff, int best_n, int n_segs, const int *qlens, int *n_, mm_reg1_t *r)
{
	if (pri_ratio > 0.0f && *n_ > 0) {
		int i, k, n = *n_, n_2nd = 0;
		int max_dist = n_segs == 2? qlens[0] + qlens[1] + max_gap_ref : 0;
		for (i = k = 0; i < n; ++i) {
			int to_keep = 0;
			if (r[i].parent == i) { // primary
				to_keep = 1;
			} else if (r[i].score + min_diff >= r[r[i].parent].score) {
				to_keep = 1;
			} else {
				mm_reg1_t *p = &r[r[i].parent], *q = &r[i];
				if (p->rev == q->rev && p->rid == q->rid && q->re - p->rs < max_dist && p->re - q->rs < max_dist) { // child and parent are close on the ref
					if (q->score >= p->score * pri1)
						to_keep = 1;
				} else {
					int is_par_both = (n_segs == 2 && p->qs < qlens[0] && p->qe > qlens[0]);
					int is_chi_both = (n_segs == 2 && q->qs < qlens[0] && q->qe > qlens[0]);
					if (is_chi_both || is_chi_both == is_par_both) {
						if (q->score >= p->score * pri_ratio)
							to_keep = 1;
					} else { // the remaining case: is_chi_both == 0 && is_par_both == 1
						if (q->score >= p->score * pri2)
							to_keep = 1;
					}
				}
			}
			if (to_keep && r[i].parent != i) {
				if (n_2nd++ >= best_n) to_keep = 0; // don't keep if there are too many secondary hits
			}
			if (to_keep) r[k++] = r[i];
			else if (r[i].p) free(r[i].p);
		}
		if (k != n) mm_sync_regs(km, k, r); // removing hits requires sync()
		*n_ = k;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x18(%rsp)
movl %r8d, 0x8(%rsp)
movss %xmm2, 0x14(%rsp)
movss %xmm1, 0xc(%rsp)
xorps %xmm1, %xmm1
movss %xmm0, 0x10(%rsp)
ucomiss %xmm1, %xmm0
jbe 0x10f5d
movq 0x70(%rsp), %rax
movl (%rax), %r10d
testl %r10d, %r10d
jle 0x10f5d
movl %ecx, %r8d
movl %edx, %ebp
movq %rdi, 0x20(%rsp)
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
cmpl $0x2, 0x8(%rsp)
jne 0x10dec
movq 0x18(%rsp), %rax
addl (%rax), %esi
addl 0x4(%rax), %esi
movl %esi, 0x4(%rsp)
movq 0x78(%rsp), %r13
movq %r13, %r14
xorl %r15d, %r15d
xorl %r12d, %r12d
movl 0x20(%r14), %eax
cmpq %rax, %r15
je 0x10ee7
movslq %eax, %rcx
movl 0xc(%r14), %eax
leal (%rax,%rbp), %esi
leaq (%rcx,%rcx,4), %r9
shlq $0x4, %r9
movl 0xc(%r13,%r9), %ecx
cmpl %ecx, %esi
jge 0x10eda
addq %r13, %r9
movl 0x3c(%r14), %esi
xorl 0x3c(%r9), %esi
btl $0xa, %esi
jb 0x10e73
movl 0x8(%r9), %esi
cmpl 0x8(%r14), %esi
jne 0x10e73
movl 0x1c(%r14), %esi
subl 0x18(%r9), %esi
cmpl 0x4(%rsp), %esi
jge 0x10e73
movl 0x1c(%r9), %esi
subl 0x18(%r14), %esi
cmpl 0x4(%rsp), %esi
jge 0x10e73
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
mulss 0xc(%rsp), %xmm1
jmp 0x10ed5
xorl %esi, %esi
movl $0x0, %edi
cmpl $0x2, 0x8(%rsp)
jne 0x10eac
movq 0x18(%rsp), %rsi
movl (%rsi), %edi
xorl %esi, %esi
cmpl %edi, 0x10(%r9)
jge 0x10e9a
xorl %esi, %esi
cmpl %edi, 0x14(%r9)
setg %sil
cmpl %edi, 0x10(%r14)
jge 0x10eaa
cmpl %edi, 0x14(%r14)
setg %dil
jmp 0x10eac
xorl %edi, %edi
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
testb %dil, %dil
jne 0x10ecf
movzbl %dil, %eax
cmpl %eax, %esi
je 0x10ecf
mulss 0x14(%rsp), %xmm1
jmp 0x10ed5
mulss 0x10(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jb 0x10f04
leal 0x1(%r12), %eax
cmpl %r8d, %r12d
movl %eax, %r12d
jge 0x10f04
movslq %ebx, %rax
incl %ebx
leaq (%rax,%rax,4), %rdi
shlq $0x4, %rdi
addq %r13, %rdi
movl $0xa, %ecx
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
jmp 0x10f32
movq 0x48(%r14), %rdi
testq %rdi, %rdi
je 0x10f32
movq %rbx, 0x30(%rsp)
movl %r8d, %ebx
movq %r12, 0x28(%rsp)
movq %r10, %r12
callq 0x2040
movq %r12, %r10
movq 0x28(%rsp), %r12
movl %ebx, %r8d
movq 0x30(%rsp), %rbx
incq %r15
addq $0x50, %r14
cmpq %r15, %r10
jne 0x10dfa
cmpl %r10d, %ebx
je 0x10f56
movq 0x20(%rsp), %rdi
movl %ebx, %esi
movq %r13, %rdx
callq 0x1c72c
movq 0x70(%rsp), %rax
movl %ebx, (%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq