// File: /workspace/github_cpp_makefile_star2_O3/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, -0x64(%rbp)
movl %edx, %ebx
movq %rsi, -0x70(%rbp)
movq %rdi, -0x78(%rbp)
xorpd %xmm0, %xmm0
leaq -0x60(%rbp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl %edx, %esi
callq 0x81be
movq -0x60(%rbp), %r14
leaq -0x2d0(%rbp), %rdi
callq 0x3170
leaq -0xe0(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x120(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x100(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xa8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq -0x78(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0xf0(%rbp), %rsi
movq -0x78(%rbp), %rdi
callq 0x33d0
movq -0x70(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
leaq -0x110(%rbp), %rsi
movq -0x70(%rbp), %rdi
callq 0x33d0
leal -0x1(%rbx), %eax
movq %rbx, -0x138(%rbp)
movl %ebx, %ecx
subl -0x64(%rbp), %ecx
movl %eax, %eax
movq %rax, -0xd0(%rbp)
movl %ecx, -0x44(%rbp)
movl %ecx, %ebx
shlq $0x4, %rbx
xorl %r13d, %r13d
cmpq $0x0, -0xe8(%rbp)
je 0x457f
leaq -0x88(%rbp), %r15
movq %r15, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movb $0x0, -0x88(%rbp)
leaq -0x2b8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x32c0
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x4031
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x2d0(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x2d0(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3350
leaq -0x2b8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x32c0
leaq -0x2d0(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0xc8(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0x148(%rbp), %rsi
callq 0x3140
movq %rax, %rdi
leaq -0x140(%rbp), %rsi
callq 0x3140
movq %r15, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movb $0x0, -0x88(%rbp)
leaq -0x2b8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x32c0
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x40e7
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x2d0(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x2d0(%rbp), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3350
leaq -0x2b8(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x32c0
leaq -0x2d0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq -0xc4(%rbp), %rsi
callq 0x3380
movq %rax, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x3130
movq %rax, %rdi
leaq -0xbc(%rbp), %rsi
callq 0x3130
movq -0xb0(%rbp), %r15
movq -0x128(%rbp), %r12
cmpq %r15, %r12
movq %r15, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0x4198
movq -0x130(%rbp), %rsi
movq -0xb8(%rbp), %rdi
callq 0x3230
cmpq %r12, %r15
jne 0x455d
testl %eax, %eax
je 0x41a1
jmp 0x455d
cmpq %r12, %r15
jne 0x455d
movl -0xc4(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jne 0x455d
movl %eax, (%r14)
movl -0xc0(%rbp), %eax
movl -0xbc(%rbp), %ecx
movl %eax, %edx
orl %ecx, %edx
movsd 0x5e4a(%rip), %xmm0     # 0xa018
js 0x426f
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
cvtsi2sd %ecx, %xmm1
movsd -0x148(%rbp), %xmm2
movsd -0x140(%rbp), %xmm3
movapd %xmm2, %xmm4
addsd %xmm2, %xmm4
movsd 0x5e09(%rip), %xmm6     # 0xa008
movapd %xmm6, %xmm5
subsd %xmm2, %xmm5
mulsd %xmm4, %xmm2
mulsd %xmm4, %xmm5
addsd %xmm2, %xmm5
movapd %xmm3, %xmm2
addsd %xmm3, %xmm2
movapd %xmm6, %xmm4
subsd %xmm3, %xmm4
mulsd %xmm2, %xmm3
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
movapd %xmm0, %xmm2
subsd %xmm5, %xmm2
movapd 0x5e11(%rip), %xmm3     # 0xa050
andpd %xmm3, %xmm2
subsd %xmm1, %xmm0
subsd %xmm4, %xmm1
andpd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd 0x5dc5(%rip), %xmm1     # 0xa020
andpd %xmm3, %xmm0
mulsd 0x5dc1(%rip), %xmm0     # 0xa028
addsd %xmm6, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%r14)
leaq 0x10(%r14), %rcx
cmpq -0x58(%rbp), %rcx
je 0x428a
incl %r13d
movq %rcx, %r14
jmp 0x4502
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
je 0x42f0
leaq -0x10(%rax), %rdx
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd 0x18(%rdx), %xmm0
xorl %esi, %esi
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
setbe %dil
ja 0x42c5
movsd -0x40(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movb %dil, %sil
addl %esi, %ecx
addq $0x10, %rdx
cmpq %r14, %rdx
jne 0x42a2
testl %ecx, %ecx
je 0x42f9
movl %ecx, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
jmp 0x43a1
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x10, %edx
movq 0x9ceb(%rip), %rdi      # 0xdff0
leaq 0x6145(%rip), %rsi      # 0xa451
callq 0x3250
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq 0x9cca(%rip), %rdi      # 0xdff0
callq 0x3250
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x68ba(%rip), %rsi      # 0xabf7
callq 0x3250
movq -0x60(%rbp), %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
movl $0x1, %edx
movq %rax, %rdi
leaq 0x6100(%rip), %rsi      # 0xa462
callq 0x3250
movq -0x60(%rbp), %rax
movq -0xd0(%rbp), %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %esi
movq %r14, %rdi
callq 0x3150
movl $0x18, %edx
movq %rax, %rdi
leaq 0x60d4(%rip), %rsi      # 0xa464
callq 0x3250
movq -0x60(%rbp), %rax
xorl %r14d, %r14d
movsd -0x40(%rbp), %xmm1
movq -0xd0(%rbp), %r12
shlq $0x4, %r12
movl (%rax,%r12), %r15d
addl (%rax), %r15d
movsd 0x5c4d(%rip), %xmm0     # 0xa008
subsd %xmm1, %xmm0
callq 0x31b0
movapd %xmm0, -0x40(%rbp)
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq 0x9bea(%rip), %rdi      # 0xdfc8
callq 0x3250
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5ef4(%rip), %rsi      # 0xa2e9
callq 0x3250
movq -0x60(%rbp), %rax
movl (%rax), %esi
movq %r13, %rdi
callq 0x3150
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5ecf(%rip), %rsi      # 0xa2e9
callq 0x3250
movq -0x60(%rbp), %rax
movl (%rax,%r12), %esi
movq %r13, %rdi
callq 0x3150
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5ea8(%rip), %rsi      # 0xa2e9
callq 0x3250
shrl %r15d
movl %r15d, %esi
movq %r13, %rdi
callq 0x3150
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5e83(%rip), %rsi      # 0xa2e9
callq 0x3250
movapd -0x40(%rbp), %xmm0
movapd %xmm0, %xmm1
xorpd 0x5be4(%rip), %xmm1     # 0xa060
cmpeqsd %xmm1, %xmm0
andnpd %xmm1, %xmm0
movq %r13, %rdi
callq 0x3320
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5e4a(%rip), %rsi      # 0xa2e9
callq 0x3250
movq %r13, %rdi
movq %r14, %rsi
callq 0x3150
movl $0x1, %edx
movq %rax, %rdi
leaq 0x66f4(%rip), %rsi      # 0xabb2
callq 0x3250
cmpl $0x0, -0x44(%rbp)
je 0x44f5
movq -0x60(%rbp), %rax
movl -0x64(%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, %rbx
jne 0x44d2
movq -0x60(%rbp), %r14
addq %rbx, %r14
movl -0x44(%rbp), %eax
movl %eax, %r13d
movq -0x78(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq -0x78(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x33d0
movq -0x70(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %edx
movq -0x70(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x33d0
jmp 0x3fd5
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0x9a87(%rip), %rdi      # 0xdff0
leaq 0x5de7(%rip), %rsi      # 0xa357
movl $0x20, %edx
callq 0x3250
jmp 0x47d6
cmpl -0x44(%rbp), %r13d
setbe %al
cmpl -0x138(%rbp), %r13d
setae %cl
xorl %ebx, %ebx
orb %al, %cl
jne 0x47d6
movl %r13d, %eax
decl %eax
movl %eax, %r12d
je 0x460e
movq -0x60(%rbp), %rcx
movq %r12, %rdx
shlq $0x4, %rdx
xorl %esi, %esi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
movsd %xmm1, -0x40(%rbp)
xorl %eax, %eax
movsd 0x8(%rcx,%rsi), %xmm1
xorl %edi, %edi
ucomisd %xmm1, %xmm0
setbe %r8b
ja 0x45e0
movsd -0x40(%rbp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, -0x40(%rbp)
movb %r8b, %dil
addl %edi, %eax
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x45c0
testl %eax, %eax
je 0x4617
movl %r13d, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
movl %eax, %r14d
jmp 0x46b7
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movq 0x99d2(%rip), %rdi      # 0xdff0
leaq 0x5e2c(%rip), %rsi      # 0xa451
movl $0x10, %edx
callq 0x3250
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq 0x99ac(%rip), %rdi      # 0xdff0
callq 0x3250
movq %rax, %r14
leaq 0x65a4(%rip), %rsi      # 0xabf7
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x60(%rbp), %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
leaq 0x5dea(%rip), %rsi      # 0xa462
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x60(%rbp), %rax
movq %r12, %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %esi
movq %r14, %rdi
callq 0x3150
leaq 0x5dc2(%rip), %rsi      # 0xa464
movl $0x18, %edx
movq %rax, %rdi
callq 0x3250
xorl %r14d, %r14d
movsd -0x40(%rbp), %xmm1
movq -0x60(%rbp), %rax
shlq $0x4, %r12
movl (%rax,%r12), %r13d
addl (%rax), %r13d
movsd 0x593a(%rip), %xmm0     # 0xa008
subsd %xmm1, %xmm0
callq 0x31b0
movapd %xmm0, -0x40(%rbp)
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq 0x98d7(%rip), %rdi      # 0xdfc8
callq 0x3250
movq %rax, %r15
leaq 0x5be9(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x60(%rbp), %rax
movl (%rax), %esi
movq %r15, %rdi
callq 0x3150
movq %rax, %r15
leaq 0x5bc4(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0x60(%rbp), %rax
movl (%rax,%r12), %esi
movq %r15, %rdi
callq 0x3150
movq %rax, %r15
leaq 0x5b9d(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
shrl %r13d
movl %r13d, %esi
movq %r15, %rdi
callq 0x3150
movq %rax, %r15
leaq 0x5b78(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movapd 0x58da(%rip), %xmm1     # 0xa060
movapd -0x40(%rbp), %xmm0
xorpd %xmm0, %xmm1
cmpeqsd %xmm1, %xmm0
andnpd %xmm1, %xmm0
movq %r15, %rdi
callq 0x3320
movq %rax, %r15
leaq 0x5b3f(%rip), %rsi      # 0xa2e9
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq %r15, %rdi
movq %r14, %rsi
callq 0x3150
leaq 0x63e9(%rip), %rsi      # 0xabb2
movl $0x1, %edx
movq %rax, %rdi
callq 0x3250
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x47f8
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x481a
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x483c
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x485e
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x974b(%rip), %rsi      # 0xdfb0
leaq -0x2d0(%rbp), %rdi
callq 0x3190
leaq -0x250(%rbp), %rdi
callq 0x30e0
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x4892
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x3220
movl %ebx, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x48a8
movq %rax, %rbx
jmp 0x4980
jmp 0x48d6
jmp 0x48d6
jmp 0x48b6
movq %rax, %rbx
movq -0x98(%rbp), %rdi
cmpq %r15, %rdi
je 0x48d9
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x3220
jmp 0x48d9
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x48fb
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x491d
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x493f
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x3220
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x4961
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x3220
movq 0x9648(%rip), %rsi      # 0xdfb0
leaq -0x2d0(%rbp), %rdi
callq 0x3190
leaq -0x250(%rbp), %rdi
callq 0x30e0
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x4995
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x3220
movq %rbx, %rdi
callq 0x3390