// File: /workspace/github_cpp_makefile_star2_O2/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 0x6a75
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r13
movq (%rdi), %rax
callq *0x10(%rax)
movq %rax, %r14
cmpq (%r12), %rax
jae 0x6a9e
movq 0x155b9(%rip), %rdi     # 0x1bff0
leaq 0xdbb8(%rip), %rsi      # 0x145f6
callq 0x42a0
movq (%r12), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0xdbbe(%rip), %rsi      # 0x14614
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x41b0
leaq 0xdbd4(%rip), %rsi      # 0x14644
movq %rax, %rdi
jmp 0x6a83
movq 0x15574(%rip), %rdi     # 0x1bff0
leaq 0xdb41(%rip), %rsi      # 0x145c4
callq 0x42a0
movq %rax, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4140
jne 0x6b9c
movq 0x15545(%rip), %rdi     # 0x1bff0
leaq 0xdba7(%rip), %rsi      # 0x14659
callq 0x42a0
movq (%r12), %rsi
movq %rax, %rdi
callq 0x41b0
leaq 0xdba6(%rip), %rsi      # 0x14670
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x41b0
leaq 0xdbaf(%rip), %rsi      # 0x14693
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
movl $0xa00000, %edi         # imm = 0xA00000
callq 0x4030
movq %rax, %r15
leaq 0x8(%r13), %r12
leaq 0x218(%r13), %r14
movq 0x218(%r13), %rsi
pushq $0xc
popq %rdx
movq %r12, %rdi
callq 0x4280
leaq 0x88(%r13), %rdi
callq 0x40b0
testb %al, %al
je 0x6d98
leaq 0x8(%rbx), %rbp
leaq 0x218(%rbx), %r14
movq 0x218(%rbx), %rsi
pushq $0x34
popq %rdx
movq %rbp, %rdi
callq 0x4280
leaq 0x88(%rbx), %rdi
callq 0x40b0
testb %al, %al
je 0x6e09
addq $0x18, %rbx
movq 0x8(%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r13,%rax)
jne 0x6d2b
movl $0xa00000, %edx         # imm = 0xA00000
movq %r12, %rdi
movq %r15, %rsi
callq 0x4340
movq 0x10(%r13), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4390
jmp 0x6b67
movq 0x240(%r13), %rdi
imulq 0x238(%r13), %rdi
movq %rdi, 0x48(%rsp)
callq 0x4030
movq %rax, 0x28(%rsp)
leaq 0x8(%r13), %rdi
leaq 0x218(%r13), %r15
movq 0x218(%r13), %rsi
pushq $0xc
popq %rdx
movq %rdi, 0x20(%rsp)
callq 0x4280
addq $0x88, %r13
movq %r13, %rdi
callq 0x40b0
testb %al, %al
je 0x6dd2
leaq 0x8(%rbx), %r15
leaq 0x88(%rbx), %r13
movq %r13, %rdi
callq 0x40b0
testb %al, %al
je 0x6c10
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
leaq 0x218(%rbx), %rbp
movq 0x218(%rbx), %rsi
pushq $0x34
popq %rdx
movq %r15, %rdi
callq 0x4280
movq %r13, %rdi
callq 0x40b0
testb %al, %al
je 0x6e40
movq (%r12), %r13
pushq $0x8
popq %rdi
callq 0x42c0
movq %rax, %rbp
movq %r15, 0x30(%rsp)
movq %rax, %rdi
callq 0x12bf2
addq $0x18, %rbx
movq %rbx, 0x40(%rsp)
xorl %r12d, %r12d
movq %r13, %r15
movq %rbp, 0x38(%rsp)
movq 0x48(%rsp), %rbx
subq $0x1, %r15
jb 0x6d51
movq %r13, %xmm0
movq 0xd81b(%rip), %xmm2     # 0x144a0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0xd81f(%rip), %xmm3     # 0x144b0
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, %rdi
movq %rsp, %rsi
leaq 0x50(%rsp), %rdx
callq 0x12edc
movq %rax, %r13
leaq (%rax,%r12), %rsi
notq %rax
addq %rax, %r14
imulq %rbx, %rsi
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x4350
movq %rbp, %rdi
movq 0x28(%rsp), %rbp
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4340
addq %r13, %r12
incq %r12
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x4390
movq %r15, %r13
movq 0x38(%rsp), %rbp
jmp 0x6c6e
movq %r12, %rdi
callq 0x4050
movq %rbp, %rdi
callq 0x4050
movq %r15, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4370
movq 0x20(%rsp), %rdi
callq 0x4050
movq 0x30(%rsp), %rdi
callq 0x4050
movq 0x28(%rsp), %rdi
callq 0x4370
movq (%rbp), %rdi
testq %rdi, %rdi
je 0x6d7e
movq (%rdi), %rax
callq *0x8(%rax)
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x42d0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x15251(%rip), %rdi     # 0x1bff0
leaq 0xd7aa(%rip), %rsi      # 0x14550
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x4270
leaq 0xd7af(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0x6e75
movq 0x15217(%rip), %rdi     # 0x1bff0
leaq 0xd770(%rip), %rsi      # 0x14550
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0xd775(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0x6e75
movq 0x151e0(%rip), %rdi     # 0x1bff0
leaq 0xd739(%rip), %rsi      # 0x14550
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x4270
leaq 0xd8b4(%rip), %rsi      # 0x146e2
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0x6e75
movq 0x151a9(%rip), %rdi     # 0x1bff0
leaq 0xd702(%rip), %rsi      # 0x14550
callq 0x42a0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x4270
leaq 0xd87d(%rip), %rsi      # 0x146e2
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x2
jmp 0x7202
movq %rdx, %r14
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %rbp, %rdi
callq 0x42d0
jmp 0x6e95
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x3, %r14d
jne 0x7218
movq %rbx, %rdi
callq 0x4330
movq %rsp, %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0x1512f(%rip), %rdi     # 0x1bff0
leaq 0xd85d(%rip), %rsi      # 0x14725
callq 0x42a0
movq %rsp, %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0x7200
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4558
callq 0x4470
jmp 0x7218
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x7218
movq %rbx, %rdi
callq 0x4110
movq 0x150db(%rip), %rdi     # 0x1bff0
leaq 0xd7e0(%rip), %rsi      # 0x146fc
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0x6f67
movq %rax, %rbx
callq 0x4470
jmp 0x7218
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x7218
movq %rbx, %rdi
callq 0x4110
movq 0x1509d(%rip), %rdi     # 0x1bff0
leaq 0xd767(%rip), %rsi      # 0x146c1
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x4
jmp 0x7202
movq %rax, %rbx
callq 0x4470
jmp 0x7218
movq %rax, %rbx
cmpl $0x2, %edx
jne 0x7218
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x15057(%rip), %rdi     # 0x1bff0
leaq 0xd5d7(%rip), %rsi      # 0x14577
callq 0x42a0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x4270
leaq 0xd737(%rip), %rsi      # 0x146ee
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
movq %rsp, %rdi
callq *0x20(%rax)
movq %rsp, %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
jmp 0x7186
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4558
jmp 0x6ffc
jmp 0x6ff9
movq %rax, %rbx
callq 0x4470
jmp 0x7218
movq %rax, %rbx
cmpl $0x2, %edx
jne 0x7218
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x14fcc(%rip), %rdi     # 0x1bff0
leaq 0xd54c(%rip), %rsi      # 0x14577
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x4270
leaq 0xd6ac(%rip), %rsi      # 0x146ee
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
movq %rsp, %rdi
callq *0x20(%rax)
movq %rsp, %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
jmp 0x7186
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4558
jmp 0x7087
jmp 0x7084
movq %rax, %rbx
callq 0x4470
jmp 0x7218
movq %rax, %rbx
cmpl $0x2, %edx
jne 0x7218
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x14f41(%rip), %rdi     # 0x1bff0
leaq 0xd4c1(%rip), %rsi      # 0x14577
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0xd930(%rip), %rsi      # 0x149fd
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
movq %rsp, %rdi
callq *0x20(%rax)
movq %rsp, %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
jmp 0x7186
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4558
jmp 0x7112
jmp 0x710f
movq %rax, %rbx
callq 0x4470
jmp 0x7218
movq %rax, %rbx
cmpl $0x2, %edx
jne 0x7218
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0x14eb6(%rip), %rdi     # 0x1bff0
leaq 0xd436(%rip), %rsi      # 0x14577
callq 0x42a0
movq %rax, %rdi
movq %r14, %rsi
callq 0x4270
leaq 0xd8a5(%rip), %rsi      # 0x149fd
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
movq %rsp, %rdi
callq *0x20(%rax)
movq %rsp, %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
movq %rsp, %rdi
callq 0x4558
leaq 0xd67d(%rip), %rdi      # 0x14812
callq 0x4250
pushq $0x1
jmp 0x7202
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4558
jmp 0x71b0
jmp 0x71ad
movq %rax, %rbx
callq 0x4470
jmp 0x7218
movq %rax, %rbx
cmpl $0x3, %edx
jne 0x7218
movq %rbx, %rdi
callq 0x4330
movq %rsp, %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0x14e0f(%rip), %rdi     # 0x1bff0
leaq 0xd53d(%rip), %rsi      # 0x14725
callq 0x42a0
movq %rsp, %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
pushq $0x5
popq %rdi
callq 0x4310
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4558
callq 0x4470
movq %rbx, %rdi
callq 0x44d0
movq %rax, %rdi
callq 0x6194