// File: /workspace/github_cpp_makefile_star2_O3/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::VcfFileI::sample(sampFiles::VcfFileO&, unsigned long const&)
void VcfFileI::sample(VcfFileO &out, const uint64_t &n){
	if (n == 0) {
		cerr << "WARNING: zero SNPs requested. Nothing to be done." << endl;
		return;
	}
	// start by figuring out the number of SNPs in the file
	if (out._varFile.is_open()) {
		out.close();
	}


	uint64_t N = _numLines(); // Number of SNPs
	try {
		_varFile.open(_fileName.c_str(), ios::in);
		if (!_varFile.is_open()) {
			cerr << "ERROR: failed to open .vcf file " << _fileName << " for input" << endl;
			exit(2);
		}

	} catch (system_error &error) {
		cerr << "ERROR: cannot open .vcf file " << _fileName << " for input: " << error.code().message() << flush;
		perror(" ");
		exit(1);
	}

	try {
		out._varFile.open(out._fileName.c_str(), ios::out | ios::trunc);
		if (!out._varFile.is_open()) {
			cerr << "ERROR: failed to open .vcf file " << out._fileName << " for output" << endl;
			exit(2);
		}

	} catch (system_error &error) {
		cerr << "ERROR: cannot open .vcf file " << out._fileName << " for output: " << error.code().message() << flush;
		perror(" ");
		exit(1);
	}

	// Skip and copy the header
	string headLine;
	while (1) {
		getline(_varFile, headLine);
		if (headLine[0] != '#') {
			uint64_t cur = _varFile.tellg();
			if (cur < headLine.size()) {
				cerr << "ERROR: current position (" << cur << ") smaller than header line length (" << headLine.size() << ")" << endl;
				exit(6);
			}
			_varFile.seekg(cur - headLine.size() - 1); // go back to the start of the line, since this is the first non-header line
			break;
		}
		out._varFile << headLine << endl;
	}

	// Test for potential problems
	if (N < n) {
		cerr << "ERROR: requested a sample of " << n << " SNPs that is greater than the number of SNPs (" << N << ") in the input file." << endl;
		return;
	} else if (N == n) {
		cerr << "WARNING: sample size (" << n << ") the same as the number of SNPs (" << N << ") in the input file. Simply copying the files." << endl;
		char *buf;
		const size_t bufSize = BUF_SIZE;
		try {
			buf = new char[bufSize];
		} catch (bad_alloc&) {
			cerr << "ERROR: failed to allocate buffer" << endl;
			exit(4);
		}

		// Copy .vcf
		while (_varFile) {
			_varFile.read(buf, bufSize);
			out._varFile.write(buf, _varFile.gcount());
		}
		_varFile.close();
		out._varFile.close();
		delete [] buf;
		return;
	}

	// Passed all the tests, proceed to sampling
	string curLine;

	uint64_t nloc = n; // local copy of n
	uint64_t S;
	RanDraw *snpSamp;  // so that I can catch RanDraw constructor exceptions
	try {
		snpSamp = new RanDraw();
	} catch (string error) {
		cerr << "ERROR: " << error << endl;
		exit(5);
	}
	while (nloc) {
		try {
			S = snpSamp->vitter(nloc, N); // sample the number of SNPs to skip
		} catch (string error) {
			cerr << "ERROR: " << error << endl;
			exit(5);
		}
		N -= S + 1;
		while (S) { // skipping S lines in the vcf file
			_varFile.ignore(numeric_limits<streamsize>::max(), '\n');
 			S--;
		}
		getline(_varFile, curLine);
		out._varFile << curLine << endl;
		nloc--;

	}
	_varFile.close();
	out._varFile.close();
	delete snpSamp;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
cmpq $0x0, (%rdx)
je 0x14da5
movq %rsi, %rbx
movq %rdi, %r13
movq %rdx, 0x28(%rsp)
leaq 0x88(%rsi), %r15
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x14bdc
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %r13, %rdi
callq 0x1473a
movq %rax, %rbp
leaq 0x8(%r13), %r14
movq 0x218(%r13), %rsi
movq %r14, %rdi
movl $0x8, %edx
callq 0x4270
leaq 0x88(%r13), %rdi
callq 0x40a0
testb %al, %al
je 0x150a1
leaq 0x8(%rbx), %r12
movq 0x218(%rbx), %rsi
movq %r12, %rdi
movl $0x30, %edx
callq 0x4270
movq %rbx, 0x30(%rsp)
movq %r12, 0x58(%rsp)
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x150f4
movq %rbp, %rbx
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
addq $0x18, 0x30(%rsp)
leaq 0x8(%rsp), %r15
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x4500
movq 0x8(%rsp), %rsi
cmpb $0x23, (%rsi)
jne 0x14cd2
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %rdi
callq 0x42f0
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %esi
movq %rbp, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41b0
jmp 0x14c6b
movq %r14, %rdi
callq 0x44c0
movq %rax, %r15
movq 0x10(%rsp), %rsi
cmpq %rsi, %rax
jb 0x15154
movq %rbx, %rbp
notq %rsi
addq %r15, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x4360
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r15
movq (%r15), %r12
cmpq %r12, %rbp
jae 0x14df8
movq 0xb2d5(%rip), %rdi      # 0x1fff0
leaq 0x38d4(%rip), %rsi      # 0x185f6
movl $0x1d, %edx
leaq 0x18(%rsp), %r12
callq 0x42f0
movq (%r15), %rsi
movq 0xb2b5(%rip), %rdi      # 0x1fff0
callq 0x41a0
movq %rax, %rbx
leaq 0x3bae(%rip), %rsi      # 0x188f8
movl $0x2f, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x38d8(%rip), %rsi      # 0x18644
movl $0x14, %edx
movq %rax, %rdi
callq 0x42f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41b0
jmp 0x15078
movq 0xb244(%rip), %rbx      # 0x1fff0
leaq 0x3811(%rip), %rsi      # 0x185c4
movl $0x31, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x41b0
jne 0x14ecc
movq 0xb1eb(%rip), %rdi      # 0x1fff0
leaq 0x384d(%rip), %rsi      # 0x18659
movl $0x16, %edx
leaq 0x18(%rsp), %r12
callq 0x42f0
movq (%r15), %rsi
movq 0xb1cb(%rip), %rdi      # 0x1fff0
callq 0x41a0
movq %rax, %r15
leaq 0x3af4(%rip), %rsi      # 0x18928
movl $0x22, %edx
movq %rax, %rdi
callq 0x42f0
movq %r15, %rdi
movq %rbp, %rsi
callq 0x41a0
movq %rax, %r15
leaq 0x3af5(%rip), %rsi      # 0x1894b
movl $0x2e, %edx
movq %rax, %rdi
callq 0x42f0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41b0
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x4030
movq %rax, %r15
movq 0x8(%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r13,%rax)
jne 0x1505e
movl $0xa00000, %edx         # imm = 0xA00000
movq %r14, %rdi
movq %r15, %rsi
callq 0x4350
movq 0x10(%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4390
jmp 0x14e97
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl $0x8, %edi
callq 0x42c0
movq %rax, 0x28(%rsp)
movq %rax, %rdi
callq 0x172de
testq %r12, %r12
je 0x15009
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
movq %r12, %xmm0
movq 0x3589(%rip), %xmm2     # 0x184a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x358d(%rip), %xmm3     # 0x184b0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x38(%rsp)
movq %rbp, %rbx
movq %rbp, %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x175fe
movq %rax, %rbp
movq %rax, %r13
testq %rax, %rax
je 0x14f95
movq %r14, %rdi
movq %r15, %rsi
movl $0xa, %edx
callq 0x4190
decq %r13
jne 0x14f80
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
callq 0x4500
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x30(%rsp), %rdi
callq 0x42f0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41b0
notq %rbp
addq %rbp, %rbx
movq %rbx, %rbp
decq %r12
jne 0x14f0a
movq %r14, %rdi
callq 0x4060
movq 0x58(%rsp), %rdi
callq 0x4060
movq 0x28(%rsp), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
leaq 0x18(%rsp), %r12
je 0x15033
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x8, %esi
movq %rbx, %rdi
callq 0x42d0
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x15078
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x15078
movq %r14, %rdi
callq 0x4060
movq 0x58(%rsp), %rdi
callq 0x4060
movq %r15, %rdi
callq 0x4380
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x1508f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42d0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xaf48(%rip), %rdi      # 0x1fff0
leaq 0x3ce9(%rip), %rsi      # 0x18d98
movl $0x20, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0xaf22(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x348f(%rip), %rsi      # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x1514a
movq 0xaef5(%rip), %rdi      # 0x1fff0
leaq 0x3c96(%rip), %rsi      # 0x18d98
movl $0x20, %edx
movq 0x30(%rsp), %rbx
callq 0x42f0
movq 0x218(%rbx), %rsi
movq 0x220(%rbx), %rdx
movq 0xaeca(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x35ad(%rip), %rsi      # 0x186e2
movl $0xb, %edx
movq %rax, %rdi
callq 0x42f0
movq %r14, %rdi
callq 0x4130
movl $0x2, %edi
callq 0x4320
movq 0xae95(%rip), %rdi      # 0x1fff0
leaq 0x3cce(%rip), %rsi      # 0x18e30
movl $0x19, %edx
callq 0x42f0
movq 0xae7d(%rip), %rdi      # 0x1fff0
movq %r15, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x3cc5(%rip), %rsi      # 0x18e4a
movl $0x23, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x3d6d(%rip), %rsi      # 0x18f16
movl $0x1, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
movl $0x6, %edi
callq 0x4320
movq %rax, %r14
cmpl $0x3, %edx
jne 0x15532
movq %r14, %rdi
callq 0x4110
movq 0xae0d(%rip), %rdi      # 0x1fff0
leaq 0x34d7(%rip), %rsi      # 0x186c1
movl $0x20, %edx
callq 0x42f0
movq 0xadf5(%rip), %rdi      # 0x1fff0
callq 0x4130
movl $0x4, %edi
callq 0x4320
movq %rax, %r14
callq 0x4480
jmp 0x15532
movq %rdx, %rbx
movq %rax, %r14
movl $0x8, %esi
movq 0x28(%rsp), %rdi
callq 0x42d0
jmp 0x15234
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x2, %ebx
jne 0x15516
movq %r14, %rdi
callq 0x4340
leaq 0x48(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x76e4
movq %r14, %rdi
callq 0x4110
movq 0xad7f(%rip), %rdi      # 0x1fff0
leaq 0x34ad(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0xad5d(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
jmp 0x154d7
movq %rax, %r14
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x152bf
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x15516
jmp 0x15513
jmp 0x1550c
jmp 0x1550c
movq %rax, %r14
cmpl $0x1, %edx
jne 0x15553
movq %r14, %rdi
callq 0x4110
movq %rax, %r14
movq 0xacfa(%rip), %rdi      # 0x1fff0
leaq 0x3abc(%rip), %rsi      # 0x18db9
movl $0x1d, %edx
callq 0x42f0
movq 0x218(%rbx), %rsi
movq 0x220(%rbx), %rdx
movq 0xacd4(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x33c3(%rip), %rsi      # 0x186ee
movl $0xd, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movq (%rsi), %rax
leaq 0x8(%rsp), %rdi
callq *0x20(%rax)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
leaq 0x8(%rsp), %rdi
callq 0x4b07
movq %rax, %r14
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15394
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x15394
jmp 0x15391
movq %rax, %r14
callq 0x4480
jmp 0x15553
movq %rax, %r14
cmpl $0x1, %edx
jne 0x15553
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xac34(%rip), %rdi      # 0x1fff0
leaq 0x39f6(%rip), %rsi      # 0x18db9
movl $0x1d, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0xac0e(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x360c(%rip), %rsi      # 0x189fd
movl $0xc, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x8(%rsp), %rdi
callq *0x20(%rax)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
leaq 0x8(%rsp), %rdi
callq 0x4aed
movq %rax, %r14
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15459
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x15459
jmp 0x15456
movq %rax, %r14
callq 0x4480
jmp 0x15553
jmp 0x1550c
movq %rax, %r14
cmpl $0x2, %edx
jne 0x15516
movq %r14, %rdi
callq 0x4340
leaq 0x48(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x76e4
movq %r14, %rdi
callq 0x4110
movq 0xab48(%rip), %rdi      # 0x1fff0
leaq 0x3276(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0xab26(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
movl $0x5, %edi
callq 0x4320
movq %rax, %r14
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x154fb
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x15516
movq %rax, %rdi
callq 0x76d8
jmp 0x15539
movq %rax, %r14
jmp 0x15532
jmp 0x15513
movq %rax, %r14
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x15532
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42d0
leaq 0x18(%rsp), %r12
jmp 0x1553c
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x15553
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42d0
movq %r14, %rdi
callq 0x44e0
nop