// File: /workspace/github_cpp_makefile_star2_O2/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 0x15fd9a
movq %rsi, %r14
movl 0x8(%rsi), %eax
testl %eax, %eax
je 0x15fd9a
cmpl $0x64, %edx
pushq $0x64
popq %rsi
cmovll %edx, %esi
cmpl $0xa, %ecx
pushq $0xa
popq %rdx
cmovll %ecx, %edx
cmpl $0x6, %edx
pushq $0x5
popq %rcx
cmovgel %edx, %ecx
movl %esi, 0x1c(%rsp)
cvtsi2sd %esi, %xmm2
movsd 0x83696(%rip), %xmm0    # 0x1e32a0
movapd %xmm0, %xmm1
divsd %xmm2, %xmm1
movl %ecx, 0x18(%rsp)
cvtsi2sd %ecx, %xmm2
divsd %xmm2, %xmm0
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
imulq $0x48, %rdx, %rsi
xorl %edx, %edx
cmpq %rcx, %rsi
je 0x15fc52
movq 0x18(%r14), %rdi
movl 0x4(%rdi,%rcx), %r8d
cmpl (%rdi,%rcx), %r8d
jne 0x15fc4c
movslq 0x14(%rdi,%rcx), %rdi
addq %rdi, %rdx
addq $0x48, %rcx
jmp 0x15fc30
cvtsd2ss %xmm1, %xmm1
movss %xmm1, 0x10(%rsp)
cvtsi2ss %rdx, %xmm1
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movslq 0x14(%r14), %rcx
addq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x14(%rsp)
pushq $0x44
popq %r12
xorl %r13d, %r13d
movss 0x80440(%rip), %xmm3    # 0x1e00d0
cltq
cmpq %rax, %r13
jge 0x15fd9a
movq 0x18(%r14), %rbp
movl -0x40(%rbp,%r12), %eax
cmpl -0x44(%rbp,%r12), %eax
jne 0x15fd84
movl -0x30(%rbp,%r12), %eax
cvtsi2ss %eax, %xmm0
movaps %xmm3, %xmm1
cmpl 0x1c(%rsp), %eax
jg 0x15fccb
movss 0x10(%rsp), %xmm1
mulss %xmm0, %xmm1
movl -0x34(%rbp,%r12), %eax
movaps %xmm3, %xmm2
cmpl 0x18(%rsp), %eax
jg 0x15fce3
cvtsi2ss %eax, %xmm2
mulss 0xc(%rsp), %xmm2
mulss 0x14(%rsp), %xmm1
minss %xmm2, %xmm1
movl -0x8(%rbp,%r12), %r15d
movl 0x20(%rsp), %eax
cmpl %eax, %r15d
cmovlel %eax, %r15d
cvtsi2ss %r15d, %xmm2
divss %xmm0, %xmm2
mulss 0x91d5a(%rip), %xmm1    # 0x1f1a68
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
cvtsi2ss %eax, %xmm0
callq 0x23e0
mulss 0x91d29(%rip), %xmm0    # 0x1f1a6c
addss 0x91d25(%rip), %xmm0    # 0x1f1a70
cvttss2si %xmm0, %eax
subl %eax, %ebx
movl %ebx, %eax
testl %ebx, %ebx
jg 0x15fd59
xorl %eax, %eax
cmpl $0x3c, %eax
jl 0x15fd61
pushq $0x3c
popq %rax
movl -0x30(%rbp,%r12), %ecx
pushq $0x1
popq %rdx
testl %ebx, %ebx
movss 0x8035d(%rip), %xmm3    # 0x1e00d0
jle 0x15fd77
movl %eax, %edx
cmpl %r15d, %ecx
cmovgl %edx, %eax
movb %al, (%rbp,%r12)
jmp 0x15fd8a
movb $0x0, (%rbp,%r12)
incq %r13
movl 0x8(%r14), %eax
addq $0x48, %r12
jmp 0x15fc90
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq