// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap2/hit.c
// Function: mm_set_mapq2
void mm_set_mapq2(void *km, int n_regs, mm_reg1_t *regs, int min_chain_sc, int match_sc, int rep_len, int is_sr, int is_splice)
{
	static const float q_coef = 40.0f;
	int64_t sum_sc = 0;
	float uniq_ratio;
	int i, n_2nd_splice = 0;
	if (n_regs == 0) return;
	for (i = 0; i < n_regs; ++i) {
		if (regs[i].parent == regs[i].id)
			sum_sc += regs[i].score;
		else if (regs[i].is_spliced)
			++n_2nd_splice;
	}
	uniq_ratio = (float)sum_sc / (sum_sc + rep_len);
	for (i = 0; i < n_regs; ++i) {
		mm_reg1_t *r = &regs[i];
		if (r->inv) {
			r->mapq = 0;
		} else if (r->parent == r->id) {
			int mapq, subsc;
			float pen_s1 = (r->score > 100? 1.0f : 0.01f * r->score) * uniq_ratio;
			float pen_cm = r->cnt > 10? 1.0f : 0.1f * r->cnt;
			pen_cm = pen_s1 < pen_cm? pen_s1 : pen_cm;
			subsc = r->subsc > min_chain_sc? r->subsc : min_chain_sc;
			if (r->p && r->p->dp_max2 > 0 && r->p->dp_max > 0) {
				float x, identity = (float)r->mlen / r->blen;
				if (is_sr && is_splice)
					x = (float)r->p->dp_max2 / r->p->dp_max; // ignore chaining score; for short RNA-seq reads, unspliced chaining score tends to be higher
				else
					x = (float)r->p->dp_max2 * subsc / r->p->dp_max / r->score0;
				mapq = (int)(identity * pen_cm * q_coef * (1.0f - x * x) * logf((float)r->p->dp_max / match_sc));
				if (!is_sr) {
					int mapq_alt = (int)(6.02f * identity * identity * (r->p->dp_max - r->p->dp_max2) / match_sc + .499f); // BWA-MEM like mapQ, mostly for short reads
					mapq = mapq < mapq_alt? mapq : mapq_alt; // in case the long-read heuristic fails
				}
				if (is_splice && is_sr && r->is_spliced && n_2nd_splice == 0)
					mapq += 10;
			} else {
				float x = (float)subsc / r->score0;
				if (r->p) {
					float identity = (float)r->mlen / r->blen;
					mapq = (int)(identity * pen_cm * q_coef * (1.0f - x) * logf((float)r->p->dp_max / match_sc));
				} else {
					mapq = (int)(pen_cm * q_coef * (1.0f - x) * logf(r->score));
				}
			}
			mapq -= (int)(4.343f * logf(r->n_sub + 1) + .499f);
			mapq = mapq > 0? mapq : 0;
			r->mapq = mapq < 60? mapq : 60;
			if (r->p && r->p->dp_max > r->p->dp_max2 && r->mapq == 0) r->mapq = 1;
		} else r->mapq = 0;
	}
	mm_set_inv_mapq(km, n_regs, regs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
testl %esi, %esi
je 0x1a8fd
movl %ecx, %ebp
movq %rdx, %rbx
movl 0x78(%rsp), %eax
xorl %ecx, %ecx
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
movl 0x70(%rsp), %edi
movl %edi, 0x18(%rsp)
imulq $0x50, %rdx, %r13
xorl %r14d, %r14d
xorl %edx, %edx
cmpq %rcx, %r13
je 0x1a605
movl 0x20(%rbx,%rcx), %edi
cmpl (%rbx,%rcx), %edi
jne 0x1a5f5
movslq 0xc(%rbx,%rcx), %rdi
addq %rdi, %rdx
jmp 0x1a5ff
btl $0x1b, 0x3c(%rbx,%rcx)
adcl $0x0, %r14d
addq $0x50, %rcx
jmp 0x1a5dd
movq %rsi, 0x30(%rsp)
cvtsi2ss %rdx, %xmm1
movslq %r9d, %rcx
addq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x24(%rsp)
cmpl $0x0, 0x18(%rsp)
setne %cl
testl %eax, %eax
cvtsi2ss %r8d, %xmm3
setne %al
andb %cl, %al
movb %al, 0xf(%rsp)
xorl %r15d, %r15d
movss 0xd5a9(%rip), %xmm4     # 0x27bf0
movss %xmm3, 0x1c(%rsp)
cmpq %r15, %r13
je 0x1a8f3
movl 0x3c(%rbx,%r15), %eax
btl $0xb, %eax
jb 0x1a703
movl 0x20(%rbx,%r15), %ecx
cmpl (%rbx,%r15), %ecx
jne 0x1a70d
movl 0xc(%rbx,%r15), %eax
cvtsi2ss %eax, %xmm0
movaps %xmm0, %xmm1
mulss 0xf2d4(%rip), %xmm1     # 0x2995c
movaps %xmm4, %xmm5
cmpl $0x65, %eax
jge 0x1a693
movaps %xmm1, %xmm5
movl 0x4(%rbx,%r15), %eax
cvtsi2ss %eax, %xmm2
mulss 0xf2bc(%rip), %xmm2     # 0x29960
movaps %xmm4, %xmm1
cmpl $0xb, %eax
jge 0x1a6af
movaps %xmm2, %xmm1
mulss 0x24(%rsp), %xmm5
minss %xmm1, %xmm5
movl 0x24(%rbx,%r15), %ecx
cmpl %ebp, %ecx
cmovlel %ebp, %ecx
movq 0x48(%rbx,%r15), %rax
testq %rax, %rax
je 0x1a717
movl 0xc(%rax), %esi
testl %esi, %esi
jle 0x1a717
movl 0x8(%rax), %edx
testl %edx, %edx
jle 0x1a717
cvtsi2ss %esi, %xmm4
cmpb $0x0, 0xf(%rsp)
movss %xmm5, 0x10(%rsp)
cvtsi2ss %edx, %xmm0
je 0x1a80a
divss %xmm0, %xmm4
movss %xmm4, 0x14(%rsp)
jmp 0x1a827
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
jmp 0x1a7fc
andl $0xfffff700, %eax       # imm = 0xFFFFF700
jmp 0x1a7fc
cvtsi2ss %ecx, %xmm1
cvtsi2ssl 0x38(%rbx,%r15), %xmm2
divss %xmm2, %xmm1
subss %xmm1, %xmm4
testq %rax, %rax
je 0x1a765
movsd 0x2c(%rbx,%r15), %xmm0
cvtdq2ps %xmm0, %xmm0
movaps %xmm0, %xmm1
shufps $0x55, %xmm0, %xmm1     # xmm1 = xmm1[1,1],xmm0[1,1]
divss %xmm1, %xmm0
mulss %xmm0, %xmm5
mulss 0xf214(%rip), %xmm5     # 0x29964
mulss %xmm5, %xmm4
movss %xmm4, 0x10(%rsp)
cvtsi2ssl 0x8(%rax), %xmm0
divss %xmm3, %xmm0
jmp 0x1a777
mulss 0xf1f7(%rip), %xmm5     # 0x29964
mulss %xmm4, %xmm5
movss %xmm5, 0x10(%rsp)
callq 0x2200
mulss 0x10(%rsp), %xmm0
cvttss2si %xmm0, %r12d
movl 0x34(%rbx,%r15), %eax
incl %eax
cvtsi2ss %eax, %xmm0
callq 0x2200
mulss 0xf1c9(%rip), %xmm0     # 0x29968
addss 0xe7c5(%rip), %xmm0     # 0x28f6c
cvttss2si %xmm0, %eax
subl %eax, %r12d
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
cmpl $0x3c, %ecx
pushq $0x3c
popq %rax
cmovgel %eax, %ecx
movl 0x3c(%rbx,%r15), %eax
movl $0xffffff00, %edx       # imm = 0xFFFFFF00
andl %edx, %eax
movb %cl, 0x3c(%rbx,%r15)
movq 0x48(%rbx,%r15), %rcx
testq %rcx, %rcx
movss 0x1c(%rsp), %xmm3
movss 0xd406(%rip), %xmm4     # 0x27bf0
je 0x1a801
movl 0x8(%rcx), %edx
cmpl 0xc(%rcx), %edx
jle 0x1a801
testl %r12d, %r12d
jg 0x1a801
orl $0x1, %eax
movl %eax, 0x3c(%rbx,%r15)
addq $0x50, %r15
jmp 0x1a64d
cvtsi2ss %ecx, %xmm1
cvtsi2ssl 0x38(%rbx,%r15), %xmm2
mulss %xmm4, %xmm1
divss %xmm0, %xmm1
divss %xmm2, %xmm1
movss %xmm1, 0x14(%rsp)
cvtsi2ssl 0x2c(%rbx,%r15), %xmm2
cvtsi2ssl 0x30(%rbx,%r15), %xmm1
divss %xmm1, %xmm2
movss %xmm2, 0x20(%rsp)
divss %xmm3, %xmm0
callq 0x2200
movss 0x20(%rsp), %xmm4
movss 0x10(%rsp), %xmm2
mulss %xmm4, %xmm2
mulss 0xf104(%rip), %xmm2     # 0x29964
movss 0x14(%rsp), %xmm3
mulss %xmm3, %xmm3
movss 0xd37e(%rip), %xmm1     # 0x27bf0
subss %xmm3, %xmm1
mulss %xmm2, %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %r12d
cmpl $0x0, 0x18(%rsp)
je 0x1a8b3
cmpb $0x0, 0xf(%rsp)
je 0x1a787
leal 0xa(%r12), %eax
testl %r14d, %r14d
cmovnel %r12d, %eax
testb $0x8, 0x3f(%rbx,%r15)
cmovel %r12d, %eax
movl %eax, %r12d
jmp 0x1a787
movaps %xmm4, %xmm0
mulss 0xe6a2(%rip), %xmm0     # 0x28f60
mulss %xmm4, %xmm0
movq 0x48(%rbx,%r15), %rax
movl 0x8(%rax), %ecx
subl 0xc(%rax), %ecx
cvtsi2ss %ecx, %xmm1
mulss %xmm0, %xmm1
divss 0x1c(%rsp), %xmm1
addss 0xe689(%rip), %xmm1     # 0x28f6c
cvttss2si %xmm1, %eax
cmpl %eax, %r12d
cmovgel %eax, %r12d
jmp 0x1a787
movq 0x30(%rsp), %rsi
cmpl $0x3, %esi
jge 0x1a90c
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, %r14d
leaq 0x3c(%rbx), %rax
movq %r14, %rdx
movq %r14, %rcx
subq $0x1, %rcx
jb 0x1a8fd
testb $0x8, 0x1(%rax)
jne 0x1a92e
addq $0x50, %rax
movq %rcx, %rdx
jmp 0x1a919
testl %edx, %edx
je 0x1a8fd
shll $0x4, %esi
movq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0x10afc
movq %rax, %r15
leaq 0x20(%rbx), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq %edx, %r12
cmpq %rcx, %r14
je 0x1a996
movl (%rax), %esi
cmpq %rsi, %rcx
setne %dil
testl %esi, %esi
setns %sil
testb %dil, %sil
jne 0x1a98d
movq %r12, %rdx
shlq $0x4, %rdx
movq %rcx, 0x8(%r15,%rdx)
movl -0x18(%rax), %esi
shlq $0x20, %rsi
movslq -0x8(%rax), %rdi
orq %rsi, %rdi
incl %r12d
movq %rdi, (%r15,%rdx)
movl %r12d, %edx
incq %rcx
addq $0x50, %rax
jmp 0x1a94d
movq %r12, %rsi
shlq $0x4, %rsi
addq %r15, %rsi
movq %r15, %rdi
callq 0x47dc
decl %r12d
movslq %r12d, %rax
movq %r15, %rcx
addq $0x28, %rcx
pushq $0x1
popq %rdx
cmpq %rax, %rdx
jge 0x1aa03
imulq $0x50, -0x10(%rcx), %rdi
movl 0x3c(%rbx,%rdi), %esi
btl $0xb, %esi
jae 0x1a9fa
imulq $0x50, -0x20(%rcx), %r8
movzbl 0x3c(%rbx,%r8), %r8d
imulq $0x50, (%rcx), %r9
movzbl 0x3c(%rbx,%r9), %r9d
cmpl %r9d, %r8d
cmovbl %r8d, %r9d
addq %rbx, %rdi
addq $0x3c, %rdi
andl $0xffffff00, %esi       # imm = 0xFFFFFF00
orl %r9d, %esi
movl %esi, (%rdi)
incq %rdx
addq $0x10, %rcx
jmp 0x1a9b8
movq %r13, %rdi
movq %r15, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10a07