// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2a8, %rsp            # imm = 0x2A8
movl %ecx, -0x68(%rbp)
movl %edx, %ebx
movq %rsi, -0x88(%rbp)
movq %rdi, -0x90(%rbp)
xorpd %xmm0, %xmm0
leaq -0x60(%rbp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movl %edx, %esi
callq 0x6dc2
movq -0x60(%rbp), %r13
leaq -0x2d0(%rbp), %rdi
callq 0x3180
leaq -0xf8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0x138(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq -0x118(%rbp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq -0x108(%rbp), %rsi
movq -0x90(%rbp), %rdi
callq 0x3080
leaq -0x128(%rbp), %rsi
movq -0x88(%rbp), %rdi
callq 0x3080
leal -0x1(%rbx), %eax
movq %rbx, -0xd8(%rbp)
movl %ebx, %ecx
subl -0x68(%rbp), %ecx
leaq -0x2b8(%rbp), %rbx
movl %eax, %eax
movl %ecx, -0x64(%rbp)
movl %ecx, %r15d
shlq $0x4, %r15
xorl %r12d, %r12d
leaq -0xb0(%rbp), %r14
shlq $0x4, %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x100(%rbp)
je 0x5827
leaq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
andq $0x0, -0xa8(%rbp)
movb $0x0, -0xa0(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x32e0
movq %r14, %rdi
callq 0x3120
movq -0x2d0(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x2d0(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3360
movq %rbx, %rdi
leaq -0x108(%rbp), %rsi
callq 0x32e0
leaq -0x2d0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x7c(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0xe8(%rbp), %rsi
callq 0x3150
movq %rax, %rdi
leaq -0xe0(%rbp), %rsi
callq 0x3150
leaq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
andq $0x0, -0xa8(%rbp)
movb $0x0, -0xa0(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x32e0
movq %r14, %rdi
callq 0x3120
movq -0x2d0(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x2d0(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3360
movq %rbx, %rdi
leaq -0x128(%rbp), %rsi
callq 0x32e0
leaq -0x2d0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0x74(%rbp), %rsi
callq 0x3140
leaq -0xd0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x3040
testl %eax, %eax
jne 0x580c
movl -0x78(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jne 0x580c
movl %eax, (%r13)
movl -0x74(%rbp), %eax
movsd 0x3a99(%rip), %xmm0     # 0x9028
testl %eax, %eax
js 0x55f6
cvtsi2sd %eax, %xmm4
movsd -0xe0(%rbp), %xmm1
movapd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd 0x3a59(%rip), %xmm2     # 0x9008
subsd %xmm1, %xmm2
movapd %xmm1, %xmm3
mulsd %xmm0, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
subsd %xmm2, %xmm4
andpd 0x3a91(%rip), %xmm4     # 0x9060
mulsd 0x3a49(%rip), %xmm4     # 0x9020
movapd %xmm4, -0x40(%rbp)
movsd -0xe8(%rbp), %xmm0
callq 0x52aa
movapd -0x40(%rbp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movsd %xmm0, 0x8(%r13)
addq $0x10, %r13
cmpq -0x58(%rbp), %r13
je 0x560e
incl %r12d
jmp 0x57e1
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
xorl %ecx, %ecx
movq -0x60(%rbp), %rax
movq %rax, %rdx
cmpq %r13, %rdx
je 0x5657
movsd 0x8(%rdx), %xmm0
xorl %esi, %esi
movsd 0x39fc(%rip), %xmm1     # 0x9030
ucomisd %xmm0, %xmm1
setbe %dil
ja 0x564c
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movb %dil, %sil
addl %esi, %ecx
addq $0x10, %rdx
jmp 0x5620
movl %ecx, %r12d
testl %ecx, %ecx
je 0x5673
cvtsi2sd %r12, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
jmp 0x56e7
movq 0x7976(%rip), %rdi      # 0xcff0
leaq 0x3dd0(%rip), %rsi      # 0x9451
callq 0x3220
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0x4558(%rip), %rsi      # 0x9bf7
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x3da6(%rip), %rsi      # 0x9462
callq 0x3220
movq -0x60(%rbp), %rcx
movq -0x70(%rbp), %rdx
movl (%rcx,%rdx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x3d86(%rip), %rsi      # 0x9464
callq 0x3220
movq -0x60(%rbp), %rax
movq -0x70(%rbp), %rcx
movl (%rax,%rcx), %r13d
addl (%rax), %r13d
movq 0x78cf(%rip), %rdi      # 0xcfc8
leaq -0xd0(%rbp), %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0x3bda(%rip), %rsi      # 0x92e9
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x3bbd(%rip), %rsi      # 0x92e9
callq 0x3220
movq -0x60(%rbp), %rcx
movq -0x70(%rbp), %rdx
movl (%rcx,%rdx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x3b9b(%rip), %rsi      # 0x92e9
callq 0x3220
shrl %r13d
movl %r13d, %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x3b7e(%rip), %rsi      # 0x92e9
callq 0x3220
movq %rax, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x3330
movq %rax, %rdi
leaq 0x3b62(%rip), %rsi      # 0x92e9
callq 0x3220
movq %rax, %rdi
movq %r12, %rsi
callq 0x3160
movq %rax, %rdi
leaq 0x4411(%rip), %rsi      # 0x9bb2
callq 0x3220
movq -0x60(%rbp), %rax
movl -0x68(%rbp), %ecx
xorl %edx, %edx
cmpq %rdx, %r15
je 0x57d4
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
jmp 0x57af
movq -0x60(%rbp), %r13
addq %r15, %r13
movl -0x64(%rbp), %eax
movl %eax, %r12d
movq -0x90(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3080
movq -0x88(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x3080
jmp 0x542d
movq 0x77dd(%rip), %rdi      # 0xcff0
leaq 0x3b3d(%rip), %rsi      # 0x9357
callq 0x3220
pushq $-0x1
popq %rbx
jmp 0x59d6
cmpl -0x64(%rbp), %r12d
setbe %al
cmpl -0xd8(%rbp), %r12d
setae %cl
orb %al, %cl
jne 0x59d4
decl %r12d
movq -0x60(%rbp), %rax
shlq $0x4, %r12
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rdx, %r12
je 0x5890
movsd 0x8(%rax,%rdx), %xmm0
xorl %esi, %esi
movsd 0x37c3(%rip), %xmm1     # 0x9030
ucomisd %xmm0, %xmm1
setbe %dil
ja 0x5885
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movb %dil, %sil
addl %esi, %ecx
addq $0x10, %rdx
jmp 0x5858
movl %ecx, %ebx
testl %ecx, %ecx
je 0x58ab
cvtsi2sd %rbx, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
jmp 0x591c
movq 0x773e(%rip), %rdi      # 0xcff0
leaq 0x3b98(%rip), %rsi      # 0x9451
callq 0x3220
leaq -0xd0(%rbp), %rsi
movq %rax, %rdi
callq 0x31f0
leaq 0x4323(%rip), %rsi      # 0x9bf7
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x3b71(%rip), %rsi      # 0x9462
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx,%r12), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x3b54(%rip), %rsi      # 0x9464
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rax
movl (%rax,%r12), %r14d
addl (%rax), %r14d
movq 0x769e(%rip), %rdi      # 0xcfc8
leaq -0xd0(%rbp), %rsi
callq 0x31f0
leaq 0x39ac(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x398f(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx,%r12), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x3970(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
shrl %r14d
movl %r14d, %esi
movq %rax, %rdi
callq 0x3160
leaq 0x3953(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movq %rax, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x3330
leaq 0x3937(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3160
leaq 0x41e6(%rip), %rsi      # 0x9bb2
movq %rax, %rdi
callq 0x3220
xorl %ebx, %ebx
leaq -0xd0(%rbp), %rdi
callq 0x3120
leaq -0x128(%rbp), %rdi
callq 0x3120
leaq -0x148(%rbp), %rdi
callq 0x3120
leaq -0x108(%rbp), %rdi
callq 0x3120
leaq -0x2d0(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x747c
movl %ebx, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5a31
movq %rax, %rbx
jmp 0x5a8c
jmp 0x5a4d
jmp 0x5a4d
jmp 0x5a3c
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3120
jmp 0x5a50
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x3120
leaq -0x128(%rbp), %rdi
callq 0x3120
leaq -0x148(%rbp), %rdi
callq 0x3120
leaq -0x108(%rbp), %rdi
callq 0x3120
leaq -0x2d0(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x747c
movq %rbx, %rdi
callq 0x33a0