// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi-lite/mag.c
// Function: mag_cal_rdist
double mag_cal_rdist(mag_t *g)
{
	magv_v *v = &g->v;
	int j;
	uint64_t *srt;
	double rdist = -1.;
	int64_t i, sum_n_all, sum_n, sum_l;

	srt = calloc(v->n, 8);
	for (i = 0, sum_n_all = 0; i < v->n; ++i) {
		srt[i] = (uint64_t)v->a[i].nsr<<32 | i;
		sum_n_all += v->a[i].nsr;
	}
	ks_introsort_uint64_t(v->n, srt);

	for (j = 0; j < 2; ++j) {
		sum_n = sum_l = 0;
		for (i = v->n - 1; i >= 0; --i) {
			const magv_t *p = &v->a[srt[i]<<32>>32];
			int tmp1, tmp2;
			tmp1 = tmp2 = 0;
			if (p->nei[0].n) ++tmp1, tmp2 += p->nei[0].a[0].y;
			if (p->nei[1].n) ++tmp1, tmp2 += p->nei[1].a[0].y;
			if (tmp1) tmp2 /= tmp1;
			if (rdist > 0.) {
				double A = (p->len - tmp1) / rdist - p->nsr * M_LN2;
				if (A < A_THRES) continue;
			}
			sum_n += p->nsr;
			sum_l += p->len - tmp1;
			if (sum_n >= sum_n_all * 0.5) break;
		}
		rdist = (double)sum_l / sum_n;
	}
	if (fm_verbose >= 3) {
		fprintf(stderr, "[M::%s] average read distance %.3f.\n", __func__, rdist);
		fprintf(stderr, "[M::%s] approximate genome size: %.0f (inaccurate!)\n", __func__, rdist * sum_n_all);
	}

	free(srt);
	return rdist;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
movq (%rdi), %r15
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2130
movq %rax, %rbx
pushq $0x4
popq %rax
xorl %ecx, %ecx
xorl %r12d, %r12d
cmpq %rcx, %r15
je 0xf977
movq 0x10(%r14), %rdx
movslq (%rdx,%rax), %rdx
addq %rdx, %r12
shlq $0x20, %rdx
orq %rcx, %rdx
movq %rdx, (%rbx,%rcx,8)
incq %rcx
addq $0x68, %rax
jmp 0xf953
movq %r15, %rdi
movq %rbx, %rsi
callq 0xcc39
cvtsi2sd %r12, %xmm6
movq (%r14), %rax
movsd 0x512e(%rip), %xmm0     # 0x14ac0
mulsd %xmm6, %xmm0
xorl %ecx, %ecx
movsd 0x5128(%rip), %xmm7     # 0x14ac8
xorpd %xmm1, %xmm1
movsd 0x5124(%rip), %xmm2     # 0x14ad0
movsd 0x5124(%rip), %xmm3     # 0x14ad8
cmpl $0x2, %ecx
je 0xfa6d
movq %rax, %rdi
xorl %edx, %edx
xorl %esi, %esi
decq %rdi
movq %rdi, %r10
testq %r10, %r10
js 0xfa58
movq %r10, %rdi
movq 0x10(%r14), %r8
movl (%rbx,%r10,8), %r9d
imulq $0x68, %r9, %r11
leaq (%r8,%r11), %r9
xorl %r15d, %r15d
cmpq $0x0, 0x20(%r8,%r11)
setne %r15b
leal 0x1(%r15), %r10d
cmpq $0x0, 0x38(%r8,%r11)
cmovel %r15d, %r10d
ucomisd %xmm1, %xmm7
jbe 0xfa33
movl (%r9), %r8d
movl 0x4(%r9), %r9d
subl %r10d, %r8d
cvtsi2sd %r8d, %xmm4
divsd %xmm7, %xmm4
cvtsi2sd %r9d, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm4, %xmm5
leaq -0x1(%rdi), %r10
ucomisd %xmm5, %xmm3
ja 0xf9ca
jmp 0xfa3d
movl (%r9), %r8d
movl 0x4(%r9), %r9d
subl %r10d, %r8d
movslq %r9d, %r9
addq %r9, %rdx
movslq %r8d, %r8
addq %r8, %rsi
cvtsi2sd %rdx, %xmm4
ucomisd %xmm0, %xmm4
jb 0xf9c4
cvtsi2sd %rsi, %xmm7
cvtsi2sd %rdx, %xmm4
divsd %xmm4, %xmm7
incl %ecx
jmp 0xf9b4
movsd %xmm7, 0x8(%rsp)
cmpl $0x3, 0xb7f6(%rip)      # 0x1b270
jge 0xfa96
movq %rbx, %rdi
callq 0x2040
movsd 0x8(%rsp), %xmm0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0xb55b(%rip), %r15      # 0x1aff8
movq (%r15), %rdi
leaq 0x5299(%rip), %rsi      # 0x14d40
leaq 0x52b7(%rip), %r14      # 0x14d65
movq %r14, %rdx
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
movsd %xmm6, 0x10(%rsp)
callq 0x2150
movq (%r15), %rdi
movsd 0x10(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
leaq 0x5299(%rip), %rsi      # 0x14d73
movq %r14, %rdx
movb $0x1, %al
callq 0x2150
jmp 0xfa7c