// File: /workspace/github_cpp_makefile_star2_O3/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 0x110d0
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 0x11000
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 0x11538
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 0x1158b
cmpq (%rbp), %r12
movq 0x8(%rsp), %rbx
jae 0x11120
movq 0xef76(%rip), %rbx      # 0x1fff0
leaq 0x7575(%rip), %rsi      # 0x185f6
movl $0x1d, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x7570(%rip), %rsi      # 0x18614
movl $0x2f, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x757e(%rip), %rsi      # 0x18644
movl $0x14, %edx
movq %rax, %rdi
jmp 0x110e6
movq 0xef19(%rip), %rbx      # 0x1fff0
leaq 0x7b2f(%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 0x111f7
movq 0xeec3(%rip), %rbx      # 0x1fff0
leaq 0x7525(%rip), %rsi      # 0x18659
movl $0x16, %edx
movq %rbx, %rdi
callq 0x42f0
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x7519(%rip), %rsi      # 0x18670
movl $0x22, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r12, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x751a(%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 0x114cc
movq 0x38(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x11436
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movq %r15, %rdi
movl $0xa, %edx
callq 0x4190
jmp 0x114cc
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpb $0x1, 0x238(%r13)
jne 0x112ae
movq 0x38(%rsp), %rax
cmpb $0x1, (%rax)
jne 0x1123c
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movq %r15, %rdi
movl $0xa, %edx
callq 0x4190
jmp 0x112ae
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 0x172de
testq %r13, %r13
je 0x113e4
addq $0x18, 0x8(%rsp)
movabsq $0x7fffffffffffffff, %rbp # imm = 0x7FFFFFFFFFFFFFFF
movq %r13, %xmm0
movq 0x71b3(%rip), %xmm2     # 0x184a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x71b7(%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 0x175fe
movq %rax, %r14
movq %rax, %rbx
testq %rax, %rax
je 0x11368
movq %r15, %rdi
movq %rbp, %rsi
movl $0xa, %edx
callq 0x4190
decq %rbx
jne 0x11353
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 0x112e0
movq %r15, %rdi
callq 0x4060
movq 0x30(%rsp), %rdi
callq 0x4060
movq (%r14), %rdi
testq %rdi, %rdi
je 0x11404
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 0x11529
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x11529
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 0x114c7
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 0x1150f
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 0x114e0
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 0xeab1(%rip), %rdi      # 0x1fff0
leaq 0x700a(%rip), %rsi      # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0xea8b(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x6ff8(%rip), %rsi      # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x115e1
movq 0xea5e(%rip), %rdi      # 0x1fff0
leaq 0x6fb7(%rip), %rsi      # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x8(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0xea33(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x7116(%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 0x11606
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x2, %ebx
jne 0x11941
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 0x76e4
movq %r14, %rdi
callq 0x4110
movq 0xe9ad(%rip), %rdi      # 0x1fff0
leaq 0x70db(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0xe98b(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
jmp 0x11909
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x11691
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x11941
movq %rax, %r14
cmpl $0x3, %edx
jne 0x1195d
movq %r14, %rdi
callq 0x4110
movq 0xe93a(%rip), %rdi      # 0x1fff0
leaq 0x7004(%rip), %rsi      # 0x186c1
movl $0x20, %edx
callq 0x42f0
movq 0xe922(%rip), %rdi      # 0x1fff0
callq 0x4130
movl $0x4, %edi
callq 0x4320
movq %rax, %r14
callq 0x4480
jmp 0x1195d
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
jne 0x11950
jmp 0x1195d
jmp 0x1193e
jmp 0x1193e
movq %rax, %r14
cmpl $0x1, %edx
jne 0x1195d
movq %r14, %rdi
callq 0x4110
movq %rax, %r14
movq 0xe8c8(%rip), %rdi      # 0x1fff0
leaq 0x7484(%rip), %rsi      # 0x18bb3
movl $0x1d, %edx
callq 0x42f0
movq 0x8(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0xe89d(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x6f8c(%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 0x117cb
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x117cb
jmp 0x117c8
movq %rax, %r14
callq 0x4480
jmp 0x1195d
movq %rax, %r14
cmpl $0x1, %edx
jne 0x1195d
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xe7fd(%rip), %rdi      # 0x1fff0
leaq 0x73b9(%rip), %rsi      # 0x18bb3
movl $0x1d, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0xe7d7(%rip), %rdi      # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x71d5(%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 0x11890
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x11890
jmp 0x1188d
movq %rax, %r14
callq 0x4480
jmp 0x1195d
movq %rax, %r14
cmpl $0x2, %edx
jne 0x11941
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 0x76e4
movq %r14, %rdi
callq 0x4110
movq 0xe716(%rip), %rdi      # 0x1fff0
leaq 0x6e44(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0xe6f4(%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 0x1192d
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x11941
movq %rax, %rdi
callq 0x76d8
jmp 0x1193e
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1195d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x42d0
movq %r14, %rdi
callq 0x44e0
nop