// File: /workspace/github_c_makefile_star2_O3/lh3[P]minigraph/gcmisc.c
// Function: mg_gchain_set_parent
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;
	for (i = 1, k = 1; i < n; ++i) {
		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;
			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_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;
			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) {
				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 0x1c861
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 0xd5c6(%rip), %xmm1     # 0x29b20
xorl %ecx, %ecx
movdqa 0xd5cc(%rip), %xmm2     # 0x29b30
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0xd5cc(%rip), %xmm4     # 0x29b40
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 0x1c5a8
movl %ecx, (%rdx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x1c5bd
leal 0x1(%rcx), %esi
movl %esi, 0x68(%rdx)
addq $0x2, %rcx
paddq %xmm4, %xmm1
addq $0xd0, %rdx
cmpq %rcx, %rax
jne 0x1c574
movq 0x10(%rsp), %r15
leaq (,%r15,8), %rsi
movq %rbx, %rdi
callq 0x30e5
leaq (,%r15,4), %rsi
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %rax, %rbx
callq 0x30e5
movq %rax, %r12
xorl %eax, %eax
movl %eax, (%r12)
movl %eax, 0x4(%r14)
cmpl $0x1, %r13d
movss 0x8(%rsp), %xmm4
je 0x1c838
movl $0x1, %r8d
movl $0x1, %ecx
movq %rbx, 0x30(%rsp)
imulq $0x68, %rcx, %rax
leaq (%r14,%rax), %rdx
movq %rdx, 0x18(%rsp)
movl 0x18(%r14,%rax), %ebp
movl 0x1c(%r14,%rax), %r13d
pxor %xmm0, %xmm0
cmpl $0x0, 0xc(%rsp)
movl %r8d, 0x4(%rsp)
movq %rcx, 0x20(%rsp)
je 0x1c700
testl %r8d, %r8d
jle 0x1c7c1
movl %r13d, %edx
subl %ebp, %edx
movl %r8d, %esi
xorl %eax, %eax
movslq (%r12,%rax,4), %rcx
imulq $0x68, %rcx, %rcx
movl 0x1c(%r14,%rcx), %r8d
movl %r8d, %edi
subl %ebp, %edi
jle 0x1c6ef
movl 0x18(%r14,%rcx), %ebx
movl %r13d, %r15d
subl %ebx, %r15d
jle 0x1c6ef
movl %r8d, %r11d
subl %ebx, %r11d
cmpl %edx, %r11d
movl %edx, %r9d
cmovll %r11d, %r9d
movl %edx, %r10d
cmovgl %r11d, %r10d
cmpl %ebx, %ebp
jge 0x1c6b3
cmpl %r8d, %r13d
cmovll %r15d, %r11d
jmp 0x1c6bc
cmpl %r13d, %r8d
cmovgel %edx, %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 0x1c7c5
incq %rax
cmpq %rax, %rsi
jne 0x1c66e
jmp 0x1c7f6
testl %r8d, %r8d
jle 0x1c7c1
movl %r8d, %eax
xorl %edx, %edx
xorl %ecx, %ecx
movslq (%r12,%rdx,4), %rsi
imulq $0x68, %rsi, %rdi
movl 0x1c(%r14,%rdi), %esi
cmpl %ebp, %esi
jle 0x1c74c
movl 0x18(%r14,%rdi), %edi
cmpl %r13d, %edi
jge 0x1c74c
cmpl %ebp, %edi
jg 0x1c731
movl %ebp, %edi
cmpl %r13d, %esi
jl 0x1c739
movl %r13d, %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 0x1c710
movl %r8d, %eax
testl %ecx, %ecx
je 0x1c7f1
jle 0x1c65b
movl %ecx, %r15d
leaq (%rbx,%r15,8), %rsi
movq %rbx, %rdi
callq 0x47e4
xorl %r8d, %r8d
xorl %ecx, %ecx
xorl %eax, %eax
movl %ebp, %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 0x1c77d
movl %r13d, %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 0x1c65b
xorl %eax, %eax
jmp 0x1c7f1
addq %r14, %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 0x1c7ec
incl 0x40(%rcx)
movl 0x4(%rsp), %r8d
cmpl %r8d, %eax
jne 0x1c81b
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 0x1c825
movl 0x4(%rsp), %r8d
movq 0x20(%rsp), %rcx
incq %rcx
cmpq 0x10(%rsp), %rcx
movq 0x30(%rsp), %rbx
jne 0x1c62b
movq %rbx, %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x301f
movq %rbx, %rdi
movq %r12, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x301f
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq