// File: /workspace/github_cpp_makefile_star2_O1/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 0x11262
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r13
movq %rcx, 0x38(%rsp)
leaq 0x8(%rsi), %rax
movq %rax, 0x30(%rsp)
leaq 0x88(%rsi), %r14
movq %r14, %rdi
callq 0x40a0
testb %al, %al
je 0x11192
movq 0x30(%rsp), %rdi
callq 0x4060
movq (%r13), %rax
movq %r13, %rdi
callq *0x10(%rax)
movq %rax, %r12
leaq 0x8(%r13), %r15
movq 0x218(%r13), %rsi
movq %r15, %rdi
movl $0x8, %edx
callq 0x4270
leaq 0x88(%r13), %rdi
callq 0x40a0
testb %al, %al
je 0x116ca
movq %rbx, 0x8(%rsp)
movq 0x218(%rbx), %rsi
movq 0x30(%rsp), %rdi
movl $0x30, %edx
callq 0x4270
movq %r14, %rdi
callq 0x40a0
testb %al, %al
je 0x1171d
cmpq (%rbp), %r12
movq 0x8(%rsp), %rbx
jae 0x112b2
movq 0xede4(%rip), %rbx      # 0x1fff0
leaq 0x73e3(%rip), %rsi      # 0x185f6
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x73de(%rip), %rsi      # 0x18614
movl $0x2f, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x73ec(%rip), %rsi      # 0x18644
movl $0x14, %edx
movq %rax, %rdi
jmp 0x11278
movq 0xed87(%rip), %rbx      # 0x1fff0
leaq 0x799d(%rip), %rsi      # 0x18c0d
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 0x11389
movq 0xed31(%rip), %rbx      # 0x1fff0
leaq 0x7393(%rip), %rsi      # 0x18659
movl $0x16, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x7387(%rip), %rsi      # 0x18670
movl $0x22, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x7388(%rip), %rsi      # 0x18693
movl $0x2d, %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, %r12
cmpb $0x0, 0x238(%r13)
movq 0x8(%rsp), %rbx
je 0x1165e
movq 0x38(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x115c8
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movq %r15, %rdi
movl $0xa, %edx
callq 0x4190
jmp 0x1165e
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpb $0x1, 0x238(%r13)
jne 0x11440
movq 0x38(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x113ce
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movq %r15, %rdi
movl $0xa, %edx
callq 0x4190
jmp 0x11440
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x4500
leaq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x42f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41b0
movq $0x0, 0x18(%rsp)
movq 0x10(%rsp), %rax
movb $0x0, (%rax)
movq (%rbp), %r13
movl $0x8, %edi
callq 0x42c0
movq %rax, %r14
movq %rax, %rdi
callq 0x174a0
testq %r13, %r13
je 0x11576
addq $0x18, 0x8(%rsp)
movabsq $0x7fffffffffffffff, %rbp # imm = 0x7FFFFFFFFFFFFFFF
movq %r13, %xmm0
movq 0x7021(%rip), %xmm2     # 0x184a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x7025(%rip), %xmm3     # 0x184b0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
movq %r12, 0x38(%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 %r14, %r12
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x177c4
movq %rax, %r14
movq %rax, %rbx
testq %rax, %rax
je 0x114fa
movq %r15, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x4190
decq %rbx
jne 0x114e5
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x4500
movq 0x10(%rsp), %rsi
movq 0x18(%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 %r14
movq 0x38(%rsp), %rax
addq %r14, %rax
decq %r13
movq %r12, %r14
movq %rax, %r12
jne 0x11472
movq %r15, %rdi
callq 0x4060
movq 0x30(%rsp), %rdi
callq 0x4060
movq (%r14), %rdi
testq %rdi, %rdi
je 0x11596
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x8, %esi
movq %r14, %rdi
callq 0x42d0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x116bb
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x116bb
leaq 0x20(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %edx
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x4500
movq 0x8(%rsp), %rax
leaq 0x18(%rax), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x42f0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41b0
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0x11659
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
movq 0x8(%rsp), %rbx
movq 0x8(%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r13,%rax)
jne 0x116a1
addq $0x18, %rbx
movl $0xa00000, %edx         # imm = 0xA00000
movq %r15, %rdi
movq %r12, %rsi
callq 0x4350
movq 0x10(%r13), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4390
movq 0x8(%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r13,%rax)
je 0x11672
movq %r15, %rdi
callq 0x4060
movq 0x30(%rsp), %rdi
callq 0x4060
movq %r12, %rdi
callq 0x4380
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe91f(%rip), %rdi      # 0x1fff0
leaq 0x6e78(%rip), %rsi      # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0xe8f9(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x6e66(%rip), %rsi      # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x11773
movq 0xe8cc(%rip), %rdi      # 0x1fff0
leaq 0x6e25(%rip), %rsi      # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x8(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0xe8a1(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x6f84(%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 %r14, %rdi
movq %rax, %r14
movl $0x8, %esi
callq 0x42d0
jmp 0x11798
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x2, %ebx
jne 0x11ad3
movq %r14, %rdi
callq 0x4340
leaq 0x50(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x7720
movq %r14, %rdi
callq 0x4110
movq 0xe81b(%rip), %rdi      # 0x1fff0
leaq 0x6f49(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0xe7f9(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
jmp 0x11a9b
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x11823
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x11ad3
movq %rax, %r14
cmpl $0x3, %edx
jne 0x11aef
movq %r14, %rdi
callq 0x4110
movq 0xe7a8(%rip), %rdi      # 0x1fff0
leaq 0x6e72(%rip), %rsi      # 0x186c1
movl $0x20, %edx
callq 0x42f0
movq 0xe790(%rip), %rdi      # 0x1fff0
callq 0x4130
movl $0x4, %edi
callq 0x4320
movq %rax, %r14
callq 0x4480
jmp 0x11aef
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
jne 0x11ae2
jmp 0x11aef
jmp 0x11ad0
jmp 0x11ad0
movq %rax, %r14
cmpl $0x1, %edx
jne 0x11aef
movq %r14, %rdi
callq 0x4110
movq %rax, %r14
movq 0xe736(%rip), %rdi      # 0x1fff0
leaq 0x72f2(%rip), %rsi      # 0x18bb3
movl $0x1d, %edx
callq 0x42f0
movq 0x8(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0xe70b(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x6dfa(%rip), %rsi      # 0x186ee
movl $0xd, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movq (%rsi), %rax
leaq 0x10(%rsp), %rdi
callq *0x20(%rax)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
leaq 0x10(%rsp), %rdi
callq 0x48e5
movq %rax, %r14
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1195d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x1195d
jmp 0x1195a
movq %rax, %r14
callq 0x4480
jmp 0x11aef
movq %rax, %r14
cmpl $0x1, %edx
jne 0x11aef
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xe66b(%rip), %rdi      # 0x1fff0
leaq 0x7227(%rip), %rsi      # 0x18bb3
movl $0x1d, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0xe645(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x7043(%rip), %rsi      # 0x189fd
movl $0xc, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x10(%rsp), %rdi
callq *0x20(%rax)
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
leaq 0x10(%rsp), %rdi
callq 0x48cb
movq %rax, %r14
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11a22
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x11a22
jmp 0x11a1f
movq %rax, %r14
callq 0x4480
jmp 0x11aef
movq %rax, %r14
cmpl $0x2, %edx
jne 0x11ad3
movq %r14, %rdi
callq 0x4340
leaq 0x50(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x7720
movq %r14, %rdi
callq 0x4110
movq 0xe584(%rip), %rdi      # 0x1fff0
leaq 0x6cb2(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0xe562(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
movl $0x5, %edi
callq 0x4320
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x11abf
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x11ad3
movq %rax, %rdi
callq 0x7714
jmp 0x11ad0
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x11aef
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
movq %r14, %rdi
callq 0x44e0
nop