// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
testl %esi, %esi
je 0x1d45d
movl %ecx, %r15d
movq %rdx, %rbx
movl %esi, %r12d
xorl %ecx, %ecx
testl %esi, %esi
movq %rdi, 0x20(%rsp)
jle 0x1d103
movq %r12, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %rdx
xorl %r11d, %r11d
xorl %eax, %eax
movl 0x20(%rbx,%r11), %r10d
cmpl (%rbx,%r11), %r10d
jne 0x1d0e7
movslq 0xc(%rbx,%r11), %r10
addq %r10, %rax
jmp 0x1d0f1
btl $0x1b, 0x3c(%rbx,%r11)
adcl $0x0, %ecx
addq $0x50, %r11
cmpq %r11, %rdx
jne 0x1d0d2
testl %ecx, %ecx
sete 0x2(%rsp)
jmp 0x1d10a
movb $0x1, 0x2(%rsp)
xorl %eax, %eax
movl %esi, 0x14(%rsp)
testl %esi, %esi
jle 0x1d430
cvtsi2ss %rax, %xmm1
movslq %r9d, %rcx
addq %rax, %rcx
cvtsi2ss %rcx, %xmm0
movl 0x60(%rsp), %eax
divss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
movl %eax, 0x10(%rsp)
testl %eax, %eax
setne %al
cmpl $0x0, 0x68(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0x3(%rsp)
cvtsi2ss %r8d, %xmm3
movq %r12, %rax
shlq $0x4, %rax
leaq (%rax,%rax,4), %r14
xorl %r13d, %r13d
movss 0xea6a(%rip), %xmm4     # 0x2bbd0
movss %xmm3, 0xc(%rsp)
movl 0x3c(%rbx,%r13), %eax
btl $0xb, %eax
jb 0x1d227
movl 0x20(%rbx,%r13), %ecx
cmpl (%rbx,%r13), %ecx
jne 0x1d231
movl 0xc(%rbx,%r13), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps %xmm0, %xmm1
mulss 0x1077b(%rip), %xmm1    # 0x2d91c
movaps %xmm4, %xmm5
cmpl $0x65, %eax
jge 0x1d1ac
movaps %xmm1, %xmm5
movl 0x4(%rbx,%r13), %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss 0x10760(%rip), %xmm2    # 0x2d920
movaps %xmm4, %xmm1
cmpl $0xb, %eax
jge 0x1d1cb
movaps %xmm2, %xmm1
mulss 0x1c(%rsp), %xmm5
minss %xmm1, %xmm5
movl 0x24(%rbx,%r13), %ecx
cmpl %r15d, %ecx
cmovlel %r15d, %ecx
movq 0x48(%rbx,%r13), %rax
testq %rax, %rax
je 0x1d23b
movl 0xc(%rax), %edx
testl %edx, %edx
jle 0x1d23b
movl 0x8(%rax), %esi
testl %esi, %esi
jle 0x1d23b
xorps %xmm4, %xmm4
cvtsi2ss %edx, %xmm4
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
cmpb $0x0, 0x3(%rsp)
movss %xmm5, 0x4(%rsp)
je 0x1d343
divss %xmm0, %xmm4
movss %xmm4, 0x8(%rsp)
jmp 0x1d366
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
jmp 0x1d32c
andl $0xfffff700, %eax       # imm = 0xFFFFF700
jmp 0x1d32c
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
xorps %xmm2, %xmm2
cvtsi2ssl 0x38(%rbx,%r13), %xmm2
divss %xmm2, %xmm1
subss %xmm1, %xmm4
testq %rax, %rax
je 0x1d295
xorps %xmm0, %xmm0
cvtsi2ssl 0x2c(%rbx,%r13), %xmm0
xorps %xmm1, %xmm1
cvtsi2ssl 0x30(%rbx,%r13), %xmm1
divss %xmm1, %xmm0
mulss %xmm0, %xmm5
mulss 0x106a7(%rip), %xmm5    # 0x2d924
mulss %xmm5, %xmm4
movss %xmm4, 0x4(%rsp)
xorps %xmm0, %xmm0
cvtsi2ssl 0x8(%rax), %xmm0
divss %xmm3, %xmm0
jmp 0x1d2a7
mulss 0x10687(%rip), %xmm5    # 0x2d924
mulss %xmm4, %xmm5
movss %xmm5, 0x4(%rsp)
callq 0x2200
mulss 0x4(%rsp), %xmm0
cvttss2si %xmm0, %ebp
movl 0x34(%rbx,%r13), %eax
incl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
callq 0x2200
mulss 0x10657(%rip), %xmm0    # 0x2d928
addss 0xfc77(%rip), %xmm0     # 0x2cf50
cvttss2si %xmm0, %eax
subl %eax, %ebp
testl %ebp, %ebp
movl $0x0, %ecx
cmovgl %ebp, %ecx
cmpl $0x3c, %ecx
movl $0x3c, %eax
cmovgel %eax, %ecx
movl 0x3c(%rbx,%r13), %eax
movl $0xffffff00, %edx       # imm = 0xFFFFFF00
andl %edx, %eax
movb %cl, 0x3c(%rbx,%r13)
movq 0x48(%rbx,%r13), %rcx
testq %rcx, %rcx
movss 0xc(%rsp), %xmm3
movss 0xe8b5(%rip), %xmm4     # 0x2bbd0
je 0x1d331
movl 0x8(%rcx), %edx
cmpl 0xc(%rcx), %edx
jle 0x1d331
testl %ebp, %ebp
jg 0x1d331
orl $0x1, %eax
movl %eax, 0x3c(%rbx,%r13)
addq $0x50, %r13
cmpq %r13, %r14
jne 0x1d16c
jmp 0x1d430
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
xorps %xmm2, %xmm2
cvtsi2ssl 0x38(%rbx,%r13), %xmm2
mulss %xmm4, %xmm1
divss %xmm0, %xmm1
divss %xmm2, %xmm1
movss %xmm1, 0x8(%rsp)
xorps %xmm2, %xmm2
cvtsi2ssl 0x2c(%rbx,%r13), %xmm2
xorps %xmm1, %xmm1
cvtsi2ssl 0x30(%rbx,%r13), %xmm1
divss %xmm1, %xmm2
movss %xmm2, 0x18(%rsp)
divss %xmm3, %xmm0
callq 0x2200
movss 0x18(%rsp), %xmm4
movss 0x4(%rsp), %xmm2
mulss %xmm4, %xmm2
mulss 0x1057f(%rip), %xmm2    # 0x2d924
movss 0x8(%rsp), %xmm3
mulss %xmm3, %xmm3
movss 0xe819(%rip), %xmm1     # 0x2bbd0
subss %xmm3, %xmm1
mulss %xmm2, %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ebp
cmpl $0x0, 0x10(%rsp)
jne 0x1d40a
movaps %xmm4, %xmm0
mulss 0xfb6b(%rip), %xmm0     # 0x2cf44
mulss %xmm4, %xmm0
movq 0x48(%rbx,%r13), %rax
movl 0x8(%rax), %ecx
subl 0xc(%rax), %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
mulss %xmm0, %xmm1
divss 0xc(%rsp), %xmm1
addss 0xfb4f(%rip), %xmm1     # 0x2cf50
cvttss2si %xmm1, %eax
cmpl %eax, %ebp
cmovgel %eax, %ebp
cmpb $0x0, 0x3(%rsp)
je 0x1d2b6
leal 0xa(%rbp), %eax
testb $0x1, 0x2(%rsp)
cmovel %ebp, %eax
testb $0x8, 0x3f(%rbx,%r13)
cmovel %ebp, %eax
movl %eax, %ebp
jmp 0x1d2b6
movl 0x14(%rsp), %ebp
cmpl $0x3, %ebp
movq 0x20(%rsp), %r13
jl 0x1d45d
leaq 0x3c(%rbx), %rcx
xorl %eax, %eax
testb $0x8, 0x1(%rcx)
jne 0x1d459
incq %rax
addq $0x50, %rcx
cmpq %rax, %r12
jne 0x1d444
movl %r12d, %eax
cmpl %ebp, %eax
jne 0x1d46c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %esi
shll $0x4, %esi
movq %r13, %rdi
callq 0x126e9
cmpl $0x2, %ebp
movl $0x1, %ecx
cmovgel %ebp, %ecx
movq %rax, %r15
leaq 0x20(%rbx), %rax
xorl %edx, %edx
xorl %r14d, %r14d
movl (%rax), %esi
cmpq %rsi, %rdx
setne %dil
testl %esi, %esi
setns %sil
testb %dil, %sil
jne 0x1d4c8
movslq %r14d, %r14
movq %r14, %rsi
shlq $0x4, %rsi
movq %rdx, 0x8(%r15,%rsi)
movl -0x18(%rax), %edi
shlq $0x20, %rdi
movslq -0x8(%rax), %r8
orq %rdi, %r8
incl %r14d
movq %r8, (%r15,%rsi)
incq %rdx
addq $0x50, %rax
cmpq %rdx, %rcx
jne 0x1d490
movslq %r14d, %r12
movq %r12, %rax
shlq $0x4, %rax
movq %r15, %rsi
addq %rax, %rsi
movq %r15, %rdi
callq 0x5ae1
cmpl $0x3, %r12d
jl 0x1d559
decl %r14d
shlq $0x4, %r14
addq $-0x10, %r14
xorl %eax, %eax
movq 0x18(%r15,%rax), %rcx
leaq (%rcx,%rcx,4), %rdx
shlq $0x4, %rdx
movl 0x3c(%rbx,%rdx), %ecx
btl $0xb, %ecx
jae 0x1d550
movq 0x8(%r15,%rax), %rsi
movq 0x28(%r15,%rax), %rdi
leaq (%rsi,%rsi,4), %rsi
shlq $0x4, %rsi
movzbl 0x3c(%rbx,%rsi), %esi
leaq (%rdi,%rdi,4), %rdi
shlq $0x4, %rdi
movzbl 0x3c(%rbx,%rdi), %edi
cmpl %edi, %esi
cmovbl %esi, %edi
addq %rbx, %rdx
addq $0x3c, %rdx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
orl %edi, %ecx
movl %ecx, (%rdx)
addq $0x10, %rax
cmpq %rax, %r14
jne 0x1d4ff
movq %r13, %rdi
movq %r15, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1261e
nop