// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movl %edx, %ebx
movq %rsi, %rbp
movq %rdi, %r14
leaq 0x68(%rsp), %r12
movq %r12, %rdi
callq 0x6efc
movl %ebx, 0xc(%rsp)
testl %ebx, %ebx
jle 0x8e1b
movq 0x8(%r14), %rdx
movq %r14, 0x30(%rsp)
subq (%r14), %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rdx
movq (%rbp), %rcx
movq 0x8(%rbp), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
imulq %rax, %rsi
movq 0x8(%rcx), %r14
subq (%rcx), %r14
shrq $0x3, %r14
movq %rsi, 0x10(%rsp)
movl %esi, %r13d
leaq (,%r13,8), %rax
movq %rax, 0x48(%rsp)
movl %r14d, %ebx
movq %rdx, 0x38(%rsp)
movl %edx, %eax
movq %rax, 0x40(%rsp)
movl %r14d, %r15d
andl $0x3, %r15d
movq $0x0, 0x18(%rsp)
leaq 0x68(%rsp), %rax
movq %rbp, %r12
movq %rbp, 0x50(%rsp)
movq %r12, 0x28(%rsp)
movq %rax, %r12
xorpd %xmm0, %xmm0
movq %rax, %rdi
callq 0x6fde
cmpl $0x0, 0x10(%rsp)
je 0x8b2b
movq 0x18(%r12), %rdi
xorl %esi, %esi
movq 0x48(%rsp), %rdx
callq 0x4190
cmpl $0x3, %r14d
jae 0x8c33
cmpl $0x0, 0x38(%rsp)
je 0x8d9e
movq 0x30(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x18(%rax), %rcx
movq 0x18(%r12), %rdx
xorl %esi, %esi
leaq (%rsi,%rsi,2), %rax
movq 0x20(%rsp), %rdi
leaq (%rdi,%rax,8), %rdi
cmpl $0x0, 0x10(%rsp)
je 0x8bd7
movq 0x28(%rsp), %rax
movq (%rax), %r8
xorl %r9d, %r9d
movsd 0x137b9(%rip), %xmm0    # 0x1c338
xorl %r10d, %r10d
testl %r14d, %r14d
je 0x8bb6
leaq (%r9,%r9,2), %rax
movq (%rdi), %r11
movq (%r8,%rax,8), %rbp
xorpd %xmm1, %xmm1
xorl %eax, %eax
movsd (%r11,%rax,8), %xmm2
subsd (%rbp,%rax,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm1
incq %rax
cmpq %rax, %r15
jne 0x8b98
jmp 0x8bba
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
cmoval %r9d, %r10d
minsd %xmm0, %xmm1
incq %r9
movapd %xmm1, %xmm0
cmpq %r13, %r9
jne 0x8b82
movl %r10d, %r8d
jmp 0x8bda
xorl %r8d, %r8d
movsd (%rcx,%rsi,8), %xmm0
testl %r14d, %r14d
je 0x8c14
movq (%rdi), %rdi
movq (%r12), %rax
leaq (%r8,%r8,2), %r9
movq (%rax,%r9,8), %rax
xorl %r9d, %r9d
movsd (%rdi,%r9,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rax,%r9,8), %xmm1
movsd %xmm1, (%rax,%r9,8)
incq %r9
cmpq %r9, %r15
jne 0x8bf6
addsd (%rdx,%r8,8), %xmm0
movsd %xmm0, (%rdx,%r8,8)
incq %rsi
cmpq 0x40(%rsp), %rsi
jne 0x8b58
jmp 0x8d9e
cmpl $0x0, 0x38(%rsp)
je 0x8d9e
movq 0x30(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x20(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x60(%rsp)
movq (%r12), %rax
movq %rax, 0x58(%rsp)
movq 0x18(%r12), %rsi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %r8
cmpl $0x0, 0x10(%rsp)
je 0x8d49
movq 0x28(%rsp), %rax
movq (%rax), %r9
movq (%r8), %r10
movsd (%r10), %xmm0
movsd 0x8(%r10), %xmm1
movsd 0x10(%r10), %xmm2
xorl %r11d, %r11d
movsd 0x13695(%rip), %xmm3    # 0x1c338
xorl %ebp, %ebp
leaq (%r11,%r11,2), %rax
movq (%r9,%rax,8), %rax
movapd %xmm0, %xmm4
subsd (%rax), %xmm4
mulsd %xmm4, %xmm4
addsd 0x1367f(%rip), %xmm4    # 0x1c340
movapd %xmm1, %xmm5
subsd 0x8(%rax), %xmm5
mulsd %xmm5, %xmm5
addsd %xmm4, %xmm5
movapd %xmm2, %xmm4
subsd 0x10(%rax), %xmm4
mulsd %xmm4, %xmm4
addsd %xmm5, %xmm4
ucomisd %xmm4, %xmm3
seta %cl
cmpl $0x4, %r14d
jb 0x8d29
ucomisd %xmm4, %xmm3
jbe 0x8d29
movl $0x4, %edx
movapd %xmm4, %xmm5
movsd -0x8(%r10,%rdx,8), %xmm4
subsd -0x8(%rax,%rdx,8), %xmm4
mulsd %xmm4, %xmm4
addsd %xmm5, %xmm4
ucomisd %xmm4, %xmm3
seta %cl
cmpq %rbx, %rdx
jae 0x8d29
incq %rdx
ucomisd %xmm4, %xmm3
ja 0x8cfb
testb %cl, %cl
cmovnel %r11d, %ebp
jne 0x8d35
movapd %xmm3, %xmm4
incq %r11
movapd %xmm4, %xmm3
cmpq %r13, %r11
jne 0x8ca5
movl %ebp, %eax
jmp 0x8d4b
xorl %eax, %eax
movq 0x60(%rsp), %rcx
movsd (%rcx,%rdi,8), %xmm0
movq (%r8), %rcx
leaq (%rax,%rax,2), %rdx
movq 0x58(%rsp), %r8
movq (%r8,%rdx,8), %rdx
xorl %r8d, %r8d
movsd (%rcx,%r8,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%rdx,%r8,8), %xmm1
movsd %xmm1, (%rdx,%r8,8)
incq %r8
cmpq %r8, %rbx
jne 0x8d68
addsd (%rsi,%rax,8), %xmm0
movsd %xmm0, (%rsi,%rax,8)
incq %rdi
cmpq 0x40(%rsp), %rdi
jne 0x8c64
cmpl $0x0, 0x10(%rsp)
je 0x8ddb
xorl %ebp, %ebp
movq 0x18(%r12), %rax
movsd (%rax,%rbp,8), %xmm0
ucomisd 0x13587(%rip), %xmm0    # 0x1c340
jbe 0x8dd3
leaq (,%rbp,2), %rdi
addq %rbp, %rdi
shlq $0x3, %rdi
addq (%r12), %rdi
callq 0x656c
incq %rbp
cmpq %r13, %rbp
jne 0x8da7
leaq 0x68(%rsp), %rdi
movq 0x50(%rsp), %rbp
movq %rbp, %rsi
callq 0x6f36
testb %al, %al
jne 0x8e20
movq 0x18(%rsp), %rcx
incl %ecx
movq 0x28(%rsp), %rdx
movq %rdx, %rax
movq %rcx, 0x18(%rsp)
cmpl 0xc(%rsp), %ecx
jne 0x8aff
movl 0xc(%rsp), %eax
movl %eax, %r14d
movq %rdx, %r12
jmp 0x8e25
xorl %r14d, %r14d
jmp 0x8e25
movq 0x18(%rsp), %r14
cmpq %rbp, %r12
je 0x8e48
movq %rbp, %rdi
movq %r12, %rsi
callq 0x9594
addq $0x18, %rbp
addq $0x18, %r12
movq %rbp, %rdi
movq %r12, %rsi
callq 0x8260
leaq 0x1e4e1(%rip), %rax     # 0x27330
movl (%rax), %ecx
shrl $0x1f, %ecx
movq 0x8(%rax,%rcx,8), %rbx
leaq 0x134f8(%rip), %rsi     # 0x1c358
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x4330
movq %rbx, %rdi
movl %r14d, %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 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x8ebf
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
leaq 0x68(%rsp), %rdi
callq 0x7bf4
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8edf
jmp 0x8edf
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x94e8
movq %rbx, %rdi
callq 0x4580