// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
callq 0x3b2e
movsd %xmm0, 0x10(%rsp)
movq (%r14), %r15
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2190
movq %rax, %rbx
pushq $0x4
popq %rax
xorl %ecx, %ecx
xorl %r12d, %r12d
cmpq %rcx, %r15
je 0xf95e
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 0xf93a
movq %r15, %rdi
movq %rbx, %rsi
callq 0xe601
cvtsi2sd %r12, %xmm6
movq (%r14), %rax
movsd 0x9bdf(%rip), %xmm0     # 0x19558
mulsd %xmm6, %xmm0
xorl %ecx, %ecx
movsd 0xa689(%rip), %xmm7     # 0x1a010
xorpd %xmm1, %xmm1
movsd 0xa685(%rip), %xmm2     # 0x1a018
movsd 0xa685(%rip), %xmm3     # 0x1a020
cmpl $0x2, %ecx
je 0xfa54
movq %rax, %rdi
xorl %edx, %edx
xorl %esi, %esi
decq %rdi
movq %rdi, %r10
testq %r10, %r10
js 0xfa3f
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 0xfa1a
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 0xf9b1
jmp 0xfa24
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 0xf9ab
cvtsi2sd %rsi, %xmm7
cvtsi2sd %rdx, %xmm4
divsd %xmm4, %xmm7
incl %ecx
jmp 0xf99b
movsd %xmm7, (%rsp)
leaq 0x12800(%rip), %rax     # 0x22260
cmpl $0x3, (%rax)
jge 0xfa7e
movq %rbx, %rdi
callq 0x2040
movsd (%rsp), %xmm0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x12573(%rip), %r12     # 0x21ff8
movq (%r12), %r14
movsd %xmm6, 0x8(%rsp)
callq 0x3b2e
movapd %xmm0, %xmm1
subsd 0x10(%rsp), %xmm1
leaq 0xa962(%rip), %rsi      # 0x1a407
leaq 0xa99a(%rip), %r15      # 0x1a446
movq %r14, %rdi
movq %r15, %rdx
movsd (%rsp), %xmm0
movb $0x2, %al
callq 0x21c0
movq (%r12), %rdi
movsd 0x8(%rsp), %xmm0
mulsd (%rsp), %xmm0
leaq 0xa980(%rip), %rsi      # 0x1a454
movq %r15, %rdx
movb $0x1, %al
callq 0x21c0
jmp 0xfa65