// File: /workspace/github_cpp_makefile_star2_O3/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 $0x268, %rsp            # imm = 0x268
movq (%rdi), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rdi,%rax)
je 0x578d
movq 0x8871(%rip), %rdi      # 0xdff0
leaq 0x4b65(%rip), %rsi      # 0xa2eb
movl $0x39, %edx
jmp 0x57b1
movq %rsi, %rbx
movq (%rsi), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rsi,%rax)
je 0x57cf
movq 0x884b(%rip), %rdi      # 0xdff0
leaq 0x4b79(%rip), %rsi      # 0xa325
movl $0x31, %edx
callq 0x3250
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r14
leaq -0x288(%rbp), %r15
movq %r15, %rdi
callq 0x3170
leaq -0x80(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xe0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq %rax, -0x100(%rbp)
movq %rax, -0x70(%rbp)
leaq -0xc0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xa0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0x90(%rbp), %rsi
movq %r14, %rdi
callq 0x33d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0xd0(%rbp), %rsi
movq %rbx, %rdi
callq 0x33d0
leaq -0x40(%rbp), %r12
leaq -0x270(%rbp), %r13
movl $0x0, -0x2c(%rbp)
xorpd %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movq %r14, -0x68(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x5af2
movq %r12, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
movq %r13, %rdi
leaq -0x50(%rbp), %rsi
callq 0x32c0
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0x58d9
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x288(%rbp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
xorl %esi, %esi
callq 0x3350
movq %r13, %rdi
leaq -0x90(%rbp), %rsi
callq 0x32c0
movq %r15, %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x60(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0x100(%rbp), %rsi
callq 0x3140
movq %rax, %rdi
leaq -0x70(%rbp), %rsi
callq 0x3140
movq %r12, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movb $0x0, -0x40(%rbp)
movq %r13, %rdi
leaq -0x50(%rbp), %rsi
callq 0x32c0
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0x5964
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x288(%rbp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
xorl %esi, %esi
callq 0x3350
movq %r13, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x32c0
movq %r15, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x5c(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0xf8(%rbp), %rsi
callq 0x3140
movq %rbx, %r14
movq %r12, %rbx
movq %r15, %r12
movq -0xa8(%rbp), %r15
movq -0xe8(%rbp), %r13
cmpq %r15, %r13
movq %r15, %rdx
cmovbq %r13, %rdx
testq %rdx, %rdx
je 0x59fd
movq -0xf0(%rbp), %rsi
movq -0xb0(%rbp), %rdi
callq 0x3230
cmpq %r13, %r15
jne 0x5ad3
testl %eax, %eax
je 0x5a06
jmp 0x5ad3
cmpq %r13, %r15
jne 0x5ad3
movl -0x5c(%rbp), %eax
cmpl -0x60(%rbp), %eax
jne 0x5ad3
movsd -0xf8(%rbp), %xmm0
ucomisd 0x45ee(%rip), %xmm0     # 0xa010
jb 0x5a6e
movsd -0x70(%rbp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm1, %xmm2
movsd 0x45cf(%rip), %xmm3     # 0xa008
subsd %xmm1, %xmm3
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
subsd %xmm3, %xmm0
andpd 0x45fb(%rip), %xmm0     # 0xa050
mulsd 0x45d3(%rip), %xmm0     # 0xa030
movsd -0x58(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x58(%rbp)
incl -0x2c(%rbp)
movq %r12, %r15
movq %rbx, %r12
movq %r14, %rbx
movq -0x68(%rbp), %r14
leaq -0x270(%rbp), %r13
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq %r14, %rdi
leaq -0x90(%rbp), %rsi
callq 0x33d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq %rbx, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x33d0
jmp 0x589a
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x8511(%rip), %rdi      # 0xdff0
leaq 0x4871(%rip), %rsi      # 0xa357
movl $0x20, %edx
callq 0x3250
jmp 0x5b4a
movl -0x2c(%rbp), %ebx
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
movsd -0x58(%rbp), %xmm0
divsd %xmm1, %xmm0
movq 0x84bb(%rip), %rdi      # 0xdfc8
callq 0x3320
movq %rax, %r14
leaq 0x47cd(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3150
xorl %ebx, %ebx
leaq 0x5075(%rip), %rsi      # 0xabb2
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x5b6c
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x5b8e
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x5bb0
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x5bcc
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x83dd(%rip), %rsi      # 0xdfb0
leaq -0x288(%rbp), %rdi
callq 0x3190
leaq -0x208(%rbp), %rdi
callq 0x30e0
jmp 0x57bb
jmp 0x5c0e
jmp 0x5bf4
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0x5c11
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x3220
jmp 0x5c11
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0x5c33
movq -0xa0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x5c55
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x5c77
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x5c93
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x8316(%rip), %rsi      # 0xdfb0
leaq -0x288(%rbp), %rdi
callq 0x3190
leaq -0x208(%rbp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x3390