// File: 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:
subq $0x248, %rsp            # imm = 0x248
movq %rdi, 0x240(%rsp)
movq %rsi, 0x238(%rsp)
movl %edx, 0x234(%rsp)
movq 0x238(%rsp), %rsi
leaq 0x200(%rsp), %rdi
movq %rdi, 0x118(%rsp)
callq 0xb050
movq 0x118(%rsp), %rax
movq %rax, 0x1f8(%rsp)
movq 0x238(%rsp), %rax
movq %rax, 0x1f0(%rsp)
movq 0x240(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x124(%rsp)
jmp 0xf6c1
movl 0x124(%rsp), %eax
movl %eax, 0x1ec(%rsp)
movq 0x238(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x114(%rsp)
jmp 0xf6e5
movl 0x114(%rsp), %eax
movl %eax, 0x1d8(%rsp)
movq 0x238(%rsp), %rdi
callq 0xc0d0
movl %eax, 0x110(%rsp)
jmp 0xf709
movl 0x110(%rsp), %eax
movl %eax, 0x1d4(%rsp)
movl $0x0, 0x1d0(%rsp)
movl 0x1d0(%rsp), %eax
cmpl 0x234(%rsp), %eax
jge 0x10138
movq 0x1f8(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0xb1e0
jmp 0xf748
movl $0x0, 0x1cc(%rsp)
movl 0x1cc(%rsp), %eax
cmpl 0x1d8(%rsp), %eax
jae 0xf7b7
movq 0x1f8(%rsp), %rdi
movl 0x1cc(%rsp), %esi
xorps %xmm0, %xmm0
callq 0xc150
jmp 0xf77c
jmp 0xf77e
movl 0x1cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1cc(%rsp)
jmp 0xf753
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
leaq 0x200(%rsp), %rdi
callq 0x10da0
jmp 0x101c7
cmpl $0x3, 0x1d4(%rsp)
jae 0xfaff
movl $0x0, 0x1c8(%rsp)
movl 0x1c8(%rsp), %eax
cmpl 0x1ec(%rsp), %eax
jae 0xfafa
movq 0x240(%rsp), %rdi
movl 0x1c8(%rsp), %eax
movl %eax, %esi
callq 0xc0f0
movq %rax, 0x1c0(%rsp)
movl $0x0, 0x1bc(%rsp)
movsd 0x23aeb(%rip), %xmm0    # 0x33300
movsd %xmm0, 0x1b0(%rsp)
movl $0x0, 0x1ac(%rsp)
movl 0x1ac(%rsp), %eax
cmpl 0x1d8(%rsp), %eax
jae 0xf9a4
xorps %xmm0, %xmm0
movsd %xmm0, 0x1a0(%rsp)
movq 0x1f0(%rsp), %rdi
movl 0x1ac(%rsp), %eax
movl %eax, %esi
callq 0xc130
movq %rax, 0x198(%rsp)
movl $0x0, 0x194(%rsp)
movl 0x194(%rsp), %eax
cmpl 0x1d4(%rsp), %eax
jae 0xf954
movq 0x1c0(%rsp), %rdi
movl 0x194(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0xf8(%rsp)
movq 0x198(%rsp), %rdi
movl 0x194(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd 0xf8(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x108(%rsp)
movq 0x1c0(%rsp), %rdi
movl 0x194(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0x100(%rsp)
movq 0x198(%rsp), %rdi
movl 0x194(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd 0x100(%rsp), %xmm2
movsd 0x108(%rsp), %xmm0
subsd (%rax), %xmm2
movsd 0x1a0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x1a0(%rsp)
movl 0x194(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x194(%rsp)
jmp 0xf872
movsd 0x1a0(%rsp), %xmm1
movsd 0x1b0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xf98c
movsd 0x1a0(%rsp), %xmm0
movsd %xmm0, 0x1b0(%rsp)
movl 0x1ac(%rsp), %eax
movl %eax, 0x1bc(%rsp)
jmp 0xf98e
movl 0x1ac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ac(%rsp)
jmp 0xf829
movq 0x240(%rsp), %rdi
movl 0x1c8(%rsp), %esi
callq 0x10d30
movsd %xmm0, 0xf0(%rsp)
jmp 0xf9c3
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x188(%rsp)
movl $0x0, 0x184(%rsp)
movl 0x184(%rsp), %eax
cmpl 0x1d4(%rsp), %eax
jae 0xfa88
movq 0x1c0(%rsp), %rdi
movl 0x184(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0x188(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
movq 0x1f8(%rsp), %rdi
movl 0x1bc(%rsp), %eax
movl %eax, %esi
callq 0xc130
movq %rax, %rdi
movl 0x184(%rsp), %eax
movl %eax, %esi
callq 0x9ba0
movsd 0xe0(%rsp), %xmm0
movsd 0xe8(%rsp), %xmm2
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl 0x184(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x184(%rsp)
jmp 0xf9e0
movq 0x1f8(%rsp), %rdi
movq %rdi, 0xc8(%rsp)
movl 0x1bc(%rsp), %esi
movl %esi, 0xd4(%rsp)
callq 0x10d30
movsd %xmm0, 0xd8(%rsp)
jmp 0xfab6
movl 0xd4(%rsp), %esi
movq 0xc8(%rsp), %rdi
movsd 0xd8(%rsp), %xmm0
movsd 0x188(%rsp), %xmm1
addsd %xmm1, %xmm0
callq 0xc150
jmp 0xfae2
jmp 0xfae4
movl 0x1c8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c8(%rsp)
jmp 0xf7d0
jmp 0x10020
movl $0x0, 0x180(%rsp)
movl 0x180(%rsp), %eax
cmpl 0x1ec(%rsp), %eax
jae 0x1001e
movq 0x240(%rsp), %rdi
movl 0x180(%rsp), %eax
movl %eax, %esi
callq 0xc0f0
movq %rax, 0x178(%rsp)
movl $0x0, 0x174(%rsp)
movsd 0x237b1(%rip), %xmm0    # 0x33300
movsd %xmm0, 0x168(%rsp)
movl $0x0, 0x164(%rsp)
movl 0x164(%rsp), %eax
cmpl 0x1d8(%rsp), %eax
jae 0xfeec
xorps %xmm0, %xmm0
movsd %xmm0, 0x158(%rsp)
movq 0x1f0(%rsp), %rdi
movl 0x164(%rsp), %eax
movl %eax, %esi
callq 0xc130
movq %rax, 0x150(%rsp)
movq 0x178(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x150(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9b80
movsd 0x80(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0x178(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0x150(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x9b80
movsd 0x88(%rsp), %xmm2
movsd 0x90(%rsp), %xmm0
subsd (%rax), %xmm2
movsd 0x158(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
movq 0x178(%rsp), %rdi
movl $0x1, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0x98(%rsp)
movq 0x150(%rsp), %rdi
movl $0x1, %esi
callq 0x9b80
movsd 0x98(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0xa8(%rsp)
movq 0x178(%rsp), %rdi
movl $0x1, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
movq 0x150(%rsp), %rdi
movl $0x1, %esi
callq 0x9b80
movsd 0xa0(%rsp), %xmm2
movsd 0xa8(%rsp), %xmm0
subsd (%rax), %xmm2
movsd 0x158(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
movq 0x178(%rsp), %rdi
movl $0x2, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0xb0(%rsp)
movq 0x150(%rsp), %rdi
movl $0x2, %esi
callq 0x9b80
movsd 0xb0(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0xc0(%rsp)
movq 0x178(%rsp), %rdi
movl $0x2, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x150(%rsp), %rdi
movl $0x2, %esi
callq 0x9b80
movsd 0xb8(%rsp), %xmm2
movsd 0xc0(%rsp), %xmm0
subsd (%rax), %xmm2
movsd 0x158(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
movl $0x3, 0x14c(%rsp)
movl 0x14c(%rsp), %ecx
xorl %eax, %eax
cmpl 0x1d4(%rsp), %ecx
movb %al, 0x7f(%rsp)
jae 0xfdd3
movsd 0x158(%rsp), %xmm1
movsd 0x168(%rsp), %xmm0
ucomisd %xmm1, %xmm0
seta %al
movb %al, 0x7f(%rsp)
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0xfde0
jmp 0xfe9c
movq 0x178(%rsp), %rdi
movl 0x14c(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x150(%rsp), %rdi
movl 0x14c(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd 0x60(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x178(%rsp), %rdi
movl 0x14c(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x150(%rsp), %rdi
movl 0x14c(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd 0x68(%rsp), %xmm2
movsd 0x70(%rsp), %xmm0
subsd (%rax), %xmm2
movsd 0x158(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
jmp 0xfda0
movsd 0x158(%rsp), %xmm1
movsd 0x168(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfed4
movsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x168(%rsp)
movl 0x164(%rsp), %eax
movl %eax, 0x174(%rsp)
jmp 0xfed6
movl 0x164(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x164(%rsp)
jmp 0xfb63
movq 0x240(%rsp), %rdi
movl 0x180(%rsp), %esi
callq 0x10d30
movsd %xmm0, 0x58(%rsp)
jmp 0xff08
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movl $0x0, 0x13c(%rsp)
movl 0x13c(%rsp), %eax
cmpl 0x1d4(%rsp), %eax
jae 0xffbe
movq 0x178(%rsp), %rdi
movl 0x13c(%rsp), %eax
movl %eax, %esi
callq 0x9b80
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x140(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x1f8(%rsp), %rdi
movl 0x174(%rsp), %eax
movl %eax, %esi
callq 0xc130
movq %rax, %rdi
movl 0x13c(%rsp), %eax
movl %eax, %esi
callq 0x9ba0
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm2
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl 0x13c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x13c(%rsp)
jmp 0xff22
movq 0x1f8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movl 0x174(%rsp), %esi
movl %esi, 0x3c(%rsp)
callq 0x10d30
movsd %xmm0, 0x40(%rsp)
jmp 0xffe3
movl 0x3c(%rsp), %esi
movq 0x30(%rsp), %rdi
movsd 0x40(%rsp), %xmm0
movsd 0x140(%rsp), %xmm1
addsd %xmm1, %xmm0
callq 0xc150
jmp 0x10006
jmp 0x10008
movl 0x180(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x180(%rsp)
jmp 0xfb0a
jmp 0x10020
movl $0x0, 0x138(%rsp)
movl 0x138(%rsp), %eax
cmpl 0x1d8(%rsp), %eax
jae 0x100cb
movq 0x1f8(%rsp), %rdi
movl 0x138(%rsp), %esi
callq 0x10d30
movsd %xmm0, 0x28(%rsp)
jmp 0x1005b
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x130(%rsp)
movsd 0x130(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x100b3
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x1f8(%rsp), %rdi
movl 0x138(%rsp), %eax
movl %eax, %esi
callq 0xc130
movsd 0x20(%rsp), %xmm0
movq %rax, %rdi
callq 0x9130
jmp 0x100b1
jmp 0x100b3
jmp 0x100b5
movl 0x138(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x138(%rsp)
jmp 0x1002b
movq 0x238(%rsp), %rsi
leaq 0x200(%rsp), %rdi
callq 0xb0b0
movb %al, 0x1f(%rsp)
jmp 0x100e6
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x100f0
jmp 0x100f2
jmp 0x10138
movq 0x1f8(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0x1f8(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x1f0(%rsp)
movl 0x1d0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1d0(%rsp)
jmp 0xf722
movq 0x1f8(%rsp), %rax
cmpq 0x238(%rsp), %rax
je 0x10163
movq 0x1f8(%rsp), %rsi
movq 0x238(%rsp), %rdi
callq 0x10d60
jmp 0x10161
jmp 0x10163
xorl %edi, %edi
callq 0x7ba0
movq %rax, 0x10(%rsp)
jmp 0x10171
movq 0x10(%rsp), %rdi
leaq 0x231ab(%rip), %rsi     # 0x33328
callq 0x5360
movq %rax, 0x8(%rsp)
jmp 0x10189
movq 0x8(%rsp), %rdi
movl 0x1d0(%rsp), %esi
callq 0x5680
movq %rax, (%rsp)
jmp 0x101a0
movq (%rsp), %rdi
movq 0x33e15(%rip), %rsi     # 0x43fc0
callq 0x53c0
jmp 0x101b2
leaq 0x200(%rsp), %rdi
callq 0x10da0
addq $0x248, %rsp            # imm = 0x248
retq
movq 0x1e0(%rsp), %rdi
callq 0x5690
nopw %cs:(%rax,%rax)