// File: /workspace/github_cpp_makefile_star2_O3/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, 0xc(%rsp)
testl %esi, %esi
jle 0x1a72fa
movq %rdx, %r14
movl %esi, %r13d
movq %rdi, %rbx
movss %xmm0, 0x8(%rsp)
movl %esi, %ecx
leal 0x1(%r13), %eax
andl $-0x2, %eax
movq %rcx, 0x10(%rsp)
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x8a2dd(%rip), %xmm1    # 0x2312d0
xorl %ecx, %ecx
movdqa 0x8a2e3(%rip), %xmm2    # 0x2312e0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x8a4f3(%rip), %xmm4    # 0x231500
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 0x1a7041
movl %ecx, (%rdx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x1a7056
leal 0x1(%rcx), %esi
movl %esi, 0x48(%rdx)
addq $0x2, %rcx
paddq %xmm4, %xmm1
addq $0x90, %rdx
cmpq %rcx, %rax
jne 0x1a700d
movq 0x10(%rsp), %r15
leaq (,%r15,8), %rsi
movq %rbx, %rdi
callq 0x1ee115
leaq (,%r15,4), %rsi
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %rax, %rbx
callq 0x1ee115
movq %rax, %r12
xorl %eax, %eax
movl %eax, (%r12)
movl %eax, 0x4(%r14)
cmpl $0x1, %r13d
movss 0x8(%rsp), %xmm4
je 0x1a72d1
movl $0x1, %r8d
movl $0x1, %ecx
movq %rbx, 0x30(%rsp)
leaq (%rcx,%rcx,8), %rax
leaq (%r14,%rax,8), %rax
movl 0x18(%rax), %r13d
movq %rax, 0x18(%rsp)
movl 0x1c(%rax), %ebp
pxor %xmm0, %xmm0
cmpl $0x0, 0xc(%rsp)
movl %r8d, 0x4(%rsp)
movq %rcx, 0x20(%rsp)
je 0x1a7198
testl %r8d, %r8d
jle 0x1a7259
movl %ebp, %ecx
subl %r13d, %ecx
movl %r8d, %edx
xorl %eax, %eax
movslq (%r12,%rax,4), %rsi
leaq (%rsi,%rsi,8), %rsi
movl 0x1c(%r14,%rsi,8), %r8d
movl %r8d, %edi
subl %r13d, %edi
jle 0x1a7187
movl 0x18(%r14,%rsi,8), %ebx
movl %ebp, %r15d
subl %ebx, %r15d
jle 0x1a7187
movl %r8d, %r11d
subl %ebx, %r11d
cmpl %ecx, %r11d
movl %ecx, %r9d
cmovll %r11d, %r9d
movl %ecx, %r10d
cmovgl %r11d, %r10d
cmpl %ebx, %r13d
jge 0x1a714b
cmpl %r8d, %ebp
cmovll %r15d, %r11d
jmp 0x1a7154
cmpl %ebp, %r8d
cmovgel %ecx, %edi
movl %edi, %r11d
xorps %xmm1, %xmm1
cvtsi2ss %r11d, %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movd %r10d, %xmm2
movd %r9d, %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 0x1a725d
incq %rax
cmpq %rax, %rdx
jne 0x1a7104
jmp 0x1a728f
testl %r8d, %r8d
jle 0x1a7259
movl %r8d, %eax
xorl %edx, %edx
xorl %ecx, %ecx
movslq (%r12,%rdx,4), %rsi
leaq (%rsi,%rsi,8), %rdi
movl 0x1c(%r14,%rdi,8), %esi
cmpl %r13d, %esi
jle 0x1a71e4
movl 0x18(%r14,%rdi,8), %edi
cmpl %ebp, %edi
jge 0x1a71e4
cmpl %r13d, %edi
jg 0x1a71cb
movl %r13d, %edi
cmpl %ebp, %esi
jl 0x1a71d1
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 0x1a71a8
movl %r8d, %eax
testl %ecx, %ecx
je 0x1a728a
jle 0x1a70f1
movl %ecx, %r15d
leaq (%rbx,%r15,8), %rsi
movq %rbx, %rdi
callq 0x19dd99
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %eax, %eax
movl %r13d, %edx
movl %edx, %esi
movq (%rbx,%rcx,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
subl %esi, %edi
cmovlel %r8d, %edi
addl %edi, %eax
cmpl %edx, %esi
cmovgl %esi, %edx
incq %rcx
cmpq %rcx, %r15
jne 0x1a7216
movl %ebp, %ecx
subl %edx, %ecx
cmovlel %r8d, %ecx
addl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss 0x8(%rsp), %xmm4
movl 0x4(%rsp), %r8d
jmp 0x1a70f1
xorl %eax, %eax
jmp 0x1a728a
leaq (%r14,%rsi,8), %rcx
movl 0x4(%rcx), %edx
movq 0x18(%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
jl 0x1a7285
incl 0x40(%rcx)
movl 0x4(%rsp), %r8d
cmpl %r8d, %eax
jne 0x1a72b4
movl 0x4(%rsp), %r8d
movslq %r8d, %rax
incl %r8d
movq 0x20(%rsp), %rcx
movl %ecx, (%r12,%rax,4)
movq 0x18(%rsp), %rax
movl %ecx, 0x4(%rax)
movl $0x0, 0x40(%rax)
jmp 0x1a72be
movl 0x4(%rsp), %r8d
movq 0x20(%rsp), %rcx
incq %rcx
cmpq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rbx
jne 0x1a70c4
movq %rbx, %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x1ee04f
movq %rbx, %rdi
movq %r12, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1ee04f
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq