// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: mg_gchain_set_mapq(void*, mg_gchains_t*, int, int, int)
void mg_gchain_set_mapq(void *km, mg_gchains_t *gcs, int qlen, int max_mini, int min_gc_score)
{
	static const float q_coef = 40.0f;
	int64_t sum_sc = 0;
	float uniq_ratio, r_sc, r_cnt;
	int i, t_sc, t_cnt;
	if (gcs == 0 || gcs->n_gc == 0) return;
	t_sc = qlen < 100? qlen : 100;
	t_cnt = max_mini < 10? max_mini : 10;
	if (t_cnt < 5) t_cnt = 5;
	r_sc = 1.0 / t_sc;
	r_cnt = 1.0 / t_cnt;
	for (i = 0; i < gcs->n_gc; ++i)
		if (gcs->gc[i].parent == gcs->gc[i].id)
			sum_sc += gcs->gc[i].score;///primary chain
	uniq_ratio = (float)sum_sc / (sum_sc + gcs->rep_len);
	for (i = 0; i < gcs->n_gc; ++i) {
		mg_gchain_t *r = &gcs->gc[i];
		if (r->parent == r->id) {///primary chain
			int mapq, subsc;
			float pen_s1 = (r->score > t_sc? 1.0f : r->score * r_sc) * uniq_ratio;
			float x, pen_cm = r->n_anchor > t_cnt? 1.0f : r->n_anchor * r_cnt;
			pen_cm = pen_s1 < pen_cm? pen_s1 : pen_cm;
			subsc = r->subsc > min_gc_score? r->subsc : min_gc_score;
			x = (float)subsc / r->score;
			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;
			if (r->score > subsc && mapq == 0) mapq = 1;
			r->mapq = mapq < 60? mapq : 60;
		} else r->mapq = 0;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, 0x20(%rsp)
testq %rsi, %rsi
je 0x19ec6d
movq %rsi, %r14
movl 0x8(%rsi), %eax
testq %rax, %rax
je 0x19ec6d
cmpl $0x64, %edx
movl $0x64, %esi
cmovll %edx, %esi
movl %esi, 0x10(%rsp)
cmpl $0xa, %ecx
movl $0xa, %edx
cmovll %ecx, %edx
cmpl $0x6, %edx
movl $0x5, %ecx
cmovgel %edx, %ecx
movl %ecx, 0xc(%rsp)
testl %eax, %eax
jle 0x19eae1
movq 0x18(%r14), %rcx
shlq $0x3, %rax
leaq (%rax,%rax,8), %rdx
xorl %esi, %esi
xorl %eax, %eax
movl 0x4(%rcx,%rsi), %edi
cmpl (%rcx,%rsi), %edi
jne 0x19ead6
movslq 0x14(%rcx,%rsi), %rdi
addq %rdi, %rax
addq $0x48, %rsi
cmpq %rsi, %rdx
jne 0x19eac5
jmp 0x19eae3
xorl %eax, %eax
cmpl $0x0, 0x8(%r14)
jle 0x19ec6d
cvtsi2sdl 0x10(%rsp), %xmm0
cvtsi2sdl 0xc(%rsp), %xmm1
movsd 0x8676e(%rip), %xmm2    # 0x225270
movapd %xmm2, %xmm3
divsd %xmm0, %xmm3
divsd %xmm1, %xmm2
xorps %xmm0, %xmm0
cvtsd2ss %xmm3, %xmm0
movss %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsd2ss %xmm2, %xmm0
movss %xmm0, 0x14(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
movslq 0x14(%r14), %rcx
addq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
movl $0x44, %r12d
xorl %r13d, %r13d
movss 0x83566(%rip), %xmm3    # 0x2220c0
movq 0x18(%r14), %rbp
movl -0x40(%rbp,%r12), %eax
cmpl -0x44(%rbp,%r12), %eax
jne 0x19ec53
movl -0x30(%rbp,%r12), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps %xmm3, %xmm1
cmpl 0x10(%rsp), %eax
jg 0x19eb8d
movss 0x18(%rsp), %xmm1
mulss %xmm0, %xmm1
movl -0x34(%rbp,%r12), %eax
movaps %xmm3, %xmm2
cmpl 0xc(%rsp), %eax
jg 0x19eba8
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss 0x14(%rsp), %xmm2
mulss 0x1c(%rsp), %xmm1
minss %xmm2, %xmm1
movl -0x8(%rbp,%r12), %r15d
movl 0x20(%rsp), %eax
cmpl %eax, %r15d
cmovlel %eax, %r15d
xorps %xmm2, %xmm2
cvtsi2ss %r15d, %xmm2
divss %xmm0, %xmm2
mulss 0x94d06(%rip), %xmm1    # 0x2338dc
subss %xmm2, %xmm3
mulss %xmm1, %xmm3
movss %xmm3, 0x24(%rsp)
callq 0x23e0
mulss 0x24(%rsp), %xmm0
cvttss2si %xmm0, %ebx
movl -0x4(%rbp,%r12), %eax
incl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
callq 0x23e0
mulss 0x94cd2(%rip), %xmm0    # 0x2338e0
addss 0x94cce(%rip), %xmm0    # 0x2338e4
cvttss2si %xmm0, %eax
subl %eax, %ebx
movl %ebx, %eax
testl %ebx, %ebx
jg 0x19ec24
xorl %eax, %eax
cmpl $0x3c, %eax
jl 0x19ec2e
movl $0x3c, %eax
movl -0x30(%rbp,%r12), %ecx
movl $0x1, %edx
testl %ebx, %ebx
movss 0x8347e(%rip), %xmm3    # 0x2220c0
jle 0x19ec46
movl %eax, %edx
cmpl %r15d, %ecx
cmovgl %edx, %eax
movb %al, (%rbp,%r12)
jmp 0x19ec59
movb $0x0, (%rbp,%r12)
incq %r13
movslq 0x8(%r14), %rax
addq $0x48, %r12
cmpq %rax, %r13
jl 0x19eb5a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq