// File: /workspace/github_cpp_makefile_star2_O3/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 0x11308(%rip), %rsi     # 0x1c910
leaq 0x11335(%rip), %rdx     # 0x1c944
leaq 0x60(%rsp), %rdi
callq 0x6058
testq %r15, %r15
jne 0xba6b
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xb639
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x42d0
movl 0xbc(%rbx), %edx
movq (%rbx), %rcx
testl %edx, %edx
jle 0xb8d6
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r12, %rax
cmpl %eax, %edx
jae 0xb8d6
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)
movl %eax, %esi
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0xf576
movq (%rbx), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
sarq $0x3, %rax
imulq %r12, %rax
testl %eax, %eax
je 0xb6e7
movq 0x18(%rcx), %rcx
movq 0x10(%rsp), %rdx
movl %eax, %esi
addq $0x8, %rdx
xorl %edi, %edi
movsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, -0x8(%rdx)
movl %edi, (%rdx)
incq %rdi
addq $0x10, %rdx
cmpq %rdi, %rsi
jne 0xb6cf
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0xb73a
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 0x10129
movq %r14, %rdi
movq %r15, %rsi
callq 0x101dc
movq (%rbx), %rcx
movq 0x8(%rcx), %rax
movq 0x10(%rsp), %r14
subq (%rcx), %rax
sarq $0x3, %rax
imulq %r12, %rax
cmpl $0x2, %eax
jb 0xb764
movl %eax, %ecx
movsd (%r14), %xmm0
shlq $0x4, %rcx
movl $0x10, %edx
addsd (%r14,%rdx), %xmm0
movsd %xmm0, (%r14,%rdx)
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xb74f
decl %eax
shlq $0x4, %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%r14,%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 0xb8df
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x60(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %r14, %rdi
callq 0x198b2
cvtss2sd %xmm0, %xmm0
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
leaq -0x10(%rax), %rdx
xorl %r8d, %r8d
movl %ecx, %r11d
movl %ecx, %edi
movl %r11d, %esi
shrl %esi
movb $0x1, %r9b
movb $0x1, %r10b
cmpl $0x2, %r11d
jb 0xb804
movq %rsi, %r10
shlq $0x4, %r10
ucomisd (%rdx,%r10), %xmm0
setae %r10b
cmpq %rsi, %rcx
jbe 0xb81e
movq %rsi, %r9
shlq $0x4, %r9
movsd (%rax,%r9), %xmm1
ucomisd %xmm0, %xmm1
setae %r9b
testb %r10b, %r9b
jne 0xb840
movl %esi, %r12d
testb %r10b, %r10b
jne 0xb82e
movl %r8d, %r12d
testb %r9b, %r9b
jne 0xb835
movl %edi, %esi
leal (%rsi,%r12), %r11d
movl %esi, %edi
movl %r12d, %r8d
jmp 0xb7e2
shlq $0x4, %rsi
addq %rax, %rsi
addq $0x8, %rsi
movq 0x40(%rsp), %r8
testq %r8, %r8
je 0xb890
movl (%rsi), %eax
movq %r13, %r9
movq %r9, %rcx
movq %r8, %rdx
movl 0x20(%r8), %edi
xorl %r8d, %r8d
cmpl %eax, %edi
setl %r8b
movq %rdx, %r9
cmovlq %rcx, %r9
movq 0x10(%rdx,%r8,8), %r8
testq %r8, %r8
jne 0xb85a
cmpq %r13, %r9
je 0xb892
cmpl %eax, %edi
cmovlq %rcx, %rdx
cmpl 0x20(%rdx), %eax
jl 0xb892
jmp 0xb8c0
movl (%rsi), %eax
movq (%rbx), %rcx
movl %eax, %eax
movq 0x18(%rcx), %rcx
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
callq 0x10694
movsd 0x8(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x58(%rsp), %r12
movl 0xbc(%rbx), %eax
cmpq %rax, %r12
jb 0xb7b9
jmp 0xb8e9
movq %rcx, 0x8(%rbx)
jmp 0xba59
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x1ba40(%rip), %r15     # 0x27330
movl (%r15), %eax
shrl $0x1f, %eax
movq 0x8(%r15,%rax,8), %r14
leaq 0x11043(%rip), %rsi     # 0x1c945
movl $0x23, %edx
movq %r14, %rdi
callq 0x4330
movl %r12d, %esi
movq %r14, %rdi
callq 0x41c0
leaq 0x11048(%rip), %rsi     # 0x1c969
movl $0x18, %edx
movq %rax, %rdi
callq 0x4330
movl (%r15), %eax
shrl $0x1f, %eax
movq 0x8(%r15,%rax,8), %r14
leaq 0x11042(%rip), %rsi     # 0x1c982
movl $0x15, %edx
movq %r14, %rdi
callq 0x4330
movsd 0x10a7b(%rip), %xmm0    # 0x1c3d0
mulsd 0x8(%rsp), %xmm0
movq %r14, %rdi
callq 0x4510
leaq 0x1102e(%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 0x6e3c
movq %r14, 0x8(%rbx)
movq 0x48(%rsp), %rax
cmpq %r13, %rax
je 0xba38
movq (%rbx), %rbx
movq (%rbx), %r15
movq 0x8(%r15), %r12
subq (%r15), %r12
shrq $0x3, %r12
movl %r12d, %ebp
xorl %r13d, %r13d
testl %r12d, %r12d
movsd 0x8(%rsp), %xmm1
je 0xba23
movl 0x20(%rax), %ecx
movslq %ecx, %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%r15,%rdx,8), %rdx
leaq (,%r13,2), %rdi
addq %r13, %rdi
movq (%r14), %r8
movq 0x18(%r14), %rsi
movq (%r8,%rdi,8), %rdi
movq 0x18(%rbx), %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,%r13,8)
incq %r9
cmpq %r9, %rbp
jne 0xb9ff
incq %r13
movq %rax, %rdi
callq 0x4340
leaq 0x38(%rsp), %rcx
cmpq %rcx, %rax
jne 0xb9c9
leaq 0x30(%rsp), %rdi
callq 0xfc66
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xba59
movq 0x20(%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 0x1b8be(%rip), %rax     # 0x27330
movl (%rax), %ecx
shrl $0x1f, %ecx
movq 0x8(%rax,%rcx,8), %rbx
leaq 0x106f5(%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 0xbaf0
jmp 0xbae4
movq %rax, %rbx
jmp 0xbafa
movq %rax, %rbx
movq 0x60(%rsp), %rdi
cmpq %r14, %rdi
je 0xbb11
movq 0x70(%rsp), %rsi
incq %rsi
jmp 0xbb0c
movq %rax, %rbx
jmp 0xbb11
jmp 0xbaed
jmp 0xbaed
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xfc66
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xbb11
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4580
nop