// File: /workspace/github_c_makefile_star2_O3/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 0x1ce13
movq %rsi, %r14
movl 0x8(%rsi), %eax
testq %rax, %rax
je 0x1ce13
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 0x1ce13
cvtsi2sdl 0x10(%rsp), %xmm0
movsd 0xc4a1(%rip), %xmm1     # 0x29110
movapd %xmm1, %xmm2
divsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsd2ss %xmm2, %xmm0
movss %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0xc(%rsp), %xmm0
divsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, 0x14(%rsp)
movq 0x18(%r14), %rdx
imulq $0x68, %rax, %rsi
xorl %edi, %edi
xorl %ecx, %ecx
movl 0x4(%rdx,%rdi), %r8d
cmpl (%rdx,%rdi), %r8d
jne 0x1ccbd
movslq 0x14(%rdx,%rdi), %r8
addq %r8, %rcx
addq $0x68, %rdi
cmpq %rdi, %rsi
jne 0x1ccaa
testl %eax, %eax
jle 0x1ce13
movslq 0x14(%r14), %rax
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
addq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
movl $0x44, %r12d
xorl %r13d, %r13d
movss 0xce50(%rip), %xmm3     # 0x29b50
movq 0x18(%r14), %rbp
movl -0x40(%rbp,%r12), %eax
cmpl -0x44(%rbp,%r12), %eax
jne 0x1cdf9
movl -0x30(%rbp,%r12), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps %xmm3, %xmm1
cmpl 0x10(%rsp), %eax
jg 0x1cd33
movss 0x18(%rsp), %xmm1
mulss %xmm0, %xmm1
movl -0x34(%rbp,%r12), %eax
movaps %xmm3, %xmm2
cmpl 0xc(%rsp), %eax
jg 0x1cd4e
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 0xcdd8(%rip), %xmm1     # 0x29b54
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 0xcda4(%rip), %xmm0     # 0x29b58
addss 0xcda0(%rip), %xmm0     # 0x29b5c
cvttss2si %xmm0, %eax
subl %eax, %ebx
movl %ebx, %eax
testl %ebx, %ebx
jg 0x1cdca
xorl %eax, %eax
cmpl $0x3c, %eax
jl 0x1cdd4
movl $0x3c, %eax
movl -0x30(%rbp,%r12), %ecx
movl $0x1, %edx
testl %ebx, %ebx
movss 0xcd68(%rip), %xmm3     # 0x29b50
jle 0x1cdec
movl %eax, %edx
cmpl %r15d, %ecx
cmovgl %edx, %eax
movb %al, (%rbp,%r12)
jmp 0x1cdff
movb $0x0, (%rbp,%r12)
incq %r13
movslq 0x8(%r14), %rax
addq $0x68, %r12
cmpq %rax, %r13
jl 0x1cd00
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop