// File: /workspace/github_cpp_makefile_star2_O3/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 0x7fc7
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r14
cmpq (%rbx), %rax
jae 0x8017
movq 0x1807e(%rip), %r15     # 0x1fff0
leaq 0x1067d(%rip), %rsi     # 0x185f6
movl $0x1d, %edx
movq %r15, %rdi
callq 0x42f0
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x10679(%rip), %rsi     # 0x18614
movl $0x2f, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x10687(%rip), %rsi     # 0x18644
movl $0x14, %edx
movq %rax, %rdi
jmp 0x7fdd
movq 0x18022(%rip), %rbx     # 0x1fff0
leaq 0x105ef(%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 0x8171
movq 0x17fcc(%rip), %r12     # 0x1fff0
leaq 0x1062e(%rip), %rsi     # 0x18659
movl $0x16, %edx
movq %r12, %rdi
callq 0x42f0
movq (%rbx), %rsi
movq %r12, %rdi
callq 0x41a0
movq %rax, %rbx
leaq 0x10623(%rip), %rsi     # 0x18670
movl $0x22, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x41a0
movq %rax, %rbx
leaq 0x10624(%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 0x834b
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 0x83f7
movq 0x8(%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r15,%rax)
jne 0x814b
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 0x811c
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 0x83a1
movq 0x20(%rsp), %r12
leaq 0x8(%r12), %rbp
leaq 0x88(%r12), %r15
movq %r15, %rdi
callq 0x40a0
testb %al, %al
je 0x81ea
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 0x844a
movq %rbp, 0x38(%rsp)
movq (%rbx), %r15
movl $0x8, %edi
callq 0x42c0
movq %rax, %rbp
movq %rax, %rdi
callq 0x172de
testq %r15, %r15
je 0x8302
addq $0x18, 0x20(%rsp)
xorl %r12d, %r12d
movq %r15, %xmm0
movq 0x10255(%rip), %xmm2    # 0x184a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x10259(%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 0x175fe
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 0x823e
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 0x832f
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 0x17c9e(%rip), %rdi     # 0x1fff0
leaq 0x101f7(%rip), %rsi     # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x218(%r15), %rsi
movq 0x220(%r15), %rdx
movq 0x17c78(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x101e5(%rip), %rsi     # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x84a0
movq 0x17c48(%rip), %rdi     # 0x1fff0
leaq 0x101a1(%rip), %rsi     # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x218(%r15), %rsi
movq 0x220(%r15), %rdx
movq 0x17c22(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x1018f(%rip), %rsi     # 0x1856c
movl $0xa, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x84a0
movq 0x17bf2(%rip), %rdi     # 0x1fff0
leaq 0x1014b(%rip), %rsi     # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0x17bcc(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x102af(%rip), %rsi     # 0x186e2
movl $0xb, %edx
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
callq 0x4130
jmp 0x84a0
movq 0x17b9f(%rip), %rdi     # 0x1fff0
leaq 0x100f8(%rip), %rsi     # 0x18550
movl $0x1b, %edx
callq 0x42f0
movq 0x20(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0x17b74(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rbx
leaq 0x10257(%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 0x84c5
movq %rdx, %rbx
movq %rax, %r14
cmpl $0x3, %ebx
jne 0x8985
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 0x76e4
movq %r14, %rdi
callq 0x4110
movq 0x17af0(%rip), %rdi     # 0x1fff0
leaq 0x1021e(%rip), %rsi     # 0x18725
movl $0x7, %edx
callq 0x42f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x17acf(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %rdi
callq 0x4130
jmp 0x895d
movq %rax, %r14
movq (%rsp), %rdi
cmpq %rbx, %rdi
je 0x854c
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
jmp 0x8985
movq %rax, %r14
cmpl $0x1, %edx
jne 0x8985
movq %r14, %rdi
callq 0x4110
movq 0x17a7f(%rip), %rdi     # 0x1fff0
leaq 0x10184(%rip), %rsi     # 0x186fc
movl $0x28, %edx
callq 0x42f0
movq 0x17a67(%rip), %rdi     # 0x1fff0
callq 0x4130
jmp 0x85d5
movq %rax, %r14
callq 0x4480
jmp 0x8985
movq %rax, %r14
cmpl $0x1, %edx
jne 0x8985
movq %r14, %rdi
callq 0x4110
movq 0x17a38(%rip), %rdi     # 0x1fff0
leaq 0x10102(%rip), %rsi     # 0x186c1
movl $0x20, %edx
callq 0x42f0
movq 0x17a20(%rip), %rdi     # 0x1fff0
callq 0x4130
movl $0x4, %edi
callq 0x4320
movq %rax, %r14
callq 0x4480
jmp 0x8985
movq %rax, %r14
cmpl $0x2, %edx
jne 0x8985
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x179e6(%rip), %rdi     # 0x1fff0
leaq 0xff66(%rip), %rsi      # 0x18577
movl $0x1f, %edx
callq 0x42f0
movq 0x20(%rsp), %rax
movq 0x218(%rax), %rsi
movq 0x220(%rax), %rdx
movq 0x179bb(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x100aa(%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 0x86a7
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x86a7
jmp 0x86a4
movq %rax, %r14
callq 0x4480
jmp 0x8985
movq %rax, %r14
cmpl $0x2, %edx
jne 0x8985
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x17921(%rip), %rdi     # 0x1fff0
leaq 0xfea1(%rip), %rsi      # 0x18577
movl $0x1f, %edx
callq 0x42f0
movq 0x218(%r13), %rsi
movq 0x220(%r13), %rdx
movq 0x178fb(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0xffea(%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 0x8767
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x8767
jmp 0x8764
movq %rax, %r14
callq 0x4480
jmp 0x8985
movq %rax, %r14
cmpl $0x2, %edx
jne 0x8985
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x17861(%rip), %rdi     # 0x1fff0
leaq 0xfde1(%rip), %rsi      # 0x18577
movl $0x1f, %edx
callq 0x42f0
movq 0x218(%r15), %rsi
movq 0x220(%r15), %rdx
movq 0x1783b(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x10239(%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 0x8827
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x8827
jmp 0x8824
movq %rax, %r14
callq 0x4480
jmp 0x8985
movq %rax, %r14
cmpl $0x2, %edx
jne 0x8985
movq %r14, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x177a1(%rip), %rdi     # 0x1fff0
leaq 0xfd21(%rip), %rsi      # 0x18577
movl $0x1f, %edx
callq 0x42f0
movq 0x218(%r15), %rsi
movq 0x220(%r15), %rdx
movq 0x1777b(%rip), %rdi     # 0x1fff0
callq 0x42f0
movq %rax, %r14
leaq 0x10179(%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 0x88e7
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
jmp 0x88e7
jmp 0x88e4
movq %rax, %r14
callq 0x4480
jmp 0x8985
movq %rax, %r14
cmpl $0x3, %edx
jne 0x8985
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 0x76e4
movq %r14, %rdi
callq 0x4110
movq 0x176c1(%rip), %rdi     # 0x1fff0
leaq 0xfdef(%rip), %rsi      # 0x18725
movl $0x7, %edx
callq 0x42f0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x176a0(%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 0x8980
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x42d0
callq 0x4480
movq %r14, %rdi
callq 0x44e0
movq %rax, %rdi
callq 0x76d8
nop