// File: /workspace/github_cpp_makefile_star2_O2/tplinderoth[P]introDist/introDist.cpp
// Function: comp2DistIndAvg(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 comp2DistIndAvg (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 genome-wide average distance between individual i and population2
	 *
	 * D = d_R(i, P2) - d_G(i, 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
	 *
	 * d_G(i, P2) = 1/N_G * sum_from=1_to=N_G |g_i - 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 += distiter->second;
			++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 genome-wide, average distance between individual and population2: " << 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, -0x40(%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 -0x138(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0x158(%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 -0x148(%rbp), %rsi
movq -0xb0(%rbp), %rdi
callq 0x3080
leaq -0x128(%rbp), %rsi
movq -0xa8(%rbp), %rdi
callq 0x3080
leaq -0x2e0(%rbp), %rdi
leaq -0x128(%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 -0x40(%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, -0x3c(%rbp)
movl %ecx, %r13d
shlq $0x4, %r13
xorpd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
leaq -0x88(%rbp), %r15
xorl %ebx, %ebx
cmpq $0x0, -0x140(%rbp)
je 0x6800
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 -0x148(%rbp), %rsi
callq 0x32e0
movq %r14, %rdi
leaq -0x168(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0x94(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0x170(%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 -0x128(%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 -0x168(%rbp), %rsi
callq 0x3040
testl %eax, %eax
jne 0x67e5
movl -0x90(%rbp), %eax
cmpl -0x94(%rbp), %eax
jne 0x67e5
movl %eax, (%r12)
movl -0x8c(%rbp), %eax
testl %eax, %eax
js 0x664b
cvtsi2sd %eax, %xmm0
movsd -0x108(%rbp), %xmm1
movapd %xmm1, %xmm2
addsd %xmm1, %xmm2
movsd 0x29fb(%rip), %xmm3     # 0x9008
subsd %xmm1, %xmm3
mulsd %xmm2, %xmm1
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
subsd %xmm3, %xmm0
andpd 0x2a37(%rip), %xmm0     # 0x9060
mulsd 0x29ef(%rip), %xmm0     # 0x9020
movsd -0x38(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
movq -0x68(%rbp), %rax
incl %eax
movq %rax, -0x68(%rbp)
jmp 0x6653
movsd 0x29d5(%rip), %xmm0     # 0x9028
movsd %xmm0, 0x8(%r12)
addq $0x10, %r12
cmpq -0x58(%rbp), %r12
je 0x666b
incl %ebx
jmp 0x67ba
xorl %ebx, %ebx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x60(%rbp), %rax
movq %rax, %rcx
cmpq %r12, %rcx
je 0x66b4
movsd 0x8(%rcx), %xmm0
xorl %edx, %edx
movsd 0x299f(%rip), %xmm1     # 0x9030
ucomisd %xmm0, %xmm1
setbe %sil
ja 0x66a9
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
movb %sil, %dl
addl %edx, %ebx
addq $0x10, %rcx
jmp 0x667d
testl %ebx, %ebx
je 0x66cd
movl %ebx, %ecx
cvtsi2sd %rcx, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
jmp 0x674d
movq 0x691c(%rip), %rdi      # 0xcff0
leaq 0x2d76(%rip), %rsi      # 0x9451
callq 0x3220
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0x34fe(%rip), %rsi      # 0x9bf7
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x2d4c(%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 0x2d25(%rip), %rsi      # 0x9464
callq 0x3220
movq -0x60(%rbp), %rax
movsd -0x30(%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 -0x40(%rbp), %ecx
xorl %edx, %edx
cmpq %rdx, %r13
je 0x67ae
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 0x6789
movq -0x60(%rbp), %r12
addq %r13, %r12
movl -0x3c(%rbp), %eax
movl %eax, %ebx
movq -0xb0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x3080
movq -0xa8(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x3080
jmp 0x6499
movq 0x6804(%rip), %rdi      # 0xcff0
leaq 0x2b64(%rip), %rsi      # 0x9357
callq 0x3220
pushq $-0x1
popq %rbx
jmp 0x6aac
cmpl -0x3c(%rbp), %ebx
setbe %al
cmpl -0x100(%rbp), %ebx
setae %cl
orb %al, %cl
movq -0x68(%rbp), %r15
jne 0x691f
decl %ebx
movq -0x60(%rbp), %rax
shlq $0x4, %rbx
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %r14d, %r14d
cmpq %rcx, %rbx
je 0x686c
movsd 0x8(%rax,%rcx), %xmm0
xorl %edx, %edx
movsd 0x27e8(%rip), %xmm1     # 0x9030
ucomisd %xmm0, %xmm1
setbe %sil
ja 0x6860
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
movb %sil, %dl
addl %edx, %r14d
addq $0x10, %rcx
jmp 0x6833
testl %r14d, %r14d
je 0x6884
movl %r14d, %ecx
cvtsi2sd %rcx, %xmm0
movsd -0x30(%rbp), %xmm1
divsd %xmm0, %xmm1
jmp 0x68f9
movq 0x6765(%rip), %rdi      # 0xcff0
leaq 0x2bbf(%rip), %rsi      # 0x9451
callq 0x3220
leaq -0xd0(%rbp), %rsi
movq %rax, %rdi
callq 0x31f0
leaq 0x334a(%rip), %rsi      # 0x9bf7
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x2b98(%rip), %rsi      # 0x9462
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx,%rbx), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x2b7c(%rip), %rsi      # 0x9464
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rax
movsd -0x30(%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 0x6a97
movq 0x66c1(%rip), %rdi      # 0xcff0
leaq 0x2bd9(%rip), %rsi      # 0x950f
callq 0x3220
movq -0xf8(%rbp), %rbx
movl %r15d, %ecx
cvtsi2sd %rcx, %xmm1
movsd -0x38(%rbp), %xmm0
divsd %xmm1, %xmm0
movq %rax, %rdi
movsd %xmm0, -0x38(%rbp)
callq 0x3330
leaq 0x324b(%rip), %rsi      # 0x9bb2
movq %rax, %rdi
callq 0x3220
testq %rbx, %rbx
je 0x69ac
movsd (%rbx), %xmm0
movsd %xmm0, -0x38(%rbp)
movq 0x666c(%rip), %rdi      # 0xcff0
leaq 0x2b72(%rip), %rsi      # 0x94fd
callq 0x3220
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x3330
leaq 0x320e(%rip), %rsi      # 0x9bb2
movq %rax, %rdi
callq 0x3220
movq 0x663d(%rip), %rdi      # 0xcff0
leaq 0x31f8(%rip), %rsi      # 0x9bb2
callq 0x3220
movq -0xf0(%rbp), %r13
leaq -0xd0(%rbp), %r14
leaq 0x2915(%rip), %r15      # 0x92e9
leaq 0x31d7(%rip), %r12      # 0x9bb2
cmpq -0xe8(%rbp), %r13
je 0x6aaa
movsd 0x10(%r13), %xmm0
movsd %xmm0, -0x30(%rbp)
movl 0x4(%r13), %ebx
addl (%r13), %ebx
movq 0x65c6(%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 -0x30(%rbp), %xmm0
subsd -0x38(%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 0x69db
movq 0x6552(%rip), %rdi      # 0xcff0
leaq 0x29d8(%rip), %rsi      # 0x947d
callq 0x3220
xorl %ebx, %ebx
leaq -0xf0(%rbp), %rdi
callq 0x7672
leaq -0xd0(%rbp), %rdi
callq 0x3120
leaq -0x128(%rbp), %rdi
callq 0x3120
leaq -0x168(%rbp), %rdi
callq 0x3120
leaq -0x148(%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 0x6b3c
jmp 0x6b3c
jmp 0x6b22
jmp 0x6b19
movq %rax, %rbx
jmp 0x6b87
jmp 0x6b3c
jmp 0x6b3c
movq %rax, %rbx
jmp 0x6b4b
jmp 0x6b29
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x3120
jmp 0x6b3f
jmp 0x6b3c
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x7672
leaq -0xd0(%rbp), %rdi
callq 0x3120
leaq -0x128(%rbp), %rdi
callq 0x3120
leaq -0x168(%rbp), %rdi
callq 0x3120
leaq -0x148(%rbp), %rdi
callq 0x3120
leaq -0x2f8(%rbp), %rdi
callq 0x31a0
leaq -0x60(%rbp), %rdi
callq 0x747c
movq %rbx, %rdi
callq 0x33a0