// File: /workspace/github_cpp_makefile_star2_O1/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, (%rsp)
testl %esi, %esi
jle 0x19e5ee
movq %rdx, %r14
movq %rdi, %rbx
movss %xmm0, 0x4(%rsp)
movl %esi, %edx
xorl %eax, %eax
movq %r14, %rcx
movl %eax, (%rcx)
incq %rax
addq $0x48, %rcx
cmpq %rax, %rdx
jne 0x19e323
leaq (,%rdx,8), %rsi
movq %rbx, %rdi
movq %rdx, 0x8(%rsp)
callq 0x1e36fd
movq %rax, %r15
movq 0x8(%rsp), %rax
leaq (,%rax,4), %rsi
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
callq 0x1e36fd
movq %r15, %r8
movq 0x8(%rsp), %rdi
movq %rax, %r12
xorl %eax, %eax
movl %eax, (%r12)
movl %eax, 0x4(%r14)
cmpl $0x1, %edi
movl (%rsp), %r11d
je 0x19e5c5
movl $0x1, %ebx
movl $0x1, %r10d
movq %r8, 0x18(%rsp)
movl %ebx, %ebp
leaq (%r10,%r10,8), %rax
leaq (%r14,%rax,8), %rax
movl 0x18(%rax), %r13d
movq %rax, 0x28(%rsp)
movl 0x1c(%rax), %r15d
xorps %xmm0, %xmm0
testl %r11d, %r11d
je 0x19e475
movss 0x4(%rsp), %xmm4
testl %ebx, %ebx
jle 0x19e4d0
movq %r10, 0x10(%rsp)
movl %r15d, %ecx
subl %r13d, %ecx
xorl %eax, %eax
movslq (%r12,%rax,4), %rdx
leaq (%rdx,%rdx,8), %rdx
movl 0x1c(%r14,%rdx,8), %r9d
cmpl %r13d, %r9d
jle 0x19e458
movl 0x18(%r14,%rdx,8), %r10d
cmpl %r10d, %r15d
jle 0x19e458
movl %r9d, %r8d
subl %r10d, %r8d
cmpl %ecx, %r8d
movl %ecx, %edi
cmovll %r8d, %edi
movl %ecx, %esi
cmovgl %r8d, %esi
cmpl %r10d, %r13d
jge 0x19e41a
movl %r15d, %r11d
subl %r10d, %r11d
jl 0x19e422
cmpl %r9d, %r15d
cmovll %r11d, %r8d
jmp 0x19e42e
movl %r9d, %r8d
subl %r13d, %r8d
jge 0x19e427
xorl %r8d, %r8d
jmp 0x19e42e
cmpl %r15d, %r9d
cmovgel %ecx, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8d, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %edi, %xmm2
divss %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %esi, %xmm2
movaps %xmm0, %xmm3
divss %xmm2, %xmm3
subss %xmm3, %xmm1
ucomiss %xmm4, %xmm1
ja 0x19e4d7
incq %rax
cmpq %rax, %rbp
jne 0x19e3d3
movl %ebp, %eax
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r8
jmp 0x19e509
testl %ebx, %ebx
movss 0x4(%rsp), %xmm4
jle 0x19e517
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%r12,%rax,4), %rdx
leaq (%rdx,%rdx,8), %rsi
movl 0x1c(%r14,%rsi,8), %edx
cmpl %r13d, %edx
jle 0x19e4c6
movl 0x18(%r14,%rsi,8), %esi
cmpl %r15d, %esi
jge 0x19e4c6
cmpl %r13d, %esi
jg 0x19e4ab
movl %r13d, %esi
cmpl %r15d, %edx
jl 0x19e4b3
movl %r15d, %edx
shlq $0x20, %rsi
movslq %edx, %rdx
orq %rsi, %rdx
movslq %ecx, %rsi
incl %ecx
movq %rdx, (%r8,%rsi,8)
incq %rax
cmpq %rax, %rbp
jne 0x19e487
jmp 0x19e51b
xorl %eax, %eax
jmp 0x19e59c
leaq (%r14,%rdx,8), %rcx
movl 0x4(%rcx), %edx
movq 0x28(%rsp), %rdi
movl %edx, 0x4(%rdi)
movl 0x3c(%rcx), %edx
movl 0x14(%rdi), %esi
cmpl %esi, %edx
cmovgl %edx, %esi
movl %esi, 0x3c(%rcx)
movl 0xc(%rdi), %edx
cmpl 0xc(%rcx), %edx
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r8
jl 0x19e509
incl 0x40(%rcx)
movq 0x10(%rsp), %r10
movl (%rsp), %r11d
jmp 0x19e59c
xorl %ecx, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x19e59c
jle 0x19e3be
movq %r10, 0x10(%rsp)
movl %ebx, 0x24(%rsp)
movl %ecx, %ebx
leaq (%r8,%rbx,8), %rsi
movq %r8, %rdi
callq 0x194fef
xorl %r9d, %r9d
movq 0x18(%rsp), %r8
xorl %ecx, %ecx
xorl %eax, %eax
movl %r13d, %edx
movl %edx, %esi
movq (%r8,%rcx,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
subl %esi, %edi
cmovlel %r9d, %edi
addl %edi, %eax
cmpl %edx, %esi
cmovgl %esi, %edx
incq %rcx
cmpq %rcx, %rbx
jne 0x19e54b
movl %r15d, %ecx
subl %edx, %ecx
cmovlel %r9d, %ecx
addl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movl (%rsp), %r11d
movss 0x4(%rsp), %xmm4
movq 0x8(%rsp), %rdi
movl 0x24(%rsp), %ebx
movq 0x10(%rsp), %r10
jmp 0x19e3be
cmpl %ebx, %eax
jne 0x19e5b9
movslq %ebx, %rax
incl %ebx
movl %r10d, (%r12,%rax,4)
movq 0x28(%rsp), %rax
movl %r10d, 0x4(%rax)
movl $0x0, 0x40(%rax)
incq %r10
cmpq %rdi, %r10
jne 0x19e395
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r8, %rsi
callq 0x1e3632
movq %rbx, %rdi
movq %r12, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1e3632
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq