// File: /workspace/github_cpp_makefile_star2_O1/hanhwi[P]SimPoint/analysiscode/Simpoint.cpp
// Function: Simpoint::sampleDataset()
void Simpoint::sampleDataset() {
    Utilities::check(sampledDataset == NULL, 
            "Simpoint::sampleDataset() sampledDataset is not NULL");

    if ((options.sampleSize > 0) && 
            ((unsigned int)options.sampleSize < wholeDataset->numRows())) {
        // choose enough samples to satisfy the number of desired samples
        Random rand(options.randSeedSample);

        vector<pair<double, int> > sortedWeights(wholeDataset->numRows());
        for (unsigned int i = 0; i < wholeDataset->numRows(); i++) {
            sortedWeights[i] = pair<double, int>(wholeDataset->getWeight(i), i);
        }
        sort(sortedWeights.begin(), sortedWeights.end(), greater<pair<double, int> >());

        for (unsigned int i = 1; i < wholeDataset->numRows(); i++) {
            sortedWeights[i].first += sortedWeights[i-1].first;
        }
        sortedWeights[wholeDataset->numRows() - 1].first = 1.0; // just to be sure

        set<int> samples;
        double sampledPct = 0.0;
        while (samples.size() < (unsigned int)options.sampleSize) {
            double r = (double)rand.randFloat();

            // binary search for the sample corresponding to the generated
            // random number
            unsigned int lower = 0, upper = sortedWeights.size();
            unsigned int sample = (upper + lower) / 2;
            while (true) {
                bool below = (sample > 0) ? (sortedWeights[sample - 1].first <= r) : true;
                bool above = (sample < sortedWeights.size()) ? 
                             (sortedWeights[sample].first >= r) : true;
                if (above && below) { break; }
                if (below) { lower = sample; }
                if (above) { upper = sample; }
                sample = (upper + lower) / 2;
            }

            if (samples.find(sortedWeights[sample].second) == samples.end()) {
                sampledPct += wholeDataset->getWeight(sortedWeights[sample].second);
                samples.insert(sortedWeights[sample].second);
            }
        }

        unsigned int sampleSize = samples.size();
        Logger::log() << "  Creating a random sample of size " << sampleSize 
            << " vectors for clustering\n";
        Logger::log() << "    which represents " << (sampledPct * 100) 
            << "% of the weights\n";
        sampledDataset = new Dataset(sampleSize, wholeDataset->numCols());

        // copy the sampled data into the working data structure and reweight it 
        int j = 0;
        for (set<int>::iterator i = samples.begin(); i != samples.end(); i++) {
            for (unsigned int col = 0; col < wholeDataset->numCols(); col++) {
                (*sampledDataset)[j][col] = (*wholeDataset)[*i][col];
                sampledDataset->setWeight(j, wholeDataset->getWeight(*i) / sampledPct);
            }
            j++;
        }
    } else {
        sampledDataset = wholeDataset;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, %rbx
movq 0x8(%rdi), %r15
leaq 0x70(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x11350(%rip), %rsi     # 0x1c910
leaq 0x1137d(%rip), %rdx     # 0x1c944
leaq 0x60(%rsp), %rdi
callq 0x6086
testq %r15, %r15
jne 0xba36
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xb5f1
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42d0
movl 0xbc(%rbx), %ecx
testl %ecx, %ecx
jle 0xb8a2
movq (%rbx), %rax
movq 0x8(%rax), %rdx
subq (%rax), %rdx
shrq $0x3, %rdx
imull $0xaaaaaaab, %edx, %edx # imm = 0xAAAAAAAB
cmpl %edx, %ecx
jae 0xb8a2
movslq 0xb0(%rbx), %rcx
movq %rcx, %rdx
negq %rdx
cmpq $0x2, %rcx
cmovlq %rcx, %rdx
movq %rdx, 0x60(%rsp)
movq $0x75bcd15, 0x68(%rsp)  # imm = 0x75BCD15
movq $0x0, 0x70(%rsp)
movq 0x8(%rax), %rcx
subq (%rax), %rcx
shrq $0x3, %rcx
imull $0xaaaaaaab, %ecx, %esi # imm = 0xAAAAAAAB
leaq 0x18(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0xf5ee
movq (%rbx), %rax
movq 0x8(%rax), %rdx
subq (%rax), %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r12, %rdx
testl %edx, %edx
je 0xb6b0
movq 0x18(%rax), %rax
movq 0x18(%rsp), %rcx
movl %edx, %edx
addq $0x8, %rcx
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm0
movsd %xmm0, -0x8(%rcx)
movl %esi, (%rcx)
incq %rsi
addq $0x10, %rcx
cmpq %rsi, %rdx
jne 0xb698
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r15
cmpq %r15, %r14
je 0xb6ec
movq %r15, %rax
subq %r14, %rax
sarq $0x4, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1018b
movq %r14, %rdi
movq %r15, %rsi
callq 0x1023e
movq (%rbx), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
imulq %r12, %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xb729
movq 0x18(%rsp), %rcx
movl %eax, %edx
addq $0x10, %rcx
decq %rdx
movsd -0x10(%rcx), %xmm0
addsd (%rcx), %xmm0
movsd %xmm0, (%rcx)
addq $0x10, %rcx
decq %rdx
jne 0xb713
decl %eax
movq 0x18(%rsp), %rcx
shlq $0x4, %rax
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rcx,%rax)
leaq 0x38(%rsp), %r13
movl $0x0, (%r13)
xorl %r12d, %r12d
movq %r12, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %r12, 0x20(%r13)
cmpl $0x0, 0xbc(%rbx)
je 0xb8ae
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x60(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %r14, %rdi
callq 0x19950
cvtss2sd %xmm0, %xmm0
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
movl %ecx, %edx
shrl %edx
leaq -0x10(%rax), %rsi
xorl %edi, %edi
movl %ecx, %r8d
movb $0x1, %r9b
movl %edx, %r11d
movb $0x1, %r10b
testl %edx, %edx
je 0xb7cb
movq %r11, %r10
shlq $0x4, %r10
ucomisd (%rsi,%r10), %xmm0
setae %r10b
cmpq %r11, %rcx
jbe 0xb7e2
shlq $0x4, %r11
movsd (%rax,%r11), %xmm1
ucomisd %xmm0, %xmm1
setae %r9b
movl %edx, %r11d
testb %r10b, %r10b
jne 0xb7ed
movl %edi, %r11d
movl %edx, %r12d
testb %r9b, %r9b
jne 0xb7f8
movl %r8d, %r12d
leal (%r12,%r11), %ebp
shrl %ebp
testb %r10b, %r9b
cmovel %ebp, %edx
cmovel %r12d, %r8d
cmovel %r11d, %edi
je 0xb7ad
movl %edx, %ecx
shlq $0x4, %rcx
leaq (%rax,%rcx), %rsi
addq $0x8, %rsi
movq 0x40(%rsp), %rcx
movq %r13, %rax
testq %rcx, %rcx
je 0xb845
movl (%rsi), %edx
movq %r13, %rax
xorl %edi, %edi
cmpl %edx, 0x20(%rcx)
setl %dil
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdi,8), %rcx
testq %rcx, %rcx
jne 0xb82e
movq %r13, %rcx
cmpq %r13, %rax
je 0xb859
movl (%rsi), %ecx
cmpl 0x20(%rax), %ecx
cmovlq %r13, %rax
movq %rax, %rcx
cmpq %r13, %rcx
jne 0xb88c
movq (%rbx), %rax
movl (%rsi), %ecx
movq 0x18(%rax), %rax
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
callq 0x106dc
movsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x58(%rsp), %r12
movl 0xbc(%rbx), %eax
cmpq %rax, %r12
jb 0xb783
jmp 0xb8b8
movq (%rbx), %rax
movq %rax, 0x8(%rbx)
jmp 0xba24
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x1ba71(%rip), %r15     # 0x27330
movl (%r15), %eax
shrl $0x1f, %eax
movq 0x8(%r15,%rax,8), %r14
leaq 0x11074(%rip), %rsi     # 0x1c945
movl $0x23, %edx
movq %r14, %rdi
callq 0x4330
movl %r12d, %esi
movq %r14, %rdi
callq 0x41c0
leaq 0x11079(%rip), %rsi     # 0x1c969
movl $0x18, %edx
movq %rax, %rdi
callq 0x4330
movl (%r15), %eax
shrl $0x1f, %eax
movq 0x8(%r15,%rax,8), %r14
leaq 0x11073(%rip), %rsi     # 0x1c982
movl $0x15, %edx
movq %r14, %rdi
callq 0x4330
movsd 0x10aa4(%rip), %xmm0    # 0x1c3c8
mulsd 0x8(%rsp), %xmm0
movq %r14, %rdi
callq 0x4510
leaq 0x1105f(%rip), %rsi     # 0x1c998
movl $0x11, %edx
movq %rax, %rdi
callq 0x4330
movl $0x30, %edi
callq 0x42c0
movq %rax, %r14
movq (%rbx), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdx
subq (%rax), %rdx
shrq $0x3, %rdx
movq %r14, %rdi
movl %r12d, %esi
callq 0x6e50
movq %r14, 0x8(%rbx)
movq 0x48(%rsp), %rax
cmpq %r13, %rax
je 0xba03
movq (%rbx), %rcx
movq %rcx, 0x10(%rsp)
movq (%rcx), %r15
movq 0x8(%r15), %r12
subq (%r15), %r12
shrq $0x3, %r12
movl %r12d, %ebp
xorl %ebx, %ebx
testl %r12d, %r12d
movsd 0x8(%rsp), %xmm1
je 0xb9f3
movl 0x20(%rax), %ecx
movslq %ecx, %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%r15,%rdx,8), %rdx
leaq (%rbx,%rbx,2), %rdi
movq (%r14), %r8
movq 0x18(%r14), %rsi
movq (%r8,%rdi,8), %rdi
movq 0x10(%rsp), %r8
movq 0x18(%r8), %r8
xorl %r9d, %r9d
movsd (%rdx,%r9,8), %xmm0
movsd %xmm0, (%rdi,%r9,8)
movsd (%r8,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rsi,%rbx,8)
incq %r9
cmpq %r9, %rbp
jne 0xb9d0
incq %rbx
movq %rax, %rdi
callq 0x4340
cmpq %r13, %rax
jne 0xb99c
leaq 0x30(%rsp), %rdi
callq 0xfcd0
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xba24
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b8f3(%rip), %rax     # 0x27330
movl (%rax), %ecx
shrl $0x1f, %ecx
movq 0x8(%rax,%rcx,8), %rbx
leaq 0x1072a(%rip), %rsi     # 0x1c178
movl $0x8, %edx
movq %rbx, %rdi
callq 0x4330
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq %rbx, %rdi
callq 0x4330
movq %rax, %rdi
callq 0x4160
movl $0x1, %edi
callq 0x4370
movq %rax, %rbx
movl $0x30, %esi
movq %r14, %rdi
callq 0x42d0
jmp 0xbabb
jmp 0xbaaf
movq %rax, %rbx
jmp 0xbac5
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xbadc
movq 0x70(%rsp), %rsi
incq %rsi
jmp 0xbad7
movq %rax, %rbx
jmp 0xbadc
jmp 0xbab8
jmp 0xbab8
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xfcd0
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xbadc
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4580