// File: /workspace/github_cpp_makefile_star2_O2/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 0x10be8
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r13
leaq 0x88(%rsi), %r15
movq %r15, %rdi
callq 0x40b0
testb %al, %al
je 0x10aa0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %r13, %rdi
callq 0x106c0
movq %rax, 0x10(%rsp)
leaq 0x8(%r13), %r14
leaq 0x218(%r13), %rbp
movq 0x218(%r13), %rsi
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x4280
leaq 0x88(%r13), %rdi
callq 0x40b0
testb %al, %al
je 0x10e26
leaq 0x8(%rbx), %rbp
leaq 0x218(%rbx), %rax
movq %rax, (%rsp)
movq 0x218(%rbx), %rsi
pushq $0x30
popq %rdx
movq %rbp, %rdi
callq 0x4280
movq %r15, %rdi
callq 0x40b0
testb %al, %al
je 0x10e5d
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
addq $0x18, %rbx
leaq 0x18(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x4060
movq 0x18(%rsp), %rax
cmpb $0x23, (%rax)
jne 0x10b53
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0x10b29
movq %r14, %rdi
callq 0x44b0
movq %rax, %r15
movq 0x20(%rsp), %rsi
cmpq %rsi, %rax
jb 0x10e9a
movq %rbp, 0x38(%rsp)
notq %rsi
addq %r15, %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x4350
movq (%r12), %rbp
movq 0x10(%rsp), %r15
cmpq %rbp, %r15
jae 0x10c14
movq 0xb456(%rip), %rdi      # 0x1bff0
leaq 0x3a55(%rip), %rsi      # 0x145f6
callq 0x42a0
movq (%r12), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0x3d3f(%rip), %rsi      # 0x148f8
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
leaq 0x3a71(%rip), %rsi      # 0x14644
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0x10e0a
movq 0xb401(%rip), %rdi      # 0x1bff0
leaq 0x39ce(%rip), %rsi      # 0x145c4
callq 0x42a0
movq %rax, %rdi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4140
jne 0x10cb1
movq 0xb3cf(%rip), %rdi      # 0x1bff0
leaq 0x3a31(%rip), %rsi      # 0x14659
callq 0x42a0
movq (%r12), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0x3ce8(%rip), %rsi      # 0x14928
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
leaq 0x3cf1(%rip), %rsi      # 0x1494b
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x4030
movq 0x38(%rsp), %r12
movq %rax, %r15
movq 0x8(%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r13,%rax)
jne 0x10db6
movl $0xa00000, %edx         # imm = 0xA00000
movq %r14, %rdi
movq %r15, %rsi
callq 0x4340
movq 0x10(%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4390
jmp 0x10c7c
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
pushq $0x8
popq %rdi
callq 0x42c0
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x12bf2
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %r13
movq %rbp, %rax
subq $0x1, %rax
movq %rax, (%rsp)
jb 0x10dd0
movq %rbx, %r12
movq %rbp, %xmm0
movq 0x379a(%rip), %xmm2     # 0x144a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x379e(%rip), %xmm3     # 0x144b0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x48(%rsp)
movq 0x10(%rsp), %rbx
movq %rbx, %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, 0x40(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x12edc
movq %rax, %rbp
notq %rax
addq %rax, %rbx
movq %rbx, 0x10(%rsp)
movq %r12, %rbx
leaq 0x68(%rsp), %r12
subq $0x1, %rbp
jb 0x10d8c
movq %r14, %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x41a0
jmp 0x10d76
movq %r14, %rdi
movq %r12, %rsi
callq 0x4060
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4270
movq %rax, %rdi
callq 0x4140
movq (%rsp), %rax
movq %rax, %rbp
jmp 0x10ce8
movq %r14, %rdi
callq 0x4050
movq %r12, %rdi
callq 0x4050
movq %r15, %rdi
callq 0x4370
jmp 0x10e0a
movq %r14, %rdi
callq 0x4050
movq 0x38(%rsp), %rdi
callq 0x4050
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x10df5
movq (%rdi), %rax
callq *0x8(%rax)
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x42d0
leaq 0x68(%rsp), %rdi
callq 0x4558
leaq 0x18(%rsp), %rdi
callq 0x4558
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb1c3(%rip), %rdi      # 0x1bff0
leaq 0x3f64(%rip), %rsi      # 0x14d98
callq 0x42a0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x4270
leaq 0x3721(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0x10e93
movq 0xb18c(%rip), %rdi      # 0x1bff0
leaq 0x3f2d(%rip), %rsi      # 0x14d98
callq 0x42a0
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x4270
leaq 0x385f(%rip), %rsi      # 0x146e2
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x2
jmp 0x11155
movq 0xb14f(%rip), %rdi      # 0x1bff0
leaq 0x3f88(%rip), %rsi      # 0x14e30
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
leaq 0x3f8b(%rip), %rsi      # 0x14e4a
movq %rax, %rdi
callq 0x42a0
movq 0x20(%rsp), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0x403b(%rip), %rsi      # 0x14f16
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x6
jmp 0x11155
movq %rax, %rbx
cmpl $0x3, %edx
jne 0x1118d
movq %rbx, %rdi
callq 0x4110
movq 0xb0e3(%rip), %rdi      # 0x1bff0
leaq 0x37ad(%rip), %rsi      # 0x146c1
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x4
jmp 0x11155
movq %rax, %rbx
callq 0x4470
jmp 0x1118d
movq %rdx, %r14
movq %rax, %rbx
pushq $0x8
popq %rsi
movq 0x8(%rsp), %rdi
callq 0x42d0
jmp 0x10f50
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x2, %r14d
jne 0x11183
movq %rbx, %rdi
callq 0x4330
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xb072(%rip), %rdi      # 0x1bff0
leaq 0x37a0(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0x11153
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0x11183
jmp 0x11180
jmp 0x1117b
jmp 0x1117b
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x11197
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xb008(%rip), %rdi      # 0x1bff0
leaq 0x3dca(%rip), %rsi      # 0x14db9
callq 0x42a0
movq %rax, %rdi
movq (%rsp), %rsi
callq 0x4270
leaq 0x36e7(%rip), %rsi      # 0x146ee
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x18(%rsp), %rdi
callq *0x20(%rax)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
jmp 0x110ca
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4558
jmp 0x11052
jmp 0x1104f
movq %rax, %rbx
callq 0x4470
jmp 0x11197
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x11197
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xaf76(%rip), %rdi      # 0x1bff0
leaq 0x3d38(%rip), %rsi      # 0x14db9
callq 0x42a0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x4270
leaq 0x3965(%rip), %rsi      # 0x149fd
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x18(%rsp), %rdi
callq *0x20(%rax)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
leaq 0x18(%rsp), %rdi
callq 0x4558
leaq 0x3737(%rip), %rdi      # 0x14812
callq 0x4250
pushq $0x1
jmp 0x11155
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4558
jmp 0x110f8
jmp 0x110f5
movq %rax, %rbx
callq 0x4470
jmp 0x11197
jmp 0x1117b
jmp 0x1117b
movq %rax, %rbx
cmpl $0x2, %edx
jne 0x11183
movq %rbx, %rdi
callq 0x4330
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xaebe(%rip), %rdi      # 0x1bff0
leaq 0x35ec(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
pushq $0x5
popq %rdi
callq 0x4310
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0x11183
movq %rax, %rdi
callq 0x6194
jmp 0x1117b
jmp 0x11180
movq %rax, %rbx
jmp 0x1118d
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x4558
leaq 0x18(%rsp), %rdi
callq 0x4558
movq %rbx, %rdi
callq 0x44d0
nop