// File: /workspace/github_cpp_makefile_star2_O2/tplinderoth[P]introDist/introDist.cpp
// Function: avg2PopDist(std::basic_fstream<char, std::char_traits<char>>&)
int avg2PopDist (std::fstream &freqfile) {
	/*
	 * this calculates the expected genetic distance between two populations
	 * D = 1/N_G * sum_from=1_to=N_G |E[g_p1] - E[g_p2]|/2
	 * where N_G is the number of sites in the genome
	 */

	if (!freqfile) {
		std::cerr << "No allele frequency file stream in call to avg2PopDist\n";
		return -1;
	}

	std::stringstream ss;
	std::string(fline);
	std::string(chr);
	unsigned int pos;
	double p1f = 0.0;
	double p2f = 0.0;
	double dist = 0.0;
	unsigned int nsites = 0;

	getline(freqfile, fline);
	while (!fline.empty()) {
		// parse frequency file line
		ss.str(std::string());
		ss.clear();
		ss.str(fline);
		ss >> chr >> pos >> p1f >> p2f;

		// update distance statistic
		dist += expect2PopDist(p1f, p2f);
		++nsites;

		// read in next line
		getline(freqfile, fline);
	}

	// print distance
	dist /= double(nsites);
	std::cout << dist << "\t" << nsites << "\n";

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq (%rdi), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rdi,%rax)
je 0x4d2a
movq 0x82da(%rip), %rdi      # 0xcff0
leaq 0x4594(%rip), %rsi      # 0x92b1
callq 0x3220
pushq $-0x1
popq %rax
jmp 0x4ebc
movq %rdi, %rbx
leaq -0x230(%rbp), %rdi
callq 0x3180
leaq -0x78(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %r14d, %r14d
leaq -0x98(%rbp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %r14b, (%rax)
andq $0x0, -0x40(%rbp)
movb %r14b, (%rcx)
andq $0x0, -0x38(%rbp)
leaq -0x88(%rbp), %rsi
movq %rbx, %rdi
callq 0x3080
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
leaq -0x218(%rbp), %r15
leaq -0x68(%rbp), %r12
leaq -0x88(%rbp), %r13
cmpq $0x0, -0x80(%rbp)
je 0x4e50
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x32e0
movq %r12, %rdi
callq 0x3120
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x230(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3360
movq %r15, %rdi
movq %r13, %rsi
callq 0x32e0
leaq -0x230(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x44(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0x40(%rbp), %rsi
callq 0x3150
movq %rax, %rdi
leaq -0x38(%rbp), %rsi
callq 0x3150
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
callq 0x52aa
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
incl %r14d
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3080
jmp 0x4d93
movl %r14d, %ebx
cvtsi2sd %rbx, %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movq 0x8160(%rip), %rdi      # 0xcfc8
callq 0x3330
leaq 0x4475(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3160
leaq 0x4d24(%rip), %rsi      # 0x9bb2
movq %rax, %rdi
callq 0x3220
leaq -0xa8(%rbp), %rdi
callq 0x3120
leaq -0x88(%rbp), %rdi
callq 0x3120
leaq -0x230(%rbp), %rdi
callq 0x31a0
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4ede
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x3120
jmp 0x4ee1
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x3120
leaq -0x88(%rbp), %rdi
callq 0x3120
leaq -0x230(%rbp), %rdi
callq 0x31a0
movq %rbx, %rdi
callq 0x33a0