// File: /workspace/github_cpp_makefile_star2_O2/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 0x1194f
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %rbp
leaq 0x88(%rsi), %r15
movq %r15, %rdi
callq 0x40b0
testb %al, %al
je 0x11887
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %rbx, 0x10(%rsp)
movq %rbp, %rdi
callq 0x115f2
movq %rax, %r12
leaq 0x8(%rbp), %r14
leaq 0x218(%rbp), %rax
movq %rax, 0x8(%rsp)
movq 0x218(%rbp), %rsi
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x4280
leaq 0x88(%rbp), %rdi
callq 0x40b0
testb %al, %al
je 0x11bfa
movq 0x10(%rsp), %rax
leaq 0x8(%rax), %rbx
leaq 0x218(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x218(%rax), %rsi
pushq $0x30
popq %rdx
movq %rbx, %rdi
callq 0x4280
movq %r15, %rdi
callq 0x40b0
testb %al, %al
je 0x11c33
cmpq (%r13), %r12
jae 0x11978
movq 0xa6df(%rip), %rdi      # 0x1bff0
leaq 0x2cde(%rip), %rsi      # 0x145f6
callq 0x42a0
movq (%r13), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0x2fc8(%rip), %rsi      # 0x148f8
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x41b0
leaq 0x2cfa(%rip), %rsi      # 0x14644
movq %rax, %rdi
jmp 0x1195d
movq 0xa69a(%rip), %rdi      # 0x1bff0
leaq 0x2c67(%rip), %rsi      # 0x145c4
callq 0x42a0
movq %rax, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4140
movq %rbx, 0x58(%rsp)
jne 0x11a1d
movq 0xa666(%rip), %rdi      # 0x1bff0
leaq 0x2cc8(%rip), %rsi      # 0x14659
callq 0x42a0
movq (%r13), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0x2f7f(%rip), %rsi      # 0x14928
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x41b0
leaq 0x2f88(%rip), %rsi      # 0x1494b
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x4030
movq %rax, %r15
movq 0x10(%rsp), %rbx
addq $0x18, %rbx
movq 0x8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%rbp,%rax)
jne 0x11b89
movl $0xa00000, %edx         # imm = 0xA00000
movq %r14, %rdi
movq %r15, %rsi
callq 0x4340
movq 0x10(%rbp), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4390
jmp 0x119e9
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x5(%rsp), %rsi
pushq $0x3
popq %rdx
movq %r14, %rdi
callq 0x4340
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4350
cmpb $0x72, 0x5(%rsp)
jne 0x11aa2
cmpb $0x73, 0x6(%rsp)
jne 0x11aa2
cmpb $0x23, 0x7(%rsp)
jne 0x11aa2
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4060
movq 0x10(%rsp), %rax
leaq 0x18(%rax), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
callq 0x4140
leaq 0x18(%rsp), %rdi
callq 0x4558
movq (%r13), %rbx
pushq $0x8
popq %rdi
callq 0x42c0
movq %rax, 0x8(%rsp)
movq %rax, %rdi
callq 0x12bf2
addq $0x18, 0x10(%rsp)
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %r13
movq %rbx, %rbp
subq $0x1, %rbp
jb 0x11bb1
movq %rbx, %xmm0
movq 0x29b7(%rip), %xmm2     # 0x144a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x29bb(%rip), %xmm3     # 0x144b0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%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 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x12edc
movq %rax, %rbx
notq %rax
addq %rax, %r12
subq $0x1, %rbx
jb 0x11b5d
movq %r14, %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x41a0
jmp 0x11b47
movq %r14, %rdi
leaq 0x38(%rsp), %rsi
callq 0x4060
movq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
callq 0x4140
movq %rbp, %rbx
jmp 0x11ad2
movq %r14, %rdi
callq 0x4050
movq 0x58(%rsp), %rdi
callq 0x4050
movq %r15, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4370
movq %r14, %rdi
callq 0x4050
movq 0x58(%rsp), %rdi
callq 0x4050
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x11bd6
movq (%rdi), %rax
callq *0x8(%rax)
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x42d0
leaq 0x38(%rsp), %rdi
callq 0x4558
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa3ef(%rip), %rdi      # 0x1bff0
leaq 0x3266(%rip), %rsi      # 0x14e6e
callq 0x42a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x4270
leaq 0x294b(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0x11c6a
movq 0xa3b6(%rip), %rdi      # 0x1bff0
leaq 0x322d(%rip), %rsi      # 0x14e6e
callq 0x42a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x4270
leaq 0x2a88(%rip), %rsi      # 0x146e2
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x2
jmp 0x11ee0
movq %rdx, %r14
movq %rax, %rbx
pushq $0x8
popq %rsi
movq 0x8(%rsp), %rdi
callq 0x42d0
jmp 0x11c8c
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x2, %r14d
jne 0x11f07
movq %rbx, %rdi
callq 0x4330
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xa336(%rip), %rdi      # 0x1bff0
leaq 0x2a64(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0x11ede
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0x11f07
movq %rax, %rbx
cmpl $0x3, %edx
jne 0x11f11
movq %rbx, %rdi
callq 0x4110
movq 0xa2de(%rip), %rdi      # 0x1bff0
leaq 0x29a8(%rip), %rsi      # 0x146c1
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x4
jmp 0x11ee0
movq %rax, %rbx
callq 0x4470
jmp 0x11f11
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4558
jmp 0x11f07
jmp 0x11f04
jmp 0x11f04
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x11f11
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xa27c(%rip), %rdi      # 0x1bff0
leaq 0x3113(%rip), %rsi      # 0x14e8e
callq 0x42a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x4270
leaq 0x295a(%rip), %rsi      # 0x146ee
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x38(%rsp), %rdi
callq *0x20(%rax)
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
jmp 0x11e59
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x4558
jmp 0x11ddf
jmp 0x11ddc
movq %rax, %rbx
callq 0x4470
jmp 0x11f11
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x11f11
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xa1e9(%rip), %rdi      # 0x1bff0
leaq 0x3080(%rip), %rsi      # 0x14e8e
callq 0x42a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x4270
leaq 0x2bd6(%rip), %rsi      # 0x149fd
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x38(%rsp), %rdi
callq *0x20(%rax)
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
leaq 0x38(%rsp), %rdi
callq 0x4558
leaq 0x29a8(%rip), %rdi      # 0x14812
callq 0x4250
pushq $0x1
jmp 0x11ee0
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x4558
jmp 0x11e87
jmp 0x11e84
movq %rax, %rbx
callq 0x4470
jmp 0x11f11
movq %rax, %rbx
cmpl $0x2, %edx
jne 0x11f07
movq %rbx, %rdi
callq 0x4330
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xa133(%rip), %rdi      # 0x1bff0
leaq 0x2861(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
pushq $0x5
popq %rdi
callq 0x4310
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0x11f07
movq %rax, %rdi
callq 0x6194
jmp 0x11f04
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x4558
movq %rbx, %rdi
callq 0x44d0
nop