// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x28bb36
movq -0x10(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x28bb3b
jmp 0x28be6e
cmpl $0x64, -0x14(%rbp)
jge 0x28bb49
movl -0x14(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x28bb53
movl $0x64, %eax
movl %eax, -0x60(%rbp)
jmp 0x28bb53
movl -0x60(%rbp), %eax
movl %eax, -0x3c(%rbp)
cmpl $0xa, -0x18(%rbp)
jge 0x28bb67
movl -0x18(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x28bb71
movl $0xa, %eax
movl %eax, -0x64(%rbp)
jmp 0x28bb71
movl -0x64(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpl $0x5, -0x40(%rbp)
jge 0x28bb84
movl $0x5, -0x40(%rbp)
cvtsi2sdl -0x3c(%rbp), %xmm1
movsd 0xf646f(%rip), %xmm0    # 0x382000
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
cvtsi2sdl -0x40(%rbp), %xmm1
movsd 0xf6455(%rip), %xmm0    # 0x382000
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x28bc24
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x38(%rbp), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movslq -0x38(%rbp), %rdx
imulq $0x48, %rdx, %rdx
addq %rdx, %rcx
cmpl (%rcx), %eax
jne 0x28bc17
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x38(%rbp), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movslq 0x14(%rax), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x28bc19
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x28bbbf
cvtsi2ssq -0x28(%rbp), %xmm0
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x14(%rcx), %rcx
addq %rcx, %rax
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x2c(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x28be6e
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movslq -0x38(%rbp), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x48(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x28be4b
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
cmpl -0x3c(%rbp), %eax
jle 0x28bca3
movss 0xf33f0(%rip), %xmm0    # 0x37f08c
movss %xmm0, -0x68(%rbp)
jmp 0x28bcb6
movq -0x48(%rbp), %rax
cvtsi2ssl 0x14(%rax), %xmm0
mulss -0x30(%rbp), %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x68(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
movss %xmm0, -0x54(%rbp)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
cmpl -0x40(%rbp), %eax
jle 0x28bce0
movss 0xf33b3(%rip), %xmm0    # 0x37f08c
movss %xmm0, -0x6c(%rbp)
jmp 0x28bcf3
movq -0x48(%rbp), %rax
cvtsi2ssl 0x10(%rax), %xmm0
mulss -0x34(%rbp), %xmm0
movss %xmm0, -0x6c(%rbp)
movss -0x6c(%rbp), %xmm0
movss %xmm0, -0x5c(%rbp)
movss -0x54(%rbp), %xmm1
movss -0x5c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x28bd18
movss -0x54(%rbp), %xmm0
movss %xmm0, -0x70(%rbp)
jmp 0x28bd22
movss -0x5c(%rbp), %xmm0
movss %xmm0, -0x70(%rbp)
movss -0x70(%rbp), %xmm0
movss %xmm0, -0x5c(%rbp)
movq -0x48(%rbp), %rax
movl 0x3c(%rax), %eax
cmpl -0x1c(%rbp), %eax
jle 0x28bd44
movq -0x48(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x74(%rbp)
jmp 0x28bd4a
movl -0x1c(%rbp), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x50(%rbp)
cvtsi2ssl -0x50(%rbp), %xmm0
movq -0x48(%rbp), %rax
cvtsi2ssl 0x14(%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x58(%rbp)
movss 0x104a0d(%rip), %xmm0   # 0x39077c
mulss -0x5c(%rbp), %xmm0
movss 0xf3310(%rip), %xmm1    # 0x37f08c
subss -0x58(%rbp), %xmm1
mulss %xmm1, %xmm0
movss %xmm0, -0x78(%rbp)
movq -0x48(%rbp), %rax
cvtsi2ssl 0x14(%rax), %xmm0
callq 0x23d0
movaps %xmm0, %xmm1
movss -0x78(%rbp), %xmm0
mulss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movl 0x40(%rax), %eax
addl $0x1, %eax
cvtsi2ss %eax, %xmm0
callq 0x23d0
movss 0x1049ae(%rip), %xmm1   # 0x390774
mulss %xmm1, %xmm0
movss 0x1049a6(%rip), %xmm1   # 0x390778
addss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
movl -0x4c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jle 0x28bdf0
movl -0x4c(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x28bdf7
xorl %eax, %eax
movl %eax, -0x7c(%rbp)
jmp 0x28bdf7
movl -0x7c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
cmpl -0x50(%rbp), %eax
jle 0x28be16
cmpl $0x0, -0x4c(%rbp)
jne 0x28be16
movl $0x1, -0x4c(%rbp)
cmpl $0x3c, -0x4c(%rbp)
jge 0x28be24
movl -0x4c(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0x28be2e
movl $0x3c, %eax
movl %eax, -0x80(%rbp)
jmp 0x28be2e
movl -0x80(%rbp), %edx
movq -0x48(%rbp), %rax
movl 0x44(%rax), %ecx
andl $0xff, %edx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
orl %edx, %ecx
movl %ecx, 0x44(%rax)
jmp 0x28be5e
movq -0x48(%rbp), %rax
movl 0x44(%rax), %ecx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
orl $0x0, %ecx
movl %ecx, 0x44(%rax)
jmp 0x28be60
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x28bc4e
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)