// File: /workspace/github_cpp_makefile_star2_O3/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::HmpFileI::sample(sampFiles::HmpFileO&, unsigned long const&)
void HmpFileI::sample(HmpFileO &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 HMP file " << _fileName << " for input" << endl;
			exit(2);
		}

	} catch (system_error &error) {
		cerr << "ERROR: cannot open HMP 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 HMP file " << out._fileName << " for output" << endl;
			exit(2);
		}

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

	// 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 .hmp
		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;
	char tag[3];
	_varFile.read(tag, 3);
	_varFile.seekg(0);
	if ( (tag[0] == 'r') && (tag[1] == 's') && (tag[2] == '#') ) {
		string header;
		getline(_varFile, header);
		out._varFile << header << endl;
	}

	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 HMP 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 $0x68, %rsp
cmpq $0x0, (%rdx)
je 0x15e32
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r13
leaq 0x88(%rsi), %r15
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x15d65
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %r13, %rdi
callq 0x15a48
movq %rax, %r12
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 0x161ff
leaq 0x8(%rbx), %rdi
movq %rbx, 0x8(%rsp)
movq 0x218(%rbx), %rsi
movq %rdi, 0x50(%rsp)
movl $0x30, %edx
callq 0x4270
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x16252
cmpq (%rbp), %r12
jae 0x15e82
movq 0xa214(%rip), %rbx      # 0x1fff0
leaq 0x2813(%rip), %rsi      # 0x185f6
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x2af2(%rip), %rsi      # 0x188f8
movl $0x2f, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x281c(%rip), %rsi      # 0x18644
movl $0x14, %edx
movq %rax, %rdi
jmp 0x15e48
movq 0xa1b7(%rip), %rbx      # 0x1fff0
leaq 0x2784(%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 $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x41b0
jne 0x15f8c
movq 0xa161(%rip), %rbx      # 0x1fff0
leaq 0x27c3(%rip), %rsi      # 0x18659
movl $0x16, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x2a6f(%rip), %rsi      # 0x18928
movl $0x22, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x2a70(%rip), %rsi      # 0x1894b
movl $0x2e, %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
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x4030
movq %rax, %r15
movq 0x8(%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r13,%rax)
movq 0x8(%rsp), %rbx
jne 0x15f64
addq $0x18, %rbx
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
movq 0x8(%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r13,%rax)
je 0x15f35
movq %r14, %rdi
callq 0x4060
movq 0x50(%rsp), %rdi
callq 0x4060
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4380
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x5(%rsp), %rsi
movl $0x3, %edx
movq %r14, %rdi
callq 0x4350
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4360
cmpb $0x72, 0x5(%rsp)
jne 0x16070
cmpb $0x73, 0x6(%rsp)
jne 0x16070
cmpb $0x23, 0x7(%rsp)
jne 0x16070
leaq 0x20(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x4500
movq 0x8(%rsp), %rax
leaq 0x18(%rax), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x42f0
movq %rax, %r15
movq (%rax), %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
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x16070
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
movq (%rbp), %r13
movl $0x8, %edi
callq 0x42c0
movq %rax, %rbp
movq %rax, %rdi
callq 0x172de
testq %r13, %r13
je 0x161a6
addq $0x18, 0x8(%rsp)
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
movq %r13, %xmm0
movq 0x23f1(%rip), %xmm2     # 0x184a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x23f5(%rip), %xmm3     # 0x184b0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %r12, 0x58(%rsp)
movq %r12, %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, 0x60(%rsp)
movq %rbp, %r12
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x175fe
movq %rax, %rbp
movq %rax, %rbx
testq %rax, %rax
je 0x1612a
movq %r14, %rdi
movq %r15, %rsi
movl $0xa, %edx
callq 0x4190
decq %rbx
jne 0x16115
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x4500
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x8(%rsp), %rdi
callq 0x42f0
movq %rax, %rbx
movq (%rax), %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
notq %rbp
movq 0x58(%rsp), %rax
addq %rbp, %rax
decq %r13
movq %r12, %rbp
movq %rax, %r12
jne 0x160a2
movq %r14, %rdi
callq 0x4060
movq 0x50(%rsp), %rdi
callq 0x4060
movq (%rbp), %rdi
testq %rdi, %rdi
leaq 0x40(%rsp), %rbx
je 0x161cc
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x8, %esi
movq %rbp, %rdi
callq 0x42d0
movq 0x30(%rsp), %rdi
cmpq %rbx, %rdi
je 0x161f0
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42d0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9dea(%rip), %rdi      # 0x1fff0
leaq 0x2c61(%rip), %rsi      # 0x18e6e
movl $0x1f, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0x9dc4(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x2331(%rip), %rsi      # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x162a8
movq 0x9d97(%rip), %rdi      # 0x1fff0
leaq 0x2c0e(%rip), %rsi      # 0x18e6e
movl $0x1f, %edx
callq 0x42f0
movq 0x8(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0x9d6c(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x244f(%rip), %rsi      # 0x186e2
movl $0xb, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
movl $0x2, %edi
callq 0x4320
movq %rdx, %rbx
movq %rax, %r14
movl $0x8, %esi
movq %rbp, %rdi
callq 0x42d0
jmp 0x162cd
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x2, %ebx
jne 0x16615
movq %r14, %rdi
callq 0x4340
leaq 0x20(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x76e4
movq %r14, %rdi
callq 0x4110
movq 0x9ce6(%rip), %rdi      # 0x1fff0
leaq 0x2414(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x9cc4(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
jmp 0x165dd
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x16358
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x16615
movq %rax, %r14
cmpl $0x3, %edx
jne 0x16631
movq %r14, %rdi
callq 0x4110
movq 0x9c73(%rip), %rdi      # 0x1fff0
leaq 0x233d(%rip), %rsi      # 0x186c1
movl $0x20, %edx
callq 0x42f0
movq 0x9c5b(%rip), %rdi      # 0x1fff0
callq 0x4130
movl $0x4, %edi
callq 0x4320
movq %rax, %r14
callq 0x4480
jmp 0x16631
jmp 0x16612
jmp 0x16612
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x16615
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x16615
movq %rax, %r14
cmpl $0x1, %edx
jne 0x16631
movq %r14, %rdi
callq 0x4110
movq %rax, %r14
movq 0x9bf4(%rip), %rdi      # 0x1fff0
leaq 0x2a8b(%rip), %rsi      # 0x18e8e
movl $0x1c, %edx
callq 0x42f0
movq 0x8(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0x9bc9(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x22b8(%rip), %rsi      # 0x186ee
movl $0xd, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movq (%rsi), %rax
leaq 0x30(%rsp), %rdi
callq *0x20(%rax)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %rbx, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
leaq 0x30(%rsp), %rdi
callq 0x4ba3
movq %rax, %r14
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1649f
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x1649f
jmp 0x1649c
movq %rax, %r14
callq 0x4480
jmp 0x16631
movq %rax, %r14
cmpl $0x1, %edx
jne 0x16631
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x9b29(%rip), %rdi      # 0x1fff0
leaq 0x29c0(%rip), %rsi      # 0x18e8e
movl $0x1c, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0x9b03(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x2501(%rip), %rsi      # 0x189fd
movl $0xc, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x30(%rsp), %rdi
callq *0x20(%rax)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
leaq 0x30(%rsp), %rdi
callq 0x4b89
movq %rax, %r14
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x16564
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x16564
jmp 0x16561
movq %rax, %r14
callq 0x4480
jmp 0x16631
movq %rax, %r14
cmpl $0x2, %edx
jne 0x16615
movq %r14, %rdi
callq 0x4340
leaq 0x20(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x10(%rsp), %rdi
callq 0x76e4
movq %r14, %rdi
callq 0x4110
movq 0x9a42(%rip), %rdi      # 0x1fff0
leaq 0x2170(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x9a20(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
movl $0x5, %edi
callq 0x4320
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x16601
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x16615
movq %rax, %rdi
callq 0x76d8
jmp 0x16612
movq %rax, %r14
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x16631
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42d0
movq %r14, %rdi
callq 0x44e0
nop