// File: 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:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq 0x130(%rsp), %rdi
callq 0xc0c0
movl %eax, %eax
movq %rax, 0x80(%rsp)
leaq 0x10f(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x10e30
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x110(%rsp), %rdi
callq 0x10e40
jmp 0x10822
leaq 0x10f(%rsp), %rdi
callq 0x10ec0
movq 0x130(%rsp), %rdi
movq 0x128(%rsp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x110(%rsp), %rdx
callq 0x101e0
jmp 0x10852
movq 0x130(%rsp), %rdi
movq 0x128(%rsp), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq 0x110(%rsp), %rsi
callq 0x10430
movsd %xmm0, 0x78(%rsp)
jmp 0x1087b
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movq 0x130(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x74(%rsp)
jmp 0x1089d
movl 0x74(%rsp), %eax
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0xe8(%rsp)
movq 0x130(%rsp), %rdi
callq 0xc0d0
movl %eax, 0x70(%rsp)
jmp 0x108c4
movl 0x70(%rsp), %eax
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0xe0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd8(%rsp)
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x68(%rsp)
movq 0x130(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x6c(%rsp)
jmp 0x1090d
movl 0x68(%rsp), %eax
movl 0x6c(%rsp), %ecx
cmpl %ecx, %eax
jae 0x109b0
movq 0x130(%rsp), %rdi
movl 0xd4(%rsp), %esi
callq 0x10d30
movsd %xmm0, 0x60(%rsp)
jmp 0x10939
movsd 0x60(%rsp), %xmm0
addsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x108ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
leaq 0x10f(%rsp), %rdi
callq 0x10ec0
jmp 0x10c96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x10ed0
jmp 0x10c96
movsd 0xf0(%rsp), %xmm0
movsd 0xe0(%rsp), %xmm1
mulsd 0xe8(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0x22938(%rip), %xmm0    # 0x33318
movsd %xmm0, 0xc0(%rsp)
movsd 0xe0(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x228f9(%rip), %xmm0    # 0x33310
mulsd 0xc8(%rsp), %xmm0
callq 0x52d0
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
movsd 0x2281a(%rip), %xmm2    # 0x33250
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd 0x228c2(%rip), %xmm1    # 0x33308
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0xd8(%rsp), %xmm0
callq 0x52d0
movaps %xmm0, %xmm1
movsd 0x58(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x130(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x4c(%rsp)
jmp 0x10a9d
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jae 0x10b78
movq 0x128(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, %esi
leaq 0x110(%rsp), %rdi
callq 0x10dd0
movq 0x38(%rsp), %rdi
movl (%rax), %esi
callq 0x10d30
movsd %xmm0, 0x40(%rsp)
jmp 0x10ae4
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x10b60
movsd 0xa8(%rsp), %xmm0
callq 0x52d0
movsd %xmm0, 0x28(%rsp)
movq 0x130(%rsp), %rdi
movl 0xb4(%rsp), %esi
callq 0x10d30
movsd %xmm0, 0x30(%rsp)
jmp 0x10b35
movsd 0x30(%rsp), %xmm1
movsd 0x28(%rsp), %xmm0
mulsd %xmm1, %xmm0
divsd 0xd8(%rsp), %xmm0
addsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
jmp 0x10b62
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x10a7f
movsd 0xb8(%rsp), %xmm0
movsd 0xe8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x128(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x24(%rsp)
jmp 0x10baa
movl 0x24(%rsp), %eax
decl %eax
movl %eax, 0x1c(%rsp)
movq 0x128(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x20(%rsp)
jmp 0x10bc7
movq 0x130(%rsp), %rdi
callq 0xc0d0
movl %eax, 0x18(%rsp)
jmp 0x10bda
movl 0x1c(%rsp), %ecx
movl 0x18(%rsp), %eax
movl 0x20(%rsp), %edx
imull %eax, %edx
movl %ecx, %eax
movl %edx, %ecx
leal 0x1(%rax,%rcx), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
movsd 0x22710(%rip), %xmm1    # 0x33320
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x130(%rsp), %rdi
callq 0xc0c0
movl %eax, 0x14(%rsp)
jmp 0x10c2d
movl 0x14(%rsp), %eax
movl %eax, %eax
cvtsi2sd %rax, %xmm0
callq 0x52d0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0xb8(%rsp), %xmm0
subsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
movsd %xmm0, (%rsp)
leaq 0x110(%rsp), %rdi
callq 0x10ed0
movsd (%rsp), %xmm0
addq $0x138, %rsp            # imm = 0x138
retq
movq 0x100(%rsp), %rdi
callq 0x5690
nopw %cs:(%rax,%rax)
nopl (%rax)