// File: /workspace/github_c_makefile_star2_O1/lh3[P]minigraph/gcmisc.c
// Function: mg_gchain_set_mapq
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;
	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) {
			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 0x1cd8d
movq %rsi, %r14
movl 0x8(%rsi), %eax
testq %rax, %rax
je 0x1cd8d
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 0x1cc01
movq 0x18(%r14), %rcx
imulq $0x68, %rax, %rdx
xorl %esi, %esi
xorl %eax, %eax
movl 0x4(%rcx,%rsi), %edi
cmpl (%rcx,%rsi), %edi
jne 0x1cbf6
movslq 0x14(%rcx,%rsi), %rdi
addq %rdi, %rax
addq $0x68, %rsi
cmpq %rsi, %rdx
jne 0x1cbe5
jmp 0x1cc03
xorl %eax, %eax
cmpl $0x0, 0x8(%r14)
jle 0x1cd8d
cvtsi2sdl 0x10(%rsp), %xmm0
cvtsi2sdl 0xc(%rsp), %xmm1
movsd 0xc49e(%rip), %xmm2     # 0x290c0
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 0xce2a(%rip), %xmm3     # 0x29aa4
movq 0x18(%r14), %rbp
movl -0x40(%rbp,%r12), %eax
cmpl -0x44(%rbp,%r12), %eax
jne 0x1cd73
movl -0x30(%rbp,%r12), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps %xmm3, %xmm1
cmpl 0x10(%rsp), %eax
jg 0x1ccad
movss 0x18(%rsp), %xmm1
mulss %xmm0, %xmm1
movl -0x34(%rbp,%r12), %eax
movaps %xmm3, %xmm2
cmpl 0xc(%rsp), %eax
jg 0x1ccc8
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 0xcdb2(%rip), %xmm1     # 0x29aa8
subss %xmm2, %xmm3
mulss %xmm1, %xmm3
movss %xmm3, 0x24(%rsp)
callq 0x21c0
mulss 0x24(%rsp), %xmm0
cvttss2si %xmm0, %ebx
movl -0x4(%rbp,%r12), %eax
incl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
callq 0x21c0
mulss 0xcd7e(%rip), %xmm0     # 0x29aac
addss 0xcd7a(%rip), %xmm0     # 0x29ab0
cvttss2si %xmm0, %eax
subl %eax, %ebx
movl %ebx, %eax
testl %ebx, %ebx
jg 0x1cd44
xorl %eax, %eax
cmpl $0x3c, %eax
jl 0x1cd4e
movl $0x3c, %eax
movl -0x30(%rbp,%r12), %ecx
movl $0x1, %edx
testl %ebx, %ebx
movss 0xcd42(%rip), %xmm3     # 0x29aa4
jle 0x1cd66
movl %eax, %edx
cmpl %r15d, %ecx
cmovgl %edx, %eax
movb %al, (%rbp,%r12)
jmp 0x1cd79
movb $0x0, (%rbp,%r12)
incq %r13
movslq 0x8(%r14), %rax
addq $0x68, %r12
cmpq %rax, %r13
jl 0x1cc7a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq