// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rdx, %rbp
movq 0x8(%rdi), %rax
movslq %esi, %rdx
leaq (%rdx,%rdx,8), %r8
movl %ecx, %r12d
movq %rcx, 0x20(%rsp)
testl %ecx, %ecx
movl %esi, 0x8(%rsp)
movq %rdi, (%rsp)
movl $0x1, %r13d
jle 0x27674
xorl %edx, %edx
movslq (%rbp,%rdx,4), %rsi
leaq (%rsi,%rsi,8), %rsi
imull 0x10(%rax,%rsi,8), %r13d
incq %rdx
cmpq %rdx, %r12
jne 0x2765d
movl 0x10(%rax,%r8,8), %eax
movl %eax, 0x14(%rsp)
movq 0x20(%rsp), %rbx
movslq %ebx, %rax
leaq 0x4(,%rax,4), %r15
leaq 0x80b7a(%rip), %rsi     # 0xa820e
movq %r15, %rdi
movl $0xa, %edx
callq 0x3a0e1
movq %rax, %r14
movq %r15, %rdi
leaq 0x80b60(%rip), %rsi     # 0xa820e
movl $0xb, %edx
callq 0x3a0e1
movq %rax, %r15
movq (%rsp), %rdi
movq %rbp, %rsi
movl %ebx, %edx
movl %r13d, %ecx
callq 0x274e5
movq %rax, 0x48(%rsp)
movl 0x8(%rsp), %eax
movl %eax, (%r14)
testl %ebx, %ebx
jle 0x276ef
xorl %eax, %eax
movl (%rbp,%rax,4), %ecx
movl %ecx, 0x4(%r14,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x276de
testl %r13d, %r13d
jle 0x27878
movq %r14, 0x50(%rsp)
movq 0x20(%rsp), %rax
incl %eax
movl %eax, 0x34(%rsp)
movl %r13d, %eax
movq %rax, 0x38(%rsp)
xorpd %xmm0, %xmm0
movq $0x0, 0x18(%rsp)
movl 0x14(%rsp), %ebp
movsd %xmm0, 0x40(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movl $0x0, %ebx
xorpd %xmm1, %xmm1
testl %ebp, %ebp
jle 0x2780d
xorl %r14d, %r14d
xorl %ebx, %ebx
movsd %xmm0, (%rsp)
movl %r14d, (%r15)
cmpl $0x0, 0x20(%rsp)
jle 0x27775
movq 0x48(%rsp), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movl %edx, 0x4(%r15,%rcx,4)
incq %rcx
cmpq %rcx, %r12
jne 0x27765
movq 0x50(%rsp), %rdi
movq %r15, %rsi
movl 0x34(%rsp), %edx
callq 0x26fc2
movl %eax, %r13d
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x277ba
movl %r13d, %ebp
movsd %xmm1, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
callq 0x4160
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
leal -0x1(%rbp), %eax
cmpl $0x1, %ebp
movl %eax, %ebp
ja 0x27794
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
addl %r13d, %ebx
incl %r14d
movl 0x14(%rsp), %ebp
cmpl %ebp, %r14d
jne 0x27746
xorpd %xmm1, %xmm1
cmpl $0x2, %ebp
movsd %xmm0, (%rsp)
jl 0x2780d
movl %ebp, %r14d
movsd %xmm1, 0x28(%rsp)
decl %r14d
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
callq 0x4160
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
cmpl $0x1, %r14d
ja 0x277e7
movsd %xmm1, 0x28(%rsp)
addl %ebp, %ebx
xorpd %xmm1, %xmm1
cmpl $0x2, %ebx
jl 0x27841
movsd %xmm1, 0x8(%rsp)
decl %ebx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
callq 0x4160
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
cmpl $0x1, %ebx
ja 0x2781e
movsd 0x28(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
addsd %xmm1, %xmm0
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq 0x38(%rsp), %rcx
jne 0x27721
jmp 0x2787c
xorpd %xmm0, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq