// File: /workspace/github_c_makefile_star2_O3/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 0x26df6
movq %rax, %r14
testq %rax, %rax
je 0x9052c
cmpl $0x3, %ebx
ja 0x9037b
movl %ebx, %eax
leaq 0x21624(%rip), %rcx     # 0xb1978
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x5450c
jmp 0x9038b
movq %r14, %rdi
callq 0x5452c
jmp 0x9038b
movq %r14, %rdi
callq 0x54771
jmp 0x9038b
leaq 0x21606(%rip), %rdi     # 0xb1988
movl %ebx, %esi
xorl %eax, %eax
callq 0x4030
cmpl $0x0, 0x10(%r14)
jle 0x90524
xorl %edx, %edx
movq 0x8(%r14), %rax
movq 0x40(%r14), %rcx
movq %rdx, 0x18(%rsp)
movslq (%rcx,%rdx,4), %r12
leaq (%r12,%r12,8), %rcx
movl 0xc(%rsp), %edx
cmpl %edx, 0x20(%rax,%rcx,8)
jge 0x904fe
leaq (,%r12,8), %rax
leaq (%rax,%rax,8), %rbp
movq %r14, %rdi
movl %r12d, %esi
callq 0x4e548
movq %rax, %r13
movq 0x8(%r14), %rax
movl 0x20(%rax,%rbp), %ecx
movq %r14, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x27625
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %r12d, %esi
callq 0x4e548
movq %rax, %r15
movq %r14, %rdi
movl %r12d, %esi
callq 0x54850
movq %rax, %rbx
movq %r14, %rdi
movl %r12d, %esi
callq 0x548aa
movq 0x8(%r14), %rcx
movl 0x20(%rcx,%rbp), %ecx
movq %r14, %rdi
movl %r12d, %esi
movq %r13, %rdx
movq %rbx, 0x10(%rsp)
movq %rbx, %r8
movl %eax, %r9d
callq 0x2788b
cmpl $-0x1, %eax
je 0x904e4
movl %eax, %ebx
movq 0x8(%r14), %rax
movslq 0x20(%rax,%rbp), %rdx
leaq 0x4(,%rdx,4), %rsi
incq %rdx
movq %r15, %rdi
callq 0x3a0b4
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 0x27625
ucomisd 0x20(%rsp), %xmm0
jbe 0x904e4
movq %r14, %rdi
callq 0x4e881
movq 0x8(%r14), %rsi
addq %rbp, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x4e8d5
movq 0x8(%r14), %rsi
addq %rbp, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x4e964
movq %r13, %rdi
callq 0x3a180
movq %r15, %rdi
callq 0x3a180
movq 0x10(%rsp), %rdi
callq 0x3a180
movq 0x8(%r14), %rax
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x20(%rax,%rbp)
jl 0x903c7
jmp 0x904fe
movq %r13, %rdi
callq 0x3a180
movq %r15, %rdi
callq 0x3a180
movq 0x10(%rsp), %rdi
callq 0x3a180
leaq 0x214af(%rip), %rdi     # 0xb19b4
movl %r12d, %esi
xorl %eax, %eax
callq 0x4030
movq 0x18(%rsp), %rdx
incq %rdx
movslq 0x10(%r14), %rax
cmpq %rax, %rdx
jl 0x90398
movq %r14, %rdi
callq 0x4f8a4
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop