// File: /workspace/github_c_makefile_star2_O1/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, 0x40(%rsp)
movl %r8d, 0x3c(%rsp)
movl %esi, 0x44(%rsp)
testl %edx, %edx
jle 0x1c46c
movq %rcx, %r15
movq %rdi, %rbx
movss %xmm1, 0x30(%rsp)
movss %xmm0, 0x2c(%rsp)
movl %edx, %edx
xorl %eax, %eax
movl %eax, (%rcx)
incq %rax
addq $0x50, %rcx
cmpq %rax, %rdx
jne 0x1c040
leaq (,%rdx,8), %rsi
movq %rbx, %rdi
movq %rdx, 0x8(%rsp)
callq 0x126e9
movq %rax, %r14
movq 0x8(%rsp), %rax
leaq (,%rax,4), %rsi
movq %rbx, 0x50(%rsp)
movq %rbx, %rdi
callq 0x126e9
movq %r14, %r11
movq 0x8(%rsp), %r8
movq %rax, %r14
xorl %eax, %eax
movl %eax, (%r14)
movl %eax, 0x20(%r15)
cmpl $0x1, %r8d
movss 0x2c(%rsp), %xmm4
je 0x1c443
xorps %xmm0, %xmm0
cvtss2sd 0x30(%rsp), %xmm0
movsd 0xdf73(%rip), %xmm1     # 0x2a028
subsd %xmm0, %xmm1
movsd %xmm1, 0x48(%rsp)
movl $0x1, %r10d
movl $0x1, %edx
movq %r11, 0x20(%rsp)
leaq (%r10,%r10,4), %rax
shlq $0x4, %rax
leaq (%r15,%rax), %rcx
movq %rcx, 0x30(%rsp)
xorl %edi, %edi
movl %edx, %ebx
movl 0x10(%r15,%rax), %r12d
movl 0x14(%r15,%rax), %r13d
cmpl $0x0, 0x40(%rsp)
je 0x1c1d6
movl $0x0, %eax
testl %edx, %edx
jle 0x1c41a
movl %edx, 0x1c(%rsp)
movq %r10, 0x10(%rsp)
movl %r13d, %r8d
subl %r12d, %r8d
xorps %xmm0, %xmm0
cvtsi2ss %edi, %xmm0
xorl %eax, %eax
movslq (%r14,%rax,4), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
movl 0x14(%r15,%rcx), %r10d
cmpl %r12d, %r10d
jle 0x1c1b0
movl 0x10(%r15,%rcx), %r11d
cmpl %r11d, %r13d
jle 0x1c1b0
movl %r10d, %esi
subl %r11d, %esi
cmpl %r8d, %esi
movl %r8d, %edx
cmovll %esi, %edx
movl %r8d, %r9d
cmovgl %esi, %r9d
cmpl %r11d, %r12d
jge 0x1c169
movl %r13d, %ebp
subl %r11d, %ebp
jl 0x1c171
cmpl %r10d, %r13d
cmovll %ebp, %esi
jmp 0x1c17c
movl %r10d, %esi
subl %r12d, %esi
jge 0x1c175
xorl %esi, %esi
jmp 0x1c17c
cmpl %r13d, %r10d
cmovgel %r8d, %esi
xorps %xmm1, %xmm1
cvtsi2ss %esi, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %edx, %xmm2
divss %xmm2, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %r9d, %xmm2
movaps %xmm0, %xmm3
divss %xmm2, %xmm3
subss %xmm3, %xmm1
ucomiss %xmm4, %xmm1
jbe 0x1c1b0
cmpl 0x44(%rsp), %edi
jle 0x1c236
incq %rax
cmpq %rax, %rbx
jne 0x1c11e
movl %ebx, %eax
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %r11
movq 0x10(%rsp), %r10
movl 0x1c(%rsp), %edx
jmp 0x1c41a
testl %edx, %edx
jle 0x1c30e
movl %edx, %edi
xorl %eax, %eax
xorl %ecx, %ecx
movslq (%r14,%rax,4), %rdx
leaq (%rdx,%rdx,4), %rsi
shlq $0x4, %rsi
movl 0x14(%r15,%rsi), %edx
cmpl %r12d, %edx
jle 0x1c227
movl 0x10(%r15,%rsi), %esi
cmpl %r13d, %esi
jge 0x1c227
cmpl %r12d, %esi
jg 0x1c20c
movl %r12d, %esi
cmpl %r13d, %edx
jl 0x1c214
movl %r13d, %edx
shlq $0x20, %rsi
movslq %edx, %rdx
orq %rsi, %rdx
movslq %ecx, %rsi
incl %ecx
movq %rdx, (%r11,%rsi,8)
incq %rax
cmpq %rax, %rbx
jne 0x1c1e4
movl %edi, %edx
jmp 0x1c312
addq %r15, %rcx
movq 0x30(%rsp), %rbx
movl 0xc(%rbx), %edi
movl 0x20(%rcx), %r8d
movl %r8d, 0x20(%rbx)
movl 0x3c(%rcx), %r8d
btl $0x19, %r8d
jb 0x1c28d
testb $0x2, 0x3f(%rbx)
sete %r9b
testl %edi, %edi
sets %r10b
orb %r9b, %r10b
jne 0x1c28d
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
mulsd 0x48(%rsp), %xmm0
addsd 0xdda4(%rip), %xmm0     # 0x2a020
cvttsd2si %xmm0, %edi
cmpl $0x2, %edi
movl $0x1, %r9d
cmovll %r9d, %edi
movq 0x10(%rsp), %r10
movl 0x24(%rcx), %r9d
cmpl %edi, %r9d
cmovgl %r9d, %edi
movl %edi, 0x24(%rcx)
movl 0x4(%rbx), %r9d
xorl %edi, %edi
cmpl 0x4(%rcx), %r9d
setge %dil
movq 0x48(%rcx), %r9
testq %r9, %r9
movq 0x20(%rsp), %r11
je 0x1c38f
movq 0x48(%rbx), %r10
testq %r10, %r10
je 0x1c405
movl 0x8(%rcx), %r11d
cmpl 0x8(%rbx), %r11d
jne 0x1c396
movl 0x18(%rcx), %r11d
cmpl 0x18(%rbx), %r11d
jne 0x1c396
movl 0x1c(%rcx), %r11d
cmpl 0x1c(%rbx), %r11d
jne 0x1c396
cmpl %edx, %esi
jne 0x1c396
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %r11
jmp 0x1c40a
xorl %ecx, %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x1c41a
jle 0x1c388
movl %edx, 0x1c(%rsp)
movq %r10, 0x10(%rsp)
movl %ecx, %ebp
leaq (%r11,%rbp,8), %rsi
movq %r11, %rdi
callq 0x5d2f
movq 0x20(%rsp), %r11
xorl %ecx, %ecx
xorl %eax, %eax
movl %r12d, %edx
xorl %r8d, %r8d
movl %edx, %esi
movq (%r11,%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, %rbp
jne 0x1c342
movl %r13d, %edi
subl %edx, %edi
cmovlel %r8d, %edi
addl %eax, %edi
movss 0x2c(%rsp), %xmm4
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r10
movl 0x1c(%rsp), %edx
jmp 0x1c0f9
xorl %edi, %edi
jmp 0x1c0f9
movq 0x8(%rsp), %r8
jmp 0x1c40f
btl $0x19, %r8d
movl 0x8(%r10), %edx
jb 0x1c3d8
testb $0x2, 0x3f(%rbx)
sete %sil
testl %edx, %edx
sets %r8b
orb %sil, %r8b
jne 0x1c3d8
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
mulsd 0x48(%rsp), %xmm0
addsd 0xdc57(%rip), %xmm0     # 0x2a020
cvttsd2si %xmm0, %edx
cmpl $0x2, %edx
movl $0x1, %esi
cmovll %esi, %edx
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %r11
movl 0xc(%r9), %esi
cmpl %edx, %esi
cmovgl %esi, %edx
movl 0x8(%r9), %esi
movl %edx, 0xc(%r9)
subl 0x8(%r10), %esi
cmpl 0x3c(%rsp), %esi
movl $0x1, %edx
cmovlel %edx, %edi
jmp 0x1c40a
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r10
testl %edi, %edi
movl 0x1c(%rsp), %edx
je 0x1c41a
incl 0x34(%rcx)
cmpl %edx, %eax
jne 0x1c437
movslq %edx, %rax
incl %edx
movl %r10d, (%r14,%rax,4)
movq 0x30(%rsp), %rax
movl %r10d, 0x20(%rax)
movl $0x0, 0x34(%rax)
incq %r10
cmpq %r8, %r10
jne 0x1c0cf
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r11, %rsi
callq 0x1261e
movq %rbx, %rdi
movq %r14, %rsi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1261e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq