// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/structure.c
// Function: k2_bn
bayesiangraphptr k2_bn(char *graph_name, char *train_file, char *cv_file, char *test_file, ORDERING_TYPE ordering, int maxparents)
{/*Last Changed 04.06.2001*/
 int i, j, nodecount;
 BOOLEAN proceed;
 bayesiangraphptr newnetwork;
 int *current_parents, *modified_parents;
 int selected_node;
 double p_old, p_new;
 int *nodeset;
 newnetwork = initial_graph_from_data(graph_name, train_file, cv_file, test_file);
 if (!newnetwork)
   return NULL;
 switch (ordering)
  {
   case ORDERING_RANDOM  :random_ordering(newnetwork);
                          break;
   case ORDERING_STATE   :state_ordering(newnetwork);
                          break;
   case ORDERING_MINTABLE:break;
   case ORDERING_ALARM   :alarm_ordering(newnetwork);
		                        break;
   default               :printf(m1240, ordering);
                          break;
  }
 for (j = 0; j < newnetwork->nodecount; j++)
  {
   i = newnetwork->ordering[j];
   proceed = BOOLEAN_TRUE;
   while ((proceed) && ((newnetwork->nodes[i].parents) < maxparents))
    {
     current_parents = find_parents(newnetwork, i);
     p_old = calculate_K2_metric(newnetwork, i, current_parents, newnetwork->nodes[i].parents);
     modified_parents = find_parents(newnetwork, i);
     nodeset = find_nodes_before(newnetwork, i);
     nodecount = find_nodecount_before(newnetwork, i);
     selected_node = select_node_maximizes_K2_metric(newnetwork, i, current_parents, newnetwork->nodes[i].parents, nodeset, nodecount);
     if (selected_node == -1)
       proceed = BOOLEAN_FALSE;
     else
      {
       modified_parents = alloc(modified_parents, ((newnetwork->nodes[i].parents + 1) * sizeof(int)), newnetwork->nodes[i].parents + 1);
       modified_parents[newnetwork->nodes[i].parents] = selected_node;
       p_new = calculate_K2_metric(newnetwork, i, modified_parents, newnetwork->nodes[i].parents + 1);
       if (p_new > p_old)
        {
         p_old = p_new;
         add_one_arc(newnetwork);
         add_one_pointer(newnetwork,&newnetwork->nodes[i],selected_node);
         add_parent_counts(newnetwork,&newnetwork->nodes[i],selected_node);
        }
       else
         proceed = BOOLEAN_FALSE;
      }
					safe_free(current_parents);
					safe_free(modified_parents);
					safe_free(nodeset);
		  }
   printf(m1317, i);
  }
 make_conditional_tables(newnetwork);
 return newnetwork;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0xc(%rsp)
movl %r8d, %ebx
callq 0x23ab7
movq %rax, %r14
testq %rax, %rax
je 0x828a9
cmpl $0x3, %ebx
ja 0x82725
movl %ebx, %eax
leaq 0x1f1da(%rip), %rcx     # 0xa18d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x4c960
jmp 0x82735
movq %r14, %rdi
callq 0x4c980
jmp 0x82735
movq %r14, %rdi
callq 0x4cba8
jmp 0x82735
leaq 0x1f1bc(%rip), %rdi     # 0xa18e8
movl %ebx, %esi
xorl %eax, %eax
callq 0x4030
xorl %ecx, %ecx
movslq 0x10(%r14), %rax
cmpq %rax, %rcx
jge 0x828a1
movq 0x40(%r14), %rax
movq %rcx, 0x10(%rsp)
movslq (%rax,%rcx,4), %r12
imulq $0x48, %r12, %rbp
pushq $0x1
popq %rbx
testl %ebx, %ebx
je 0x82883
movq 0x8(%r14), %rax
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x20(%rax,%rbp)
jge 0x82883
movq %r14, %rdi
movl %r12d, %esi
callq 0x47301
movq %rax, %r13
movq 0x8(%r14), %rax
movl 0x20(%rax,%rbp), %ecx
movq %r14, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x241e2
movsd %xmm0, 0x18(%rsp)
movq %r14, %rdi
movl %r12d, %esi
callq 0x47301
movq %rax, %r15
movq %r14, %rdi
movl %r12d, %esi
callq 0x4cc80
movq %rax, %rbx
movq %r14, %rdi
movl %r12d, %esi
callq 0x4ccce
movq 0x8(%r14), %rcx
movl 0x20(%rcx,%rbp), %ecx
movq %r14, %rdi
movl %r12d, %esi
movq %r13, %rdx
movq %rbx, 0x20(%rsp)
movq %rbx, %r8
movl %eax, %r9d
callq 0x243a8
cmpl $-0x1, %eax
je 0x82862
movl %eax, %ebx
movq 0x8(%r14), %rax
movslq 0x20(%rax,%rbp), %rdx
leaq 0x4(,%rdx,4), %rsi
incq %rdx
movq %r15, %rdi
callq 0x34f30
movq %rax, %r15
movq 0x8(%r14), %rax
movslq 0x20(%rax,%rbp), %rcx
movl %ebx, (%r15,%rcx,4)
movl 0x20(%rax,%rbp), %ecx
incl %ecx
movq %r14, %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x241e2
ucomisd 0x18(%rsp), %xmm0
jbe 0x82862
movq %r14, %rdi
callq 0x47603
movq 0x8(%r14), %rsi
addq %rbp, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x47654
movq 0x8(%r14), %rsi
addq %rbp, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x476d6
pushq $0x1
popq %rbx
jmp 0x82864
xorl %ebx, %ebx
movq %r13, %rdi
callq 0x34ff7
movq %r15, %rdi
callq 0x34ff7
movq 0x20(%rsp), %rdi
callq 0x34ff7
jmp 0x82758
leaq 0x1f08a(%rip), %rdi     # 0xa1914
movl %r12d, %esi
xorl %eax, %eax
callq 0x4030
movq 0x10(%rsp), %rcx
incq %rcx
jmp 0x82737
movq %r14, %rdi
callq 0x48369
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop