// File: /workspace/github_cpp_makefile_star2_O2/hanhwi[P]SimPoint/analysiscode/KMeans.cpp
// Function: KMeans::runKMeans(Dataset const&, Dataset*, int)
void KMeans::runKMeans(const Dataset &data, Dataset *centers,
                       int maxIterations) {
    Dataset tempCenters(*centers);
    Dataset *newCenters = &tempCenters, *oldCenters = centers;

    unsigned int numPoints = data.numRows();
    unsigned int k = centers->numRows();
    unsigned int dimension = centers->numCols();

    int iter;
    for (iter = 0; iter < maxIterations; iter++) {
        newCenters->fill(0.0);
        for (unsigned int ctr = 0; ctr < k; ctr++) {
            newCenters->setWeight(ctr, 0.0);
        }

        if (dimension < 3) {
            // if the dimension is low, just use this non-unrolled loop code,
            // and don't use partial distance search
            for (unsigned int point = 0; point < numPoints; point++) {
                const Datapoint &vector = data[point];
                unsigned int label = 0;
                double dist2 = DBL_MAX;
                for (unsigned int ctr = 0; ctr < k; ctr++) {
                    double d2 = 0.0;
                    const Datapoint &center = (*oldCenters)[ctr];
                    for (unsigned int d = 0; d < dimension; d++) {
                        d2 += (vector[d] - center[d]) * (vector[d] - center[d]);
                    }
                    if (d2 < dist2) { dist2 = d2; label = ctr; }
                }

                double weight = data.getWeight(point);
                for (unsigned int d = 0; d < dimension; d++) {
                    (*newCenters)[label][d] += vector[d] * weight;
                }
                newCenters->setWeight(label, newCenters->getWeight(label) + weight);
            }
        } else {
            // if the dimension is 3 or higher, use a partially-unrolled inner
            // loop and partial distance search
            for (unsigned int point = 0; point < numPoints; point++) {
                const Datapoint &vector = data[point];
                unsigned int label = 0;
                double dist2 = DBL_MAX;
                for (unsigned int ctr = 0; ctr < k; ctr++) {
                    double d2 = 0.0;
                    const Datapoint &center = (*oldCenters)[ctr];
                    // three loop iterations unrolled
                    d2 += (vector[0] - center[0]) * (vector[0] - center[0]);
                    d2 += (vector[1] - center[1]) * (vector[1] - center[1]);
                    d2 += (vector[2] - center[2]) * (vector[2] - center[2]);
                    // partial distance search ---------------> |**********|
                    for (unsigned int d = 3; (d < dimension) && (d2 < dist2); d++) {
                        d2 += (vector[d] - center[d]) * (vector[d] - center[d]);
                    }
                    if (d2 < dist2) { dist2 = d2; label = ctr; }
                }

                double weight = data.getWeight(point);
                for (unsigned int d = 0; d < dimension; d++) {
                    (*newCenters)[label][d] += vector[d] * weight;
                }
                newCenters->setWeight(label, newCenters->getWeight(label) + weight);
            }
        }

        for (unsigned int ctr = 0; ctr < k; ctr++) {
            double weight = newCenters->getWeight(ctr);
            if (weight > 0) { (*newCenters)[ctr] /= weight; }
        }

        if (tempCenters == *centers) { break; }

        Dataset *temp = newCenters;
        newCenters = oldCenters;
        oldCenters = temp;
    }

    if (newCenters != centers) {
        *centers = *newCenters;
    }

    Logger::log() << "  Number of k-means iterations performed: " << iter << endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x626a
movq 0x8(%r14), %rax
movq %r14, 0x28(%rsp)
subq (%r14), %rax
pushq $0x18
popq %rsi
cqto
idivq %rsi
movq %rax, %rcx
movq (%r15), %rdi
movq 0x8(%r15), %rax
subq %rdi, %rax
cqto
idivq %rsi
movq 0x8(%rdi), %rdx
subq (%rdi), %rdx
shrq $0x3, %rdx
movq %rdx, 0x20(%rsp)
movl %edx, %r12d
xorl %ebp, %ebp
testl %ebx, %ebx
cmovlel %ebp, %ebx
movl %ebx, 0xc(%rsp)
movl %eax, %r14d
movl %ecx, %eax
movq %rax, 0x10(%rsp)
movq %r15, %rax
movq %r13, %rbx
movq %r15, 0x18(%rsp)
cmpl 0xc(%rsp), %ebp
je 0x7c0b
movq %rax, %r13
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x632c
xorl %eax, %eax
cmpq %rax, %r14
je 0x79d6
movq 0x18(%rbx), %rcx
andq $0x0, (%rcx,%rax,8)
incq %rax
jmp 0x79c3
movq 0x28(%rsp), %rcx
movq (%rcx), %rax
movq 0x18(%rcx), %rcx
cmpl $0x3, 0x20(%rsp)
jae 0x7aaa
xorl %edx, %edx
cmpq 0x10(%rsp), %rdx
je 0x7ba8
imulq $0x18, %rdx, %rsi
addq %rax, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movsd 0xd8f2(%rip), %xmm0     # 0x15300
cmpq %r14, %rdi
je 0x7a5b
imulq $0x18, %rdi, %r9
addq (%r13), %r9
xorpd %xmm1, %xmm1
xorl %r10d, %r10d
cmpq %r10, %r12
je 0x7a46
movq (%rsi), %r11
movsd (%r11,%r10,8), %xmm2
movq (%r9), %r11
subsd (%r11,%r10,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm1
incq %r10
jmp 0x7a22
ucomisd %xmm1, %xmm0
cmoval %edi, %r8d
minsd %xmm0, %xmm1
incq %rdi
movapd %xmm1, %xmm0
jmp 0x7a0e
movsd (%rcx,%rdx,8), %xmm0
movl %r8d, %edi
xorl %r8d, %r8d
cmpq %r8, %r12
je 0x7a94
movq (%rsi), %r9
movq (%rbx), %r10
imulq $0x18, %rdi, %r11
movq (%r10,%r11), %r10
movsd (%r9,%r8,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r10,%r8,8), %xmm1
movsd %xmm1, (%r10,%r8,8)
incq %r8
jmp 0x7a66
movq 0x18(%rbx), %rsi
addsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdx
jmp 0x79ef
xorl %edx, %edx
cmpq 0x10(%rsp), %rdx
je 0x7ba8
imulq $0x18, %rdx, %rsi
movq (%rax,%rsi), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movsd 0xd834(%rip), %xmm0     # 0x15300
cmpq %r14, %rdi
je 0x7b5c
movq (%r13), %r9
imulq $0x18, %rdi, %r10
movq (%r9,%r10), %r9
movsd (%rsi), %xmm2
movsd 0x8(%rsi), %xmm3
subsd (%r9), %xmm2
mulsd %xmm2, %xmm2
addsd 0xd80d(%rip), %xmm2     # 0x15308
subsd 0x8(%r9), %xmm3
mulsd %xmm3, %xmm3
movsd 0x10(%rsi), %xmm1
subsd 0x10(%r9), %xmm1
addsd %xmm2, %xmm3
mulsd %xmm1, %xmm1
addsd %xmm3, %xmm1
pushq $0x3
popq %r10
cmpq %r12, %r10
jae 0x7b44
ucomisd %xmm1, %xmm0
jbe 0x7b44
movsd (%rsi,%r10,8), %xmm2
subsd (%r9,%r10,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm1
incq %r10
jmp 0x7b20
ucomisd %xmm1, %xmm0
cmoval %edi, %r8d
minsd %xmm0, %xmm1
incq %rdi
movapd %xmm1, %xmm0
jmp 0x7acc
movsd (%rcx,%rdx,8), %xmm0
movl %r8d, %edi
movq (%rbx), %r8
imulq $0x18, %rdi, %r9
movq (%r8,%r9), %r8
xorl %r9d, %r9d
cmpq %r9, %r12
je 0x7b92
movsd (%rsi,%r9,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r8,%r9,8), %xmm1
movsd %xmm1, (%r8,%r9,8)
incq %r9
jmp 0x7b72
movq 0x18(%rbx), %rsi
addsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdx
jmp 0x7aac
movq %rbp, 0x30(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x7bdf
movq 0x18(%rbx), %rax
movsd (%rax,%r15,8), %xmm0
ucomisd 0xd73f(%rip), %xmm0     # 0x15308
jbe 0x7bd6
movq (%rbx), %rdi
addq %rbp, %rdi
callq 0x59a8
incq %r15
addq $0x18, %rbp
jmp 0x7bb2
leaq 0x38(%rsp), %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x62a4
movq 0x30(%rsp), %rbp
testb %al, %al
jne 0x7c07
incl %ebp
movq %rbx, %rax
movq %r13, %rbx
jmp 0x79a8
movl %ebp, 0xc(%rsp)
cmpq %r15, %rbx
je 0x7c1b
movq %r15, %rdi
movq %rbx, %rsi
callq 0x8170
leaq 0x196f6(%rip), %rax     # 0x21318
movl (%rax), %ecx
shrl $0x1f, %ecx
movq 0x8(%rax,%rcx,8), %rdi
leaq 0xd6ed(%rip), %rsi      # 0x15320
callq 0x4290
movq %rax, %rdi
movl 0xc(%rsp), %esi
callq 0x4510
movq %rax, %rdi
callq 0x4180
leaq 0x38(%rsp), %rdi
callq 0x819a
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7c69
jmp 0x7c69
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x819a
movq %rbx, %rdi
callq 0x4530