// File: /workspace/github_cpp_makefile_star2_O1/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 0x15fdc
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r13
leaq 0x88(%rsi), %r15
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x15f0f
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %r13, %rdi
callq 0x15bf2
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 0x163a9
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 0x163fc
cmpq (%rbp), %r12
jae 0x1602c
movq 0xa06a(%rip), %rbx      # 0x1fff0
leaq 0x2669(%rip), %rsi      # 0x185f6
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x2948(%rip), %rsi      # 0x188f8
movl $0x2f, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x2672(%rip), %rsi      # 0x18644
movl $0x14, %edx
movq %rax, %rdi
jmp 0x15ff2
movq 0xa00d(%rip), %rbx      # 0x1fff0
leaq 0x25da(%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 0x16136
movq 0x9fb7(%rip), %rbx      # 0x1fff0
leaq 0x2619(%rip), %rsi      # 0x18659
movl $0x16, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x28c5(%rip), %rsi      # 0x18928
movl $0x22, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x28c6(%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 0x1610e
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 0x160df
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 0x1621a
cmpb $0x73, 0x6(%rsp)
jne 0x1621a
cmpb $0x23, 0x7(%rsp)
jne 0x1621a
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 0x1621a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
movq (%rbp), %r13
movl $0x8, %edi
callq 0x42c0
movq %rax, %rbp
movq %rax, %rdi
callq 0x174a0
testq %r13, %r13
je 0x16350
addq $0x18, 0x8(%rsp)
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
movq %r13, %xmm0
movq 0x2247(%rip), %xmm2     # 0x184a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x224b(%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 0x177c4
movq %rax, %rbp
movq %rax, %rbx
testq %rax, %rax
je 0x162d4
movq %r14, %rdi
movq %r15, %rsi
movl $0xa, %edx
callq 0x4190
decq %rbx
jne 0x162bf
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 0x1624c
movq %r14, %rdi
callq 0x4060
movq 0x50(%rsp), %rdi
callq 0x4060
movq (%rbp), %rdi
testq %rdi, %rdi
leaq 0x40(%rsp), %rbx
je 0x16376
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x8, %esi
movq %rbp, %rdi
callq 0x42d0
movq 0x30(%rsp), %rdi
cmpq %rbx, %rdi
je 0x1639a
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 0x9c40(%rip), %rdi      # 0x1fff0
leaq 0x2ab7(%rip), %rsi      # 0x18e6e
movl $0x1f, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0x9c1a(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x2187(%rip), %rsi      # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x16452
movq 0x9bed(%rip), %rdi      # 0x1fff0
leaq 0x2a64(%rip), %rsi      # 0x18e6e
movl $0x1f, %edx
callq 0x42f0
movq 0x8(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0x9bc2(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x22a5(%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 0x16477
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x2, %ebx
jne 0x167bf
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 0x7720
movq %r14, %rdi
callq 0x4110
movq 0x9b3c(%rip), %rdi      # 0x1fff0
leaq 0x226a(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x9b1a(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
jmp 0x16787
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x16502
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x167bf
movq %rax, %r14
cmpl $0x3, %edx
jne 0x167db
movq %r14, %rdi
callq 0x4110
movq 0x9ac9(%rip), %rdi      # 0x1fff0
leaq 0x2193(%rip), %rsi      # 0x186c1
movl $0x20, %edx
callq 0x42f0
movq 0x9ab1(%rip), %rdi      # 0x1fff0
callq 0x4130
movl $0x4, %edi
callq 0x4320
movq %rax, %r14
callq 0x4480
jmp 0x167db
jmp 0x167bc
jmp 0x167bc
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x167bf
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x167bf
movq %rax, %r14
cmpl $0x1, %edx
jne 0x167db
movq %r14, %rdi
callq 0x4110
movq %rax, %r14
movq 0x9a4a(%rip), %rdi      # 0x1fff0
leaq 0x28e1(%rip), %rsi      # 0x18e8e
movl $0x1c, %edx
callq 0x42f0
movq 0x8(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0x9a1f(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x210e(%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 0x16649
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x16649
jmp 0x16646
movq %rax, %r14
callq 0x4480
jmp 0x167db
movq %rax, %r14
cmpl $0x1, %edx
jne 0x167db
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x997f(%rip), %rdi      # 0x1fff0
leaq 0x2816(%rip), %rsi      # 0x18e8e
movl $0x1c, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0x9959(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x2357(%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 0x1670e
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x1670e
jmp 0x1670b
movq %rax, %r14
callq 0x4480
jmp 0x167db
movq %rax, %r14
cmpl $0x2, %edx
jne 0x167bf
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 0x7720
movq %r14, %rdi
callq 0x4110
movq 0x9898(%rip), %rdi      # 0x1fff0
leaq 0x1fc6(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x9876(%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 0x167ab
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x167bf
movq %rax, %rdi
callq 0x7714
jmp 0x167bc
movq %rax, %r14
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x167db
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x42d0
movq %r14, %rdi
callq 0x44e0
nop