// File: /workspace/github_c_makefile_star2_O1/lh3[P]fermi2/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., t;
	int64_t i, sum_n_all, sum_n, sum_l;

	t = cputime();
	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, computed in %.3f seconds.\n", __func__, rdist, cputime() - t);
		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 %rbx
subq $0x20, %rsp
movq %rdi, %r14
callq 0x5117
movsd %xmm0, 0x10(%rsp)
movq (%r14), %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x2190
movq %rax, %rbx
xorpd %xmm0, %xmm0
testq %r15, %r15
je 0x1187b
movq 0x10(%r14), %rax
addq $0x4, %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%rax), %rsi
addq %rsi, %rcx
shlq $0x20, %rsi
orq %rdx, %rsi
movq %rsi, (%rbx,%rdx,8)
incq %rdx
addq $0x68, %rax
cmpq %rdx, %r15
jne 0x11856
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x10160
movsd 0x18(%rsp), %xmm2
xorpd %xmm5, %xmm5
movq (%r14), %rax
movsd 0xabff(%rip), %xmm0     # 0x1c4a0
mulsd %xmm2, %xmm0
decq %rax
movsd 0xb670(%rip), %xmm7     # 0x1cf20
xorl %ecx, %ecx
movsd 0xb66e(%rip), %xmm1     # 0x1cf28
movsd 0xb66e(%rip), %xmm6     # 0x1cf30
movq %rax, %rdi
xorl %edx, %edx
xorl %esi, %esi
testq %rdi, %rdi
js 0x11960
movq 0x10(%r14), %r10
movl (%rbx,%rdi,8), %r8d
imulq $0x68, %r8, %r11
leaq (%r10,%r11), %r8
xorl %r15d, %r15d
cmpq $0x0, 0x20(%r10,%r11)
setne %r15b
leal 0x1(%r15), %r9d
cmpq $0x0, 0x38(%r10,%r11)
cmovel %r15d, %r9d
ucomisd %xmm5, %xmm7
jbe 0x1192c
movl (%r8), %r10d
subl %r9d, %r10d
xorps %xmm3, %xmm3
cvtsi2sd %r10d, %xmm3
divsd %xmm7, %xmm3
xorps %xmm4, %xmm4
cvtsi2sdl 0x4(%r8), %xmm4
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
ucomisd %xmm4, %xmm6
ja 0x11951
movslq 0x4(%r8), %r10
addq %r10, %rdx
movslq (%r8), %r8
movl %r9d, %r9d
subq %r9, %r8
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
addq %r8, %rsi
ucomisd %xmm0, %xmm3
setae %r8b
jmp 0x11954
xorl %r8d, %r8d
decq %rdi
testb %r8b, %r8b
je 0x118c9
xorps %xmm7, %xmm7
cvtsi2sd %rsi, %xmm7
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
divsd %xmm3, %xmm7
leal 0x1(%rcx), %edx
testl %ecx, %ecx
movl %edx, %ecx
je 0x118c2
movsd %xmm7, 0x8(%rsp)
leaq 0x128d2(%rip), %rax     # 0x24260
cmpl $0x3, (%rax)
jge 0x119ab
movq %rbx, %rdi
callq 0x2040
movsd 0x8(%rsp), %xmm0
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
movsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x2dd7
jmp 0x11993