// File: /workspace/github_c_makefile_star2_O3/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 %rsi, 0x18(%rsp)
testl %esi, %esi
je 0x1d598
jle 0x1d598
movl %ecx, %ebp
movq %rdx, %rbx
movl 0x78(%rsp), %eax
movl 0x70(%rsp), %ecx
movl %ecx, 0x10(%rsp)
movl 0x18(%rsp), %r10d
movq %r10, %rcx
shlq $0x4, %rcx
leaq (%rcx,%rcx,4), %r14
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r13d, %r13d
movl 0x20(%rbx,%rdx), %esi
cmpl (%rbx,%rdx), %esi
jne 0x1d100
movslq 0xc(%rbx,%rdx), %rsi
addq %rsi, %rcx
jmp 0x1d10a
btl $0x1b, 0x3c(%rbx,%rdx)
adcl $0x0, %r13d
addq $0x50, %rdx
cmpq %rdx, %r14
jne 0x1d0ed
cmpl $0x0, 0x18(%rsp)
jle 0x1d598
movq %r10, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
movslq %r9d, %rdx
cvtsi2ss %rcx, %xmm1
addq %rcx, %rdx
cvtsi2ss %rdx, %xmm0
cvtsi2ss %r8d, %xmm3
divss %xmm0, %xmm1
movss %xmm1, 0x24(%rsp)
cmpl $0x0, 0x10(%rsp)
setne %cl
testl %eax, %eax
setne %al
andb %cl, %al
movb %al, 0x7(%rsp)
xorl %r12d, %r12d
movss 0xfa8b(%rip), %xmm4     # 0x2cbf0
movss %xmm3, 0x14(%rsp)
movl 0x3c(%rbx,%r12), %eax
btl $0xb, %eax
jb 0x1d231
movl 0x20(%rbx,%r12), %ecx
cmpl (%rbx,%r12), %ecx
jne 0x1d23b
movl 0xc(%rbx,%r12), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps %xmm0, %xmm1
mulss 0x11824(%rip), %xmm1    # 0x2e9c4
movaps %xmm4, %xmm5
cmpl $0x65, %eax
jge 0x1d1ab
movaps %xmm1, %xmm5
movl 0x4(%rbx,%r12), %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss 0x11809(%rip), %xmm2    # 0x2e9c8
movaps %xmm4, %xmm1
cmpl $0xb, %eax
jge 0x1d1ca
movaps %xmm2, %xmm1
mulss 0x24(%rsp), %xmm5
minss %xmm1, %xmm5
movl 0x24(%rbx,%r12), %ecx
cmpl %ebp, %ecx
cmovlel %ebp, %ecx
movq 0x48(%rbx,%r12), %rdx
testq %rdx, %rdx
je 0x1d245
movl 0x8(%rdx), %eax
movl 0xc(%rdx), %edx
testl %edx, %edx
setg %sil
testl %eax, %eax
setg %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x1d272
xorps %xmm4, %xmm4
cvtsi2ss %edx, %xmm4
cmpb $0x0, 0x7(%rsp)
movss %xmm5, 0x8(%rsp)
je 0x1d362
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
divss %xmm0, %xmm4
movss %xmm4, 0xc(%rsp)
jmp 0x1d38c
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
jmp 0x1d34b
andl $0xfffff700, %eax       # imm = 0xFFFFF700
jmp 0x1d34b
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
xorps %xmm2, %xmm2
cvtsi2ssl 0x38(%rbx,%r12), %xmm2
divss %xmm2, %xmm1
mulss 0x1176a(%rip), %xmm5    # 0x2e9cc
subss %xmm1, %xmm4
mulss %xmm5, %xmm4
movss %xmm4, 0x8(%rsp)
jmp 0x1d2c1
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ssl 0x38(%rbx,%r12), %xmm1
divss %xmm1, %xmm0
movsd 0x2c(%rbx,%r12), %xmm1
cvtdq2ps %xmm1, %xmm1
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
divss %xmm2, %xmm1
mulss %xmm1, %xmm5
mulss 0x11724(%rip), %xmm5    # 0x2e9cc
subss %xmm0, %xmm4
mulss %xmm5, %xmm4
movss %xmm4, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
divss %xmm3, %xmm0
callq 0x2200
mulss 0x8(%rsp), %xmm0
cvttss2si %xmm0, %r15d
movl 0x34(%rbx,%r12), %eax
incl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
callq 0x2200
mulss 0x116e4(%rip), %xmm0    # 0x2e9d0
addss 0x10c78(%rip), %xmm0    # 0x2df6c
cvttss2si %xmm0, %eax
subl %eax, %r15d
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
cmpl $0x3c, %ecx
movl $0x3c, %eax
cmovgel %eax, %ecx
movl 0x3c(%rbx,%r12), %eax
movl $0xffffff00, %edx       # imm = 0xFFFFFF00
andl %edx, %eax
movb %cl, 0x3c(%rbx,%r12)
movq 0x48(%rbx,%r12), %rcx
testq %rcx, %rcx
movss 0x14(%rsp), %xmm3
movss 0xf8b7(%rip), %xmm4     # 0x2cbf0
je 0x1d350
movl 0x8(%rcx), %edx
cmpl 0xc(%rcx), %edx
jle 0x1d350
testl %r15d, %r15d
jg 0x1d350
orl $0x1, %eax
movl %eax, 0x3c(%rbx,%r12)
addq $0x50, %r12
cmpq %r12, %r14
jne 0x1d16b
jmp 0x1d460
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
xorps %xmm2, %xmm2
cvtsi2ssl 0x38(%rbx,%r12), %xmm2
mulss %xmm4, %xmm1
divss %xmm0, %xmm1
divss %xmm2, %xmm1
movss %xmm1, 0xc(%rsp)
xorps %xmm2, %xmm2
cvtsi2ssl 0x2c(%rbx,%r12), %xmm2
xorps %xmm1, %xmm1
cvtsi2ssl 0x30(%rbx,%r12), %xmm1
divss %xmm1, %xmm2
movss %xmm2, 0x20(%rsp)
divss %xmm3, %xmm0
callq 0x2200
movss 0x20(%rsp), %xmm4
movss 0x8(%rsp), %xmm2
mulss %xmm4, %xmm2
mulss 0x11601(%rip), %xmm2    # 0x2e9cc
movss 0xc(%rsp), %xmm3
mulss %xmm3, %xmm3
movss 0xf813(%rip), %xmm1     # 0x2cbf0
subss %xmm3, %xmm1
mulss %xmm2, %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %r15d
cmpl $0x0, 0x10(%rsp)
je 0x1d41d
cmpb $0x0, 0x7(%rsp)
je 0x1d2d1
leal 0xa(%r15), %eax
testl %r13d, %r13d
cmovnel %r15d, %eax
testb $0x8, 0x3f(%rbx,%r12)
cmovel %r15d, %eax
movl %eax, %r15d
jmp 0x1d2d1
movaps %xmm4, %xmm0
mulss 0x10b38(%rip), %xmm0    # 0x2df60
mulss %xmm4, %xmm0
movq 0x48(%rbx,%r12), %rax
movl 0x8(%rax), %ecx
subl 0xc(%rax), %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
mulss %xmm0, %xmm1
divss 0x14(%rsp), %xmm1
addss 0x10b1c(%rip), %xmm1    # 0x2df6c
cvttss2si %xmm1, %eax
cmpl %eax, %r15d
cmovgel %eax, %r15d
jmp 0x1d2d1
cmpl $0x3, 0x18(%rsp)
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %r12
jl 0x1d598
leaq 0x3c(%rbx), %rcx
movq %r12, %rax
testb $0x8, 0x1(%rcx)
jne 0x1d490
addq $0x50, %rcx
decq %rax
jne 0x1d47c
jmp 0x1d598
testl %eax, %eax
je 0x1d598
movq 0x18(%rsp), %rsi
shll $0x4, %esi
callq 0x1264d
movq %rax, %r15
leaq 0x20(%rbx), %rax
xorl %ecx, %ecx
xorl %r14d, %r14d
movl (%rax), %edx
cmpq %rdx, %rcx
setne %sil
testl %edx, %edx
setns %dl
testb %sil, %dl
jne 0x1d4e8
movslq %r14d, %r14
movq %r14, %rdx
shlq $0x4, %rdx
movq %rcx, 0x8(%r15,%rdx)
movl -0x18(%rax), %esi
shlq $0x20, %rsi
movslq -0x8(%rax), %rdi
orq %rsi, %rdi
incl %r14d
movq %rdi, (%r15,%rdx)
incq %rcx
addq $0x50, %rax
cmpq %rcx, %r12
jne 0x1d4b1
movslq %r14d, %r12
movq %r12, %rax
shlq $0x4, %rax
movq %r15, %rsi
addq %rax, %rsi
movq %r15, %rdi
callq 0x5a49
cmpl $0x3, %r12d
movq 0x28(%rsp), %rdi
jl 0x1d582
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 0x1d579
movq 0x8(%r15,%rax), %rsi
movq 0x28(%r15,%rax), %r8
leaq (%rsi,%rsi,4), %rsi
shlq $0x4, %rsi
movzbl 0x3c(%rbx,%rsi), %esi
leaq (%r8,%r8,4), %r8
shlq $0x4, %r8
movzbl 0x3c(%rbx,%r8), %r8d
cmpl %r8d, %esi
cmovbl %esi, %r8d
addq %rbx, %rdx
addq $0x3c, %rdx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
orl %r8d, %ecx
movl %ecx, (%rdx)
addq $0x10, %rax
cmpq %rax, %r14
jne 0x1d524
movq %r15, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12587
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop