// File: /workspace/github_c_makefile_star2_O0/lh3[P]minimap2/hit.c
// Function: mm_set_parent
void mm_set_parent(void *km, float mask_level, int mask_len, int n, mm_reg1_t *r, int sub_diff, int hard_mask_level, float alt_diff_frac) // and compute mm_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) {
		mm_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
			mm_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_64(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
			mm_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->score;
				ri->parent = rp->parent;
				if (!rp->is_alt && ri->is_alt) sci = mm_alt_score(sci, alt_diff_frac);
				rp->subsc = rp->subsc > sci? rp->subsc : sci;
				if (ri->cnt >= rp->cnt) cnt_sub = 1;
				if (rp->p && ri->p && (rp->rid != ri->rid || rp->rs != ri->rs || rp->re != ri->re || ol != min)) { // the last condition excludes identical hits after DP
					sci = ri->p->dp_max;
					if (!rp->is_alt && ri->is_alt) sci = mm_alt_score(sci, alt_diff_frac);
					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)
movss %xmm1, -0x2c(%rbp)
cmpl $0x0, -0x14(%rbp)
jg 0x2a3e6
jmp 0x2aa61
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2a414
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rdx
imulq $0x50, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2a3ed
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
shlq $0x3, %rsi
callq 0x1aae0
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x14(%rbp), %rsi
shlq $0x2, %rsi
callq 0x1aae0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x20(%rax)
movl $0x1, -0x30(%rbp)
movl $0x1, -0x38(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2aa47
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x2a4ad
jmp 0x2a624
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x2a550
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x34(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x68(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x54(%rbp), %eax
jle 0x2a4ff
movl -0x6c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jl 0x2a501
jmp 0x2a542
movl -0x6c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x2a50f
movl -0x54(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x58(%rbp), %eax
jle 0x2a51d
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 -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2a4b4
cmpl $0x0, -0x5c(%rbp)
jne 0x2a55b
jmp 0x2aa02
cmpl $0x0, -0x5c(%rbp)
jle 0x2a620
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 0x5f90
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x2a60a
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpl -0x78(%rbp), %eax
jle 0x2a5c8
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 0x2a5ed
movq -0x48(%rbp), %rax
movslq -0x74(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0xa0(%rbp)
jmp 0x2a5f6
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 0x2a58a
movl -0x58(%rbp), %eax
cmpl -0x78(%rbp), %eax
jle 0x2a61e
movl -0x58(%rbp), %eax
subl -0x78(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x2a620
jmp 0x2a622
jmp 0x2a624
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x2aa00
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x34(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x80(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x88(%rbp)
movl -0x88(%rbp), %eax
cmpl -0x54(%rbp), %eax
jle 0x2a682
movl -0x84(%rbp), %eax
cmpl -0x58(%rbp), %eax
jl 0x2a687
jmp 0x2a9f2
movl -0x88(%rbp), %eax
subl -0x84(%rbp), %eax
movl -0x58(%rbp), %ecx
subl -0x54(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2a6b1
movl -0x88(%rbp), %eax
subl -0x84(%rbp), %eax
movl %eax, -0xa4(%rbp)
jmp 0x2a6bd
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 0x2a6f3
movl -0x88(%rbp), %eax
subl -0x84(%rbp), %eax
movl %eax, -0xa8(%rbp)
jmp 0x2a6ff
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 0x2a773
movl -0x58(%rbp), %eax
cmpl -0x84(%rbp), %eax
jge 0x2a72b
xorl %eax, %eax
movl %eax, -0xac(%rbp)
jmp 0x2a765
movl -0x58(%rbp), %eax
cmpl -0x88(%rbp), %eax
jge 0x2a747
movl -0x58(%rbp), %eax
subl -0x84(%rbp), %eax
movl %eax, -0xb0(%rbp)
jmp 0x2a759
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 0x2a7c8
movl -0x88(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x2a788
xorl %eax, %eax
movl %eax, -0xb8(%rbp)
jmp 0x2a7bc
movl -0x88(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x2a7a4
movl -0x88(%rbp), %eax
subl -0x54(%rbp), %eax
movl %eax, -0xbc(%rbp)
jmp 0x2a7b0
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 0x2a9f0
movl -0x60(%rbp), %eax
cmpl -0x10(%rbp), %eax
jg 0x2a9f0
movl $0x0, -0x98(%rbp)
movq -0x50(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x80(%rbp), %rax
movl 0x20(%rax), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x80(%rbp), %rax
movl 0x3c(%rax), %eax
shrl $0x19, %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x2a872
movq -0x50(%rbp), %rax
movl 0x3c(%rax), %eax
shrl $0x19, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2a872
movl -0x9c(%rbp), %edi
movss -0x2c(%rbp), %xmm0
callq 0x2aa70
movl %eax, -0x9c(%rbp)
movq -0x80(%rbp), %rax
movl 0x24(%rax), %eax
cmpl -0x9c(%rbp), %eax
jle 0x2a890
movq -0x80(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0xc0(%rbp)
jmp 0x2a89c
movl -0x9c(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl -0xc0(%rbp), %ecx
movq -0x80(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x80(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jl 0x2a8c3
movl $0x1, -0x98(%rbp)
movq -0x80(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x2a9d8
movq -0x50(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x2a9d8
movq -0x80(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x50(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jne 0x2a923
movq -0x80(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x50(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jne 0x2a923
movq -0x80(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x50(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jne 0x2a923
movl -0x94(%rbp), %eax
cmpl -0x8c(%rbp), %eax
je 0x2a9d8
movq -0x50(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x9c(%rbp)
movq -0x80(%rbp), %rax
movl 0x3c(%rax), %eax
shrl $0x19, %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x2a96e
movq -0x50(%rbp), %rax
movl 0x3c(%rax), %eax
shrl $0x19, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2a96e
movl -0x9c(%rbp), %edi
movss -0x2c(%rbp), %xmm0
callq 0x2aa70
movl %eax, -0x9c(%rbp)
movq -0x80(%rbp), %rax
movq 0x48(%rax), %rax
movl 0xc(%rax), %eax
cmpl -0x9c(%rbp), %eax
jle 0x2a994
movq -0x80(%rbp), %rax
movq 0x48(%rax), %rax
movl 0xc(%rax), %eax
movl %eax, -0xc4(%rbp)
jmp 0x2a9a0
movl -0x9c(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl -0xc4(%rbp), %ecx
movq -0x80(%rbp), %rax
movq 0x48(%rax), %rax
movl %ecx, 0xc(%rax)
movq -0x80(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x8(%rax), %eax
movq -0x50(%rbp), %rcx
movq 0x48(%rcx), %rcx
subl 0x8(%rcx), %eax
cmpl -0x24(%rbp), %eax
jg 0x2a9d6
movl $0x1, -0x98(%rbp)
jmp 0x2a9d8
cmpl $0x0, -0x98(%rbp)
je 0x2a9ee
movq -0x80(%rbp), %rax
movl 0x34(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x34(%rax)
jmp 0x2aa00
jmp 0x2a9f2
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x2a62b
jmp 0x2aa02
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jne 0x2aa37
movl -0x30(%rbp), %edx
movq -0x40(%rbp), %rax
movl -0x38(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x38(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x30(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x50(%rbp), %rax
movl $0x0, 0x34(%rax)
jmp 0x2aa39
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2a461
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x1a8c0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x1a8c0
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)