// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%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 0x643c
testq %rbx, %rbx
je 0x90c9
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x6390
movq (%r12), %rsi
movq 0x8(%r12), %rax
subq %rsi, %rax
shrq $0x3, %rax
imull $0xaaaaaaab, %eax, %eax # imm = 0xAAAAAAAB
testl %eax, %eax
je 0x91e1
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
leaq 0x40(%rsp), %r13
xorl %r14d, %r14d
movsd %xmm0, (%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
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
movq 0x10(%rsp), %rax
addq (%rax), %rdx
movq %r13, %rdi
callq 0x65d2
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0x647c
movapd %xmm0, %xmm1
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x9167
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
movsd %xmm1, 0x20(%rsp)
callq 0x42d0
movsd 0x20(%rsp), %xmm1
mulsd 0x18(%rsp), %xmm1
testq %rbx, %rbx
je 0x918a
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 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
incq %r14
movq (%r12), %rsi
movq 0x8(%r12), %rax
subq %rsi, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
movl %eax, %ecx
addq $0x18, %rbp
cmpq %rcx, %r14
jb 0x9100
movsd (%rsp), %xmm1
jmp 0x91ef
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
testq %rbx, %rbx
je 0x9241
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rax # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
testl %ecx, %ecx
je 0x9241
movq (%rbx), %rax
movl %ecx, %ecx
xorl %edx, %edx
movsd (%rax,%rdx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x922b
movsd %xmm1, (%rsp)
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x925d
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
movsd 0x8(%rsp), %xmm0
divsd (%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9279
movq %rax, %rbx
jmp 0x9298
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x9298
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x92af
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4580
nop