// File: /workspace/github_cpp_makefile_star2_O3/tplinderoth[P]introDist/introDist.cpp
// Function: comp2DistReg(std::basic_fstream<char, std::char_traits<char>>&, std::basic_fstream<char, std::char_traits<char>>&, unsigned int, unsigned int)
int comp2DistReg (std::fstream &freqfile, std::fstream &genofile, unsigned int window, unsigned int step) {

	/*
	 * this calculates the difference in the genetic distance between individual i and population2
	 * and the expected distance between individual i's population and population2 in a region
	 *
	 * D = d_R(i, P2) - d_R(Pi, P2)
	 * subscript R denotes average across region
	 *
	 * at site s:
	 * expected distance between individual i and population 2: E[d(i, P2)] = |g_i - E[g_p2]|/2
	 * expected difference between the distance of individuals i's population and population 2: d(Pi, P2) = |E[g_p1] - E[g_p2]|/2
	 *
	 * d_R(Pi, P2) = 1/N_R * sum_from=1_to=N_R |E[g_p1] - E[g_p2]|/2, where N_R is total number of sites in the window
	 *
	 * d_R(i, P2) = 1/N_R * sum_from=1_to=N_R |g_i - E[g_p2]|/2, where N_R is the number of sites in the window
	 *
	 * assumes individual i is from population 1
	 *
	 */

	std::vector<sitedist> dist;
	dist.resize(window);
	std::vector<sitedist>::iterator distiter = dist.begin();
	unsigned int lastidx = window-1;
	unsigned int nsites = 0;
	unsigned int dr_nsites = 0;
	double windD = 0.0;
	unsigned int midpoint = 0;
	unsigned int i = 0;
	unsigned int n = window-step;

	std::stringstream ss;

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

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

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

	// calculate windows
	while (!fline.empty()) {

		// parse frequency file lines
		ss.str(std::string());
		ss.clear();
		ss.str(fline);
		ss >> fchr >> fpos >> p1f >> p2f;

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

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

		// update window
		distiter->first=gpos;
		distiter->second = geno < 0 ? -3.0 : expectIndPopDist(geno, p2f) - expect2PopDist(p1f, p2f);
		++distiter;
		++nsites;

		if (distiter == dist.end()) {

			// calculate window value
			windD = 0.0;
			dr_nsites = 0;
			for (distiter = dist.begin(); distiter != dist.end(); ++distiter) {
				if (distiter->second < -2.0) continue;
				windD += distiter->second;
				++dr_nsites;
			}
			if (dr_nsites > 0) {
				windD /= (double)dr_nsites;
			} else {
				std::cerr << "Warning: window " << gchr << " " << dist[0].first << "," << dist[lastidx].first << " has no sites with data\n";
			}
			midpoint = (dist[0].first + dist[lastidx].first)/2;
			std::cout << gchr << "\t" << dist[0].first << "\t" << dist[lastidx].first << "\t" << midpoint << "\t" << windD << "\t" << dr_nsites << "\n";

			// prepare vector for new values
			for (i = 0; i < n; ++i) {
				dist[i].first = dist[step+i].first;
				dist[i].second = dist[step+i].second;
			}

			nsites = n;
			distiter = dist.begin() + n;
		}

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

	// print last window
	if (nsites > n && nsites < window) {
		unsigned int lastwinidx = nsites-1;
		dr_nsites = 0;
		windD = 0.0;
		for (i=0; i < lastwinidx; ++i) {
			if (dist[i].second < -2.0) continue;
			windD += dist[i].second;
			++dr_nsites;
		}
		if (dr_nsites > 0) {
			windD /= (double)dr_nsites;
		} else {
			std::cerr << "Warning: window " << gchr << " " << dist[0].first << "," << dist[lastwinidx].first << " has no sites with data\n";
		}
		midpoint = (dist[0].first + dist[lastwinidx].first)/2;
		std::cout << gchr << "\t" << dist[0].first << "\t" << dist[lastwinidx].first << "\t" << midpoint << "\t" << windD << "\t" << dr_nsites << "\n";
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movl %ecx, -0x54(%rbp)
movl %edx, %ebx
movq %rsi, -0x60(%rbp)
movq %rdi, -0x68(%rbp)
xorpd %xmm0, %xmm0
leaq -0x50(%rbp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl %edx, %esi
callq 0x81be
movq -0x50(%rbp), %r13
leaq -0x2c0(%rbp), %rdi
callq 0x3170
leaq -0xd0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x110(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xf0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x98(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq -0x68(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0xe0(%rbp), %rsi
movq -0x68(%rbp), %rdi
callq 0x33d0
movq -0x60(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0x100(%rbp), %rsi
movq -0x60(%rbp), %rdi
callq 0x33d0
leal -0x1(%rbx), %eax
movq %rbx, -0x128(%rbp)
movl %ebx, %ecx
subl -0x54(%rbp), %ecx
movl %eax, %eax
movq %rax, -0xc0(%rbp)
movl %ecx, -0x34(%rbp)
movl %ecx, %ebx
shlq $0x4, %rbx
xorl %r15d, %r15d
cmpq $0x0, -0xd8(%rbp)
je 0x6391
leaq -0x78(%rbp), %r14
movq %r14, -0x88(%rbp)
movq $0x0, -0x80(%rbp)
movb $0x0, -0x78(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x32c0
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x5ea6
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x2c0(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x2c0(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3350
leaq -0x2a8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x32c0
leaq -0x2c0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0xb4(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0x138(%rbp), %rsi
callq 0x3140
movq %rax, %rdi
leaq -0x130(%rbp), %rsi
callq 0x3140
movq %r14, -0x88(%rbp)
movq $0x0, -0x80(%rbp)
movb $0x0, -0x78(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x32c0
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x5f53
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x2c0(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x2c0(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3350
leaq -0x2a8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x32c0
leaq -0x2c0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0xac(%rbp), %rsi
callq 0x3130
movq -0xa0(%rbp), %r14
movq -0x118(%rbp), %r12
cmpq %r14, %r12
movq %r14, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0x5ff5
movq -0x120(%rbp), %rsi
movq -0xa8(%rbp), %rdi
callq 0x3230
cmpq %r12, %r14
jne 0x636f
testl %eax, %eax
je 0x5ffe
jmp 0x636f
cmpq %r12, %r14
jne 0x636f
movl -0xb0(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jne 0x636f
movl %eax, (%r13)
movl -0xac(%rbp), %eax
movsd 0x4016(%rip), %xmm0     # 0xa038
testl %eax, %eax
js 0x60a5
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd -0x130(%rbp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm1, %xmm2
movsd 0x3fc3(%rip), %xmm4     # 0xa008
movapd %xmm4, %xmm3
subsd %xmm1, %xmm3
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
subsd %xmm3, %xmm0
movapd 0x3feb(%rip), %xmm5     # 0xa050
andpd %xmm5, %xmm0
movsd 0x3fbf(%rip), %xmm6     # 0xa030
mulsd %xmm6, %xmm0
movsd -0x138(%rbp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm1, %xmm2
subsd %xmm1, %xmm4
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm4
addsd %xmm1, %xmm4
subsd %xmm3, %xmm4
andpd %xmm5, %xmm4
mulsd %xmm6, %xmm4
subsd %xmm4, %xmm0
movsd %xmm0, 0x8(%r13)
leaq 0x10(%r13), %rcx
cmpq -0x48(%rbp), %rcx
je 0x60c0
incl %r15d
movq %rcx, %r13
jmp 0x6314
movq -0x50(%rbp), %rax
cmpq %rcx, %rax
je 0x612f
leaq -0x10(%rax), %rdx
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0x18(%rdx), %xmm0
xorl %esi, %esi
movsd 0x3f59(%rip), %xmm1     # 0xa040
ucomisd %xmm0, %xmm1
setbe %dil
ja 0x60ff
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
movb %dil, %sil
addl %esi, %ecx
addq $0x10, %rdx
cmpq %r13, %rdx
jne 0x60d8
testl %ecx, %ecx
je 0x6138
movl %ecx, %r13d
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
jmp 0x61e2
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl $0x10, %edx
movq 0x7eac(%rip), %rdi      # 0xdff0
leaq 0x4306(%rip), %rsi      # 0xa451
callq 0x3250
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movq 0x7e8b(%rip), %rdi      # 0xdff0
callq 0x3250
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4a7b(%rip), %rsi      # 0xabf7
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x42c1(%rip), %rsi      # 0xa462
callq 0x3250
movq -0x50(%rbp), %rax
movq -0xc0(%rbp), %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %esi
movq %r14, %rdi
callq 0x3150
movl $0x18, %edx
movq %rax, %rdi
leaq 0x4295(%rip), %rsi      # 0xa464
callq 0x3250
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %r14
xorl %r13d, %r13d
movq -0xc0(%rbp), %r12
shlq $0x4, %r12
movl (%rax,%r12), %r15d
addl (%rax), %r15d
movq -0xa8(%rbp), %rsi
movq 0x7dc6(%rip), %rdi      # 0xdfc8
movq %r14, %rdx
callq 0x3250
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x40cd(%rip), %rsi      # 0xa2e9
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x40a8(%rip), %rsi      # 0xa2e9
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax,%r12), %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4081(%rip), %rsi      # 0xa2e9
callq 0x3250
shrl %r15d
movl %r15d, %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x405c(%rip), %rsi      # 0xa2e9
callq 0x3250
movq %r14, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x3320
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4038(%rip), %rsi      # 0xa2e9
callq 0x3250
movq %r14, %rdi
movq %r13, %rsi
callq 0x3150
movl $0x1, %edx
movq %rax, %rdi
leaq 0x48e2(%rip), %rsi      # 0xabb2
callq 0x3250
cmpl $0x0, -0x34(%rbp)
je 0x6307
movq -0x50(%rbp), %rax
movl -0x54(%rbp), %ecx
xorl %edx, %edx
movl %ecx, %esi
shlq $0x4, %rsi
movl (%rax,%rsi), %edi
movl %edi, (%rax,%rdx)
movsd 0x8(%rax,%rsi), %xmm0
movsd %xmm0, 0x8(%rax,%rdx)
addq $0x10, %rdx
incl %ecx
cmpq %rdx, %rbx
jne 0x62e4
movq -0x50(%rbp), %r13
addq %rbx, %r13
movl -0x34(%rbp), %eax
movl %eax, %r15d
movq -0x68(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq -0x68(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x33d0
movq -0x60(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq -0x60(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x33d0
jmp 0x5e56
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x7c75(%rip), %rdi      # 0xdff0
leaq 0x3fd5(%rip), %rsi      # 0xa357
movl $0x20, %edx
callq 0x3250
jmp 0x65bc
cmpl -0x34(%rbp), %r15d
setbe %al
cmpl -0x128(%rbp), %r15d
setae %cl
xorl %ebx, %ebx
orb %al, %cl
jne 0x65bc
decl %r15d
movl %r15d, %r12d
je 0x6424
movq -0x50(%rbp), %rax
movq %r12, %rcx
shlq $0x4, %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %esi, %esi
movsd 0x8(%rax,%rdx), %xmm0
xorl %edi, %edi
movsd 0x3c64(%rip), %xmm1     # 0xa040
ucomisd %xmm0, %xmm1
setbe %r8b
ja 0x63f4
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
movb %r8b, %dil
addl %edi, %esi
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x63cc
testl %esi, %esi
je 0x642d
movl %esi, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
jmp 0x64c8
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x7bbc(%rip), %rdi      # 0xdff0
leaq 0x4016(%rip), %rsi      # 0xa451
movl $0x10, %edx
callq 0x3250
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movq 0x7b96(%rip), %rdi      # 0xdff0
callq 0x3250
movq %rax, %r14
leaq 0x478e(%rip), %rsi      # 0xabf7
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
leaq 0x3fd4(%rip), %rsi      # 0xa462
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x50(%rbp), %rax
movq %r12, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %esi
movq %r14, %rdi
callq 0x3150
leaq 0x3fac(%rip), %rsi      # 0xa464
movl $0x18, %edx
movq %rax, %rdi
callq 0x3250
xorl %r14d, %r14d
movq -0x50(%rbp), %rax
shlq $0x4, %r12
movl (%rax,%r12), %r13d
addl (%rax), %r13d
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movq 0x7adc(%rip), %rdi      # 0xdfc8
callq 0x3250
movq %rax, %r15
leaq 0x3dee(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax), %esi
movq %r15, %rdi
callq 0x3150
movq %rax, %r15
leaq 0x3dc9(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax,%r12), %esi
movq %r15, %rdi
callq 0x3150
movq %rax, %r15
leaq 0x3da2(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
shrl %r13d
movl %r13d, %esi
movq %r15, %rdi
callq 0x3150
movq %rax, %r15
leaq 0x3d7d(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq %r15, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x3320
movq %rax, %r15
leaq 0x3d59(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq %r15, %rdi
movq %r14, %rsi
callq 0x3150
leaq 0x4603(%rip), %rsi      # 0xabb2
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x65de
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
cmpq %rax, %rdi
je 0x6600
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0x6622
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x6644
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x7965(%rip), %rsi      # 0xdfb0
leaq -0x2c0(%rbp), %rdi
callq 0x3190
leaq -0x240(%rbp), %rdi
callq 0x30e0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x6678
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x3220
movl %ebx, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x668e
movq %rax, %rbx
jmp 0x6763
jmp 0x66b9
jmp 0x66b9
jmp 0x669c
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r14, %rdi
je 0x66bc
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x3220
jmp 0x66bc
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x66de
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x100(%rbp), %rdi
leaq -0xf0(%rbp), %rax
cmpq %rax, %rdi
je 0x6700
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0x6722
movq -0x110(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x6744
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x7865(%rip), %rsi      # 0xdfb0
leaq -0x2c0(%rbp), %rdi
callq 0x3190
leaq -0x240(%rbp), %rdi
callq 0x30e0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x6778
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x3220
movq %rbx, %rdi
callq 0x3390