// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movss %xmm0, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
cmpl $0x0, -0x10(%rbp)
jg 0x2b65d
jmp 0x2bb33
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x2b68b
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rdx
imulq $0x68, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2b664
movq -0x8(%rbp), %rdi
movslq -0x10(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2890
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x10(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2890
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x4(%rax)
movl $0x1, -0x24(%rbp)
movl $0x1, -0x2c(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x2bb19
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x2b724
jmp 0x2b89b
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x2b7c7
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0x28(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x60(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jle 0x2b776
movl -0x64(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x2b778
jmp 0x2b7b9
movl -0x64(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x2b786
movl -0x4c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x50(%rbp), %eax
jle 0x2b794
movl -0x50(%rbp), %eax
movl %eax, -0x68(%rbp)
movslq -0x64(%rbp), %rdx
shlq $0x20, %rdx
movslq -0x68(%rbp), %rax
orq %rax, %rdx
movq -0x40(%rbp), %rax
movl -0x54(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x54(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2b72b
cmpl $0x0, -0x54(%rbp)
jne 0x2b7d2
jmp 0x2bad4
cmpl $0x0, -0x54(%rbp)
jle 0x2b897
movl -0x4c(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x40(%rbp), %rdi
movq -0x40(%rbp), %rsi
movslq -0x54(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x5440
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x2b881
movq -0x40(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpl -0x70(%rbp), %eax
jle 0x2b83f
movq -0x40(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
shrq $0x20, %rcx
movslq -0x70(%rbp), %rax
subq %rax, %rcx
movslq -0x58(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl -0x70(%rbp), %eax
jle 0x2b864
movq -0x40(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x94(%rbp)
jmp 0x2b86d
movl -0x70(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x2b801
movl -0x50(%rbp), %eax
cmpl -0x70(%rbp), %eax
jle 0x2b895
movl -0x50(%rbp), %eax
subl -0x70(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x2b897
jmp 0x2b899
jmp 0x2b89b
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x2bad2
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0x28(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x7c(%rbp)
movq -0x78(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jle 0x2b8ed
movl -0x7c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x2b8f2
jmp 0x2bac4
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl -0x50(%rbp), %ecx
subl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2b910
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, -0x98(%rbp)
jmp 0x2b91c
movl -0x50(%rbp), %eax
subl -0x4c(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x98(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl -0x50(%rbp), %ecx
subl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jle 0x2b946
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, -0x9c(%rbp)
jmp 0x2b952
movl -0x50(%rbp), %eax
subl -0x4c(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jge 0x2b9b4
movl -0x50(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jge 0x2b978
xorl %eax, %eax
movl %eax, -0xa0(%rbp)
jmp 0x2b9a6
movl -0x50(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0x2b98e
movl -0x50(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, -0xa4(%rbp)
jmp 0x2b99a
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0xa8(%rbp)
jmp 0x2ba00
movl -0x80(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x2b9c6
xorl %eax, %eax
movl %eax, -0xac(%rbp)
jmp 0x2b9f4
movl -0x80(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x2b9dc
movl -0x80(%rbp), %eax
subl -0x4c(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x2b9e8
movl -0x50(%rbp), %eax
subl -0x4c(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x8c(%rbp)
cvtsi2ssl -0x8c(%rbp), %xmm0
cvtsi2ssl -0x84(%rbp), %xmm1
divss %xmm1, %xmm0
cvtsi2ssl -0x58(%rbp), %xmm1
cvtsi2ssl -0x88(%rbp), %xmm2
divss %xmm2, %xmm1
subss %xmm1, %xmm0
ucomiss -0xc(%rbp), %xmm0
jbe 0x2bac2
movl $0x0, -0x90(%rbp)
movq -0x78(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x78(%rbp), %rax
movl 0x3c(%rax), %eax
movq -0x48(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jle 0x2ba76
movq -0x78(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0xb4(%rbp)
jmp 0x2ba83
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x3c(%rax)
movq -0x48(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jl 0x2baaa
movl $0x1, -0x90(%rbp)
cmpl $0x0, -0x90(%rbp)
je 0x2bac0
movq -0x78(%rbp), %rax
movl 0x40(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x40(%rax)
jmp 0x2bad2
jmp 0x2bac4
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2b8a2
jmp 0x2bad4
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x2bb09
movl -0x24(%rbp), %edx
movq -0x38(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x2c(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x40(%rax)
jmp 0x2bb0b
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2b6d8
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x2670
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2670
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)