// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: mg_gchain_set_parent(void*, float, int, mg_gchain_t*, int, int)
void mg_gchain_set_parent(void *km, float mask_level, int n, mg_gchain_t *r, int sub_diff, int hard_mask_level)
{
	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;///the first gchain is a primary hits; since all gchains have already been sorted by scores
	for (i = 1, k = 1; i < n; ++i) {///start from the 1-th chain, instead of the 0-th chain
		mg_gchain_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
			mg_gchain_t *rp = &r[w[j]];
			int sj = rp->qs, ej = rp->qe;
			if (ej <= si || sj >= ei) continue;///no overlaps
			if (sj < si) sj = si;///MAX(si, sj)
			if (ej > ei) ej = ei;///MIN(ei, ej)
			cov[n_cov++] = (uint64_t)sj<<32 | ej;///overlap coordinates
		}
		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_gfa64(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
			mg_gchain_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;///chain length
			max = ej - sj > ei - si? ej - sj : ei - si;///chain length
			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) {
				int cnt_sub = 0;
				ri->parent = rp->parent;
				rp->subsc = rp->subsc > ri->score? rp->subsc : ri->score;
				if (ri->cnt >= rp->cnt) 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 $0x38, %rsp
movl %r8d, 0x1c(%rsp)
testl %esi, %esi
jle 0x15f7ef
movq %rdx, %r14
movq %rdi, %rbx
movl %esi, %r15d
xorl %eax, %eax
movq %rdx, %rcx
cmpq %rax, %r15
je 0x15f564
movl %eax, (%rcx)
incq %rax
addq $0x48, %rcx
jmp 0x15f554
movss %xmm0, (%rsp)
leaq (,%r15,8), %rsi
movq %rbx, %rdi
callq 0x1a39c4
movq %rax, 0x8(%rsp)
leaq (,%r15,4), %rsi
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
callq 0x1a39c4
movq %rax, %r12
andl $0x0, (%rax)
andl $0x0, 0x4(%r14)
pushq $0x1
popq %rdx
pushq $0x1
popq %rax
movq %r15, 0x20(%rsp)
cmpq %r15, %rax
je 0x15f7c4
movq %rax, 0x28(%rsp)
imulq $0x48, %rax, %rax
leaq (%r14,%rax), %r8
testl %edx, %edx
movl $0x0, %r13d
movl %edx, 0x4(%rsp)
cmovgl %edx, %r13d
movl 0x18(%r14,%rax), %ebx
movl 0x1c(%r14,%rax), %ebp
xorps %xmm0, %xmm0
cmpl $0x0, 0x1c(%rsp)
je 0x15f692
movq %r8, 0x10(%rsp)
movss (%rsp), %xmm4
movl %ebp, %edx
subl %ebx, %edx
xorl %eax, %eax
cmpq %rax, %r13
je 0x15f684
movslq (%r12,%rax,4), %rcx
imulq $0x48, %rcx, %rcx
movl 0x1c(%r14,%rcx), %edi
movl %edi, %esi
subl %ebx, %esi
jle 0x15f67c
movl 0x18(%r14,%rcx), %r15d
movl %ebp, %r11d
subl %r15d, %r11d
jle 0x15f67c
movl %edi, %r10d
subl %r15d, %r10d
cmpl %edx, %r10d
movl %edx, %r8d
cmovll %r10d, %r8d
movl %edx, %r9d
cmovgl %r10d, %r9d
cmpl %r15d, %ebx
jge 0x15f644
cmpl %edi, %ebp
cmovll %r11d, %r10d
jmp 0x15f64c
cmpl %ebp, %edi
cmovgel %edx, %esi
movl %esi, %r10d
cvtsi2ss %r10d, %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movd %r9d, %xmm2
movd %r8d, %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
ja 0x15f73e
incq %rax
jmp 0x15f5f7
movl 0x4(%rsp), %edx
movq 0x10(%rsp), %r8
jmp 0x15f78c
xorl %ecx, %ecx
xorl %eax, %eax
movss (%rsp), %xmm4
cmpq %rcx, %r13
je 0x15f6e3
movslq (%r12,%rcx,4), %rdx
imulq $0x48, %rdx, %rsi
movl 0x1c(%r14,%rsi), %edx
cmpl %ebx, %edx
jle 0x15f6de
movl 0x18(%r14,%rsi), %esi
cmpl %ebp, %esi
jge 0x15f6de
cmpl %ebx, %esi
jg 0x15f6c0
movl %ebx, %esi
cmpl %ebp, %edx
jl 0x15f6c6
movl %ebp, %edx
shlq $0x20, %rsi
movslq %edx, %rdx
orq %rsi, %rdx
movslq %eax, %rsi
incl %eax
movq 0x8(%rsp), %rdi
movq %rdx, (%rdi,%rsi,8)
incq %rcx
jmp 0x15f69b
testl %eax, %eax
je 0x15f788
movq %r8, 0x10(%rsp)
jle 0x15f5f1
movl %eax, %r15d
movq 0x8(%rsp), %rdi
leaq (%rdi,%r15,8), %rsi
callq 0x156b0d
xorl %eax, %eax
movl %ebx, %ecx
xorl %edx, %edx
cmpq %rax, %r15
je 0x15f771
movq 0x8(%rsp), %rsi
movq (%rsi,%rax,8), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
subl %ecx, %edi
movl $0x0, %r8d
cmovlel %r8d, %edi
addl %edx, %edi
cmpl %esi, %ecx
cmovgl %ecx, %esi
incq %rax
movl %esi, %ecx
movl %edi, %edx
jmp 0x15f70d
addq %r14, %rcx
movl 0x4(%rcx), %edx
movq 0x10(%rsp), %r8
movl %edx, 0x4(%r8)
movl 0x3c(%rcx), %edx
movl 0x14(%r8), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x3c(%rcx)
movl 0xc(%r8), %edx
cmpl 0xc(%rcx), %edx
movl 0x4(%rsp), %edx
jl 0x15f76c
incl 0x40(%rcx)
movl %eax, %r13d
jmp 0x15f78c
movl %ebp, %eax
subl %ecx, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
addl %edx, %eax
cvtsi2ss %eax, %xmm0
jmp 0x15f5ec
movl 0x4(%rsp), %edx
cmpl %edx, %r13d
jne 0x15f7b2
movslq %edx, %rax
incl %edx
movq 0x28(%rsp), %rcx
movl %ecx, (%r12,%rax,4)
movq %rcx, %rax
movl %eax, 0x4(%r8)
andl $0x0, 0x40(%r8)
movq 0x20(%rsp), %r15
jmp 0x15f7bc
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rax
incq %rax
jmp 0x15f5a9
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x1a38cf
movq %rbx, %rdi
movq %r12, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1a38cf
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq