// File: hanhwi[P]SimPoint/analysiscode/KMeans.cpp
// Function: KMeans::distortion(Dataset const&, std::vector<int, std::allocator<int>> const&, Dataset const&, Datapoint*)
double KMeans::distortion(const Dataset &data, const vector<int> &labels,
                          const Dataset &centers, Datapoint *distortionPerCluster) {
    double dist = 0.0;
    Datapoint origin(data.numCols()); // the zero vector

    if (distortionPerCluster) {
        distortionPerCluster->fill(0.0);
    }

    unsigned int i;
    double avgWeight = 0.0;
    for (i = 0; i < data.numRows(); i++) {
        double weight = data.getWeight(i);
        double pointDistortion = 
            (data[i] - centers[labels[i]]).distSquared(origin) * weight;
        dist += pointDistortion;
        avgWeight += weight;
        if (distortionPerCluster) {
            (*distortionPerCluster)[labels[i]] += pointDistortion;
        }
    }
    avgWeight = avgWeight / data.numRows();

    dist = dist / avgWeight;

    if (distortionPerCluster) {
        for (unsigned int k = 0; k < centers.numRows(); k++) {
            (*distortionPerCluster)[k] /= avgWeight;
        }
    }

    return dist;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq %rdx, 0xe0(%rsp)
movq %rcx, 0xd8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0xc0d0
movl %eax, %esi
leaq 0xb8(%rsp), %rdi
callq 0x8d60
cmpq $0x0, 0xd8(%rsp)
je 0x104b7
movq 0xd8(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x8cf0
jmp 0x1049c
jmp 0x104b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x1079b
xorps %xmm0, %xmm0
movsd %xmm0, 0xa0(%rsp)
movl $0x0, 0xa8(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0x6c(%rsp)
movq 0xf0(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x70(%rsp)
jmp 0x104ec
movl 0x6c(%rsp), %eax
movl 0x70(%rsp), %ecx
cmpl %ecx, %eax
jae 0x10699
movq 0xf0(%rsp), %rdi
movl 0xa8(%rsp), %esi
callq 0x10d30
movsd %xmm0, 0x60(%rsp)
jmp 0x10518
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movq 0xf0(%rsp), %rdi
movl 0xa8(%rsp), %eax
movl %eax, %esi
callq 0xc0f0
movq %rax, 0x58(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xe8(%rsp), %rdi
movl 0xa8(%rsp), %eax
movl %eax, %esi
callq 0x10e10
movq 0x50(%rsp), %rdi
movslq (%rax), %rsi
callq 0xc0f0
movq 0x58(%rsp), %rsi
movq %rax, %rdx
leaq 0x78(%rsp), %rdi
callq 0x9250
jmp 0x10586
leaq 0x78(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x8e30
movsd %xmm0, 0x48(%rsp)
jmp 0x105a0
movsd 0x48(%rsp), %xmm0
mulsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x9bc0
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
addsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x98(%rsp), %xmm0
addsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
cmpq $0x0, 0xd8(%rsp)
je 0x10681
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xe8(%rsp), %rdi
movl 0xa8(%rsp), %eax
movl %eax, %esi
callq 0x10e10
movq 0x30(%rsp), %rdi
movslq (%rax), %rsi
callq 0x9ba0
movsd 0x38(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x10681
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x9bc0
jmp 0x1079b
jmp 0x10683
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x104ce
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0xf0(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x2c(%rsp)
jmp 0x106bb
movsd 0x20(%rsp), %xmm0
movl 0x2c(%rsp), %eax
movl %eax, %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xd0(%rsp), %xmm0
divsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
cmpq $0x0, 0xd8(%rsp)
je 0x10771
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0xe0(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x1c(%rsp)
jmp 0x10722
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
cmpl %ecx, %eax
jae 0x1076f
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xd8(%rsp), %rdi
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x9ba0
movsd 0x10(%rsp), %xmm1
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x10707
jmp 0x10771
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x9bc0
movsd 0x8(%rsp), %xmm0
addq $0xf8, %rsp
retq
leaq 0xb8(%rsp), %rdi
callq 0x9bc0
movq 0xb0(%rsp), %rdi
callq 0x5690
nopw %cs:(%rax,%rax)