// File: /workspace/github_cpp_makefile_star2_O2/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::GtxtFileI::sample(sampFiles::GtxtFileO&, unsigned long const&, bool const&)
void GtxtFileI::sample(GtxtFileO &out, const uint64_t &n, const bool &headSkip){
    if (n == 0) {
        cerr << "WARNING: zero rows requested. Nothing to be done." << endl;
        return;
    }
    // start by figuring out the number of rows in the file
    if (out._varFile.is_open()) {
        out._varFile.close();
    }

    uint64_t N = _numLines(); // Number of rows

    try {
        _varFile.open(_fileName.c_str(), ios::in);
		if (!_varFile.is_open()) {
			cerr << "ERROR: failed to open file " << _fileName << " for input" << endl;
			exit(2);
		}

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

    } catch (system_error &error) {
        cerr << "ERROR: cannot open text 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 << " rows that is greater than the number of rows (" << N << ") in the input file." << endl;
        return;
    } else if (N == n) {
        cerr << "WARNING: sample size (" << n << ") the same as the number of rows (" << N << ") in the input file. Simply copying the file." << 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);
        }
        if (_head) {
            if (headSkip) {
                _varFile.ignore(numeric_limits<streamsize>::max(), '\n');
            } else {
                string header;
                getline(_varFile, header);
                out._varFile << header << endl;
            }
        }

        // Copy the file
        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;
    if (_head) {
        if (headSkip) {
            _varFile.ignore(numeric_limits<streamsize>::max(), '\n');
        } else {
            getline(_varFile, curLine);
            out._varFile << curLine << endl;
            curLine.erase();
        }
    }

    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 0xdda6
