// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/srm.c
// Function: vc_estimate_optimized_of_decision_tree
void vc_estimate_optimized_of_decision_tree(int featurecount, int nodecount, int experimentcount, char* featuretypes, int* featurevaluecounts)
{
 /*!Last Changed 24.09.2006 added different feature types*/
 /*!Last Changed 03.05.2006 run for a specific featurecount and nodecount*/
 /*!Last Changed 01.04.2006*/
 int positivecount, positivenotsaturated, total, totalmove;
 void* parameters;
 double vcdimension, vcdimensionremove, msefitting, msefittingremove, averagecount;
 Vcestimateptr estimates, tmpvc, nextvc, tmpvc2, vc1, vc2, vc3, vc4, removed, added;
 parameters = prepare_supervised_algorithm_parameters(NBTREE);
 ((Nodeboundedtree_parameterptr) parameters)->prunetype = NOPRUNING; 
 if (featurevaluecounts[0] > 1)
   ((Nodeboundedtree_parameterptr) parameters)->usediscrete = BOOLEAN_TRUE;
 else
   ((Nodeboundedtree_parameterptr) parameters)->usediscrete = BOOLEAN_FALSE;
 current_dataset = generate_random_dataset("VC-Estimate", featurecount, featuretypes, featurevaluecounts, 2);
 averagecount = average_valuecount(current_dataset);
 estimates = vc_estimates_for_a_design(parameters, nodecount, current_dataset, experimentcount, averagecount);
 total = number_of_experiments(estimates);
 totalmove = 0;
 while (1)
  {
   vcdimension = vc_dimension_for_a_design(estimates, OPTIMIZED_DESIGN, -1);
   msefitting = mse_fitting_for_vc_dimension(estimates, vcdimension, -1);
   positivecount = 0;
   positivenotsaturated = 0;
   /*Calculate contributions*/
   tmpvc = estimates;
   while (tmpvc)
    {
     vcdimensionremove = vc_dimension_for_a_design(estimates, OPTIMIZED_DESIGN, tmpvc->n);
     msefittingremove = mse_fitting_for_vc_dimension(estimates, vcdimensionremove, tmpvc->n);
     tmpvc->contribution = (msefittingremove - msefitting) / tmpvc->count;
     if (tmpvc->contribution > 0)
      {
       if (tmpvc->count / (total + 0.0) < 0.25)
        {
         positivenotsaturated++;
         tmpvc->issaturated = BOOLEAN_FALSE;
        }
       else
         tmpvc->issaturated = BOOLEAN_TRUE;
       positivecount++;
      }
     else
       tmpvc->issaturated = BOOLEAN_FALSE;
     tmpvc = tmpvc->next;
    }
   if (!positivecount || !positivenotsaturated)
     break;
   /*Sort contributions*/
   tmpvc = estimates->next;
   while (tmpvc)
    {
     nextvc = tmpvc->next;
     tmpvc2 = tmpvc;
     while (tmpvc2->before && tmpvc2->contribution > tmpvc2->before->contribution)
      {
       vc1 = tmpvc2->before->before;
       vc2 = tmpvc2->before;
       vc3 = tmpvc2;
       vc4 = tmpvc2->next;
       if (vc1)
         vc1->next = vc3;
       else
         estimates = vc3;
       if (vc4)
         vc4->before = vc2;
       vc2->before = vc3;
       vc2->next = vc4;
       vc3->before = vc1;
       vc3->next = vc2;
      }
     tmpvc = nextvc;
    }
   /*Move one experiment from the worst design point to the best design point, if not succesful to the second best etc.*/
   removed = estimates;
   while (removed->next)
     removed = removed->next;
   added = estimates;
   totalmove++;
   while (added && !(move_vc_estimates(estimates, removed, added, current_dataset, parameters, nodecount, msefitting)))
     added = added->next;
   if (!added)
     break;
  }
 deallocate_vc_estimates(estimates);
 set_default_variable("out1", vcdimension);
 set_default_variable("out2", totalmove);
 free_dataset(current_dataset);
 free_supervised_algorithm_parameters(NBTREE, parameters);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, %r15
movq %rcx, %r12
movl %edx, %r14d
movl %esi, %ebp
movl %edi, %r13d
movl $0x3fd, %edi            # imm = 0x3FD
callq 0x5a9c
movq %rax, %rbx
movl $0x3, 0x14(%rax)
xorl %eax, %eax
cmpl $0x2, (%r15)
setge %al
movl %eax, 0xc(%rbx)
leaq 0x21a00(%rip), %rdi     # 0xa1419
pushq $0x2
popq %r8
movl %r13d, %esi
movq %r12, %rdx
movq %r15, %rcx
callq 0x143f8
leaq 0x4de3e(%rip), %r15     # 0xcd870
movq %rax, (%r15)
movq %rax, %rdi
callq 0x13dd9
movq (%r15), %rdx
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %ecx
callq 0x7f7e4
movq %rax, %r14
movq %rax, %rdi
callq 0x7f27c
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x0, 0x4(%rsp)
pushq $0x1
popq %r15
pushq $-0x1
popq %rbx
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x7f28d
movq %r14, %rdi
movsd %xmm0, 0x18(%rsp)
movl %ebx, %esi
callq 0x7f5e2
movsd %xmm0, 0x10(%rsp)
movq %r14, %r13
xorl %r12d, %r12d
xorl %ebx, %ebx
testq %r13, %r13
je 0x7fb13
movl (%r13), %edx
movq %r14, %rdi
movl %r15d, %esi
callq 0x7f28d
movl (%r13), %esi
movq %r14, %rdi
callq 0x7f5e2
subsd 0x10(%rsp), %xmm0
cvtsi2sdl 0x4(%r13), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%r13)
ucomisd 0x16239(%rip), %xmm0    # 0x95d18
jbe 0x7fb08
divsd 0x20(%rsp), %xmm1
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x1621d(%rip), %xmm0    # 0x95d10
ucomisd %xmm1, %xmm0
seta %al
setbe %cl
addl %eax, %r12d
movl %ecx, 0x8(%r13)
incl %ebx
jmp 0x7fb0d
andl $0x0, 0x8(%r13)
movq 0x20(%r13), %r13
jmp 0x7faa1
testl %ebx, %ebx
je 0x7fbe1
testl %r12d, %r12d
movq 0x8(%rsp), %rbx
je 0x7fbe6
movq 0x20(%r14), %rax
movl 0x4(%rsp), %r9d
testq %rax, %rax
je 0x7fb8f
movq 0x20(%rax), %rcx
movq 0x28(%rax), %rdx
movq %rcx, %rsi
testq %rdx, %rdx
je 0x7fb8a
movsd 0x10(%rax), %xmm0
ucomisd 0x10(%rdx), %xmm0
jbe 0x7fb8a
movq 0x28(%rdx), %rdi
movq %rax, %r8
testq %rdi, %rdi
je 0x7fb66
movq %rax, 0x20(%rdi)
movq %r14, %r8
testq %rsi, %rsi
je 0x7fb6f
movq %rdx, 0x28(%rsi)
movq %rax, 0x28(%rdx)
movq %rsi, 0x20(%rdx)
movq %rdi, 0x28(%rax)
movq %rdx, 0x20(%rax)
movq %rdx, %rsi
movq %rdi, %rdx
movq %r8, %r14
jmp 0x7fb42
movq %rcx, %rax
jmp 0x7fb32
movq %r14, %rax
movq %rax, %r13
movq 0x20(%rax), %rax
testq %rax, %rax
jne 0x7fb92
incl %r9d
movl %r9d, 0x4(%rsp)
movq %r14, %r12
testq %r12, %r12
je 0x7fbe6
leaq 0x4dcbb(%rip), %rax     # 0xcd870
movq (%rax), %rcx
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rbx, %r8
movl %ebp, %r9d
movsd 0x10(%rsp), %xmm0
callq 0x7fc3e
testl %eax, %eax
jne 0x7fa73
movq 0x20(%r12), %r12
jmp 0x7fba9
movq 0x8(%rsp), %rbx
movl 0x4(%rsp), %ebp
movq %r14, %rdi
callq 0x7f1f4
leaq 0x1935f(%rip), %rdi     # 0x98f58
movsd 0x18(%rsp), %xmm0
callq 0x30597
cvtsi2sd %ebp, %xmm0
leaq 0x1934f(%rip), %rdi     # 0x98f5e
callq 0x30597
leaq 0x4dc55(%rip), %rax     # 0xcd870
movq (%rax), %rdi
callq 0x12723
movl $0x3fd, %edi            # imm = 0x3FD
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4cf6