// File: /workspace/github_c_makefile_star2_O1/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 0x266b6
testq %rax, %rax
je 0x8db8b
movq %rax, %r14
cmpl $0x3, %ebx
ja 0x8dba7
movl %ebx, %eax
leaq 0x20d70(%rip), %rcx     # 0xae8e8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x5300c
jmp 0x8dbb7
xorl %r14d, %r14d
jmp 0x8dd58
movq %r14, %rdi
callq 0x5302c
jmp 0x8dbb7
movq %r14, %rdi
callq 0x5326c
jmp 0x8dbb7
leaq 0x20d4a(%rip), %rdi     # 0xae8f8
movl %ebx, %esi
xorl %eax, %eax
callq 0x4030
cmpl $0x0, 0x10(%r14)
jle 0x8dd50
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 0x8dd2a
leaq (,%r12,8), %rax
leaq (%rax,%rax,8), %rbp
movq %r14, %rdi
movl %r12d, %esi
callq 0x4d0d4
movq %rax, %r13
movq 0x8(%r14), %rax
movl 0x20(%rax,%rbp), %ecx
movq %r14, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x26ee5
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %r12d, %esi
callq 0x4d0d4
movq %rax, %r15
movq %r14, %rdi
movl %r12d, %esi
callq 0x5334b
movq %rax, %rbx
movq %r14, %rdi
movl %r12d, %esi
callq 0x533a5
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 0x27176
cmpl $-0x1, %eax
je 0x8dd10
movl %eax, %ebx
movq 0x8(%r14), %rax
movslq 0x20(%rax,%rbp), %rdx
leaq 0x4(,%rdx,4), %rsi
incq %rdx
movq %r15, %rdi
callq 0x390c8
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 0x26ee5
ucomisd 0x20(%rsp), %xmm0
jbe 0x8dd10
movq %r14, %rdi
callq 0x4d426
movq 0x8(%r14), %rsi
addq %rbp, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x4d47a
movq 0x8(%r14), %rsi
addq %rbp, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x4d508
movq %r13, %rdi
callq 0x39194
movq %r15, %rdi
callq 0x39194
movq 0x10(%rsp), %rdi
callq 0x39194
movq 0x8(%r14), %rax
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x20(%rax,%rbp)
jl 0x8dbf3
jmp 0x8dd2a
movq %r13, %rdi
callq 0x39194
movq %r15, %rdi
callq 0x39194
movq 0x10(%rsp), %rdi
callq 0x39194
leaq 0x20bf3(%rip), %rdi     # 0xae924
movl %r12d, %esi
xorl %eax, %eax
callq 0x4030
movq 0x18(%rsp), %rdx
incq %rdx
movslq 0x10(%r14), %rax
cmpq %rax, %rdx
jl 0x8dbc4
movq %r14, %rdi
callq 0x4e366
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop