// File: /workspace/github_c_makefile_star2_O0/lh3[P]miniprot/hit.c
// Function: mp_set_parent
void mp_set_parent(void *km, float mask_level, int mask_len, int n, mp_reg1_t *r, int sub_diff, int hard_mask_level) // and compute mp_reg1_t::subsc
{
	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) {
		mp_reg1_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
			mp_reg1_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_mp64(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
			mp_reg1_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 && uncov_len <= mask_len) { // then this is a secondary hit
				int cnt_sub = 0, sci = ri->chn_sc;
				ri->parent = rp->parent;
				rp->subsc = rp->subsc > sci? rp->subsc : sci;
				if (ri->cnt >= rp->cnt) cnt_sub = 1;
				if (rp->p && ri->p && (rp->vid != ri->vid || rp->vs != ri->vs || rp->ve != ri->ve || ol != min)) { // the last condition excludes identical hits after DP
					sci = ri->p->dp_max;
					rp->p->dp_max2 = rp->p->dp_max2 > sci? rp->p->dp_max2 : sci;
					if (rp->p->dp_max - ri->p->dp_max <= sub_diff) 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 $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movss %xmm0, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
cmpl $0x0, -0x14(%rbp)
jg 0x1aac1
jmp 0x1b0c9
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1aaf0
movl -0x2c(%rbp), %ecx
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rdx
imulq $0x68, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1aac8
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
shlq $0x3, %rsi
callq 0xaeb0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
shlq $0x2, %rsi
callq 0xaeb0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0xc(%rax)
movl $0x1, -0x2c(%rbp)
movl $0x1, -0x34(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x1b0af
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x1ab89
jmp 0x1ad00
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1ac2c
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x54(%rbp), %eax
jle 0x1abdb
movl -0x6c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jl 0x1abdd
jmp 0x1ac1e
movl -0x6c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x1abeb
movl -0x54(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x58(%rbp), %eax
jle 0x1abf9
movl -0x58(%rbp), %eax
movl %eax, -0x70(%rbp)
movslq -0x6c(%rbp), %rdx
shlq $0x20, %rdx
movslq -0x70(%rbp), %rax
orq %rax, %rdx
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x5c(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1ab90
cmpl $0x0, -0x5c(%rbp)
jne 0x1ac37
jmp 0x1b06a
cmpl $0x0, -0x5c(%rbp)
jle 0x1acfc
movl -0x54(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x48(%rbp), %rdi
movq -0x48(%rbp), %rsi
movslq -0x5c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x46c0
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x1ace6
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpl -0x78(%rbp), %eax
jle 0x1aca4
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
shrq $0x20, %rcx
movslq -0x78(%rbp), %rax
subq %rax, %rcx
movslq -0x60(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl -0x78(%rbp), %eax
jle 0x1acc9
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0xa0(%rbp)
jmp 0x1acd2
movl -0x78(%rbp), %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x1ac66
movl -0x58(%rbp), %eax
cmpl -0x78(%rbp), %eax
jle 0x1acfa
movl -0x58(%rbp), %eax
subl -0x78(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x1acfc
jmp 0x1acfe
jmp 0x1ad00
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1b068
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl 0x34(%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x80(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpl -0x54(%rbp), %eax
jle 0x1ad5e
movl -0x84(%rbp), %eax
cmpl -0x58(%rbp), %eax
jl 0x1ad63
jmp 0x1b05a
movl -0x88(%rbp), %eax
subl -0x84(%rbp), %eax
movl -0x58(%rbp), %ecx
subl -0x54(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1ad8d
movl -0x88(%rbp), %eax
subl -0x84(%rbp), %eax
movl %eax, -0xa4(%rbp)
jmp 0x1ad99
movl -0x58(%rbp), %eax
subl -0x54(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x88(%rbp), %eax
subl -0x84(%rbp), %eax
movl -0x58(%rbp), %ecx
subl -0x54(%rbp), %ecx
cmpl %ecx, %eax
jle 0x1adcf
movl -0x88(%rbp), %eax
subl -0x84(%rbp), %eax
movl %eax, -0xa8(%rbp)
jmp 0x1addb
movl -0x58(%rbp), %eax
subl -0x54(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0x1ae4f
movl -0x58(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0x1ae07
xorl %eax, %eax
movl %eax, -0xac(%rbp)
jmp 0x1ae41
movl -0x58(%rbp), %eax
cmpl -0x88(%rbp), %eax
jge 0x1ae23
movl -0x58(%rbp), %eax
subl -0x84(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x1ae35
movl -0x88(%rbp), %eax
subl -0x84(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0xb4(%rbp)
jmp 0x1aea4
movl -0x88(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x1ae64
xorl %eax, %eax
movl %eax, -0xb8(%rbp)
jmp 0x1ae98
movl -0x88(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x1ae80
movl -0x88(%rbp), %eax
subl -0x54(%rbp), %eax
movl %eax, -0xbc(%rbp)
jmp 0x1ae8c
movl -0x58(%rbp), %eax
subl -0x54(%rbp), %eax
movl %eax, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl %eax, -0xb8(%rbp)
movl -0xb8(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, -0x94(%rbp)
cvtsi2ssl -0x94(%rbp), %xmm0
cvtsi2ssl -0x8c(%rbp), %xmm1
divss %xmm1, %xmm0
cvtsi2ssl -0x60(%rbp), %xmm1
cvtsi2ssl -0x90(%rbp), %xmm2
divss %xmm2, %xmm1
subss %xmm1, %xmm0
ucomiss -0xc(%rbp), %xmm0
jbe 0x1b058
movl -0x60(%rbp), %eax
cmpl -0x10(%rbp), %eax
jg 0x1b058
movl $0x0, -0x98(%rbp)
movq -0x50(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x80(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x80(%rbp), %rax
movl 0x14(%rax), %eax
cmpl -0x9c(%rbp), %eax
jle 0x1af32
movq -0x80(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0xc0(%rbp)
jmp 0x1af3e
movl -0x9c(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl -0xc0(%rbp), %ecx
movq -0x80(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x80(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jl 0x1af65
movl $0x1, -0x98(%rbp)
movq -0x80(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x1b040
movq -0x50(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x1b040
movq -0x80(%rbp), %rax
movl 0x30(%rax), %eax
movq -0x50(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jne 0x1afc5
movq -0x80(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x50(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jne 0x1afc5
movq -0x80(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x50(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jne 0x1afc5
movl -0x94(%rbp), %eax
cmpl -0x8c(%rbp), %eax
je 0x1b040
movq -0x50(%rbp), %rax
movq 0x60(%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x80(%rbp), %rax
movq 0x60(%rax), %rax
movl 0x8(%rax), %eax
cmpl -0x9c(%rbp), %eax
jle 0x1affc
movq -0x80(%rbp), %rax
movq 0x60(%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0xc4(%rbp)
jmp 0x1b008
movl -0x9c(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl -0xc4(%rbp), %ecx
movq -0x80(%rbp), %rax
movq 0x60(%rax), %rax
movl %ecx, 0x8(%rax)
movq -0x80(%rbp), %rax
movq 0x60(%rax), %rax
movl 0x4(%rax), %eax
movq -0x50(%rbp), %rcx
movq 0x60(%rcx), %rcx
subl 0x4(%rcx), %eax
cmpl -0x24(%rbp), %eax
jg 0x1b03e
movl $0x1, -0x98(%rbp)
jmp 0x1b040
cmpl $0x0, -0x98(%rbp)
je 0x1b056
movq -0x80(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
jmp 0x1b068
jmp 0x1b05a
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1ad07
jmp 0x1b06a
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jne 0x1b09f
movl -0x2c(%rbp), %edx
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x34(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x50(%rbp), %rax
movl $0x0, 0x10(%rax)
jmp 0x1b0a1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1ab3d
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xac90
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xac90
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)