// File: /workspace/github_c_makefile_star2_O1/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, (%rsp)
testl %esi, %esi
jle 0x1c7fa
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 $0x68, %rcx
cmpq %rax, %rdx
jne 0x1c531
leaq (,%rdx,8), %rsi
movq %rbx, %rdi
movq %rdx, 0x8(%rsp)
callq 0x31c1
movq %rax, %r15
movq 0x8(%rsp), %rax
leaq (,%rax,4), %rsi
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
callq 0x31c1
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 0x1c7d1
movl $0x1, %ebx
movl $0x1, %r10d
movq %r8, 0x18(%rsp)
imulq $0x68, %r10, %rax
leaq (%r14,%rax), %rcx
movq %rcx, 0x28(%rsp)
movl %ebx, %r13d
movl 0x18(%r14,%rax), %ebp
movl 0x1c(%r14,%rax), %r15d
xorps %xmm0, %xmm0
testl %r11d, %r11d
je 0x1c686
movss 0x4(%rsp), %xmm4
testl %ebx, %ebx
jle 0x1c6de
movq %r10, 0x10(%rsp)
movl %r15d, %edx
subl %ebp, %edx
xorl %eax, %eax
movslq (%r12,%rax,4), %rcx
imulq $0x68, %rcx, %rcx
movl 0x1c(%r14,%rcx), %r9d
cmpl %ebp, %r9d
jle 0x1c668
movl 0x18(%r14,%rcx), %r10d
cmpl %r10d, %r15d
jle 0x1c668
movl %r9d, %r8d
subl %r10d, %r8d
cmpl %edx, %r8d
movl %edx, %edi
cmovll %r8d, %edi
movl %edx, %esi
cmovgl %r8d, %esi
cmpl %r10d, %ebp
jge 0x1c62a
movl %r15d, %r11d
subl %r10d, %r11d
jl 0x1c632
cmpl %r9d, %r15d
cmovll %r11d, %r8d
jmp 0x1c63e
movl %r9d, %r8d
subl %ebp, %r8d
jge 0x1c637
xorl %r8d, %r8d
jmp 0x1c63e
cmpl %r15d, %r9d
cmovgel %edx, %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 0x1c6e5
incq %rax
cmpq %rax, %r13
jne 0x1c5e3
movl %r13d, %eax
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r8
jmp 0x1c716
testl %ebx, %ebx
movss 0x4(%rsp), %xmm4
jle 0x1c724
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%r12,%rax,4), %rdx
imulq $0x68, %rdx, %rsi
movl 0x1c(%r14,%rsi), %edx
cmpl %ebp, %edx
jle 0x1c6d4
movl 0x18(%r14,%rsi), %esi
cmpl %r15d, %esi
jge 0x1c6d4
cmpl %ebp, %esi
jg 0x1c6b9
movl %ebp, %esi
cmpl %r15d, %edx
jl 0x1c6c1
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, %r13
jne 0x1c698
jmp 0x1c728
xorl %eax, %eax
jmp 0x1c7a8
addq %r14, %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 0x1c716
incl 0x40(%rcx)
movq 0x10(%rsp), %r10
movl (%rsp), %r11d
jmp 0x1c7a8
xorl %ecx, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x1c7a8
jle 0x1c5cf
movq %r10, 0x10(%rsp)
movl %ebx, 0x24(%rsp)
movl %ecx, %ebx
leaq (%r8,%rbx,8), %rsi
movq %r8, %rdi
callq 0x48ff
xorl %r9d, %r9d
movq 0x18(%rsp), %r8
xorl %ecx, %ecx
xorl %eax, %eax
movl %ebp, %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 0x1c757
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 0x1c5cf
cmpl %ebx, %eax
jne 0x1c7c5
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 0x1c5a3
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r8, %rsi
callq 0x30f6
movq %rbx, %rdi
movq %r12, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x30f6
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq