// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdi, %r12
xorpd %xmm0, %xmm0
leaq -0x50(%rbp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl %edx, %esi
callq 0x81c6
movq -0x50(%rbp), %r14
leaq -0x2c0(%rbp), %rdi
callq 0x3170
leaq -0xc0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x100(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xe0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x88(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0xd0(%rbp), %rsi
movq %r12, %rdi
callq 0x33d0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0xf0(%rbp), %rsi
movq %r15, %rdi
callq 0x33d0
leal -0x1(%rbx), %eax
movq %rbx, -0x118(%rbp)
movl %ebx, %ecx
subl -0x54(%rbp), %ecx
movl %eax, %eax
movl %ecx, -0x34(%rbp)
movl %ecx, %ecx
shlq $0x4, %rcx
movq %rcx, -0x120(%rbp)
movq $0x0, -0x30(%rbp)
shlq $0x4, %rax
movq %rax, -0xa0(%rbp)
leaq -0x2c0(%rbp), %rbx
cmpq $0x0, -0xc8(%rbp)
je 0x639b
leaq -0x68(%rbp), %r13
movq %r13, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movb $0x0, -0x68(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x32c0
movq -0x78(%rbp), %rdi
cmpq %r13, %rdi
je 0x5eaf
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x2c0(%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
xorl %esi, %esi
callq 0x3350
leaq -0x2a8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x32c0
movq %rbx, %rdi
leaq -0x110(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0xac(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0x138(%rbp), %rsi
callq 0x3140
movq %rax, %rdi
leaq -0x130(%rbp), %rsi
callq 0x3140
movq %r13, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movb $0x0, -0x68(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x32c0
movq -0x78(%rbp), %rdi
cmpq %r13, %rdi
je 0x5f48
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x2c0(%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
xorl %esi, %esi
callq 0x3350
leaq -0x2a8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x32c0
movq %rbx, %rdi
leaq -0x98(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0xa4(%rbp), %rsi
callq 0x3130
movq %r12, %rbx
movq %r15, %r13
movq -0x90(%rbp), %r15
movq -0x108(%rbp), %r12
cmpq %r15, %r12
movq %r15, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0x5fda
movq -0x110(%rbp), %rsi
movq -0x98(%rbp), %rdi
callq 0x3230
testl %eax, %eax
sete %al
jmp 0x5fdc
movb $0x1, %al
testb %al, %al
je 0x6379
cmpq %r12, %r15
jne 0x6379
movl -0xa8(%rbp), %eax
cmpl -0xac(%rbp), %eax
jne 0x6379
movl %eax, (%r14)
movl -0xa4(%rbp), %eax
movsd 0x4028(%rip), %xmm0     # 0xa038
testl %eax, %eax
js 0x6093
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd -0x130(%rbp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm1, %xmm2
movsd 0x3fd5(%rip), %xmm4     # 0xa008
movapd %xmm4, %xmm3
subsd %xmm1, %xmm3
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
subsd %xmm3, %xmm0
movapd 0x3ffd(%rip), %xmm5     # 0xa050
andpd %xmm5, %xmm0
movsd 0x3fd1(%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(%r14)
addq $0x10, %r14
movq -0x48(%rbp), %rcx
cmpq %rcx, %r14
movq %r13, %r15
movq %rbx, %r12
je 0x60c2
movq -0x30(%rbp), %rax
incl %eax
movq %rax, -0x30(%rbp)
leaq -0x2c0(%rbp), %rbx
jmp 0x6327
movq -0x50(%rbp), %rdx
cmpq %rcx, %rdx
leaq -0x2c0(%rbp), %rbx
je 0x6114
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0x8(%rdx), %xmm0
xorl %esi, %esi
movsd 0x3f54(%rip), %xmm1     # 0xa040
ucomisd %xmm0, %xmm1
setbe %dil
ja 0x6104
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
movb %dil, %sil
addl %esi, %eax
addq $0x10, %rdx
cmpq %rcx, %rdx
jne 0x60dd
jmp 0x611f
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %eax, %eax
movl %eax, %ecx
testl %eax, %eax
movq %rcx, -0x128(%rbp)
je 0x6147
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
jmp 0x61df
movl $0x10, %edx
movq 0x7e9d(%rip), %rdi      # 0xdff0
leaq 0x42f7(%rip), %rsi      # 0xa451
callq 0x3250
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq 0x7e7c(%rip), %rdi      # 0xdff0
callq 0x3250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4a6c(%rip), %rsi      # 0xabf7
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax), %esi
movq %r13, %rdi
callq 0x3150
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x42b2(%rip), %rsi      # 0xa462
callq 0x3250
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl (%rax,%rcx), %esi
movq %r13, %rdi
callq 0x3150
movl $0x18, %edx
movq %rax, %rdi
leaq 0x428a(%rip), %rsi      # 0xa464
callq 0x3250
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl (%rax,%rcx), %r14d
addl (%rax), %r14d
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq 0x7dc2(%rip), %rdi      # 0xdfc8
callq 0x3250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x40cc(%rip), %rsi      # 0xa2e9
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax), %esi
movq %r13, %rdi
callq 0x3150
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x40a7(%rip), %rsi      # 0xa2e9
callq 0x3250
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
movl (%rax,%rcx), %esi
movq %r13, %rdi
callq 0x3150
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x407a(%rip), %rsi      # 0xa2e9
callq 0x3250
shrl %r14d
movl %r14d, %esi
movq %r13, %rdi
callq 0x3150
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4055(%rip), %rsi      # 0xa2e9
callq 0x3250
movq %r13, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x3320
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x4031(%rip), %rsi      # 0xa2e9
callq 0x3250
movq %r13, %rdi
movq -0x128(%rbp), %rsi
callq 0x3150
movl $0x1, %edx
movq %rax, %rdi
leaq 0x48d7(%rip), %rsi      # 0xabb2
callq 0x3250
cmpl $0x0, -0x34(%rbp)
movq -0x120(%rbp), %r8
je 0x6319
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, %r8
jne 0x62f6
movq -0x50(%rbp), %r14
addq %r8, %r14
movl -0x34(%rbp), %eax
movq %rax, -0x30(%rbp)
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq %r12, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x33d0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq %r15, %rdi
leaq -0xf0(%rbp), %rsi
callq 0x33d0
jmp 0x5e68
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x7c6b(%rip), %rdi      # 0xdff0
leaq 0x3fcb(%rip), %rsi      # 0xa357
movl $0x20, %edx
callq 0x3250
jmp 0x65c7
movq -0x30(%rbp), %rdx
cmpl -0x34(%rbp), %edx
setbe %al
cmpl -0x118(%rbp), %edx
setae %cl
orb %al, %cl
jne 0x65c5
decl %edx
movl %edx, %r15d
je 0x640d
movq -0x50(%rbp), %rcx
movq %r15, %rdx
shlq $0x4, %rdx
xorl %esi, %esi
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %eax, %eax
movsd 0x8(%rcx,%rsi), %xmm0
xorl %edi, %edi
movsd 0x3c5b(%rip), %xmm1     # 0xa040
ucomisd %xmm0, %xmm1
setbe %r8b
ja 0x63fd
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
movb %r8b, %dil
addl %edi, %eax
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x63d5
jmp 0x6418
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl %eax, %ebx
testl %eax, %eax
je 0x6439
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
jmp 0x64d1
movq 0x7bb0(%rip), %rdi      # 0xdff0
leaq 0x400a(%rip), %rsi      # 0xa451
movl $0x10, %edx
callq 0x3250
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq 0x7b8a(%rip), %rdi      # 0xdff0
callq 0x3250
movq %rax, %r14
leaq 0x4782(%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 0x3fc8(%rip), %rsi      # 0xa462
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x50(%rbp), %rax
movq %r15, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %esi
movq %r14, %rdi
callq 0x3150
leaq 0x3fa0(%rip), %rsi      # 0xa464
movl $0x18, %edx
movq %rax, %rdi
callq 0x3250
movq -0x50(%rbp), %rax
shlq $0x4, %r15
movl (%rax,%r15), %r12d
addl (%rax), %r12d
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq 0x7ad3(%rip), %rdi      # 0xdfc8
callq 0x3250
movq %rax, %r14
leaq 0x3de5(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
leaq 0x3dc0(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x50(%rbp), %rax
movl (%rax,%r15), %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
leaq 0x3d99(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
shrl %r12d
movl %r12d, %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
leaq 0x3d74(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq %r14, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x3320
movq %rax, %r14
leaq 0x3d50(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3150
leaq 0x45fa(%rip), %rsi      # 0xabb2
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
xorl %ebx, %ebx
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x65e9
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x660b
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x662d
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x664f
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x795a(%rip), %rsi      # 0xdfb0
leaq -0x2c0(%rbp), %rdi
callq 0x3190
leaq -0x240(%rbp), %rdi
callq 0x30e0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x6683
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 0x6699
movq %rax, %rbx
jmp 0x676b
jmp 0x66c1
jmp 0x66c1
jmp 0x66a7
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %r13, %rdi
je 0x66c4
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x3220
jmp 0x66c4
movq %rax, %rbx
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x66e6
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x6708
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x672a
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x674c
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x785d(%rip), %rsi      # 0xdfb0
leaq -0x2c0(%rbp), %rdi
callq 0x3190
leaq -0x240(%rbp), %rdi
callq 0x30e0
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x6780
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x3220
movq %rbx, %rdi
callq 0x3390