// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %r9d, 0x10(%rsp)
movl %r8d, 0xc(%rsp)
movl %esi, 0x14(%rsp)
testl %edx, %edx
jle 0x1c11d
movq %rcx, %r15
movl %edx, %ebx
movq %rdi, %r14
movss %xmm1, 0x4(%rsp)
movss %xmm0, 0x8(%rsp)
movl %edx, %ecx
leal 0x1(%rbx), %eax
andl $-0x2, %eax
movq %rcx, 0x20(%rsp)
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x125aa(%rip), %xmm1    # 0x2e5e0
xorl %ecx, %ecx
movdqa 0x12960(%rip), %xmm2    # 0x2e9a0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x12960(%rip), %xmm4    # 0x2e9b0
movq %r15, %rdx
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x1c087
movl %ecx, (%rdx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x1c09c
leal 0x1(%rcx), %esi
movl %esi, 0x50(%rdx)
addq $0x2, %rcx
paddq %xmm4, %xmm1
addq $0xa0, %rdx
cmpq %rcx, %rax
jne 0x1c053
movq 0x20(%rsp), %r12
leaq (,%r12,8), %rsi
movq %r14, %rdi
callq 0x1264d
movq %rax, %rbp
leaq (,%r12,4), %rsi
movq %r14, 0x38(%rsp)
movq %r14, %rdi
callq 0x1264d
movq %rax, %r14
xorl %eax, %eax
movl %eax, (%r14)
movl %eax, 0x20(%r15)
cmpl $0x1, %ebx
movss 0x8(%rsp), %xmm4
jne 0x1c12c
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x12587
movq %rbx, %rdi
movq %r14, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12587
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rsp), %xmm0
movsd 0xeeeb(%rip), %xmm1     # 0x2b028
subsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movl $0x1, %eax
movl $0x1, %r9d
movq %rbp, 0x40(%rsp)
movq %rax, 0x30(%rsp)
leaq (%rax,%rax,4), %rax
shlq $0x4, %rax
leaq (%r15,%rax), %rcx
movq %rcx, 0x28(%rsp)
movl $0x0, 0x4(%rsp)
movl 0x10(%r15,%rax), %r12d
movl 0x14(%r15,%rax), %ebx
cmpl $0x0, 0x10(%rsp)
movl %r9d, (%rsp)
je 0x1c24c
movl $0x0, %eax
testl %r9d, %r9d
jle 0x1c439
xorps %xmm0, %xmm0
cvtsi2ssl 0x4(%rsp), %xmm0
movl %ebx, %r8d
subl %r12d, %r8d
movl %r9d, %r9d
xorl %eax, %eax
movslq (%r14,%rax,4), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl 0x14(%r15,%rcx), %r11d
movl %r11d, %r10d
subl %r12d, %r10d
jle 0x1c23b
movl 0x10(%r15,%rcx), %edi
movl %ebx, %ebp
subl %edi, %ebp
jle 0x1c23b
movl %r11d, %esi
subl %edi, %esi
cmpl %r8d, %esi
movl %r8d, %edx
cmovll %esi, %edx
movl %r8d, %r13d
cmovgl %esi, %r13d
cmpl %edi, %r12d
jge 0x1c1f6
cmpl %r11d, %ebx
cmovll %ebp, %esi
jmp 0x1c200
cmpl %ebx, %r11d
cmovgel %r8d, %r10d
movl %r10d, %esi
xorps %xmm1, %xmm1
cvtsi2ss %esi, %xmm1
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movd %r13d, %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 0x1c23b
movl 0x4(%rsp), %edi
cmpl 0x14(%rsp), %edi
jle 0x1c316
incq %rax
cmpq %rax, %r9
jne 0x1c1b0
jmp 0x1c43e
testl %r9d, %r9d
jle 0x1c437
movl %r9d, %eax
xorl %edx, %edx
xorl %ecx, %ecx
movslq (%r14,%rdx,4), %rsi
leaq (%rsi,%rsi,4), %rdi
shlq $0x4, %rdi
movl 0x14(%r15,%rdi), %esi
cmpl %r12d, %esi
jle 0x1c29d
movl 0x10(%r15,%rdi), %edi
cmpl %ebx, %edi
jge 0x1c29d
cmpl %r12d, %edi
jg 0x1c283
movl %r12d, %edi
cmpl %ebx, %esi
jl 0x1c289
movl %ebx, %esi
shlq $0x20, %rdi
movslq %esi, %rsi
orq %rdi, %rsi
movslq %ecx, %rdi
incl %ecx
movq %rsi, (%rbp,%rdi,8)
incq %rdx
cmpq %rdx, %rax
jne 0x1c25c
movl %r9d, %eax
testl %ecx, %ecx
je 0x1c439
jle 0x1c486
movl %ecx, %r13d
leaq (,%r13,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x5c97
xorl %ecx, %ecx
xorl %eax, %eax
movl %r12d, %edx
xorl %r8d, %r8d
movl %edx, %esi
movq (%rbp,%rcx,8), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
subl %esi, %edi
cmovlel %r8d, %edi
addl %edi, %eax
cmpl %edx, %esi
cmovgl %esi, %edx
incq %rcx
cmpq %rcx, %r13
jne 0x1c2d6
movl %ebx, %ecx
subl %edx, %ecx
cmovlel %r8d, %ecx
addl %eax, %ecx
movl %ecx, 0x4(%rsp)
movss 0x8(%rsp), %xmm4
movl (%rsp), %r9d
jmp 0x1c18e
addq %r15, %rcx
movq 0x28(%rsp), %r12
movl 0xc(%r12), %r8d
movl 0x20(%rcx), %edi
movl %edi, 0x20(%r12)
movl 0x3c(%rcx), %r9d
btl $0x19, %r9d
jb 0x1c374
testb $0x2, 0x3f(%r12)
sete %dil
testl %r8d, %r8d
sets %r10b
orb %dil, %r10b
jne 0x1c374
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
mulsd 0x18(%rsp), %xmm0
addsd 0xecbe(%rip), %xmm0     # 0x2b020
cvttsd2si %xmm0, %r8d
cmpl $0x2, %r8d
movl $0x1, %edi
cmovll %edi, %r8d
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 0x1c429
movq 0x48(%r12), %r11
testq %r11, %r11
je 0x1c429
movl 0x8(%rcx), %ebx
cmpl 0x8(%r12), %ebx
jne 0x1c3c8
movl 0x18(%rcx), %ebx
cmpl 0x18(%r12), %ebx
jne 0x1c3c8
movl 0x1c(%rcx), %ebx
cmpl 0x1c(%r12), %ebx
jne 0x1c3c8
cmpl %edx, %esi
je 0x1c429
btl $0x19, %r9d
movl 0x8(%r11), %edx
jb 0x1c40c
testb $0x2, 0x3f(%r12)
sete %sil
testl %edx, %edx
sets %r9b
orb %sil, %r9b
jne 0x1c40c
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
mulsd 0x18(%rsp), %xmm0
addsd 0xec23(%rip), %xmm0     # 0x2b020
cvttsd2si %xmm0, %edx
cmpl $0x2, %edx
movl $0x1, %esi
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 0xc(%rsp), %esi
jle 0x1c42e
cmpl %edi, %r8d
jl 0x1c431
incl 0x34(%rcx)
movl (%rsp), %r9d
jmp 0x1c439
xorl %eax, %eax
cmpl %r9d, %eax
jne 0x1c465
movl (%rsp), %r9d
movslq %r9d, %rax
incl %r9d
movq 0x30(%rsp), %rcx
movl %ecx, (%r14,%rax,4)
movq %rcx, %rax
movq 0x28(%rsp), %rcx
movl %eax, 0x20(%rcx)
movl $0x0, 0x34(%rcx)
jmp 0x1c46e
movq 0x30(%rsp), %rax
movl (%rsp), %r9d
incq %rax
cmpq 0x20(%rsp), %rax
movq 0x40(%rsp), %rbp
jne 0x1c157
jmp 0x1c0f4
movl $0x0, 0x4(%rsp)
jmp 0x1c18e