// File: /workspace/github_cpp_makefile_star2_O1/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::GbinFileI::sample(sampFiles::GbinFileO&, unsigned long const&)
void GbinFileI::sample(GbinFileO &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 rows


    // calculate number of rows in the input binary file; file is closed before the function exits.
    uint64_t N = _numLines();

    // 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);
        }

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

        } catch (system_error &error) {
            cerr << "ERROR: cannot open binary file " << _fileName << " for input: " << error.code().message() << flush;
            perror(" ");
            exit(1);
        }
        try {
            out._varFile.open(out._fileName.c_str(), ios::out | ios::binary | 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 binary file " << out._fileName << " for output: " << error.code().message() << flush;
            perror(" ");
            exit(1);
        }

        while (_varFile) {
            _varFile.read(buf, bufSize);
            out._varFile.write(buf, _varFile.gcount()); // must be gcount() in case we got to the end of the file
        }
        _varFile.close();
        out._varFile.close();
        delete [] buf;
        return;
    }

    // Passed all the tests, proceed to sampling
    char *ROWbuf;
    size_t rowSize = _nCols*_elemSize;
    try {
        ROWbuf = new char[rowSize]; // .read() does not append a null-character
    } catch (bad_alloc &) {
        cerr << "ERROR: failed to allocate the row buffer" << endl;
        exit(4);
    }

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

    } catch (system_error &error) {
        cerr << "ERROR: cannot open binary file " << _fileName << " for input: " << error.code().message() << flush;
        perror(" ");
        exit(1);
    }

    if (out._varFile.is_open()) {
        out.close();
    }
    try {
        out._varFile.open(out._fileName.c_str(), ios::out | ios::binary | 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 binary file " << out._fileName << " for output: " << error.code().message() << flush;
        perror(" ");
        exit(1);
    }

    uint64_t nloc = n; // local copy of n
    uint64_t S;
    uint64_t cumS = 0; // cumulative position in the file
    RanDraw *rowSamp;  // so that I can catch RanDraw constructor exceptions
    try {
        rowSamp = new RanDraw();
    } catch (string error) {
        cerr << "ERROR: " << error << endl;
        exit(5);
    }
    while (nloc) {
        try {
            S = rowSamp->vitter(nloc, N); // sample the number of rows to skip; keep track of the running total
        } catch (string error) {
            cerr << "ERROR: " << error << endl;
            exit(5);
        }
        cumS += S;
        N    -= S + 1;
        _varFile.seekg(cumS*rowSize); // seekg() index is base-0
        _varFile.read(ROWbuf, rowSize);
        cumS++;                        // step up cumS because seekg() will not change it (unlike the getline() that automatially advances)
        out._varFile.write(ROWbuf, rowSize);
        nloc--;

    }
    _varFile.close();
    out._varFile.close();
    delete [] ROWbuf;
    delete rowSamp;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpq $0x0, (%rdx)
je 0x803f
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r14
cmpq (%rbx), %rax
jae 0x808f
movq 0x18006(%rip), %r15     # 0x1fff0
leaq 0x10605(%rip), %rsi     # 0x185f6
movl $0x1d, %edx
movq %r15, %rdi
callq 0x42f0
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x10601(%rip), %rsi     # 0x18614
movl $0x2f, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x1060f(%rip), %rsi     # 0x18644
movl $0x14, %edx
movq %rax, %rdi
jmp 0x8055
movq 0x17faa(%rip), %rbx     # 0x1fff0
leaq 0x10577(%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 $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x41b0
jne 0x81e9
movq 0x17f54(%rip), %r12     # 0x1fff0
leaq 0x105b6(%rip), %rsi     # 0x18659
movl $0x16, %edx
movq %r12, %rdi
callq 0x42f0
movq (%rbx), %rsi
movq %r12, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x105ab(%rip), %rsi     # 0x18670
movl $0x22, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x105ac(%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, %rbx
leaq 0x8(%r15), %r14
movq 0x218(%r15), %rsi
movq %r14, %rdi
movl $0xc, %edx
callq 0x4270
leaq 0x88(%r15), %rdi
callq 0x40a0
testb %al, %al
je 0x83c3
leaq 0x8(%r13), %r12
movq 0x218(%r13), %rsi
movq %r12, %rdi
movl $0x34, %edx
callq 0x4270
leaq 0x88(%r13), %rdi
callq 0x40a0
testb %al, %al
je 0x846f
movq 0x8(%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r15,%rax)
jne 0x81c3
addq $0x18, %r13
movl $0xa00000, %edx         # imm = 0xA00000
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4350
movq 0x10(%r15), %rdx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x4390
movq 0x8(%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r15,%rax)
je 0x8194
movq %r14, %rdi
callq 0x4060
movq %r12, %rdi
callq 0x4060
movq %rbx, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4380
movq %r13, 0x20(%rsp)
movq 0x240(%r15), %rdi
imulq 0x238(%r15), %rdi
movq %rdi, 0x40(%rsp)
callq 0x4030
movq %rax, 0x30(%rsp)
leaq 0x8(%r15), %rdi
movq 0x218(%r15), %rsi
movq %rdi, 0x28(%rsp)
movl $0xc, %edx
callq 0x4270
leaq 0x88(%r15), %rdi
callq 0x40a0
testb %al, %al
je 0x8419
movq 0x20(%rsp), %r12
leaq 0x8(%r12), %rbp
leaq 0x88(%r12), %r15
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x8262
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
movq 0x218(%r12), %rsi
movq %rbp, %rdi
movl $0x34, %edx
callq 0x4270
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x84c2
movq %rbp, 0x38(%rsp)
movq (%rbx), %r15
movl $0x8, %edi
callq 0x42c0
movq %rax, %rbp
movq %rax, %rdi
callq 0x174a0
testq %r15, %r15
je 0x837a
addq $0x18, 0x20(%rsp)
xorl %r12d, %r12d
movq %r15, %xmm0
movq 0x101dd(%rip), %xmm2    # 0x184a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x101e1(%rip), %xmm3    # 0x184b0
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq %r14, %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, 0x50(%rsp)
movq %rbp, %r13
movq %rbp, %rdi
movq %rsp, %rsi
leaq 0x50(%rsp), %rdx
callq 0x177c4
movq %rax, %rbx
leaq (%rax,%r12), %rsi
notq %rax
addq %rax, %r14
movq %r14, 0x48(%rsp)
movq 0x40(%rsp), %r14
imulq %r14, %rsi
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x4360
movq %rbp, %rdi
movq 0x30(%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x4350
addq %rbx, %r12
incq %r12
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq 0x48(%rsp), %r14
callq 0x4390
decq %r15
movq %r13, %rbp
jne 0x82b6
movq 0x28(%rsp), %rdi
callq 0x4060
movq 0x38(%rsp), %rdi
callq 0x4060
movq 0x30(%rsp), %rdi
callq 0x4380
movq (%rbp), %rdi
testq %rdi, %rdi
je 0x83a7
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x8, %esi
movq %rbp, %rdi
callq 0x42d0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17c26(%rip), %rdi     # 0x1fff0
leaq 0x1017f(%rip), %rsi     # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x218(%r15), %rsi
movq 0x220(%r15), %rdx
movq 0x17c00(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x1016d(%rip), %rsi     # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x8518
movq 0x17bd0(%rip), %rdi     # 0x1fff0
leaq 0x10129(%rip), %rsi     # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x218(%r15), %rsi
movq 0x220(%r15), %rdx
movq 0x17baa(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x10117(%rip), %rsi     # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x8518
movq 0x17b7a(%rip), %rdi     # 0x1fff0
leaq 0x100d3(%rip), %rsi     # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0x17b54(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x10237(%rip), %rsi     # 0x186e2
movl $0xb, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x8518
movq 0x17b27(%rip), %rdi     # 0x1fff0
leaq 0x10080(%rip), %rsi     # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x20(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0x17afc(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x101df(%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 0x853d
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x3, %ebx
jne 0x89fd
movq %r14, %rdi
callq 0x4340
leaq 0x10(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x7720
movq %r14, %rdi
callq 0x4110
movq 0x17a78(%rip), %rdi     # 0x1fff0
leaq 0x101a6(%rip), %rsi     # 0x18725
movl $0x7, %edx
callq 0x42f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x17a57(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
jmp 0x89d5
movq %rax, %r14
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x85c4
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x89fd
movq %rax, %r14
cmpl $0x1, %edx
jne 0x89fd
movq %r14, %rdi
callq 0x4110
movq 0x17a07(%rip), %rdi     # 0x1fff0
leaq 0x1010c(%rip), %rsi     # 0x186fc
movl $0x28, %edx
callq 0x42f0
movq 0x179ef(%rip), %rdi     # 0x1fff0
callq 0x4130
jmp 0x864d
movq %rax, %r14
callq 0x4480
jmp 0x89fd
movq %rax, %r14
cmpl $0x1, %edx
jne 0x89fd
movq %r14, %rdi
callq 0x4110
movq 0x179c0(%rip), %rdi     # 0x1fff0
leaq 0x1008a(%rip), %rsi     # 0x186c1
movl $0x20, %edx
callq 0x42f0
movq 0x179a8(%rip), %rdi     # 0x1fff0
callq 0x4130
movl $0x4, %edi
callq 0x4320
movq %rax, %r14
callq 0x4480
jmp 0x89fd
movq %rax, %r14
cmpl $0x2, %edx
jne 0x89fd
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x1796e(%rip), %rdi     # 0x1fff0
leaq 0xfeee(%rip), %rsi      # 0x18577
movl $0x1f, %edx
callq 0x42f0
movq 0x20(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0x17943(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x10032(%rip), %rsi     # 0x186ee
movl $0xd, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
movq %rsp, %rdi
callq *0x20(%rax)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
movq %rsp, %rdi
callq 0x45bf
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x871f
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x871f
jmp 0x871c
movq %rax, %r14
callq 0x4480
jmp 0x89fd
movq %rax, %r14
cmpl $0x2, %edx
jne 0x89fd
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x178a9(%rip), %rdi     # 0x1fff0
leaq 0xfe29(%rip), %rsi      # 0x18577
movl $0x1f, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0x17883(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0xff72(%rip), %rsi      # 0x186ee
movl $0xd, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
movq %rsp, %rdi
callq *0x20(%rax)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
movq %rsp, %rdi
callq 0x45f3
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x87df
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x87df
jmp 0x87dc
movq %rax, %r14
callq 0x4480
jmp 0x89fd
movq %rax, %r14
cmpl $0x2, %edx
jne 0x89fd
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x177e9(%rip), %rdi     # 0x1fff0
leaq 0xfd69(%rip), %rsi      # 0x18577
movl $0x1f, %edx
callq 0x42f0
movq 0x218(%r15), %rsi
movq 0x220(%r15), %rdx
movq 0x177c3(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x101c1(%rip), %rsi     # 0x189fd
movl $0xc, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
movq %rsp, %rdi
callq *0x20(%rax)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
movq %rsp, %rdi
callq 0x45a5
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x889f
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x889f
jmp 0x889c
movq %rax, %r14
callq 0x4480
jmp 0x89fd
movq %rax, %r14
cmpl $0x2, %edx
jne 0x89fd
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x17729(%rip), %rdi     # 0x1fff0
leaq 0xfca9(%rip), %rsi      # 0x18577
movl $0x1f, %edx
callq 0x42f0
movq 0x218(%r15), %rsi
movq 0x220(%r15), %rdx
movq 0x17703(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x10101(%rip), %rsi     # 0x189fd
movl $0xc, %edx
movq %rax, %rdi
callq 0x42f0
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
movq %rsp, %rdi
callq *0x20(%rax)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x42f0
movq %rax, %rdi
callq 0x41b0
movq %rsp, %rdi
callq 0x45d9
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x895f
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x895f
jmp 0x895c
movq %rax, %r14
callq 0x4480
jmp 0x89fd
movq %rax, %r14
cmpl $0x3, %edx
jne 0x89fd
movq %r14, %rdi
callq 0x4340
leaq 0x10(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x7720
movq %r14, %rdi
callq 0x4110
movq 0x17649(%rip), %rdi     # 0x1fff0
leaq 0xfd77(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x17628(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
movl $0x5, %edi
callq 0x4320
movq %rax, %r14
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x89f8
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
movq %r14, %rdi
callq 0x44e0
movq %rax, %rdi
callq 0x7714
nop