// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rdx, %rbp
movq 0x8(%rdi), %rax
movslq %esi, %rdx
leaq (%rdx,%rdx,8), %r8
movl %ecx, %r12d
movq %rcx, 0x30(%rsp)
testl %ecx, %ecx
movl %esi, 0x8(%rsp)
movq %rdi, 0x18(%rsp)
movl $0x1, %r13d
jle 0x26f35
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 0x26f1e
movl 0x10(%rax,%r8,8), %eax
movq %rax, 0x10(%rsp)
movq 0x30(%rsp), %rbx
movslq %ebx, %rax
leaq 0x4(,%rax,4), %r15
leaq 0x7e260(%rip), %rsi     # 0xa51b6
movq %r15, %rdi
movl $0xa, %edx
callq 0x390f5
movq %rax, %r14
movq %r15, %rdi
leaq 0x7e246(%rip), %rsi     # 0xa51b6
movl $0xb, %edx
callq 0x390f5
movq %rax, %r15
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movl %ebx, %edx
movl %r13d, %ecx
callq 0x26da5
movq %rax, 0x50(%rsp)
movl 0x8(%rsp), %eax
movl %eax, (%r14)
testl %ebx, %ebx
jle 0x26fb2
xorl %eax, %eax
movl (%rbp,%rax,4), %ecx
movl %ecx, 0x4(%r14,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x26fa1
testl %r13d, %r13d
jle 0x27163
movq 0x30(%rsp), %rax
incl %eax
movl %eax, 0x3c(%rsp)
movl %r13d, %eax
movq %rax, 0x40(%rsp)
movq 0x10(%rsp), %rax
incl %eax
movl %eax, 0x24(%rsp)
xorpd %xmm0, %xmm0
movq $0x0, 0x28(%rsp)
movq %r14, 0x58(%rsp)
movsd %xmm0, 0x48(%rsp)
movl $0x0, %ebx
xorpd %xmm0, %xmm0
cmpl $0x0, 0x10(%rsp)
jle 0x2709e
xorl %ebp, %ebp
xorl %ebx, %ebx
movsd %xmm0, 0x18(%rsp)
movl %ebp, (%r15)
cmpl $0x0, 0x30(%rsp)
jle 0x27039
movq 0x50(%rsp), %rax
movq 0x28(%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 0x27029
movq %r14, %rdi
movq %r15, %rsi
movl 0x3c(%rsp), %edx
callq 0x26882
movl %eax, %r13d
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x27080
movl %r13d, %r14d
movsd %xmm1, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
callq 0x4160
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
leal -0x1(%r14), %eax
cmpl $0x1, %r14d
movl %eax, %r14d
jg 0x27056
movsd 0x18(%rsp), %xmm0
addsd %xmm1, %xmm0
addl %r13d, %ebx
incl %ebp
cmpl 0x10(%rsp), %ebp
movq 0x58(%rsp), %r14
jne 0x27009
movsd %xmm0, 0x18(%rsp)
xorpd %xmm0, %xmm0
cmpl $0x2, 0x10(%rsp)
jl 0x270eb
movl 0x24(%rsp), %eax
movl %eax, %r13d
movsd %xmm0, 0x8(%rsp)
leal -0x2(%r13), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0x4160
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
decl %r13d
cmpl $0x2, %r13d
jg 0x270b6
movsd %xmm0, 0x8(%rsp)
movq 0x10(%rsp), %rax
addl %ebx, %eax
cmpl $0x2, %eax
xorpd %xmm1, %xmm1
jl 0x2712b
addl 0x24(%rsp), %ebx
movsd %xmm1, 0x60(%rsp)
leal -0x2(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0x4160
movsd 0x60(%rsp), %xmm1
addsd %xmm0, %xmm1
decl %ebx
cmpl $0x2, %ebx
jg 0x27105
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
addsd %xmm1, %xmm0
movq 0x28(%rsp), %rcx
incq %rcx
movq %rcx, 0x28(%rsp)
cmpq 0x40(%rsp), %rcx
jne 0x26feb
jmp 0x27167
xorpd %xmm0, %xmm0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq