// File: /workspace/github_cpp_makefile_star2_O2/hanhwi[P]SimPoint/analysiscode/KMeans.cpp
// Function: KMeans::bicScore(Dataset const&, Dataset const&)
double KMeans::bicScore(const Dataset &data, const Dataset &centers) {
    vector<int> labels(data.numRows());
    findLabelsAndDists(data, centers, &labels);

    double dist = distortion(data, labels, centers);

    double n = data.numRows();
    double dim = data.numCols();
    double totalWeight = 0.0;
    for (unsigned int i = 0; i < data.numRows(); i++) {
        totalWeight += data.getWeight(i);
    }
    double sigma2 = dist / (dim * n);

    const double PI = 3.14159265358979;

    double likelihood = - dim * (log(2.0 * PI * sigma2) + 1) / 2.0 - log(totalWeight);

    for (unsigned int i = 0; i < data.numRows(); i++) {
        double wt = centers.getWeight(labels[i]);
        if (wt > 0) {
            likelihood += log(wt) * data.getWeight(i) / totalWeight;
        }
    }
    likelihood = likelihood * n;

    double numParameters = (centers.numRows() - 1) + // cluster probabilities
                           centers.numRows() * data.numCols() + // cluster means
                           1; // variances

    double penalty = numParameters / 2.0 * log((double)data.numRows());

    double score = likelihood - penalty;

    return score;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x8(%rdi), %rax
subq (%rdi), %rax
pushq $0x18
popq %r12
cqto
idivq %r12
movl %eax, %esi
leaq 0x20(%rsp), %r15
leaq 0xf(%rsp), %rdx
movq %r15, %rdi
callq 0x81b0
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x7c7e
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x7dd2
movq (%r14), %rsi
movq 0x8(%r14), %rax
subq %rsi, %rax
cqto
idivq %r12
movq 0x8(%rsi), %rcx
subq (%rsi), %rcx
shrq $0x3, %rcx
movq 0x18(%r14), %rdx
movl %eax, %eax
xorpd %xmm1, %xmm1
xorl %esi, %esi
cmpq %rsi, %rax
je 0x8006
addsd (%rdx,%rsi,8), %xmm1
incq %rsi
jmp 0x7ff7
movsd %xmm1, 0x10(%rsp)
cvtsi2sd %rax, %xmm1
movl %ecx, %eax
cvtsi2sd %rax, %xmm2
movsd %xmm2, (%rsp)
movsd %xmm1, 0x18(%rsp)
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
mulsd 0xd2dd(%rip), %xmm0     # 0x15310
callq 0x4230
addsd 0xd210(%rip), %xmm0     # 0x15250
mulsd (%rsp), %xmm0
mulsd 0xd2cb(%rip), %xmm0     # 0x15318
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x4230
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
xorl %r15d, %r15d
xorpd %xmm1, %xmm1
movq (%r14), %rsi
movq 0x8(%r14), %rax
subq %rsi, %rax
cqto
idivq %r12
movq %rax, %rcx
movl %ecx, %eax
cmpq %rax, %r15
jae 0x80cf
movq 0x20(%rsp), %rax
movl (%rax,%r15,4), %eax
movq 0x18(%rbx), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x80ca
callq 0x4230
movq 0x18(%r14), %rax
mulsd (%rax,%r15,8), %xmm0
divsd 0x10(%rsp), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
xorpd %xmm1, %xmm1
incq %r15
jmp 0x8072
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
movq 0x8(%rsi), %rdx
subq (%rsi), %rdx
shrq $0x3, %rdx
incl %edx
imull %eax, %edx
cvtsi2sd %rdx, %xmm0
mulsd 0xd220(%rip), %xmm0     # 0x15318
movsd %xmm0, 0x10(%rsp)
movsd (%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rsp)
movl %ecx, %eax
cvtsi2sd %rax, %xmm0
callq 0x4230
mulsd 0x10(%rsp), %xmm0
addsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
leaq 0x20(%rsp), %rdi
callq 0x8498
movsd (%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x8147
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8498
movq %rbx, %rdi
callq 0x4530