// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %rbx
movq %rdx, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movq (%rdi), %rax
movq 0x8(%rax), %rsi
subq (%rax), %rsi
shrq $0x3, %rsi
leaq 0x28(%rsp), %rdi
callq 0x589e
testq %rbx, %rbx
je 0x7e17
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x587c
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
leaq 0x40(%rsp), %r13
xorl %r14d, %r14d
xorpd %xmm2, %xmm2
movq (%r12), %rsi
movq 0x8(%r12), %rax
subq %rsi, %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movl %eax, %ecx
cmpq %rcx, %r14
jae 0x7ee9
movsd %xmm2, (%rsp)
movsd %xmm1, 0x20(%rsp)
movq 0x18(%r12), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x18(%rsp)
addq %rbp, %rsi
movq (%r15), %rax
movslq (%rax,%r14,4), %rax
imulq $0x18, %rax, %rdx
movq 0x8(%rsp), %rax
addq (%rax), %rdx
movq %r13, %rdi
callq 0x5a0a
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0x58ca
movsd %xmm0, 0x10(%rsp)
movq %r13, %rdi
callq 0x5f0a
movsd 0x18(%rsp), %xmm3
movsd 0x10(%rsp), %xmm1
mulsd %xmm3, %xmm1
testq %rbx, %rbx
je 0x7eca
movq (%r15), %rax
movslq (%rax,%r14,4), %rax
movq (%rbx), %rcx
movsd (%rcx,%rax,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
movsd (%rsp), %xmm2
addsd %xmm1, %xmm2
movsd 0x20(%rsp), %xmm1
addsd %xmm3, %xmm1
incq %r14
addq $0x18, %rbp
jmp 0x7e29
movl %eax, %eax
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
testq %rbx, %rbx
je 0x7f2c
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movl %eax, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x7f2c
movq (%rbx), %rdx
movsd (%rdx,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0x7f11
divsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
leaq 0x28(%rsp), %rdi
callq 0x5f0a
movsd (%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7f64
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5f0a
jmp 0x7f67
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x5f0a
movq %rbx, %rdi
callq 0x4530
nop