// File: /workspace/github_c_makefile_star2_O2/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, 0x20(%rsp)
movl %r8d, 0x8(%rsp)
movl %ecx, 0x18(%rsp)
movq %rdx, 0x30(%rsp)
movss %xmm2, 0x14(%rsp)
movss %xmm1, 0xc(%rsp)
xorps %xmm1, %xmm1
movss %xmm0, 0x10(%rsp)
ucomiss %xmm1, %xmm0
jbe 0xf496
movq 0x70(%rsp), %rax
movslq (%rax), %r15
testq %r15, %r15
jle 0xf496
movq %rdi, 0x28(%rsp)
xorl %r11d, %r11d
movl $0x0, 0x4(%rsp)
cmpl $0x2, 0x8(%rsp)
jne 0xf345
movq 0x20(%rsp), %rax
addl (%rax), %esi
addl 0x4(%rax), %esi
movl %esi, 0x4(%rsp)
movq 0x78(%rsp), %r9
pushq $0xa
popq %rbp
movq %r9, %r13
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq %r12, %r15
je 0xf47b
movl 0x20(%r13), %eax
cmpq %rax, %r12
je 0xf43b
movslq %eax, %rcx
movl 0xc(%r13), %eax
movq 0x30(%rsp), %rdx
leal (%rax,%rdx), %esi
imulq $0x50, %rcx, %rdx
movl 0xc(%r9,%rdx), %ecx
cmpl %ecx, %esi
jge 0xf42d
addq %r9, %rdx
movl 0x3c(%r13), %esi
xorl 0x3c(%rdx), %esi
btl $0xa, %esi
jb 0xf3ce
movl 0x8(%rdx), %esi
cmpl 0x8(%r13), %esi
jne 0xf3ce
movl 0x1c(%r13), %esi
subl 0x18(%rdx), %esi
cmpl 0x4(%rsp), %esi
jge 0xf3ce
movl 0x1c(%rdx), %esi
subl 0x18(%r13), %esi
cmpl 0x4(%rsp), %esi
jge 0xf3ce
cvtsi2ss %eax, %xmm0
cvtsi2ss %ecx, %xmm1
mulss 0xc(%rsp), %xmm1
jmp 0xf428
xorl %esi, %esi
movl $0x0, %edi
cmpl $0x2, 0x8(%rsp)
jne 0xf405
movq 0x20(%rsp), %rsi
movl (%rsi), %edi
xorl %esi, %esi
cmpl %edi, 0x10(%rdx)
jge 0xf3f3
xorl %esi, %esi
cmpl %edi, 0x14(%rdx)
setg %sil
cmpl %edi, 0x10(%r13)
jge 0xf403
cmpl %edi, 0x14(%r13)
setg %dil
jmp 0xf405
xorl %edi, %edi
cvtsi2ss %eax, %xmm0
cvtsi2ss %ecx, %xmm1
testb %dil, %dil
jne 0xf422
movzbl %dil, %eax
cmpl %eax, %esi
je 0xf422
mulss 0x14(%rsp), %xmm1
jmp 0xf428
mulss 0x10(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jb 0xf452
leal 0x1(%r11), %eax
cmpl 0x18(%rsp), %r11d
movl %eax, %r11d
jge 0xf452
movslq %ebx, %rax
incl %ebx
imulq $0x50, %rax, %rdi
addq %r9, %rdi
movq %rbp, %rcx
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
jmp 0xf46f
movq 0x48(%r13), %rdi
movl %ebx, 0x1c(%rsp)
movq %r9, %rbx
movq %r11, %r14
callq 0x2040
movq %r14, %r11
movq %rbx, %r9
movl 0x1c(%rsp), %ebx
incq %r12
addq $0x50, %r13
jmp 0xf355
cmpl %r15d, %ebx
je 0xf48f
movq 0x28(%rsp), %rdi
movl %ebx, %esi
movq %r9, %rdx
callq 0x19ce0
movq 0x70(%rsp), %rax
movl %ebx, (%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq