// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x6ee8
movl %ebx, 0xc(%rsp)
testl %ebx, %ebx
jle 0x8e2a
movq 0x8(%r15), %rdx
movq %r15, 0x20(%rsp)
subq (%r15), %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rdx
movq (%r14), %rcx
movq 0x8(%r14), %rsi
movq 0x8(%rcx), %r15
subq (%rcx), %r15
shrq $0x3, %r15
subq %rcx, %rsi
sarq $0x3, %rsi
imulq %rax, %rsi
movq %rsi, 0x10(%rsp)
movl %esi, %r13d
leaq (,%r13,8), %rax
movq %rax, 0x40(%rsp)
movl %r15d, %ebx
movq %rdx, 0x28(%rsp)
movl %edx, %eax
movq %rax, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x58(%rsp), %rax
movq %r14, %r12
movq %r14, 0x48(%rsp)
movq %r12, %rbp
movq %rax, %r12
xorpd %xmm0, %xmm0
movq %rax, %rdi
callq 0x6fc2
cmpl $0x0, 0x10(%rsp)
je 0x8b55
movq 0x18(%r12), %rdi
xorl %esi, %esi
movq 0x40(%rsp), %rdx
callq 0x4190
cmpl $0x3, %r15d
movq %rbp, 0x30(%rsp)
jae 0x8c59
cmpl $0x0, 0x28(%rsp)
je 0x8db7
movq 0x20(%rsp), %rcx
movq (%rcx), %rax
movq 0x18(%rcx), %rcx
movq 0x18(%r12), %rdx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rdi
leaq (%rax,%rdi,8), %rdi
cmpl $0x0, 0x10(%rsp)
je 0x8bfd
movq (%rbp), %r8
xorl %r9d, %r9d
movsd 0x13798(%rip), %xmm0    # 0x1c338
xorl %r10d, %r10d
testl %r15d, %r15d
je 0x8bd7
leaq (%r9,%r9,2), %r14
movq (%rdi), %r11
movq (%r8,%r14,8), %r14
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
movsd (%r11,%rbp,8), %xmm2
subsd (%r14,%rbp,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm1
incq %rbp
cmpq %rbp, %rbx
jne 0x8bb9
jmp 0x8bdb
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
cmoval %r9d, %r10d
minsd %xmm0, %xmm1
incq %r9
movapd %xmm1, %xmm0
cmpq %r13, %r9
jne 0x8ba3
movl %r10d, %r8d
movq 0x30(%rsp), %rbp
jmp 0x8c00
xorl %r8d, %r8d
movsd (%rcx,%rsi,8), %xmm0
testl %r15d, %r15d
je 0x8c3a
movq (%rdi), %rdi
movq (%r12), %r9
leaq (%r8,%r8,2), %r10
movq (%r9,%r10,8), %r9
xorl %r10d, %r10d
movsd (%rdi,%r10,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r9,%r10,8), %xmm1
movsd %xmm1, (%r9,%r10,8)
incq %r10
cmpq %r10, %rbx
jne 0x8c1c
addsd (%rdx,%r8,8), %xmm0
movsd %xmm0, (%rdx,%r8,8)
incq %rsi
cmpq 0x38(%rsp), %rsi
jne 0x8b82
jmp 0x8db7
cmpl $0x0, 0x28(%rsp)
je 0x8db7
movq 0x20(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x50(%rsp)
movq 0x18(%rax), %rcx
movq (%r12), %rdx
movq 0x18(%r12), %rsi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %rax
movq 0x50(%rsp), %r8
movq (%r8,%rax,8), %r8
cmpl $0x0, 0x10(%rsp)
je 0x8d6c
movq (%rbp), %r9
movsd (%r8), %xmm0
movsd 0x8(%r8), %xmm1
movsd 0x10(%r8), %xmm2
xorl %r10d, %r10d
movsd 0x13680(%rip), %xmm3    # 0x1c338
xorl %r11d, %r11d
leaq (%r10,%r10,2), %rax
movq (%r9,%rax,8), %r14
movapd %xmm0, %xmm4
subsd (%r14), %xmm4
mulsd %xmm4, %xmm4
addsd 0x13668(%rip), %xmm4    # 0x1c340
movapd %xmm1, %xmm5
subsd 0x8(%r14), %xmm5
mulsd %xmm5, %xmm5
addsd %xmm4, %xmm5
movapd %xmm2, %xmm4
subsd 0x10(%r14), %xmm4
mulsd %xmm4, %xmm4
addsd %xmm5, %xmm4
ucomisd %xmm4, %xmm3
seta %bpl
cmpl $0x4, %r15d
jb 0x8d45
ucomisd %xmm4, %xmm3
jbe 0x8d45
movl $0x4, %eax
movapd %xmm4, %xmm5
movsd -0x8(%r8,%rax,8), %xmm4
subsd -0x8(%r14,%rax,8), %xmm4
mulsd %xmm4, %xmm4
addsd %xmm5, %xmm4
ucomisd %xmm4, %xmm3
seta %bpl
cmpq %rbx, %rax
jae 0x8d45
incq %rax
ucomisd %xmm4, %xmm3
ja 0x8d15
testb %bpl, %bpl
cmovnel %r10d, %r11d
jne 0x8d52
movapd %xmm3, %xmm4
incq %r10
movapd %xmm4, %xmm3
cmpq %r13, %r10
jne 0x8cbb
movl %r11d, %r9d
movq 0x30(%rsp), %rbp
jmp 0x8d6f
xorl %r9d, %r9d
movsd (%rcx,%rdi,8), %xmm0
leaq (%r9,%r9,2), %rax
movq (%rdx,%rax,8), %rax
xorl %r10d, %r10d
movsd (%r8,%r10,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rax,%r10,8), %xmm1
movsd %xmm1, (%rax,%r10,8)
incq %r10
cmpq %r10, %rbx
jne 0x8d7f
addsd (%rsi,%r9,8), %xmm0
movsd %xmm0, (%rsi,%r9,8)
incq %rdi
cmpq 0x38(%rsp), %rdi
jne 0x8c80
cmpl $0x0, 0x10(%rsp)
je 0x8def
xorl %r14d, %r14d
movq 0x18(%r12), %rax
movsd (%rax,%r14,8), %xmm0
ucomisd 0x1356c(%rip), %xmm0    # 0x1c340
jbe 0x8de7
leaq (%r14,%r14,2), %rdi
shlq $0x3, %rdi
addq (%r12), %rdi
callq 0x6554
incq %r14
cmpq %r13, %r14
jne 0x8dc1
leaq 0x58(%rsp), %rdi
movq 0x48(%rsp), %r14
movq %r14, %rsi
callq 0x6f22
testb %al, %al
jne 0x8e2f
movq 0x18(%rsp), %rcx
incl %ecx
movq %rbp, %rax
movq %rcx, 0x18(%rsp)
cmpl 0xc(%rsp), %ecx
jne 0x8b2b
movl 0xc(%rsp), %eax
movl %eax, %r15d
movq %rbp, %r12
jmp 0x8e34
xorl %r15d, %r15d
jmp 0x8e34
movq 0x18(%rsp), %r15
cmpq %r14, %r12
je 0x8e57
movq %r14, %rdi
movq %r12, %rsi
callq 0x95b6
addq $0x18, %r14
addq $0x18, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x828a
leaq 0x1e4d2(%rip), %rax     # 0x27330
movl (%rax), %ecx
shrl $0x1f, %ecx
movq 0x8(%rax,%rcx,8), %rbx
leaq 0x134f1(%rip), %rsi     # 0x1c360
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x4330
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4560
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x41d0
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x8ecb
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
leaq 0x58(%rsp), %rdi
callq 0x7be2
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8eeb
jmp 0x8eeb
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x9508
movq %rbx, %rdi
callq 0x4580