// File: /workspace/github_cpp_makefile_star2_O1/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
shrq $0x3, %rax
imull $0xaaaaaaab, %eax, %esi # imm = 0xAAAAAAAB
leaq 0x18(%rsp), %r15
leaq 0xf(%rsp), %rdx
movq %r15, %rdi
callq 0x950a
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x8ef4
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x9084
movq (%r14), %rcx
movq 0x8(%r14), %rax
subq %rcx, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r15, %rax
movl %eax, %edx
cvtsi2sd %rdx, %xmm1
movq 0x8(%rcx), %rdx
subq (%rcx), %rdx
shrq $0x3, %rdx
movl %edx, %ecx
cvtsi2sd %rcx, %xmm3
testl %eax, %eax
je 0x9362
movq 0x18(%r14), %rcx
movl %eax, %eax
xorpd %xmm2, %xmm2
xorl %edx, %edx
addsd (%rcx,%rdx,8), %xmm2
incq %rdx
cmpq %rdx, %rax
jne 0x9353
jmp 0x9366
xorpd %xmm2, %xmm2
movsd %xmm2, 0x10(%rsp)
movsd %xmm1, 0x30(%rsp)
mulsd %xmm3, %xmm1
divsd %xmm1, %xmm0
mulsd 0x12fc6(%rip), %xmm0    # 0x1c348
movsd %xmm3, (%rsp)
callq 0x4220
addsd 0x12ef4(%rip), %xmm0    # 0x1c288
mulsd (%rsp), %xmm0
mulsd 0x12faf(%rip), %xmm0    # 0x1c350
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x4220
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
imull $0xaaaaaaab, %ecx, %ecx # imm = 0xAAAAAAAB
testl %ecx, %ecx
je 0x9439
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
movq 0x18(%rsp), %rax
movl (%rax,%r12,4), %eax
movq 0x18(%rbx), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x941d
callq 0x4220
movq 0x18(%r14), %rax
mulsd (%rax,%r12,8), %xmm0
divsd 0x10(%rsp), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
xorpd %xmm1, %xmm1
incq %r12
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r15, %rcx
movl %ecx, %edx
cmpq %rdx, %r12
jb 0x93de
movq (%rbx), %r15
movq 0x8(%rbx), %rbx
movq (%rax), %r12
movq 0x8(%rax), %r14
movl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
callq 0x4220
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x9479
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
movsd %xmm0, 0x10(%rsp)
callq 0x42d0
movsd 0x10(%rsp), %xmm0
subq %r12, %r14
shrq $0x3, %r14
incl %r14d
subq %r15, %rbx
shrq $0x3, %rbx
imull %r14d, %ebx
imull $0xaaaaaaab, %ebx, %eax # imm = 0xAAAAAAAB
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd 0x12eac(%rip), %xmm2    # 0x1c350
movsd (%rsp), %xmm1
mulsd 0x30(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x94c5
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x94df
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4580
nop