// File: /workspace/github_cpp_makefile_star2_O2/tplinderoth[P]introDist/introDist.cpp
// Function: comp2DistPopAvg(std::basic_fstream<char, std::char_traits<char>>&, std::basic_fstream<char, std::char_traits<char>>&, unsigned int, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, double const*)
int comp2DistPopAvg (std::fstream &freqfile, std::fstream &genofile, unsigned int window, unsigned int step, std::string* genofname, const double* distance) {

	/*
	 * this calculates the difference in the genetic distance between individual i and population2 in a region and
	 * the expected distance between individual i's population and population2 averaged across the genome
	 *
	 * D = d_R(i, P2) - d_G(Pi, P2)
	 * subscript G denotes average over the genome
	 * 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_G(Pi, P2) = 1/N_G * sum_from=1_to=N_G |E[g_p1] - E[g_p2]|/2, where N_G is total number of sites analyzed
	 *
	 * 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 i = 0;
	unsigned int n = window-step;
	double dg = 0.0;
	unsigned int dg_nsites = 0;

	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);

	// estimate number of lines of input files to set up vector to hold distances
	ss.str(gline);
	ss >> gchr;
	unsigned int nlines = ceil(filesize(genofname->c_str())/(gchr.size()+2*sizeof(int)-1));
	std::vector<WindowInfo> windist;
	windist.reserve(nlines/step + 10);

	// 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;
		}

		distiter->first = gpos;
		if (geno < 0) {
			// missing data
			distiter->second = -3.0;
		} else {
			// update window information
			distiter->second = expectIndPopDist(geno, p2f);
			// update genome-wide distance
			dg += expect2PopDist(p1f, p2f);
			++dg_nsites;
		}
		++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";
			}
			windist.push_back(makeWinInfo(dist[0].first, dist[lastidx].first, dr_nsites, windD));

			// 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);
	}

	// calculate 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";
		}
		windist.push_back(makeWinInfo(dist[0].first, dist[lastwinidx].first, dr_nsites, windD));
	}

	// calculate and print distance statistic
	if (dg_nsites > 0) {
		dg /= (double)dg_nsites;
	} else {
		std::cerr << "There were no sites with complete data, cannot calculate distance statistic\n";
		return 0; // use better exception handling here
	}
	std::cerr << "\nCalculated average distance between populations: " << dg << "\n";

	if (distance) {
		// use provided distance
		dg = *distance;
		std::cerr << "\nUsing distance: " << dg <<  "\n";
	}
	std::cerr << "\n";

	unsigned int midpoint = 0;
	double Dstat = 0.0;
	for (std::vector<WindowInfo>::iterator winditer = windist.begin(); winditer != windist.end(); ++winditer) {
		Dstat = winditer->d - dg;
		midpoint = (winditer->start + winditer->stop)/2;
		std::cout << gchr << "\t" << winditer->start << "\t" << winditer->stop << "\t" << midpoint << "\t" << Dstat << "\t" << winditer->nsites << "\n";
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movq %r9, %rbx
movq %r8, %r15
movl %ecx, -0x48(%rbp)
movl %edx, %r13d
movq %rsi, -0xa8(%rbp)
movq %rdi, -0xb0(%rbp)
xorpd %xmm0, %xmm0
leaq -0x60(%rbp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movl %edx, %esi
callq 0x6dc2
movq -0x60(%rbp), %r12
leaq -0x2f8(%rbp), %rdi
callq 0x3180
leaq -0x140(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0x160(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq -0x120(%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 -0x150(%rbp), %rsi
movq -0xb0(%rbp), %rdi
callq 0x3080
leaq -0x130(%rbp), %rsi
movq -0xa8(%rbp), %rdi
callq 0x3080
leaq -0x2e0(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x32e0
leaq -0x2f8(%rbp), %r14
leaq -0xd0(%rbp), %rsi
movq %r14, %rdi
callq 0x3280
movq (%r15), %rdi
callq 0x7c86
movq -0xc8(%rbp), %rcx
addq $0x7, %rcx
xorl %edx, %edx
divq %rcx
leaq -0xf0(%rbp), %rdi
andq $0x0, 0x10(%rdi)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
xorl %edx, %edx
movl -0x48(%rbp), %r15d
divl %r15d
leal 0xa(%rax), %esi
callq 0x6dfc
movq $0x0, -0x68(%rbp)
movq %rbx, -0xf8(%rbp)
leal -0x1(%r13), %eax
movq %r13, -0x100(%rbp)
movl %r13d, %ecx
subl %r15d, %ecx
movl %eax, %eax
movq %rax, -0xa0(%rbp)
movl %ecx, -0x44(%rbp)
movl %ecx, %r13d
shlq $0x4, %r13
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
leaq -0x88(%rbp), %r15
xorl %ebx, %ebx
cmpq $0x0, -0x148(%rbp)
je 0x5f90
leaq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
andq $0x0, -0x80(%rbp)
movb $0x0, -0x78(%rbp)
leaq -0x2e0(%rbp), %rdi
movq %r15, %rsi
callq 0x32e0
movq %r15, %rdi
callq 0x3120
movq -0x2f8(%rbp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
xorl %esi, %esi
callq 0x3360
leaq -0x2e0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x32e0
movq %r14, %rdi
leaq -0x170(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x94(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0x110(%rbp), %rsi
callq 0x3150
movq %rax, %rdi
leaq -0x108(%rbp), %rsi
callq 0x3150
leaq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
andq $0x0, -0x80(%rbp)
movb $0x0, -0x78(%rbp)
leaq -0x2e0(%rbp), %rdi
movq %r15, %rsi
callq 0x32e0
movq %r15, %rdi
callq 0x3120
movq -0x2f8(%rbp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
xorl %esi, %esi
callq 0x3360
leaq -0x2e0(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x32e0
movq %r14, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x90(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0x8c(%rbp), %rsi
callq 0x3140
leaq -0xd0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x3040
testl %eax, %eax
jne 0x5f75
movl -0x90(%rbp), %eax
cmpl -0x94(%rbp), %eax
jne 0x5f75
movl %eax, (%r12)
movl -0x8c(%rbp), %eax
testl %eax, %eax
js 0x5ddb
cvtsi2sd %eax, %xmm4
movsd -0x108(%rbp), %xmm1
movapd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd 0x3286(%rip), %xmm2     # 0x9008
subsd %xmm1, %xmm2
movapd %xmm1, %xmm3
mulsd %xmm0, %xmm3
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
subsd %xmm2, %xmm4
andpd 0x32be(%rip), %xmm4     # 0x9060
mulsd 0x3276(%rip), %xmm4     # 0x9020
movapd %xmm4, -0x40(%rbp)
movsd -0x110(%rbp), %xmm0
callq 0x52aa
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movapd -0x40(%rbp), %xmm0
movsd %xmm1, -0x30(%rbp)
movq -0x68(%rbp), %rax
incl %eax
movq %rax, -0x68(%rbp)
jmp 0x5de3
movsd 0x3245(%rip), %xmm0     # 0x9028
movsd %xmm0, 0x8(%r12)
addq $0x10, %r12
cmpq -0x58(%rbp), %r12
je 0x5dfb
incl %ebx
jmp 0x5f4a
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x60(%rbp), %rax
movq %rax, %rcx
cmpq %r12, %rcx
je 0x5e44
movsd 0x8(%rcx), %xmm0
xorl %edx, %edx
movsd 0x320f(%rip), %xmm1     # 0x9030
ucomisd %xmm0, %xmm1
setbe %sil
ja 0x5e39
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movb %sil, %dl
addl %edx, %ebx
addq $0x10, %rcx
jmp 0x5e0d
testl %ebx, %ebx
je 0x5e5d
movl %ebx, %ecx
cvtsi2sd %rcx, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
jmp 0x5edd
movq 0x718c(%rip), %rdi      # 0xcff0
leaq 0x35e6(%rip), %rsi      # 0x9451
callq 0x3220
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0x3d6e(%rip), %rsi      # 0x9bf7
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x35bc(%rip), %rsi      # 0x9462
callq 0x3220
movq -0x60(%rbp), %rcx
movq -0xa0(%rbp), %rdx
shlq $0x4, %rdx
movl (%rcx,%rdx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x3595(%rip), %rsi      # 0x9464
callq 0x3220
movq -0x60(%rbp), %rax
movsd -0x40(%rbp), %xmm1
movl (%rax), %ecx
movq -0xa0(%rbp), %rdx
shlq $0x4, %rdx
movl (%rax,%rdx), %eax
movl %ecx, -0x88(%rbp)
movl %eax, -0x84(%rbp)
movl %ebx, -0x80(%rbp)
movsd %xmm1, -0x78(%rbp)
leaq -0xf0(%rbp), %rdi
movq %r15, %rsi
callq 0x770c
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
xorl %edx, %edx
cmpq %rdx, %r13
je 0x5f3e
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 0x5f19
movq -0x60(%rbp), %r12
addq %r13, %r12
movl -0x44(%rbp), %eax
movl %eax, %ebx
movq -0xb0(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x3080
movq -0xa8(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x3080
jmp 0x5c0e
movq 0x7074(%rip), %rdi      # 0xcff0
leaq 0x33d4(%rip), %rsi      # 0x9357
callq 0x3220
pushq $-0x1
popq %rbx
jmp 0x623c
cmpl -0x44(%rbp), %ebx
setbe %al
cmpl -0x100(%rbp), %ebx
setae %cl
orb %al, %cl
movq -0x68(%rbp), %r15
jne 0x60af
decl %ebx
movq -0x60(%rbp), %rax
shlq $0x4, %rbx
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
xorl %r14d, %r14d
cmpq %rcx, %rbx
je 0x5ffc
movsd 0x8(%rax,%rcx), %xmm0
xorl %edx, %edx
movsd 0x3058(%rip), %xmm1     # 0x9030
ucomisd %xmm0, %xmm1
setbe %sil
ja 0x5ff0
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movb %sil, %dl
addl %edx, %r14d
addq $0x10, %rcx
jmp 0x5fc3
testl %r14d, %r14d
je 0x6014
movl %r14d, %ecx
cvtsi2sd %rcx, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
jmp 0x6089
movq 0x6fd5(%rip), %rdi      # 0xcff0
leaq 0x342f(%rip), %rsi      # 0x9451
callq 0x3220
leaq -0xd0(%rbp), %rsi
movq %rax, %rdi
callq 0x31f0
leaq 0x3bba(%rip), %rsi      # 0x9bf7
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x3408(%rip), %rsi      # 0x9462
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx,%rbx), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x33ec(%rip), %rsi      # 0x9464
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rax
movsd -0x40(%rbp), %xmm1
movl (%rax), %ecx
movl (%rax,%rbx), %eax
leaq -0x88(%rbp), %rsi
movl %ecx, (%rsi)
movl %eax, 0x4(%rsi)
movl %r14d, 0x8(%rsi)
movsd %xmm1, 0x10(%rsi)
leaq -0xf0(%rbp), %rdi
callq 0x770c
testl %r15d, %r15d
je 0x6227
movq 0x6f31(%rip), %rdi      # 0xcff0
leaq 0x3404(%rip), %rsi      # 0x94ca
callq 0x3220
movq -0xf8(%rbp), %rbx
movl %r15d, %ecx
cvtsi2sd %rcx, %xmm1
movsd -0x30(%rbp), %xmm0
divsd %xmm1, %xmm0
movq %rax, %rdi
movsd %xmm0, -0x30(%rbp)
callq 0x3330
leaq 0x3abb(%rip), %rsi      # 0x9bb2
movq %rax, %rdi
callq 0x3220
testq %rbx, %rbx
je 0x613c
movsd (%rbx), %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x6edc(%rip), %rdi      # 0xcff0
leaq 0x33e2(%rip), %rsi      # 0x94fd
callq 0x3220
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x3330
leaq 0x3a7e(%rip), %rsi      # 0x9bb2
movq %rax, %rdi
callq 0x3220
movq 0x6ead(%rip), %rdi      # 0xcff0
leaq 0x3a68(%rip), %rsi      # 0x9bb2
callq 0x3220
movq -0xf0(%rbp), %r13
leaq -0xd0(%rbp), %r14
leaq 0x3185(%rip), %r15      # 0x92e9
leaq 0x3a47(%rip), %r12      # 0x9bb2
cmpq -0xe8(%rbp), %r13
je 0x623a
movsd 0x10(%r13), %xmm0
movsd %xmm0, -0x40(%rbp)
movl 0x4(%r13), %ebx
addl (%r13), %ebx
movq 0x6e36(%rip), %rdi      # 0xcfc8
movq %r14, %rsi
callq 0x31f0
movq %rax, %rdi
movq %r15, %rsi
callq 0x3220
movl (%r13), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
movq %r15, %rsi
callq 0x3220
movl 0x4(%r13), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
movq %r15, %rsi
callq 0x3220
shrl %ebx
movl %ebx, %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
movq %r15, %rsi
callq 0x3220
movsd -0x40(%rbp), %xmm0
subsd -0x30(%rbp), %xmm0
movq %rax, %rdi
callq 0x3330
movq %rax, %rdi
movq %r15, %rsi
callq 0x3220
movl 0x8(%r13), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
movq %r12, %rsi
callq 0x3220
addq $0x18, %r13
jmp 0x616b
movq 0x6dc2(%rip), %rdi      # 0xcff0
leaq 0x3248(%rip), %rsi      # 0x947d
callq 0x3220
xorl %ebx, %ebx
leaq -0xf0(%rbp), %rdi
callq 0x7672
leaq -0xd0(%rbp), %rdi
callq 0x3120
leaq -0x130(%rbp), %rdi
callq 0x3120
leaq -0x170(%rbp), %rdi
callq 0x3120
leaq -0x150(%rbp), %rdi
callq 0x3120
leaq -0x2f8(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x747c
movl %ebx, %eax
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x62cc
jmp 0x62cc
jmp 0x62b2
jmp 0x62a9
movq %rax, %rbx
jmp 0x6317
jmp 0x62cc
jmp 0x62cc
movq %rax, %rbx
jmp 0x62db
jmp 0x62b9
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x3120
jmp 0x62cf
jmp 0x62cc
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x7672
leaq -0xd0(%rbp), %rdi
callq 0x3120
leaq -0x130(%rbp), %rdi
callq 0x3120
leaq -0x170(%rbp), %rdi
callq 0x3120
leaq -0x150(%rbp), %rdi
callq 0x3120
leaq -0x2f8(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x747c
movq %rbx, %rdi
callq 0x33a0