// File: /workspace/github_c_makefile_star2_O3/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, 0x18(%rsp)
movl %r8d, 0x14(%rsp)
movl %esi, 0x1c(%rsp)
testl %edx, %edx
jle 0xf061
movq %rcx, %r15
movl %edx, %ebx
movq %rdi, %r14
movss %xmm0, 0x10(%rsp)
movl %edx, %ecx
leal 0x1(%rbx), %eax
andl $-0x2, %eax
movq %rcx, 0x20(%rsp)
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq 0x8(%r15), %rcx
movdqa 0x6236(%rip), %xmm1     # 0x151b0
xorl %edx, %edx
movdqa 0x623c(%rip), %xmm2     # 0x151c0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x623c(%rip), %xmm4     # 0x151d0
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0xefc8
movl %edx, (%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0xefdd
leal 0x1(%rdx), %esi
movl %esi, 0x68(%rcx)
addq $0x2, %rdx
paddq %xmm4, %xmm1
addq $0xd0, %rcx
cmpq %rdx, %rax
jne 0xef94
movq 0x20(%rsp), %r12
leaq (,%r12,8), %rsi
movq %r14, %rdi
callq 0x84a5
movq %rax, %r13
leaq (,%r12,4), %rsi
movq %r14, 0x38(%rsp)
movq %r14, %rdi
callq 0x84a5
movq %rax, %r14
xorl %eax, %eax
movl %eax, (%r14)
movl %eax, 0xc(%r15)
cmpl $0x1, %ebx
movq %r13, %rbx
movss 0x10(%rsp), %xmm4
jne 0xf070
movq %rbx, %rsi
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x83df
movq %rbx, %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x83df
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r9d
movl $0x1, %eax
movq %rbx, 0x40(%rsp)
movq %rax, 0x30(%rsp)
imulq $0x68, %rax, %rax
leaq (%r15,%rax), %rcx
movq %rcx, 0x28(%rsp)
movl $0x0, 0xc(%rsp)
movl 0x34(%r15,%rax), %r12d
movl 0x38(%r15,%rax), %ebp
cmpl $0x0, 0x18(%rsp)
movl %r9d, 0x8(%rsp)
je 0xf16e
movl $0x0, %eax
testl %r9d, %r9d
jle 0xf2c6
xorps %xmm0, %xmm0
cvtsi2ssl 0xc(%rsp), %xmm0
movl %ebp, %r8d
subl %r12d, %r8d
movl %r9d, %r9d
xorl %eax, %eax
movslq (%r14,%rax,4), %rcx
imulq $0x68, %rcx, %rcx
movl 0x38(%r15,%rcx), %r11d
movl %r11d, %r10d
subl %r12d, %r10d
jle 0xf15d
movl 0x34(%r15,%rcx), %edx
movl %ebp, %ebx
subl %edx, %ebx
jle 0xf15d
movl %r11d, %edi
subl %edx, %edi
cmpl %r8d, %edi
movl %r8d, %esi
cmovll %edi, %esi
movl %r8d, %r13d
cmovgl %edi, %r13d
cmpl %edx, %r12d
jge 0xf118
cmpl %r11d, %ebp
cmovll %ebx, %edi
jmp 0xf122
cmpl %ebp, %r11d
cmovgel %r8d, %r10d
movl %r10d, %edi
xorps %xmm1, %xmm1
cvtsi2ss %edi, %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movd %r13d, %xmm2
movd %esi, %xmm3
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
cvtdq2ps %xmm3, %xmm2
divps %xmm2, %xmm1
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
subss %xmm2, %xmm1
ucomiss %xmm4, %xmm1
jbe 0xf15d
movl 0xc(%rsp), %edx
cmpl 0x1c(%rsp), %edx
jle 0xf22c
incq %rax
cmpq %rax, %r9
jne 0xf0d6
jmp 0xf2cb
testl %r9d, %r9d
jle 0xf294
movl %r9d, %eax
xorl %edx, %edx
xorl %ecx, %ecx
movslq (%r14,%rdx,4), %rsi
imulq $0x68, %rsi, %rdi
movl 0x38(%r15,%rdi), %esi
cmpl %r12d, %esi
jle 0xf1ba
movl 0x34(%r15,%rdi), %edi
cmpl %ebp, %edi
jge 0xf1ba
cmpl %r12d, %edi
jg 0xf1a1
movl %r12d, %edi
cmpl %ebp, %esi
jl 0xf1a7
movl %ebp, %esi
shlq $0x20, %rdi
movslq %esi, %rsi
orq %rdi, %rsi
movslq %ecx, %rdi
incl %ecx
movq %rsi, (%rbx,%rdi,8)
incq %rdx
cmpq %rdx, %rax
jne 0xf17e
movl %r9d, %eax
testl %ecx, %ecx
je 0xf2c6
jle 0xf315
movl %ecx, %r13d
leaq (%rbx,%r13,8), %rsi
movq %rbx, %rdi
callq 0x4a34
xorl %ecx, %ecx
xorl %eax, %eax
movl %r12d, %esi
xorl %r8d, %r8d
movl %esi, %edx
movq (%rbx,%rcx,8), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
subl %edx, %edi
cmovlel %r8d, %edi
addl %edi, %eax
cmpl %esi, %edx
cmovgl %edx, %esi
incq %rcx
cmpq %rcx, %r13
jne 0xf1ec
movl %ebp, %ecx
subl %esi, %ecx
cmovlel %r8d, %ecx
addl %eax, %ecx
movl %ecx, 0xc(%rsp)
movss 0x10(%rsp), %xmm4
movl 0x8(%rsp), %r9d
jmp 0xf0b4
addq %r15, %rcx
movq 0x28(%rsp), %rbx
movl 0x24(%rbx), %r8d
movl 0xc(%rcx), %edx
movl %edx, 0xc(%rbx)
movl 0x4(%rcx), %edx
movl 0x14(%rcx), %r9d
cmpl %r8d, %r9d
cmovgl %r9d, %r8d
movl %r8d, 0x14(%rcx)
movl 0x4(%rbx), %r8d
movq 0x60(%rcx), %r9
testq %r9, %r9
je 0xf288
movq 0x60(%rbx), %r10
testq %r10, %r10
je 0xf288
movl 0x30(%rcx), %r11d
cmpl 0x30(%rbx), %r11d
jne 0xf298
movq 0x40(%rcx), %r11
cmpq 0x40(%rbx), %r11
jne 0xf298
movq 0x48(%rcx), %r11
cmpq 0x48(%rbx), %r11
jne 0xf298
cmpl %esi, %edi
jne 0xf298
cmpl %edx, %r8d
movl 0x8(%rsp), %r9d
jge 0xf2c3
jmp 0xf2c6
xorl %eax, %eax
jmp 0xf2c6
movl 0x4(%r10), %esi
movl 0x4(%r9), %edi
movl 0x8(%r9), %r11d
cmpl %esi, %r11d
cmovgl %r11d, %esi
movl %esi, 0x8(%r9)
subl 0x4(%r10), %edi
cmpl 0x14(%rsp), %edi
movl 0x8(%rsp), %r9d
jle 0xf2c3
cmpl %edx, %r8d
jl 0xf2c6
incl 0x10(%rcx)
cmpl %r9d, %eax
jne 0xf2f3
movl 0x8(%rsp), %r9d
movslq %r9d, %rax
incl %r9d
movq 0x30(%rsp), %rcx
movl %ecx, (%r14,%rax,4)
movq %rcx, %rax
movq 0x28(%rsp), %rcx
movl %eax, 0xc(%rcx)
movl $0x0, 0x10(%rcx)
jmp 0xf2fd
movl 0x8(%rsp), %r9d
movq 0x30(%rsp), %rax
incq %rax
cmpq 0x20(%rsp), %rax
movq 0x40(%rsp), %rbx
jne 0xf080
jmp 0xf038
movl $0x0, 0xc(%rsp)
jmp 0xf0b4