// File: /workspace/github_cpp_makefile_star2_O3/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 0x952a
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x8f00
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x9090
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
xorpd %xmm2, %xmm2
testl %eax, %eax
je 0x937a
movq 0x18(%r14), %rcx
movl %eax, %eax
xorl %edx, %edx
addsd (%rcx,%rdx,8), %xmm2
incq %rdx
cmpq %rdx, %rax
jne 0x936d
movsd %xmm2, 0x10(%rsp)
movsd %xmm1, 0x30(%rsp)
mulsd %xmm3, %xmm1
divsd %xmm1, %xmm0
mulsd 0x12fba(%rip), %xmm0    # 0x1c350
movsd %xmm3, (%rsp)
callq 0x4220
addsd 0x12ee0(%rip), %xmm0    # 0x1c288
mulsd (%rsp), %xmm0
mulsd 0x12fa3(%rip), %xmm0    # 0x1c358
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
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
imull $0xaaaaaaab, %edx, %edx # imm = 0xAAAAAAAB
testl %edx, %edx
je 0x945f
xorl %r12d, %r12d
xorpd %xmm1, %xmm1
movq 0x18(%rsp), %rdx
movl (%rdx,%r12,4), %edx
movq 0x18(%rbx), %rsi
movsd (%rsi,%rdx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x943b
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
movq (%r14), %rax
movq 0x8(%r14), %rcx
incq %r12
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r15, %rdx
movl %edx, %esi
cmpq %rsi, %r12
jb 0x93f5
movl %edx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
jmp 0x9463
xorpd %xmm0, %xmm0
movq (%rbx), %r15
movq 0x8(%rbx), %rbx
movq (%rax), %r12
movq 0x8(%rax), %r14
callq 0x4220
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x9499
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 0x12e94(%rip), %xmm2    # 0x1c358
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 0x94e5
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x94ff
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4580
nop