movq %rdx, %rbp
movq %rdi, %r13
movq %rcx, 0x10(%rsp)
leaq 0x8(%rsi), %rax
movq %rax, 0x38(%rsp)
movq %rsi, %r14
leaq 0x88(%rsi), %rbx
movq %rbx, %rdi
callq 0x40b0
testb %al, %al
je 0xdce8
movq 0x38(%rsp), %rdi
callq 0x4050
movq (%r13), %rax
movq %r13, %rdi
callq *0x10(%rax)
movq %rax, %r12
leaq 0x8(%r13), %r15
leaq 0x218(%r13), %rax
movq %rax, 0x8(%rsp)
movq 0x218(%r13), %rsi
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x4280
leaq 0x88(%r13), %rdi
callq 0x40b0
testb %al, %al
je 0xe09e
leaq 0x218(%r14), %rcx
movq %rcx, 0x8(%rsp)
movq 0x218(%r14), %rsi
pushq $0x30
popq %rdx
movq 0x38(%rsp), %rdi
callq 0x4280
movq %rbx, %rdi
callq 0x40b0
testb %al, %al
je 0xe0d7
cmpq (%rbp), %r12
jae 0xddcf
movq 0xe288(%rip), %rdi      # 0x1bff0
leaq 0x6887(%rip), %rsi      # 0x145f6
callq 0x42a0
movq (%rbp), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0x688d(%rip), %rsi      # 0x14614
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x41b0
leaq 0x68a3(%rip), %rsi      # 0x14644
movq %rax, %rdi
jmp 0xddb4
movq 0xe243(%rip), %rdi      # 0x1bff0
leaq 0x6e59(%rip), %rsi      # 0x14c0d
callq 0x42a0
movq %rax, %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4140
movq %r14, %rbx
jne 0xde6b
movq 0xe211(%rip), %rdi      # 0x1bff0
leaq 0x6873(%rip), %rsi      # 0x14659
callq 0x42a0
movq (%rbp), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0x6872(%rip), %rsi      # 0x14670
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x41b0
leaq 0x687b(%rip), %rsi      # 0x14693
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x4030
movq %rax, %r12
cmpb $0x0, 0x238(%r13)
je 0xe040
movq 0x10(%rsp), %rax
cmpb $0x1, (%rax)
jne 0xe002
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %rdx
movq %r15, %rdi
callq 0x41a0
jmp 0xe040
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpb $0x1, 0x238(%r13)
jne 0xded9
movq 0x10(%rsp), %rax
cmpb $0x1, (%rax)
jne 0xdea7
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %rdx
movq %r15, %rdi
callq 0x41a0
jmp 0xded9
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x4060
leaq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
callq 0x4140
leaq 0x18(%rsp), %rdi
pushq $-0x1
popq %rdx
xorl %esi, %esi
callq 0x40a0
movq (%rbp), %r14
pushq $0x8
popq %rdi
callq 0x42c0
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x12bf2
addq $0x18, %rbx
movq %rbx, 0x8(%rsp)
movabsq $0x7fffffffffffffff, %rbp # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %r13
movq %r14, %rbx
subq $0x1, %rbx
jb 0xdfc3
movq %r14, %xmm0
movq 0x657d(%rip), %xmm2     # 0x144a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x6581(%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 %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, 0x40(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x12edc
movq %rax, %r14
notq %rax
addq %rax, %r12
subq $0x1, %r14
jb 0xdf97
movq %r15, %rdi
movq %rbp, %rsi
movl %r13d, %edx
callq 0x41a0
jmp 0xdf81
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
callq 0x4060
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
callq 0x4140
movq %rbx, %r14
jmp 0xdf0c
movq %r15, %rdi
callq 0x4050
movq 0x38(%rsp), %rdi
callq 0x4050
movq 0x10(%rsp), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0xdfe8
movq (%rdi), %rax
callq *0x8(%rax)
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x42d0
leaq 0x18(%rsp), %rdi
callq 0x4558
jmp 0xe08f
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x4060
leaq 0x18(%rbx), %rdi
leaq 0x18(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
callq 0x4140
leaq 0x18(%rsp), %rdi
callq 0x4558
addq $0x18, %rbx
movq 0x8(%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r13,%rax)
jne 0xe075
movl $0xa00000, %edx         # imm = 0xA00000
movq %r15, %rdi
movq %r12, %rsi
callq 0x4340
movq 0x10(%r13), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4390
jmp 0xe044
movq %r15, %rdi
callq 0x4050
movq 0x38(%rsp), %rdi
callq 0x4050
movq %r12, %rdi
callq 0x4370
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xdf4b(%rip), %rdi      # 0x1bff0
leaq 0x64a4(%rip), %rsi      # 0x14550
callq 0x42a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x4270
leaq 0x64a7(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xe10e
movq 0xdf12(%rip), %rdi      # 0x1bff0
leaq 0x646b(%rip), %rsi      # 0x14550
callq 0x42a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x4270
leaq 0x65e4(%rip), %rsi      # 0x146e2
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x2
jmp 0xe377
jmp 0xe39b
movq %rdx, %r14
movq %rax, %rbx
pushq $0x8
popq %rsi
movq 0x10(%rsp), %rdi
callq 0x42d0
jmp 0xe135
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x2, %r14d
jne 0xe39e
movq %rbx, %rdi
callq 0x4330
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xde8d(%rip), %rdi      # 0x1bff0
leaq 0x65bb(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0xe375
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0xe39e
movq %rax, %rbx
cmpl $0x3, %edx
jne 0xe3a8
movq %rbx, %rdi
callq 0x4110
movq 0xde35(%rip), %rdi      # 0x1bff0
leaq 0x64ff(%rip), %rsi      # 0x146c1
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x4
jmp 0xe377
movq %rax, %rbx
callq 0x4470
jmp 0xe3a8
jmp 0xe39b
jmp 0xe39b
movq %rax, %rbx
cmpl $0x1, %edx
jne 0xe3a8
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xdde5(%rip), %rdi      # 0x1bff0
leaq 0x69a1(%rip), %rsi      # 0x14bb3
callq 0x42a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x4270
leaq 0x64c3(%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 0xe2f0
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4558
jmp 0xe276
jmp 0xe273
movq %rax, %rbx
callq 0x4470
jmp 0xe3a8
movq %rax, %rbx
cmpl $0x1, %edx
jne 0xe3a8
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xdd52(%rip), %rdi      # 0x1bff0
leaq 0x690e(%rip), %rsi      # 0x14bb3
callq 0x42a0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x4270
leaq 0x673f(%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 0x6511(%rip), %rdi      # 0x14812
callq 0x4250
pushq $0x1
jmp 0xe377
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4558
jmp 0xe31e
jmp 0xe31b
movq %rax, %rbx
callq 0x4470
jmp 0xe3a8
movq %rax, %rbx
cmpl $0x2, %edx
jne 0xe39e
movq %rbx, %rdi
callq 0x4330
leaq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xdc9c(%rip), %rdi      # 0x1bff0
leaq 0x63ca(%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 0xe39e
movq %rax, %rdi
callq 0x6194
jmp 0xe39b
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4558
movq %rbx, %rdi
callq 0x44d0