// File: /workspace/github_c_makefile_star2_O2/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 0x19560
movq %rsi, %r14
movl 0x8(%rsi), %eax
testl %eax, %eax
je 0x19560
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 0xad50(%rip), %xmm0     # 0x24120
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 $0x68, %rdx, %rsi
xorl %edx, %edx
cmpq %rcx, %rsi
je 0x19418
movq 0x18(%r14), %rdi
movl 0x4(%rdi,%rcx), %r8d
cmpl (%rdi,%rcx), %r8d
jne 0x19412
movslq 0x14(%rdi,%rcx), %rdi
addq %rdi, %rdx
addq $0x68, %rcx
jmp 0x193f6
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 0xb6d6(%rip), %xmm3     # 0x24b2c
cltq
cmpq %rax, %r13
jge 0x19560
movq 0x18(%r14), %rbp
movl -0x40(%rbp,%r12), %eax
cmpl -0x44(%rbp,%r12), %eax
jne 0x1954a
movl -0x30(%rbp,%r12), %eax
cvtsi2ss %eax, %xmm0
movaps %xmm3, %xmm1
cmpl 0x1c(%rsp), %eax
jg 0x19491
movss 0x10(%rsp), %xmm1
mulss %xmm0, %xmm1
movl -0x34(%rbp,%r12), %eax
movaps %xmm3, %xmm2
cmpl 0x18(%rsp), %eax
jg 0x194a9
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 0xb65c(%rip), %xmm1     # 0x24b30
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
cvtsi2ss %eax, %xmm0
callq 0x21c0
mulss 0xb62b(%rip), %xmm0     # 0x24b34
addss 0xb627(%rip), %xmm0     # 0x24b38
cvttss2si %xmm0, %eax
subl %eax, %ebx
movl %ebx, %eax
testl %ebx, %ebx
jg 0x1951f
xorl %eax, %eax
cmpl $0x3c, %eax
jl 0x19527
pushq $0x3c
popq %rax
movl -0x30(%rbp,%r12), %ecx
pushq $0x1
popq %rdx
testl %ebx, %ebx
movss 0xb5f3(%rip), %xmm3     # 0x24b2c
jle 0x1953d
movl %eax, %edx
cmpl %r15d, %ecx
cmovgl %edx, %eax
movb %al, (%rbp,%r12)
jmp 0x19550
movb $0x0, (%rbp,%r12)
incq %r13
movl 0x8(%r14), %eax
addq $0x68, %r12
jmp 0x19456
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop