// File: 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
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 0x28af4d
jmp 0x28b423
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x28af7b
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rdx
imulq $0x48, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x28af54
movq -0x8(%rbp), %rdi
movslq -0x10(%rbp), %rsi
shlq $0x3, %rsi
callq 0x30e4f0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x10(%rbp), %rsi
shlq $0x2, %rsi
callq 0x30e4f0
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 0x28b409
movq -0x18(%rbp), %rax
movslq -0x24(%rbp), %rcx
imulq $0x48, %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 0x28b014
jmp 0x28b18b
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x28b0b7
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0x28(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x48, %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 0x28b066
movl -0x64(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x28b068
jmp 0x28b0a9
movl -0x64(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x28b076
movl -0x4c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x50(%rbp), %eax
jle 0x28b084
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 0x28b01b
cmpl $0x0, -0x54(%rbp)
jne 0x28b0c2
jmp 0x28b3c4
cmpl $0x0, -0x54(%rbp)
jle 0x28b187
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 0x27a9b0
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x28b171
movq -0x40(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpl -0x70(%rbp), %eax
jle 0x28b12f
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 0x28b154
movq -0x40(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x94(%rbp)
jmp 0x28b15d
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 0x28b0f1
movl -0x50(%rbp), %eax
cmpl -0x70(%rbp), %eax
jle 0x28b185
movl -0x50(%rbp), %eax
subl -0x70(%rbp), %eax
addl -0x58(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x28b187
jmp 0x28b189
jmp 0x28b18b
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x28b3c2
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0x28(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x48, %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 0x28b1dd
movl -0x7c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x28b1e2
jmp 0x28b3b4
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl -0x50(%rbp), %ecx
subl -0x4c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x28b200
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, -0x98(%rbp)
jmp 0x28b20c
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 0x28b236
movl -0x80(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, -0x9c(%rbp)
jmp 0x28b242
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 0x28b2a4
movl -0x50(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jge 0x28b268
xorl %eax, %eax
movl %eax, -0xa0(%rbp)
jmp 0x28b296
movl -0x50(%rbp), %eax
cmpl -0x80(%rbp), %eax
jge 0x28b27e
movl -0x50(%rbp), %eax
subl -0x7c(%rbp), %eax
movl %eax, -0xa4(%rbp)
jmp 0x28b28a
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 0x28b2f0
movl -0x80(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x28b2b6
xorl %eax, %eax
movl %eax, -0xac(%rbp)
jmp 0x28b2e4
movl -0x80(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x28b2cc
movl -0x80(%rbp), %eax
subl -0x4c(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x28b2d8
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 0x28b3b2
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 0x28b366
movq -0x78(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0xb4(%rbp)
jmp 0x28b373
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 0x28b39a
movl $0x1, -0x90(%rbp)
cmpl $0x0, -0x90(%rbp)
je 0x28b3b0
movq -0x78(%rbp), %rax
movl 0x40(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x40(%rax)
jmp 0x28b3c2
jmp 0x28b3b4
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x28b192
jmp 0x28b3c4
movl -0x28(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jne 0x28b3f9
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 0x28b3fb
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x28afc8
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x30e2d0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x30e2d0
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)