// File: /workspace/github_c_makefile_star2_O2/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, 0x34(%rsp)
movl %r8d, 0x30(%rsp)
movl %esi, 0x38(%rsp)
testl %edx, %edx
jle 0xd798
movq %rcx, %r15
movq %rdi, %rbx
movl %edx, %r13d
leaq 0x8(%rcx), %rax
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0xd49b
movl %ecx, (%rax)
incq %rcx
addq $0x68, %rax
jmp 0xd48b
movss %xmm0, 0xc(%rsp)
leaq (,%r13,8), %rsi
movq %rbx, %rdi
callq 0x7460
movq %rax, 0x20(%rsp)
leaq (,%r13,4), %rsi
movq %rbx, 0x40(%rsp)
movq %rbx, %rdi
callq 0x7460
movq %rax, %r14
andl $0x0, (%rax)
andl $0x0, 0xc(%r15)
pushq $0x1
popq %rsi
pushq $0x1
popq %rdi
movq %r13, 0x10(%rsp)
cmpq %r13, %rdi
je 0xd76d
movq %rdi, 0x18(%rsp)
imulq $0x68, %rdi, %rax
xorl %edx, %edx
testl %esi, %esi
movl $0x0, %r12d
movl %esi, 0x8(%rsp)
cmovgl %esi, %r12d
leaq (%r15,%rax), %rcx
movq %rcx, 0x28(%rsp)
movl 0x34(%r15,%rax), %ebp
movl 0x38(%r15,%rax), %ebx
cmpl $0x0, 0x34(%rsp)
movss 0xc(%rsp), %xmm4
je 0xd5d0
movl %ebx, %r8d
subl %ebp, %r8d
movl %edx, 0x3c(%rsp)
cvtsi2ss %edx, %xmm0
xorl %eax, %eax
cmpq %rax, %r12
je 0xd673
movslq (%r14,%rax,4), %rcx
imulq $0x68, %rcx, %rcx
movl 0x38(%r15,%rcx), %r10d
movl %r10d, %r9d
subl %ebp, %r9d
jle 0xd5c8
movl 0x34(%r15,%rcx), %edx
movl %ebx, %r13d
subl %edx, %r13d
jle 0xd5c8
movl %r10d, %edi
subl %edx, %edi
cmpl %r8d, %edi
movl %r8d, %esi
cmovll %edi, %esi
movl %r8d, %r11d
cmovgl %edi, %r11d
cmpl %edx, %ebp
jge 0xd586
cmpl %r10d, %ebx
cmovll %r13d, %edi
jmp 0xd590
cmpl %ebx, %r10d
cmovgel %r8d, %r9d
movl %r9d, %edi
cvtsi2ss %edi, %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movd %r11d, %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 0xd5c8
movl 0x3c(%rsp), %edx
cmpl 0x38(%rsp), %edx
jle 0xd68b
incq %rax
jmp 0xd539
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x20(%rsp), %rdi
cmpq %rcx, %r12
je 0xd61c
movslq (%r14,%rcx,4), %rdx
imulq $0x68, %rdx, %rsi
movl 0x38(%r15,%rsi), %edx
cmpl %ebp, %edx
jle 0xd617
movl 0x34(%r15,%rsi), %esi
cmpl %ebx, %esi
jge 0xd617
cmpl %ebp, %esi
jg 0xd5fe
movl %ebp, %esi
cmpl %ebx, %edx
jl 0xd604
movl %ebx, %edx
shlq $0x20, %rsi
movslq %edx, %rdx
orq %rsi, %rdx
movslq %eax, %rsi
incl %eax
movq %rdx, (%rdi,%rsi,8)
incq %rcx
jmp 0xd5d9
testl %eax, %eax
je 0xd673
jle 0xd766
movl %eax, %r13d
leaq (%rdi,%r13,8), %rsi
callq 0x3a66
movq 0x20(%rsp), %r8
movss 0xc(%rsp), %xmm4
xorl %eax, %eax
movl %ebp, %ecx
xorl %esi, %esi
cmpq %rax, %r13
je 0xd6fc
movq (%r8,%rax,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
subl %ecx, %edi
movl $0x0, %r9d
cmovlel %r9d, %edi
addl %esi, %edi
cmpl %edx, %ecx
cmovgl %ecx, %edx
incq %rax
movl %edx, %ecx
movl %edi, %esi
jmp 0xd643
movq 0x10(%rsp), %r13
movl 0x8(%rsp), %esi
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rbx
jmp 0xd749
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 0xd6e7
movq 0x60(%rbx), %r10
testq %r10, %r10
je 0xd6e7
movl 0x30(%rcx), %r11d
cmpl 0x30(%rbx), %r11d
jne 0xd70f
movq 0x40(%rcx), %r11
cmpq 0x40(%rbx), %r11
jne 0xd70f
movq 0x48(%rcx), %r11
cmpq 0x48(%rbx), %r11
jne 0xd70f
cmpl %esi, %edi
jne 0xd70f
cmpl %edx, %r8d
movq 0x10(%rsp), %r13
movl 0x8(%rsp), %esi
movq 0x18(%rsp), %rdi
jge 0xd743
jmp 0xd746
movl %ebx, %edx
subl %ecx, %edx
movl $0x0, %eax
cmovlel %eax, %edx
addl %esi, %edx
jmp 0xd529
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 0x30(%rsp), %edi
movq 0x10(%rsp), %r13
movl 0x8(%rsp), %esi
movq 0x18(%rsp), %rdi
jle 0xd743
cmpl %edx, %r8d
jl 0xd746
incl 0x10(%rcx)
movl %eax, %r12d
cmpl %esi, %r12d
jne 0xd75e
movslq %esi, %rax
incl %esi
movl %edi, (%r14,%rax,4)
movl %edi, 0xc(%rbx)
andl $0x0, 0x10(%rbx)
incq %rdi
jmp 0xd4e1
xorl %edx, %edx
jmp 0xd529
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x736b
movq %rbx, %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x736b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq