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

	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;
	double logwinD = 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 geno1;
	int geno2;

	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 >> geno1 >> geno2;

		// 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 = (geno1 < 0 || geno2 < 0) ? -1.0 : calc3Dist(geno1, geno2, p1f, p2f);
		++distiter;
		++nsites;

		if (distiter == dist.end()) {
			windD = 0.0;
			dr_nsites = 0;
			for (distiter = dist.begin(); distiter != dist.end(); ++distiter) {
				if (distiter->second < 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;
			logwinD = -log(1.0-windD);
			if (logwinD == 0.0) logwinD = 0.0; // no negative zero
			std::cout << gchr << "\t" << dist[0].first << "\t" << dist[lastidx].first << "\t" << midpoint << "\t" << logwinD << "\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;
		windD = 0.0;
		dr_nsites = 0;
		for (i=0; i < lastwinidx; ++i) {
			if (dist[i].second < 0) continue;
			windD += dist[i].second;
			++dr_nsites;
		}
		if (dr_nsites > 0) {
			windD /= (double)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;
		logwinD = -log(1.0-windD);
		if (logwinD == 0.0) logwinD = 0.0; // no negative zero
		std::cout << gchr << "\t" << dist[0].first << "\t" << dist[lastwinidx].first << "\t" << midpoint << "\t" << logwinD << "\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), %r12
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 %r13d, %r13d
leaq -0xb0(%rbp), %r14
shlq $0x4, %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x100(%rbp)
je 0x41ef
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 -0x80(%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 -0x7c(%rbp), %rsi
callq 0x3390
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
callq 0x3140
movq %rax, %rdi
leaq -0x74(%rbp), %rsi
callq 0x3140
leaq -0xd0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x3040
testl %eax, %eax
jne 0x41d4
movl -0x7c(%rbp), %eax
cmpl -0x80(%rbp), %eax
jne 0x41d4
movl %eax, (%r12)
movl -0x78(%rbp), %eax
movl -0x74(%rbp), %ecx
movl %eax, %edx
orl %ecx, %edx
movsd 0x50a1(%rip), %xmm0     # 0x9018
js 0x3f96
cvtsi2sd %eax, %xmm0
cvtsi2sd %ecx, %xmm1
movsd -0xe8(%rbp), %xmm2
movsd -0xe0(%rbp), %xmm3
callq 0x6bae
movsd %xmm0, 0x8(%r12)
addq $0x10, %r12
cmpq -0x58(%rbp), %r12
je 0x3faf
incl %r13d
jmp 0x41a9
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
xorl %ecx, %ecx
movq -0x60(%rbp), %rax
movq %rax, %rdx
cmpq %r12, %rdx
je 0x3ff4
movsd 0x8(%rdx), %xmm0
xorl %esi, %esi
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
setbe %dil
ja 0x3fe9
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movb %dil, %sil
addl %esi, %ecx
addq $0x10, %rdx
jmp 0x3fc1
movl %ecx, %r12d
testl %ecx, %ecx
je 0x400b
cvtsi2sd %r12, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
jmp 0x4084
movq 0x8fde(%rip), %rdi      # 0xcff0
leaq 0x5438(%rip), %rsi      # 0x9451
callq 0x3220
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0x5bc0(%rip), %rsi      # 0x9bf7
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x540e(%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 0x53ee(%rip), %rsi      # 0x9464
callq 0x3220
movq -0x60(%rbp), %rax
movsd -0x40(%rbp), %xmm1
movq -0x70(%rbp), %rcx
movl (%rax,%rcx), %r13d
addl (%rax), %r13d
movsd 0x4f71(%rip), %xmm0     # 0x9008
subsd %xmm1, %xmm0
callq 0x31c0
movapd %xmm0, -0x40(%rbp)
movq 0x8f1c(%rip), %rdi      # 0xcfc8
leaq -0xd0(%rbp), %rsi
callq 0x31f0
movq %rax, %rdi
leaq 0x5227(%rip), %rsi      # 0x92e9
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x520a(%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 0x51e8(%rip), %rsi      # 0x92e9
callq 0x3220
shrl %r13d
movl %r13d, %esi
movq %rax, %rdi
callq 0x3160
movq %rax, %rdi
leaq 0x51cb(%rip), %rsi      # 0x92e9
callq 0x3220
movapd -0x40(%rbp), %xmm0
movapd %xmm0, %xmm1
xorpd 0x4f1c(%rip), %xmm1     # 0x9050
cmpeqsd %xmm1, %xmm0
andnpd %xmm1, %xmm0
movq %rax, %rdi
callq 0x3330
movq %rax, %rdi
leaq 0x519a(%rip), %rsi      # 0x92e9
callq 0x3220
movq %rax, %rdi
movq %r12, %rsi
callq 0x3160
movq %rax, %rdi
leaq 0x5a49(%rip), %rsi      # 0x9bb2
callq 0x3220
movq -0x60(%rbp), %rax
movl -0x68(%rbp), %ecx
xorl %edx, %edx
cmpq %rdx, %r15
je 0x419c
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 0x4177
movq -0x60(%rbp), %r12
addq %r15, %r12
movl -0x64(%rbp), %eax
movl %eax, %r13d
movq -0x90(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x3080
movq -0x88(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x3080
jmp 0x3e02
movq 0x8e15(%rip), %rdi      # 0xcff0
leaq 0x5175(%rip), %rsi      # 0x9357
callq 0x3220
pushq $-0x1
popq %rbx
jmp 0x43c6
cmpl -0x64(%rbp), %r13d
setbe %al
cmpl -0xd8(%rbp), %r13d
setae %cl
orb %al, %cl
jne 0x43c4
leal -0x1(%r13), %r14d
movq -0x60(%rbp), %rax
shlq $0x4, %r14
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
xorl %ebx, %ebx
xorpd %xmm1, %xmm1
movsd %xmm1, -0x40(%rbp)
cmpq %rcx, %r14
je 0x4255
movsd 0x8(%rax,%rcx), %xmm1
xorl %edx, %edx
ucomisd %xmm1, %xmm0
setbe %sil
ja 0x424a
movsd -0x40(%rbp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, -0x40(%rbp)
movb %sil, %dl
addl %edx, %ebx
addq $0x10, %rcx
jmp 0x4225
testl %ebx, %ebx
je 0x426c
movl %r13d, %ecx
cvtsi2sd %rcx, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
jmp 0x42e2
movq 0x8d7d(%rip), %rdi      # 0xcff0
leaq 0x51d7(%rip), %rsi      # 0x9451
callq 0x3220
leaq -0xd0(%rbp), %rsi
movq %rax, %rdi
callq 0x31f0
leaq 0x5962(%rip), %rsi      # 0x9bf7
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x51b0(%rip), %rsi      # 0x9462
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx,%r14), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x5193(%rip), %rsi      # 0x9464
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rax
movsd -0x40(%rbp), %xmm1
movl (%rax,%r14), %r15d
addl (%rax), %r15d
movsd 0x4d17(%rip), %xmm0     # 0x9008
subsd %xmm1, %xmm0
callq 0x31c0
movapd %xmm0, -0x40(%rbp)
movq 0x8cc2(%rip), %rdi      # 0xcfc8
leaq -0xd0(%rbp), %rsi
callq 0x31f0
leaq 0x4fd0(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x4fb3(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movq -0x60(%rbp), %rcx
movl (%rcx,%r14), %esi
movq %rax, %rdi
callq 0x3160
leaq 0x4f94(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
shrl %r15d
movl %r15d, %esi
movq %rax, %rdi
callq 0x3160
leaq 0x4f77(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movapd 0x4cce(%rip), %xmm1     # 0x9050
movapd -0x40(%rbp), %xmm0
xorpd %xmm0, %xmm1
cmpeqsd %xmm1, %xmm0
andnpd %xmm1, %xmm0
movq %rax, %rdi
callq 0x3330
leaq 0x4f46(%rip), %rsi      # 0x92e9
movq %rax, %rdi
callq 0x3220
movl %ebx, %esi
movq %rax, %rdi
callq 0x3160
leaq 0x57f6(%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 0x4421
movq %rax, %rbx
jmp 0x447c
jmp 0x443d
jmp 0x443d
jmp 0x442c
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3120
jmp 0x4440
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