// File: 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
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x3170
testb $0x1, %al
jne 0x5b05
jmp 0x5b24
movq 0xf4e4(%rip), %rdi      # 0x14ff0
leaq 0x97d8(%rip), %rsi      # 0xf2eb
callq 0x32c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5de8
leaq -0x198(%rbp), %rdi
callq 0x31f0
leaq -0x1b8(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x33d0
leaq -0x1d8(%rbp), %rdi
callq 0x33d0
movq -0x238(%rbp), %rsi
movq $0x0, -0x1e8(%rbp)
movq $0x0, -0x1f0(%rbp)
movq $0x0, -0x1f8(%rbp)
movl $0x0, -0x1fc(%rbp)
movq -0x10(%rbp), %rdi
callq 0x30b0
jmp 0x5b8c
jmp 0x5b8e
leaq -0x1b8(%rbp), %rdi
callq 0x3470
xorb $-0x1, %al
testb $0x1, %al
jne 0x5ba5
jmp 0x5d0d
leaq -0x230(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x33d0
movq -0x240(%rbp), %rsi
leaq -0x198(%rbp), %rdi
callq 0x3340
jmp 0x5bcd
leaq -0x230(%rbp), %rdi
callq 0x31b0
movq -0x198(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x198(%rbp,%rax), %rdi
xorl %esi, %esi
callq 0x3480
jmp 0x5bf5
leaq -0x198(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x3340
jmp 0x5c0a
leaq -0x198(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x33a0
movq %rax, -0x248(%rbp)
jmp 0x5c26
movq -0x248(%rbp), %rdi
leaq -0x1dc(%rbp), %rsi
callq 0x3060
movq %rax, -0x250(%rbp)
jmp 0x5c42
movq -0x250(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x3250
movq %rax, -0x258(%rbp)
jmp 0x5c5e
movq -0x258(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x3250
jmp 0x5c73
movsd -0x1e8(%rbp), %xmm0
movsd -0x1f0(%rbp), %xmm1
callq 0x6320
movsd %xmm0, -0x260(%rbp)
jmp 0x5c92
movsd -0x260(%rbp), %xmm1
movsd -0x1f8(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x1f8(%rbp)
movl -0x1fc(%rbp), %eax
incl %eax
movl %eax, -0x1fc(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x30b0
jmp 0x5cce
jmp 0x5b8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x208(%rbp)
movl %eax, -0x20c(%rbp)
jmp 0x5dc2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x208(%rbp)
movl %eax, -0x20c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x31b0
jmp 0x5dc2
movl -0x1fc(%rbp), %eax
cvtsi2sd %rax, %xmm1
movsd -0x1f8(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x1f8(%rbp)
movsd -0x1f8(%rbp), %xmm0
movq 0xf28d(%rip), %rdi      # 0x14fc8
callq 0x3530
movq %rax, -0x268(%rbp)
jmp 0x5d49
movq -0x268(%rbp), %rdi
leaq 0x95cc(%rip), %rsi      # 0xf323
callq 0x32c0
movq %rax, -0x270(%rbp)
jmp 0x5d65
movq -0x270(%rbp), %rdi
movl -0x1fc(%rbp), %esi
callq 0x33b0
movq %rax, -0x278(%rbp)
jmp 0x5d80
movq -0x278(%rbp), %rdi
leaq 0x9e5e(%rip), %rsi      # 0xfbec
callq 0x32c0
jmp 0x5d95
movl $0x0, -0x4(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x31b0
leaq -0x1b8(%rbp), %rdi
callq 0x31b0
leaq -0x198(%rbp), %rdi
callq 0x3210
jmp 0x5de8
leaq -0x1d8(%rbp), %rdi
callq 0x31b0
leaq -0x1b8(%rbp), %rdi
callq 0x31b0
leaq -0x198(%rbp), %rdi
callq 0x3210
jmp 0x5df4
movl -0x4(%rbp), %eax
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
movq -0x208(%rbp), %rdi
callq 0x34c0