// File: /workspace/github_cpp_makefile_star2_O1/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 $0x218, %rsp            # imm = 0x218
movq (%rdi), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rdi,%rax)
je 0x5453
movq 0x8bb8(%rip), %rdi      # 0xdff0
leaq 0x4e72(%rip), %rsi      # 0xa2b1
movl $0x37, %edx
callq 0x3250
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x56ca
movq %rdi, %rbx
leaq -0x240(%rbp), %rdi
callq 0x3170
leaq -0x50(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xa0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq %rax, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0x60(%rbp), %rsi
movq %rbx, %rdi
callq 0x33d0
leaq -0x70(%rbp), %r12
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
leaq -0x228(%rbp), %r15
leaq -0x60(%rbp), %r13
cmpq $0x0, -0x58(%rbp)
je 0x5618
movq %r12, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
movb $0x0, -0x70(%rbp)
movq %r15, %rdi
leaq -0x80(%rbp), %rsi
callq 0x32c0
movq -0x80(%rbp), %rdi
cmpq %r12, %rdi
je 0x5505
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x240(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x240(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3350
movq %r15, %rdi
movq %r13, %rsi
callq 0x32c0
leaq -0x240(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0xb4(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0x40(%rbp), %rsi
callq 0x3140
movq %rax, %rdi
leaq -0x38(%rbp), %rsi
callq 0x3140
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
incl %r14d
movsd -0x88(%rbp), %xmm3
movapd %xmm3, %xmm0
addsd %xmm3, %xmm0
movsd 0x4a59(%rip), %xmm2     # 0xa008
movapd %xmm2, %xmm1
subsd %xmm3, %xmm1
mulsd %xmm0, %xmm3
mulsd %xmm0, %xmm1
addsd %xmm3, %xmm1
movsd -0x90(%rbp), %xmm3
movapd %xmm3, %xmm0
addsd %xmm3, %xmm0
subsd %xmm3, %xmm2
mulsd %xmm0, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
subsd %xmm2, %xmm1
andpd 0x4a61(%rip), %xmm1     # 0xa050
mulsd 0x4a39(%rip), %xmm1     # 0xa030
movsd -0x30(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x30(%rbp)
movsbl %al, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x33d0
jmp 0x54c9
movl %r14d, %ebx
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movq 0x8995(%rip), %rdi      # 0xdfc8
callq 0x3320
movq %rax, %r14
leaq 0x4ca7(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3150
leaq 0x5551(%rip), %rsi      # 0xabb2
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x5690
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x56a9
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x8900(%rip), %rsi      # 0xdfb0
leaq -0x240(%rbp), %rdi
callq 0x3190
leaq -0x1c0(%rbp), %rdi
callq 0x30e0
xorl %eax, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x56f8
movq %rax, %rbx
movq -0x80(%rbp), %rdi
cmpq %r12, %rdi
je 0x56fb
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x3220
jmp 0x56fb
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x571d
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x5736
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x8873(%rip), %rsi      # 0xdfb0
leaq -0x240(%rbp), %rdi
callq 0x3190
leaq -0x1c0(%rbp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x3390