// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movsd 0x980c(%rip), %xmm0     # 0x259a8
movsd %xmm0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl $0x8, %esi
callq 0x2140
movq %rax, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1c225
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x68, -0x30(%rbp), %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rdx
shlq $0x20, %rdx
orq -0x30(%rbp), %rdx
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x68, -0x30(%rbp), %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x1c1c6
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x163d0
movl $0x0, -0x14(%rbp)
cmpl $0x2, -0x14(%rbp)
jge 0x1c3ec
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
subq $0x1, %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jl 0x1c3c9
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shlq $0x20, %rcx
shrq $0x20, %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x50(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x1c2cf
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x8(%rax), %rcx
movslq -0x58(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x58(%rbp)
movq -0x50(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x1c2f9
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x50(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rcx
movslq -0x58(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x1c30b
movl -0x54(%rbp), %ecx
movl -0x58(%rbp), %eax
cltd
idivl %ecx
movl %eax, -0x58(%rbp)
movsd -0x28(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1c373
movq -0x50(%rbp), %rax
movl (%rax), %eax
subl -0x54(%rbp), %eax
cvtsi2sd %eax, %xmm1
divsd -0x28(%rbp), %xmm1
movq -0x50(%rbp), %rax
cvtsi2sdl 0x4(%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x9665(%rip), %xmm2     # 0x259b8
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd 0x9648(%rip), %xmm0     # 0x259b0
ucomisd -0x60(%rbp), %xmm0
jbe 0x1c371
jmp 0x1c3b8
jmp 0x1c373
movq -0x50(%rbp), %rax
movslq 0x4(%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
subl -0x54(%rbp), %eax
cltq
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
cvtsi2sdq -0x40(%rbp), %xmm0
cvtsi2sdq -0x38(%rbp), %xmm1
movsd 0x9616(%rip), %xmm2     # 0x259c0
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1c3b6
jmp 0x1c3c9
jmp 0x1c3b8
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x1c265
cvtsi2sdq -0x48(%rbp), %xmm0
cvtsi2sdq -0x40(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1c23c
cmpl $0x3, 0xdebd(%rip)      # 0x2a2b0
jl 0x1c447
movq 0xdbfc(%rip), %rax      # 0x29ff8
movq (%rax), %rdi
movsd -0x28(%rbp), %xmm0
leaq 0x9829(%rip), %rsi      # 0x25c34
leaq 0x9847(%rip), %rdx      # 0x25c59
movb $0x1, %al
callq 0x2170
movq 0xdbd8(%rip), %rax      # 0x29ff8
movq (%rax), %rdi
movsd -0x28(%rbp), %xmm0
cvtsi2sdq -0x38(%rbp), %xmm1
mulsd %xmm1, %xmm0
leaq 0x982e(%rip), %rsi      # 0x25c67
leaq 0x9819(%rip), %rdx      # 0x25c59
movb $0x1, %al
callq 0x2170
movq -0x20(%rbp), %rdi
callq 0x2040
movsd -0x28(%rbp), %xmm0
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)