// File: /workspace/github_c_makefile_star2_O3/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 0x4fe7
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 0x11eea
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 0x11ec5
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x10778
movsd 0x18(%rsp), %xmm2
xorpd %xmm5, %xmm5
movq (%r14), %rax
movsd 0xb6f8(%rip), %xmm0     # 0x1d608
mulsd %xmm2, %xmm0
movsd 0xc1a4(%rip), %xmm7     # 0x1e0c0
movb $0x1, %cl
movsd 0xc1a2(%rip), %xmm1     # 0x1e0c8
movsd 0xc1a2(%rip), %xmm6     # 0x1e0d0
movq %rax, %rdi
xorl %edx, %edx
xorl %esi, %esi
decq %rdi
movq %rdi, %r10
testq %r10, %r10
js 0x11fd2
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 %xmm5, %xmm7
jbe 0x11faa
movl (%r9), %r8d
movl 0x4(%r9), %r9d
subl %r10d, %r8d
xorps %xmm3, %xmm3
cvtsi2sd %r8d, %xmm3
divsd %xmm7, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %r9d, %xmm4
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
leaq -0x1(%rdi), %r10
ucomisd %xmm4, %xmm6
ja 0x11f3b
jmp 0x11fb4
movl (%r9), %r8d
movl 0x4(%r9), %r9d
subl %r10d, %r8d
movslq %r9d, %r9
addq %r9, %rdx
movslq %r8d, %r8
addq %r8, %rsi
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
ucomisd %xmm0, %xmm3
jb 0x11f35
xorps %xmm7, %xmm7
cvtsi2sd %rsi, %xmm7
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
divsd %xmm3, %xmm7
testb $0x1, %cl
movl $0x0, %ecx
jne 0x11f2e
movsd %xmm7, 0x8(%rsp)
leaq 0x1325f(%rip), %rax     # 0x25260
cmpl $0x3, (%rax)
jge 0x1201e
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 0x2d96
jmp 0x12006