// File: /workspace/github_cpp_makefile_star2_O2/tplinderoth[P]introDist/introDist.cpp
// Function: avgIndPopDist(std::basic_fstream<char, std::char_traits<char>>&, std::basic_fstream<char, std::char_traits<char>>&)
int avgIndPopDist (std::fstream &freqfile, std::fstream &genofile) {
	/*
	 * this calculates the genetic distance between an individual and population2
	 * D = 1/N_G * sum_from=1_to=N_G |g_i - 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 avgIndPopDist\n";
		return -1;
	}

	if (!genofile) {
		std::cerr << "No genotype file stream in call to avgIndPopDist\n";
		return -1;
	}

	std::stringstream ss;

	std::string(fline);
	std::string(fchr);
	unsigned int fpos;
	double p1f = 0.0;
	double p2f = 0.0;

	std::string(gline);
	std::string(gchr);
	unsigned int gpos;
	double geno;

	double dist = 0.0;
	unsigned int nsites = 0;

	getline(freqfile, fline);
	getline(genofile, gline);

	while (!fline.empty()) {
		// parse frequency file line
		ss.str(std::string());
		ss.clear();
		ss.str(fline);
		ss >> fchr >> fpos >> p1f >> p2f;

		// parse genotype file line
		ss.str(std::string());
		ss.clear();
		ss.str(gline);
		ss >> gchr >> gpos >> geno;

		if (gchr.compare(fchr) != 0 || gpos != fpos) {
			std::cerr << "Positions in input files differ\n";
			return -1;
		}

		if (geno >= 0) {
			// update distance statistic
			dist += expectIndPopDist(geno, p2f);
			++nsites;
		}

		getline(freqfile, fline);
		getline(genofile, gline);
	}

	// 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 $0x258, %rsp            # imm = 0x258
movq (%rdi), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rdi,%rax)
je 0x4f3f
movq 0x80ba(%rip), %rdi      # 0xcff0
leaq 0x43ae(%rip), %rsi      # 0x92eb
jmp 0x4f5e
movq %rsi, %rbx
movq (%rsi), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rsi,%rax)
je 0x4f7a
movq 0x8099(%rip), %rdi      # 0xcff0
leaq 0x43c7(%rip), %rsi      # 0x9325
callq 0x3220
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r14
leaq -0x280(%rbp), %r15
movq %r15, %rdi
callq 0x3180
leaq -0x88(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq -0xe8(%rbp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
andq $0x0, -0x78(%rbp)
andq $0x0, -0x48(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rdx)
movb %cl, (%rax)
leaq -0xc8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq -0x98(%rbp), %rsi
movq %r14, %rdi
callq 0x3080
leaq -0xb8(%rbp), %rsi
movq %rbx, %rdi
callq 0x3080
leaq -0x268(%rbp), %r12
movl $0x0, -0x2c(%rbp)
leaq -0x68(%rbp), %r13
xorpd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0x51c8
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x32e0
movq %r13, %rdi
callq 0x3120
movq -0x280(%rbp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
xorl %esi, %esi
callq 0x3360
movq %r12, %rdi
leaq -0x98(%rbp), %rsi
callq 0x32e0
movq %r15, %rdi
leaq -0xf8(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x40(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
callq 0x3150
movq %rax, %rdi
leaq -0x48(%rbp), %rsi
callq 0x3150
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x32e0
movq %r13, %rdi
callq 0x3120
movq -0x280(%rbp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
xorl %esi, %esi
callq 0x3360
movq %r12, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x32e0
movq %r15, %rdi
leaq -0xd8(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x3c(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0x70(%rbp), %rsi
callq 0x3150
leaq -0xd8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x3040
testl %eax, %eax
jne 0x51b0
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jne 0x51b0
movsd -0x70(%rbp), %xmm0
ucomisd 0x3ecf(%rip), %xmm0     # 0x9010
jb 0x518d
movsd -0x48(%rbp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm1, %xmm2
movsd 0x3eb0(%rip), %xmm3     # 0x9008
subsd %xmm1, %xmm3
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
subsd %xmm3, %xmm0
andpd 0x3eec(%rip), %xmm0     # 0x9060
mulsd 0x3ea4(%rip), %xmm0     # 0x9020
movsd -0x38(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
incl -0x2c(%rbp)
movq %r14, %rdi
leaq -0x98(%rbp), %rsi
callq 0x3080
movq %rbx, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x3080
jmp 0x5019
movq 0x7e39(%rip), %rdi      # 0xcff0
leaq 0x4199(%rip), %rsi      # 0x9357
callq 0x3220
pushq $-0x1
popq %rbx
jmp 0x5210
movl -0x2c(%rbp), %ebx
cvtsi2sd %rbx, %xmm1
movsd -0x38(%rbp), %xmm0
divsd %xmm1, %xmm0
movq 0x7de8(%rip), %rdi      # 0xcfc8
callq 0x3330
leaq 0x40fd(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3160
xorl %ebx, %ebx
leaq 0x49aa(%rip), %rsi      # 0x9bb2
movq %rax, %rdi
callq 0x3220
leaq -0xd8(%rbp), %rdi
callq 0x3120
leaq -0xb8(%rbp), %rdi
callq 0x3120
leaq -0xf8(%rbp), %rdi
callq 0x3120
leaq -0x98(%rbp), %rdi
callq 0x3120
leaq -0x280(%rbp), %rdi
callq 0x31a0
jmp 0x4f66
jmp 0x5263
jmp 0x5255
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x3120
jmp 0x5266
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x3120
leaq -0xb8(%rbp), %rdi
callq 0x3120
leaq -0xf8(%rbp), %rdi
callq 0x3120
leaq -0x98(%rbp), %rdi
callq 0x3120
leaq -0x280(%rbp), %rdi
callq 0x31a0
movq %rbx, %rdi
callq 0x33a0