// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x170, %rsp            # imm = 0x170
movq %rdi, %rbx
movq 0x8(%rdi), %r14
leaq 0xbec6(%rip), %rsi      # 0x158c0
leaq 0x58(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x53a6
xorl %edi, %edi
testq %r14, %r14
sete %dil
leaq 0x58(%rsp), %rsi
callq 0x5364
leaq 0x58(%rsp), %rdi
callq 0x45e8
movl 0xbc(%rbx), %esi
movq (%rbx), %rcx
testl %esi, %esi
jle 0x9abf
movq 0x8(%rcx), %rax
subq (%rcx), %rax
pushq $0x18
popq %r14
cqto
idivq %r14
cmpl %eax, %esi
jae 0x9abf
movslq 0xb0(%rbx), %rcx
movq %rcx, %rdx
negq %rdx
cmpq $0x2, %rcx
cmovlq %rcx, %rdx
movq %rdx, 0x58(%rsp)
movq $0x75bcd15, 0x60(%rsp)  # imm = 0x75BCD15
andq $0x0, 0x68(%rsp)
movl %eax, %esi
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0xc242
movq (%rbx), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
idivq %r14
movq 0x8(%rsp), %rdx
movl %eax, %eax
addq $0x8, %rdx
xorl %esi, %esi
cmpq %rsi, %rax
je 0x9ac8
movq 0x18(%rcx), %rdi
movsd (%rdi,%rsi,8), %xmm0
movsd %xmm0, -0x8(%rdx)
movl %esi, (%rdx)
incq %rsi
addq $0x10, %rdx
jmp 0x9aa1
movq %rcx, 0x8(%rbx)
jmp 0x9d78
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0xcd4f
movq (%rbx), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
idivq %r14
movq 0x8(%rsp), %rcx
movl %eax, %edx
addq $0x10, %rcx
pushq $0x1
popq %rsi
cmpq %rdx, %rsi
jae 0x9b0f
movsd -0x10(%rcx), %xmm0
addsd (%rcx), %xmm0
movsd %xmm0, (%rcx)
incq %rsi
addq $0x10, %rcx
jmp 0x9af4
decl %eax
movq 0x8(%rsp), %rcx
shlq $0x4, %rax
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
movq %rdx, (%rcx,%rax)
leaq 0x30(%rsp), %r13
andl $0x0, (%r13)
andq $0x0, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
andq $0x0, 0x20(%r13)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %r12d, %r12d
leaq 0x58(%rsp), %r14
leaq 0x28(%rsp), %r15
movl 0xbc(%rbx), %eax
cmpq %rax, %r12
jae 0x9c4e
movq %r14, %rdi
callq 0x13d46
cvtss2sd %xmm0, %xmm0
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
leaq -0x10(%rax), %rdx
xorl %edi, %edi
movl %ecx, %r10d
movl %ecx, %esi
movl %r10d, %r12d
shrl %r12d
movb $0x1, %r8b
movb $0x1, %r9b
cmpl $0x2, %r10d
jb 0x9bb4
movq %r12, %r9
shlq $0x4, %r9
ucomisd (%rdx,%r9), %xmm0
setae %r9b
cmpq %r12, %rcx
jbe 0x9bce
movq %r12, %r8
shlq $0x4, %r8
movsd (%rax,%r8), %xmm1
ucomisd %xmm0, %xmm1
setae %r8b
testb %r9b, %r8b
jne 0x9bf2
movl %r12d, %r11d
testb %r9b, %r9b
jne 0x9bde
movl %edi, %r11d
testb %r8b, %r8b
jne 0x9be6
movl %esi, %r12d
leal (%r12,%r11), %r10d
movl %r12d, %esi
movl %r11d, %edi
jmp 0x9b91
shlq $0x4, %r12
leaq (%rax,%r12), %rsi
addq $0x8, %rsi
movq %r15, %rdi
callq 0xd3a0
cmpq %r13, %rax
jne 0x9c44
movq (%rbx), %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%r12), %rsi
addq $0x8, %rsi
movl (%rsi), %ecx
movq 0x18(%rax), %rax
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r15, %rdi
callq 0xd3f2
movsd (%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x50(%rsp), %r12
jmp 0x9b5a
leaq 0x176c3(%rip), %r14     # 0x21318
movl (%r14), %eax
shrl $0x1f, %eax
movq 0x8(%r14,%rax,8), %rdi
leaq 0xbc8e(%rip), %rsi      # 0x158f5
callq 0x4290
movl %r12d, %esi
movq %rax, %rdi
callq 0x41e0
leaq 0xbc9b(%rip), %rsi      # 0x15919
movq %rax, %rdi
callq 0x4290
movl (%r14), %eax
shrl $0x1f, %eax
movq 0x8(%r14,%rax,8), %rdi
leaq 0xbc9a(%rip), %rsi      # 0x15932
callq 0x4290
movsd 0xb6eb(%rip), %xmm0     # 0x15390
mulsd (%rsp), %xmm0
movq %rax, %rdi
callq 0x44d0
leaq 0xbc8f(%rip), %rsi      # 0x15948
movq %rax, %rdi
callq 0x4290
pushq $0x30
popq %rdi
callq 0x42b0
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 0x61d4
movq %r14, 0x8(%rbx)
movq 0x40(%rsp), %rax
movq (%rbx), %rbx
xorl %r15d, %r15d
cmpq %r13, %rax
je 0x9d64
movq (%rbx), %rcx
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
shrq $0x3, %rdx
movl %edx, %edx
imulq $0x18, %r15, %rsi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x9d57
movl 0x20(%rax), %r8d
movslq %r8d, %r9
imulq $0x18, %r9, %r9
movq (%rcx,%r9), %r9
movsd (%r9,%rdi,8), %xmm0
movq (%r14), %r9
movq (%r9,%rsi), %r9
movsd %xmm0, (%r9,%rdi,8)
movq 0x18(%rbx), %r9
movsd (%r9,%r8,8), %xmm0
divsd (%rsp), %xmm0
movq 0x18(%r14), %r8
movsd %xmm0, (%r8,%r15,8)
incq %rdi
jmp 0x9d12
incq %r15
movq %rax, %rdi
callq 0x4300
jmp 0x9cf7
leaq 0x28(%rsp), %rdi
callq 0xc720
leaq 0x8(%rsp), %rdi
callq 0xcc9e
addq $0x170, %rsp            # imm = 0x170
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %r14, %rdi
callq 0x42c0
jmp 0x9dbd
movq %rax, %rbx
jmp 0x9dc7
jmp 0x9daf
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x45e8
jmp 0x9dd1
movq %rax, %rbx
jmp 0x9dd1
jmp 0x9dba
jmp 0x9dba
jmp 0x9dba
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0xc720
leaq 0x8(%rsp), %rdi
callq 0xcc9e
movq %rbx, %rdi
callq 0x4530
nop