// File: /workspace/github_c_makefile_star2_O1/lh3[P]miniprot/hit.c
// Function: mp_set_parent
void mp_set_parent(void *km, float mask_level, int mask_len, int n, mp_reg1_t *r, int sub_diff, int hard_mask_level) // and compute mp_reg1_t::subsc
{
	int i, j, k, *w;
	uint64_t *cov;
	if (n <= 0) return;
	for (i = 0; i < n; ++i) r[i].id = i;
	cov = (uint64_t*)kmalloc(km, n * sizeof(uint64_t));
	w = (int*)kmalloc(km, n * sizeof(int));
	w[0] = 0, r[0].parent = 0;
	for (i = 1, k = 1; i < n; ++i) {
		mp_reg1_t *ri = &r[i];
		int si = ri->qs, ei = ri->qe, n_cov = 0, uncov_len = 0;
		if (hard_mask_level) goto skip_uncov;
		for (j = 0; j < k; ++j) { // traverse existing primary hits to find overlapping hits
			mp_reg1_t *rp = &r[w[j]];
			int sj = rp->qs, ej = rp->qe;
			if (ej <= si || sj >= ei) continue;
			if (sj < si) sj = si;
			if (ej > ei) ej = ei;
			cov[n_cov++] = (uint64_t)sj<<32 | ej;
		}
		if (n_cov == 0) {
			goto set_parent_test; // no overlapping primary hits; then i is a new primary hit
		} else if (n_cov > 0) { // there are overlapping primary hits; find the length not covered by existing primary hits
			int j, x = si;
			radix_sort_mp64(cov, cov + n_cov);
			for (j = 0; j < n_cov; ++j) {
				if ((int)(cov[j]>>32) > x) uncov_len += (cov[j]>>32) - x;
				x = (int32_t)cov[j] > x? (int32_t)cov[j] : x;
			}
			if (ei > x) uncov_len += ei - x;
		}
skip_uncov:
		for (j = 0; j < k; ++j) { // traverse existing primary hits again
			mp_reg1_t *rp = &r[w[j]];
			int sj = rp->qs, ej = rp->qe, min, max, ol;
			if (ej <= si || sj >= ei) continue; // no overlap
			min = ej - sj < ei - si? ej - sj : ei - si;
			max = ej - sj > ei - si? ej - sj : ei - si;
			ol = si < sj? (ei < sj? 0 : ei < ej? ei - sj : ej - sj) : (ej < si? 0 : ej < ei? ej - si : ei - si); // overlap length; TODO: this can be simplified
			if ((float)ol / min - (float)uncov_len / max > mask_level && uncov_len <= mask_len) { // then this is a secondary hit
				int cnt_sub = 0, sci = ri->chn_sc;
				ri->parent = rp->parent;
				rp->subsc = rp->subsc > sci? rp->subsc : sci;
				if (ri->cnt >= rp->cnt) cnt_sub = 1;
				if (rp->p && ri->p && (rp->vid != ri->vid || rp->vs != ri->vs || rp->ve != ri->ve || ol != min)) { // the last condition excludes identical hits after DP
					sci = ri->p->dp_max;
					rp->p->dp_max2 = rp->p->dp_max2 > sci? rp->p->dp_max2 : sci;
					if (rp->p->dp_max - ri->p->dp_max <= sub_diff) cnt_sub = 1;
				}
				if (cnt_sub) ++rp->n_sub;
				break;
			}
		}
set_parent_test:
		if (j == k) w[k++] = i, ri->parent = i, ri->n_sub = 0;
	}
	kfree(km, cov);
	kfree(km, w);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, 0x38(%rsp)
movl %r8d, 0x34(%rsp)
movl %esi, 0x3c(%rsp)
testl %edx, %edx
jle 0xec70
movq %rcx, %r15
movq %rdi, %rbx
movss %xmm0, 0x1c(%rsp)
movl %edx, %edx
leaq 0x8(%rcx), %rax
xorl %ecx, %ecx
movl %ecx, (%rax)
incq %rcx
addq $0x68, %rax
cmpq %rcx, %rdx
jne 0xebe7
leaq (,%rdx,8), %rsi
movq %rbx, %rdi
movq %rdx, 0x10(%rsp)
callq 0x80a1
movq %rax, %r14
movq 0x10(%rsp), %rax
leaq (,%rax,4), %rsi
movq %rbx, 0x40(%rsp)
movq %rbx, %rdi
callq 0x80a1
movq %r14, %r8
movq 0x10(%rsp), %r11
movq %rax, %r14
xorl %eax, %eax
movl %eax, (%r14)
movl %eax, 0xc(%r15)
cmpl $0x1, %r11d
movss 0x1c(%rsp), %xmm4
jne 0xec7f
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r8, %rsi
callq 0x7fd6
movq %rbx, %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7fd6
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
movl $0x1, %r9d
movq %r8, 0x28(%rsp)
imulq $0x68, %rdi, %rax
leaq (%r15,%rax), %r10
xorl %edx, %edx
movl %r9d, %ebx
movl 0x34(%r15,%rax), %r12d
movl 0x38(%r15,%rax), %r13d
cmpl $0x0, 0x38(%rsp)
je 0xed95
movl $0x0, %eax
testl %r9d, %r9d
jle 0xef46
movq %r10, (%rsp)
movl %r9d, 0xc(%rsp)
movq %rdi, 0x20(%rsp)
movl %r13d, %r8d
subl %r12d, %r8d
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorl %eax, %eax
movslq (%r14,%rax,4), %rcx
imulq $0x68, %rcx, %rcx
movl 0x38(%r15,%rcx), %r10d
cmpl %r12d, %r10d
jle 0xed6a
movl 0x34(%r15,%rcx), %r11d
cmpl %r11d, %r13d
jle 0xed6a
movl %r10d, %edi
subl %r11d, %edi
cmpl %r8d, %edi
movl %r8d, %esi
cmovll %edi, %esi
movl %r8d, %r9d
cmovgl %edi, %r9d
cmpl %r11d, %r12d
jge 0xed23
movl %r13d, %ebp
subl %r11d, %ebp
jl 0xed2b
cmpl %r10d, %r13d
cmovll %ebp, %edi
jmp 0xed36
movl %r10d, %edi
subl %r12d, %edi
jge 0xed2f
xorl %edi, %edi
jmp 0xed36
cmpl %r13d, %r10d
cmovgel %r8d, %edi
xorps %xmm1, %xmm1
cvtsi2ss %edi, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %esi, %xmm2
divss %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %r9d, %xmm2
movaps %xmm0, %xmm3
divss %xmm2, %xmm3
subss %xmm3, %xmm1
ucomiss %xmm4, %xmm1
jbe 0xed6a
cmpl 0x3c(%rsp), %edx
jle 0xedee
incq %rax
cmpq %rax, %rbx
jne 0xecdc
movl %ebx, %eax
movq 0x10(%rsp), %r11
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %r9d
movq (%rsp), %r10
jmp 0xef46
testl %r9d, %r9d
jle 0xeea3
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%r14,%rax,4), %rdx
imulq $0x68, %rdx, %rsi
movl 0x38(%r15,%rsi), %edx
cmpl %r12d, %edx
jle 0xede1
movl 0x34(%r15,%rsi), %esi
cmpl %r13d, %esi
jge 0xede1
cmpl %r12d, %esi
jg 0xedc6
movl %r12d, %esi
cmpl %r13d, %edx
jl 0xedce
movl %r13d, %edx
shlq $0x20, %rsi
movslq %edx, %rdx
orq %rsi, %rdx
movslq %ecx, %rsi
incl %ecx
movq %rdx, (%r8,%rsi,8)
incq %rax
cmpq %rax, %rbx
jne 0xeda2
jmp 0xeea7
addq %r15, %rcx
movq (%rsp), %r10
movl 0x24(%r10), %edx
movl 0xc(%rcx), %r8d
movl %r8d, 0xc(%r10)
movl 0x14(%rcx), %r8d
cmpl %edx, %r8d
cmovgl %r8d, %edx
movl %edx, 0x14(%rcx)
movl 0x4(%r10), %r8d
xorl %edx, %edx
cmpl 0x4(%rcx), %r8d
setge %dl
movq 0x60(%rcx), %r8
testq %r8, %r8
je 0xef2b
movq 0x60(%r10), %r9
testq %r9, %r9
je 0xef2b
movl 0x30(%rcx), %r10d
movq (%rsp), %r11
cmpl 0x30(%r11), %r10d
movq (%rsp), %r10
movq 0x10(%rsp), %r11
jne 0xee79
movq 0x40(%rcx), %r10
movq (%rsp), %rbx
cmpq 0x40(%rbx), %r10
movq (%rsp), %r10
jne 0xee79
movq 0x48(%rcx), %r10
movq (%rsp), %rbx
cmpq 0x48(%rbx), %r10
movq (%rsp), %r10
jne 0xee79
cmpl %esi, %edi
je 0xef30
movl 0x4(%r9), %esi
movl 0x8(%r8), %edi
cmpl %esi, %edi
cmovgl %edi, %esi
movl 0x4(%r8), %edi
movl %esi, 0x8(%r8)
subl 0x4(%r9), %edi
cmpl 0x34(%rsp), %edi
movl $0x1, %esi
cmovlel %esi, %edx
jmp 0xef30
xorl %ecx, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0xef46
jle 0xef72
movq %r10, (%rsp)
movl %r9d, 0xc(%rsp)
movq %rdi, 0x20(%rsp)
movl %ecx, %ebp
leaq (%r8,%rbp,8), %rsi
movq %r8, %rdi
callq 0x46b4
movq 0x28(%rsp), %r8
xorl %ecx, %ecx
xorl %eax, %eax
movl %r12d, %esi
xorl %r9d, %r9d
movl %esi, %edx
movq (%r8,%rcx,8), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
subl %edx, %edi
cmovlel %r9d, %edi
addl %edi, %eax
cmpl %esi, %edx
cmovgl %edx, %esi
incq %rcx
cmpq %rcx, %rbp
jne 0xeee0
movl %r13d, %edx
subl %esi, %edx
cmovlel %r9d, %edx
addl %eax, %edx
movss 0x1c(%rsp), %xmm4
movq 0x10(%rsp), %r11
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %r9d
movq (%rsp), %r10
jmp 0xecb1
movq 0x10(%rsp), %r11
testl %edx, %edx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %r9d
je 0xef46
incl 0x10(%rcx)
cmpl %r9d, %eax
jne 0xef61
movslq %r9d, %rax
incl %r9d
movl %edi, (%r14,%rax,4)
movl %edi, 0xc(%r10)
movl $0x0, 0x10(%r10)
incq %rdi
cmpq %r11, %rdi
jne 0xec8f
jmp 0xec47
xorl %edx, %edx
jmp 0xecb1