// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movl %r8d, 0x4(%rsp)
movl %ecx, 0x24(%rsp)
movq %rdx, 0x30(%rsp)
movss %xmm2, 0x20(%rsp)
movss %xmm1, 0x18(%rsp)
xorps %xmm1, %xmm1
movss %xmm0, 0x1c(%rsp)
ucomiss %xmm1, %xmm0
jbe 0x11053
movq 0x70(%rsp), %rax
movl (%rax), %ecx
testl %ecx, %ecx
jle 0x11053
movq %rdi, 0x28(%rsp)
xorl %r13d, %r13d
movl $0x0, (%rsp)
cmpl $0x2, 0x4(%rsp)
jne 0x10ed1
movq 0x8(%rsp), %rax
addl (%rax), %esi
addl 0x4(%rax), %esi
movl %esi, (%rsp)
cmpl $0x2, %ecx
movl $0x1, %ebx
movl %ecx, 0x14(%rsp)
cmovgel %ecx, %ebx
movq 0x78(%rsp), %r15
movq %r15, %r14
xorl %ebp, %ebp
xorl %r12d, %r12d
movl 0x20(%r14), %eax
cmpq %rax, %rbp
jne 0x10efd
xorl %ecx, %ecx
jmp 0x10ff4
movslq %eax, %rdx
movl 0xc(%r14), %ecx
movq 0x30(%rsp), %rsi
addl %ecx, %esi
leaq (%rdx,%rdx,4), %rdi
shlq $0x4, %rdi
movl 0xc(%r15,%rdi), %edx
cmpl %edx, %esi
jge 0x10f63
addq %r15, %rdi
movl 0x3c(%r14), %esi
xorl 0x3c(%rdi), %esi
btl $0xa, %esi
jb 0x10f6a
movl 0x8(%rdi), %esi
cmpl 0x8(%r14), %esi
jne 0x10f6a
movl 0x1c(%r14), %esi
subl 0x18(%rdi), %esi
cmpl (%rsp), %esi
jge 0x10f6a
movl 0x1c(%rdi), %esi
subl 0x18(%r14), %esi
cmpl (%rsp), %esi
jge 0x10f6a
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
mulss 0x18(%rsp), %xmm1
jmp 0x10fd4
cmpq %rax, %rbp
je 0x10ef6
jmp 0x10fe4
xorl %esi, %esi
cmpl $0x2, 0x4(%rsp)
jne 0x10f8b
movq 0x8(%rsp), %r8
movl (%r8), %r8d
cmpl %r8d, 0x10(%rdi)
jge 0x10f8b
xorl %esi, %esi
cmpl %r8d, 0x14(%rdi)
setg %sil
cmpl $0x2, 0x4(%rsp)
jne 0x10fa9
movq 0x8(%rsp), %rdi
movl (%rdi), %edi
cmpl %edi, 0x10(%r14)
jge 0x10fa9
cmpl %edi, 0x14(%r14)
setg %dil
jmp 0x10fab
xorl %edi, %edi
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
testb %dil, %dil
jne 0x10fce
movzbl %dil, %ecx
cmpl %ecx, %esi
je 0x10fce
mulss 0x20(%rsp), %xmm1
jmp 0x10fd4
mulss 0x1c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
setb %cl
cmpq %rax, %rbp
je 0x10ff4
ucomiss %xmm1, %xmm0
jb 0x10ff4
cmpl 0x24(%rsp), %r12d
leal 0x1(%r12), %eax
setge %cl
movl %eax, %r12d
testb %cl, %cl
je 0x11008
movq 0x48(%r14), %rdi
testq %rdi, %rdi
je 0x11024
callq 0x2040
jmp 0x11024
movslq %r13d, %rax
incl %r13d
leaq (%rax,%rax,4), %rdi
shlq $0x4, %rdi
addq %r15, %rdi
movl $0xa, %ecx
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
incq %rbp
addq $0x50, %r14
cmpq %rbp, %rbx
jne 0x10eed
cmpl 0x14(%rsp), %r13d
je 0x1104b
movq 0x28(%rsp), %rdi
movl %r13d, %esi
movq %r15, %rdx
callq 0x1c717
movq 0x70(%rsp), %rax
movl %r13d, (%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq