// File: /workspace/github_c_makefile_star2_O2/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, 0x1c(%rsp)
testl %esi, %esi
jle 0x1903c
movq %rdx, %r14
movq %rdi, %rbx
movl %esi, %r15d
xorl %eax, %eax
movq %rdx, %rcx
cmpq %rax, %r15
je 0x18db1
movl %eax, (%rcx)
incq %rax
addq $0x68, %rcx
jmp 0x18da1
movss %xmm0, (%rsp)
leaq (,%r15,8), %rsi
movq %rbx, %rdi
callq 0x266c
movq %rax, 0x8(%rsp)
leaq (,%r15,4), %rsi
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
callq 0x266c
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 0x19011
movq %rax, 0x28(%rsp)
imulq $0x68, %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 0x18edf
movq %r8, 0x10(%rsp)
movss (%rsp), %xmm4
movl %ebp, %edx
subl %ebx, %edx
xorl %eax, %eax
cmpq %rax, %r13
je 0x18ed1
movslq (%r12,%rax,4), %rcx
imulq $0x68, %rcx, %rcx
movl 0x1c(%r14,%rcx), %edi
movl %edi, %esi
subl %ebx, %esi
jle 0x18ec9
movl 0x18(%r14,%rcx), %r15d
movl %ebp, %r11d
subl %r15d, %r11d
jle 0x18ec9
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 0x18e91
cmpl %edi, %ebp
cmovll %r11d, %r10d
jmp 0x18e99
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 0x18f8b
incq %rax
jmp 0x18e44
movl 0x4(%rsp), %edx
movq 0x10(%rsp), %r8
jmp 0x18fd9
xorl %ecx, %ecx
xorl %eax, %eax
movss (%rsp), %xmm4
cmpq %rcx, %r13
je 0x18f30
movslq (%r12,%rcx,4), %rdx
imulq $0x68, %rdx, %rsi
movl 0x1c(%r14,%rsi), %edx
cmpl %ebx, %edx
jle 0x18f2b
movl 0x18(%r14,%rsi), %esi
cmpl %ebp, %esi
jge 0x18f2b
cmpl %ebx, %esi
jg 0x18f0d
movl %ebx, %esi
cmpl %ebp, %edx
jl 0x18f13
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 0x18ee8
testl %eax, %eax
je 0x18fd5
movq %r8, 0x10(%rsp)
jle 0x18e3e
movl %eax, %r15d
movq 0x8(%rsp), %rdi
leaq (%rdi,%r15,8), %rsi
callq 0x3d53
xorl %eax, %eax
movl %ebx, %ecx
xorl %edx, %edx
cmpq %rax, %r15
je 0x18fbe
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 0x18f5a
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 0x18fb9
incl 0x40(%rcx)
movl %eax, %r13d
jmp 0x18fd9
movl %ebp, %eax
subl %ecx, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
addl %edx, %eax
cvtsi2ss %eax, %xmm0
jmp 0x18e39
movl 0x4(%rsp), %edx
cmpl %edx, %r13d
jne 0x18fff
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 0x19009
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rax
incq %rax
jmp 0x18df6
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x2577
movq %rbx, %rdi
movq %r12, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2577
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq