// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/graphdata.c
// Function: calculate_K2_metric
double calculate_K2_metric(bayesiangraphptr mygraph, int node, int *parents, int parentcount)
{
 /*!Last Changed 02.02.2004*/
 int i, j, k;
 int ri, qi, xijk, Nij;
 double lnsum, result;
 int *list, *value;
 int **combinations;
 ri = mygraph->nodes[node].value_count;
 qi = 1;/*qi is the number of different instantiations of the parents of node*/
 for (i = 0; i < parentcount; i++)
   qi = qi * mygraph->nodes[parents[i]].value_count;
 list = (int *) safemalloc ((parentcount + 1) * sizeof(int), "calculate_K2_metric", 10);	/*sizes are for node+parents*/
 value = (int *) safemalloc ((parentcount + 1) * sizeof(int), "calculate_K2_metric", 11);
 combinations = parent_value_combinations(mygraph, parents, parentcount, qi);
 list[0] = node;
 for (i = 0; i < parentcount; i++)
   list[i + 1] = parents[i];
 result = 0;
 for (j = 0; j < qi; j++)
  {
   Nij = 0;
   lnsum = 0;
   for (k = 0; k < ri; k++)
    {
     value[0] = k;
     for (i = 0; i < parentcount; i++)
       value[i + 1] = combinations[j][i];
     xijk = count_data(list, value, parentcount + 1);
     Nij = Nij + xijk;
     lnsum = lnsum + lnfact(xijk);
    }
   result = result + (lnfact(ri - 1) - lnfact(Nij + ri - 1) + lnsum);
  }
 return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %r12d
movq %rdx, %rbx
movq %rdi, 0x8(%rsp)
movq 0x8(%rdi), %rax
movl %esi, (%rsp)
movslq %esi, %rcx
imulq $0x48, %rcx, %rcx
movl 0x10(%rax,%rcx), %ecx
movq %rcx, 0x18(%rsp)
xorl %ecx, %ecx
testl %r12d, %r12d
movl $0x0, %r13d
cmovgl %r12d, %r13d
pushq $0x1
popq %rbp
cmpq %rcx, %r13
je 0x2423b
movslq (%rbx,%rcx,4), %rdx
imulq $0x48, %rdx, %rdx
imull 0x10(%rax,%rdx), %ebp
incq %rcx
jmp 0x24224
leal 0x1(%r12), %eax
movl %eax, 0x24(%rsp)
movslq %r12d, %rax
leaq 0x4(,%rax,4), %r15
leaq 0x73f38(%rip), %rsi     # 0x9818e
pushq $0xa
popq %rdx
movq %r15, %rdi
callq 0x34f59
movq %rax, %r14
pushq $0xb
popq %rdx
movq %r15, %rdi
leaq 0x73f1d(%rip), %rsi     # 0x9818e
callq 0x34f59
movq %rax, %r15
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %r12d, %edx
movl %ebp, %ecx
callq 0x240a0
movq %rax, %r12
movq %r14, 0x8(%rsp)
movl (%rsp), %eax
movl %eax, (%r14)
xorl %eax, %eax
movq 0x8(%rsp), %rdx
cmpq %rax, %r13
je 0x242b1
movl (%rbx,%rax,4), %ecx
movl %ecx, 0x4(%rdx,%rax,4)
incq %rax
jmp 0x242a0
movq 0x18(%rsp), %r14
leal -0x1(%r14), %eax
movl %eax, 0x20(%rsp)
movq %rbp, %rax
xorl %ebp, %ebp
testl %r14d, %r14d
cmovlel %ebp, %r14d
movq %r14, 0x18(%rsp)
testl %eax, %eax
cmovlel %ebp, %eax
movq %rax, 0x28(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
cmpq 0x28(%rsp), %rbp
je 0x24393
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpl 0x18(%rsp), %r14d
je 0x2434b
movl %r14d, (%r15)
xorl %eax, %eax
cmpq %rax, %r13
je 0x2431e
movq (%r12,%rbp,8), %rcx
movl (%rcx,%rax,4), %ecx
movl %ecx, 0x4(%r15,%rax,4)
incq %rax
jmp 0x24308
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movl 0x24(%rsp), %edx
callq 0x23c61
addl %eax, %ebx
movl %eax, %edi
callq 0x241a5
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
incl %r14d
jmp 0x242fc
movl 0x20(%rsp), %r14d
movl %r14d, %edi
callq 0x241a5
movsd %xmm0, 0x30(%rsp)
addl %r14d, %ebx
movl %ebx, %edi
callq 0x241a5
movsd 0x30(%rsp), %xmm2
subsd %xmm0, %xmm2
movsd (%rsp), %xmm1
addsd %xmm2, %xmm1
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
incq %rbp
jmp 0x242e3
movsd 0x10(%rsp), %xmm0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq