// File: /workspace/github_cpp_makefile_star2_O1/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 0x14f4f
movq %rsi, %rbx
movq %rdi, %r13
movq %rdx, 0x28(%rsp)
leaq 0x88(%rsi), %r15
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x14d86
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %r13, %rdi
callq 0x148e4
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 0x1524b
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 0x1529e
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 0x14e7c
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 0x14e15
movq %r14, %rdi
callq 0x44c0
movq %rax, %r15
movq 0x10(%rsp), %rsi
cmpq %rsi, %rax
jb 0x152fe
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 0x14fa2
movq 0xb12b(%rip), %rdi      # 0x1fff0
leaq 0x372a(%rip), %rsi      # 0x185f6
movl $0x1d, %edx
leaq 0x18(%rsp), %r12
callq 0x42f0
movq (%r15), %rsi
movq 0xb10b(%rip), %rdi      # 0x1fff0
callq 0x41a0
movq %rax, %rbx
leaq 0x3a04(%rip), %rsi      # 0x188f8
movl $0x2f, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x372e(%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 0x15222
movq 0xb09a(%rip), %rbx      # 0x1fff0
leaq 0x3667(%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 0x15076
movq 0xb041(%rip), %rdi      # 0x1fff0
leaq 0x36a3(%rip), %rsi      # 0x18659
movl $0x16, %edx
leaq 0x18(%rsp), %r12
callq 0x42f0
movq (%r15), %rsi
movq 0xb021(%rip), %rdi      # 0x1fff0
callq 0x41a0
movq %rax, %r15
leaq 0x394a(%rip), %rsi      # 0x18928
movl $0x22, %edx
movq %rax, %rdi
callq 0x42f0
movq %r15, %rdi
movq %rbp, %rsi
callq 0x41a0
movq %rax, %r15
leaq 0x394b(%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 0x15208
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 0x15041
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 0x174a0
testq %r12, %r12
je 0x151b3
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
movq %r12, %xmm0
movq 0x33df(%rip), %xmm2     # 0x184a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x33e3(%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 0x177c4
movq %rax, %rbp
movq %rax, %r13
testq %rax, %rax
je 0x1513f
movq %r14, %rdi
movq %r15, %rsi
movl $0xa, %edx
callq 0x4190
decq %r13
jne 0x1512a
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 0x150b4
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 0x151dd
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 0x15222
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x15222
movq %r14, %rdi
callq 0x4060
movq 0x58(%rsp), %rdi
callq 0x4060
movq %r15, %rdi
callq 0x4380
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x15239
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 0xad9e(%rip), %rdi      # 0x1fff0
leaq 0x3b3f(%rip), %rsi      # 0x18d98
movl $0x20, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0xad78(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x32e5(%rip), %rsi      # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x152f4
movq 0xad4b(%rip), %rdi      # 0x1fff0
leaq 0x3aec(%rip), %rsi      # 0x18d98
movl $0x20, %edx
movq 0x30(%rsp), %rbx
callq 0x42f0
movq 0x218(%rbx), %rsi
movq 0x220(%rbx), %rdx
movq 0xad20(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x3403(%rip), %rsi      # 0x186e2
movl $0xb, %edx
movq %rax, %rdi
callq 0x42f0
movq %r14, %rdi
callq 0x4130
movl $0x2, %edi
callq 0x4320
movq 0xaceb(%rip), %rdi      # 0x1fff0
leaq 0x3b24(%rip), %rsi      # 0x18e30
movl $0x19, %edx
callq 0x42f0
movq 0xacd3(%rip), %rdi      # 0x1fff0
movq %r15, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x3b1b(%rip), %rsi      # 0x18e4a
movl $0x23, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x3bc3(%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 0x156dc
movq %r14, %rdi
callq 0x4110
movq 0xac63(%rip), %rdi      # 0x1fff0
leaq 0x332d(%rip), %rsi      # 0x186c1
movl $0x20, %edx
callq 0x42f0
movq 0xac4b(%rip), %rdi      # 0x1fff0
callq 0x4130
movl $0x4, %edi
callq 0x4320
movq %rax, %r14
callq 0x4480
jmp 0x156dc
movq %rdx, %rbx
movq %rax, %r14
movl $0x8, %esi
movq 0x28(%rsp), %rdi
callq 0x42d0
jmp 0x153de
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x2, %ebx
jne 0x156c0
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 0x7720
movq %r14, %rdi
callq 0x4110
movq 0xabd5(%rip), %rdi      # 0x1fff0
leaq 0x3303(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0xabb3(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
jmp 0x15681
movq %rax, %r14
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x15469
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x156c0
jmp 0x156bd
jmp 0x156b6
jmp 0x156b6
movq %rax, %r14
cmpl $0x1, %edx
jne 0x156fd
movq %r14, %rdi
callq 0x4110
movq %rax, %r14
movq 0xab50(%rip), %rdi      # 0x1fff0
leaq 0x3912(%rip), %rsi      # 0x18db9
movl $0x1d, %edx
callq 0x42f0
movq 0x218(%rbx), %rsi
movq 0x220(%rbx), %rdx
movq 0xab2a(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x3219(%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 0x1553e
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x1553e
jmp 0x1553b
movq %rax, %r14
callq 0x4480
jmp 0x156fd
movq %rax, %r14
cmpl $0x1, %edx
jne 0x156fd
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xaa8a(%rip), %rdi      # 0x1fff0
leaq 0x384c(%rip), %rsi      # 0x18db9
movl $0x1d, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0xaa64(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x3462(%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 0x15603
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x15603
jmp 0x15600
movq %rax, %r14
callq 0x4480
jmp 0x156fd
jmp 0x156b6
movq %rax, %r14
cmpl $0x2, %edx
jne 0x156c0
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 0x7720
movq %r14, %rdi
callq 0x4110
movq 0xa99e(%rip), %rdi      # 0x1fff0
leaq 0x30cc(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0xa97c(%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 0x156a5
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x156c0
movq %rax, %rdi
callq 0x7714
jmp 0x156e3
movq %rax, %r14
jmp 0x156dc
jmp 0x156bd
movq %rax, %r14
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x156dc
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42d0
leaq 0x18(%rsp), %r12
jmp 0x156e6
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x156fd
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x42d0
movq %r14, %rdi
callq 0x44e0
nop