// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, 0x30(%rsp)
movl %r8d, 0x2c(%rsp)
movl %esi, 0x34(%rsp)
testl %edx, %edx
jle 0x19a5b
movq %rcx, %r15
movq %rdi, %rbx
movl %edx, %r12d
xorl %eax, %eax
cmpq %rax, %r12
je 0x196c5
movl %eax, (%rcx)
incq %rax
addq $0x50, %rcx
jmp 0x196b5
movss %xmm0, 0x14(%rsp)
leaq (,%r12,8), %rsi
movq %rbx, %rdi
movss %xmm1, 0x10(%rsp)
callq 0x10afc
movq %rax, 0x20(%rsp)
leaq (,%r12,4), %rsi
movq %rbx, 0x50(%rsp)
movq %rbx, %rdi
callq 0x10afc
movq %rax, %r14
andl $0x0, (%rax)
andl $0x0, 0x20(%r15)
cvtss2sd 0x10(%rsp), %xmm0
movsd 0xc8fc(%rip), %xmm1     # 0x26010
subsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
pushq $0x1
popq %rbp
pushq $0x1
popq %r13
movq %r12, 0x40(%rsp)
cmpq %r12, %r13
je 0x19a30
movq %r13, 0x18(%rsp)
imulq $0x50, %r13, %rax
xorl %esi, %esi
testl %ebp, %ebp
movl $0x0, %r12d
movl %ebp, 0xc(%rsp)
cmovgl %ebp, %r12d
leaq (%r15,%rax), %rcx
movq %rcx, 0x48(%rsp)
movl 0x10(%r15,%rax), %ebx
movl 0x14(%r15,%rax), %ebp
cmpl $0x0, 0x30(%rsp)
movss 0x14(%rsp), %xmm4
je 0x19819
movl %ebp, %r8d
subl %ebx, %r8d
movl %esi, 0x10(%rsp)
cvtsi2ss %esi, %xmm0
xorl %eax, %eax
cmpq %rax, %r12
je 0x198bc
movslq (%r14,%rax,4), %rcx
imulq $0x50, %rcx, %rcx
movl 0x14(%r15,%rcx), %r10d
movl %r10d, %r9d
subl %ebx, %r9d
jle 0x19811
movl 0x10(%r15,%rcx), %edi
movl %ebp, %r13d
subl %edi, %r13d
jle 0x19811
movl %r10d, %esi
subl %edi, %esi
cmpl %r8d, %esi
movl %r8d, %edx
cmovll %esi, %edx
movl %r8d, %r11d
cmovgl %esi, %r11d
cmpl %edi, %ebx
jge 0x197cf
cmpl %r10d, %ebp
cmovll %r13d, %esi
jmp 0x197d9
cmpl %ebp, %r10d
cmovgel %r8d, %r9d
movl %r9d, %esi
cvtsi2ss %esi, %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movd %r11d, %xmm2
movd %edx, %xmm3
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
cvtdq2ps %xmm3, %xmm2
divps %xmm2, %xmm1
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
subss %xmm2, %xmm1
ucomiss %xmm4, %xmm1
jbe 0x19811
movl 0x10(%rsp), %edi
cmpl 0x34(%rsp), %edi
jle 0x198ca
incq %rax
jmp 0x19782
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x20(%rsp), %rdi
cmpq %rcx, %r12
je 0x19865
movslq (%r14,%rcx,4), %rdx
imulq $0x50, %rdx, %rsi
movl 0x14(%r15,%rsi), %edx
cmpl %ebx, %edx
jle 0x19860
movl 0x10(%r15,%rsi), %esi
cmpl %ebp, %esi
jge 0x19860
cmpl %ebx, %esi
jg 0x19847
movl %ebx, %esi
cmpl %ebp, %edx
jl 0x1984d
movl %ebp, %edx
shlq $0x20, %rsi
movslq %edx, %rdx
orq %rsi, %rdx
movslq %eax, %rsi
incl %eax
movq %rdx, (%rdi,%rsi,8)
incq %rcx
jmp 0x19822
testl %eax, %eax
je 0x198bc
jle 0x19a29
movl %eax, %r13d
leaq (%rdi,%r13,8), %rsi
callq 0x49de
movq 0x20(%rsp), %r8
movss 0x14(%rsp), %xmm4
xorl %eax, %eax
movl %ebx, %ecx
xorl %edx, %edx
cmpq %rax, %r13
je 0x1992e
movq (%r8,%rax,8), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
subl %ecx, %edi
movl $0x0, %r9d
cmovlel %r9d, %edi
addl %edx, %edi
cmpl %esi, %ecx
cmovgl %ecx, %esi
incq %rax
movl %esi, %ecx
movl %edi, %edx
jmp 0x1988c
movq 0x18(%rsp), %r13
movl 0xc(%rsp), %ebp
jmp 0x19a01
addq %r15, %rcx
movq 0x48(%rsp), %r12
movl 0xc(%r12), %r8d
movl 0x20(%rcx), %edi
movl %edi, 0x20(%r12)
movl 0x3c(%rcx), %r9d
btl $0x19, %r9d
jb 0x19941
testb $0x2, 0x3f(%r12)
sete %dil
testl %r8d, %r8d
sets %r10b
orb %dil, %r10b
movq 0x18(%rsp), %r13
movl 0xc(%rsp), %ebp
jne 0x1994a
cvtsi2sd %r8d, %xmm0
mulsd 0x38(%rsp), %xmm0
addsd 0xc6fc(%rip), %xmm0     # 0x26018
cvttsd2si %xmm0, %r8d
cmpl $0x2, %r8d
pushq $0x1
popq %rdi
cmovll %edi, %r8d
jmp 0x1994a
movl %ebp, %esi
subl %ecx, %esi
movl $0x0, %eax
cmovlel %eax, %esi
addl %edx, %esi
jmp 0x19772
movq 0x18(%rsp), %r13
movl 0xc(%rsp), %ebp
movl 0x4(%rcx), %edi
movl 0x24(%rcx), %r10d
cmpl %r8d, %r10d
cmovgl %r10d, %r8d
movl %r8d, 0x24(%rcx)
movl 0x4(%r12), %r8d
movq 0x48(%rcx), %r10
testq %r10, %r10
je 0x199f6
movq 0x48(%r12), %r11
testq %r11, %r11
je 0x199f6
movl 0x8(%rcx), %ebx
cmpl 0x8(%r12), %ebx
jne 0x1999a
movl 0x18(%rcx), %ebx
cmpl 0x18(%r12), %ebx
jne 0x1999a
movl 0x1c(%rcx), %ebx
cmpl 0x1c(%r12), %ebx
jne 0x1999a
cmpl %edx, %esi
je 0x199f6
btl $0x19, %r9d
movl 0x8(%r11), %edx
jb 0x199d9
testb $0x2, 0x3f(%r12)
sete %sil
testl %edx, %edx
sets %r9b
orb %sil, %r9b
jne 0x199d9
cvtsi2sd %edx, %xmm0
mulsd 0x38(%rsp), %xmm0
addsd 0xc64c(%rip), %xmm0     # 0x26018
cvttsd2si %xmm0, %edx
cmpl $0x2, %edx
pushq $0x1
popq %rsi
cmovll %esi, %edx
movl 0x8(%r10), %esi
movl 0xc(%r10), %r9d
cmpl %edx, %r9d
cmovgl %r9d, %edx
movl %edx, 0xc(%r10)
subl 0x8(%r11), %esi
cmpl 0x2c(%rsp), %esi
jle 0x199fb
cmpl %edi, %r8d
jl 0x199fe
incl 0x34(%rcx)
movl %eax, %r12d
cmpl %ebp, %r12d
jne 0x19a1c
movslq %ebp, %rax
incl %ebp
movl %r13d, (%r14,%rax,4)
movq 0x48(%rsp), %rax
movl %r13d, 0x20(%rax)
andl $0x0, 0x34(%rax)
movq 0x40(%rsp), %r12
incq %r13
jmp 0x1972a
xorl %esi, %esi
jmp 0x19772
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x10a07
movq %rbx, %rdi
movq %r14, %rsi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10a07
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq