// File: /workspace/github_cpp_makefile_star2_O3/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 0x544c
movq 0x8bbf(%rip), %rdi      # 0xdff0
leaq 0x4e79(%rip), %rsi      # 0xa2b1
movl $0x37, %edx
callq 0x3250
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x56c3
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 0x5611
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 0x54fe
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 0x4a60(%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 0x4a68(%rip), %xmm1     # 0xa050
mulsd 0x4a40(%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 0x54c2
movl %r14d, %ebx
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movq 0x899c(%rip), %rdi      # 0xdfc8
callq 0x3320
movq %rax, %r14
leaq 0x4cae(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3150
leaq 0x5558(%rip), %rsi      # 0xabb2
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x5689
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x56a2
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x8907(%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 0x56f1
movq %rax, %rbx
movq -0x80(%rbp), %rdi
cmpq %r12, %rdi
je 0x56f4
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x3220
jmp 0x56f4
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x5716
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0x572f
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x887a(%rip), %rsi      # 0xdfb0
leaq -0x240(%rbp), %rdi
callq 0x3190
leaq -0x1c0(%rbp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x3390