// File: /workspace/github_c_makefile_star2_O1/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 0x5b37
movq %rax, %rbx
movl $0x3, 0x14(%rax)
xorl %eax, %eax
cmpl $0x2, (%r15)
setge %al
movl %eax, 0xc(%rbx)
leaq 0x238b7(%rip), %rdi     # 0xae421
movl %r13d, %esi
movq %r12, %rdx
movq %r15, %rcx
movl $0x2, %r8d
callq 0x1560a
leaq 0x47ceb(%rip), %r13     # 0xd2870
movq %rax, (%r13)
movq %rax, %rdi
callq 0x14f4e
movq (%r13), %rdx
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %ecx
callq 0x8a924
movq %rax, %r12
testq %rax, %rax
je 0x8abc4
xorl %eax, %eax
movq %r12, %rcx
addl 0x4(%rcx), %eax
movq 0x20(%rcx), %rcx
testq %rcx, %rcx
jne 0x8abaf
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x8abc8
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r12, %rdi
movl $0x1, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x8a3da
movq %r12, %rdi
movsd %xmm0, 0x18(%rsp)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x8a6e6
movsd %xmm0, 0x10(%rsp)
testq %r12, %r12
je 0x8ad8f
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r12, %r14
movl (%r14), %edx
movq %r12, %rdi
movl $0x1, %esi
callq 0x8a3da
movl (%r14), %esi
movq %r12, %rdi
callq 0x8a6e6
subsd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x4(%r14), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%r14)
ucomisd 0x180b9(%rip), %xmm0    # 0xa2d08
jbe 0x8ac79
divsd 0x20(%rsp), %xmm1
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x18095(%rip), %xmm0    # 0xa2cf8
ucomisd %xmm1, %xmm0
seta %al
setbe %cl
addl %eax, %r15d
movl %ecx, 0x8(%r14)
incl %r13d
jmp 0x8ac81
movl $0x0, 0x8(%r14)
movq 0x20(%r14), %r14
testq %r14, %r14
jne 0x8ac13
testl %r13d, %r13d
je 0x8ad94
testl %r15d, %r15d
leaq 0x47bd3(%rip), %r13     # 0xd2870
movl 0xc(%rsp), %r14d
je 0x8ada0
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x8ad13
movq %rax, %rcx
movq 0x20(%rax), %rax
movq 0x28(%rcx), %rdx
testq %rdx, %rdx
je 0x8ad0b
movsd 0x10(%rcx), %xmm0
ucomisd 0x10(%rdx), %xmm0
jbe 0x8ad0b
movq 0x28(%rdx), %rdi
movq 0x20(%rcx), %r8
movq %rcx, %rsi
testq %rdi, %rdi
je 0x8ace5
movq %rcx, 0x20(%rdi)
movq %r12, %rsi
testq %r8, %r8
je 0x8acee
movq %rdx, 0x28(%r8)
movq %rcx, 0x28(%rdx)
movq %r8, 0x20(%rdx)
movq %rdi, 0x28(%rcx)
movq %rdx, 0x20(%rcx)
movq %rdi, %rdx
movq %rsi, %r12
testq %rdi, %rdi
jne 0x8acc7
jmp 0x8ad0e
movq %r12, %rsi
movq %rsi, %r12
jmp 0x8acad
movq %r12, %rax
movq %rax, %r15
movq 0x20(%rax), %rax
testq %rax, %rax
jne 0x8ad16
incl %r14d
testq %r12, %r12
je 0x8ada0
movl %r14d, 0xc(%rsp)
movq %r12, %r14
movq (%r13), %rcx
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %rbx, %r8
movl %ebp, %r9d
movsd 0x10(%rsp), %xmm0
callq 0x8adf2
testl %eax, %eax
jne 0x8ad5f
movq 0x20(%r14), %r14
testq %r14, %r14
jne 0x8ad32
jmp 0x8ad9b
movq %r12, %rdi
movl $0x1, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x8a3da
movq %r12, %rdi
movsd %xmm0, 0x18(%rsp)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x8a6e6
movsd %xmm0, 0x10(%rsp)
jmp 0x8ac0a
xorl %r14d, %r14d
jmp 0x8ada0
leaq 0x47ad5(%rip), %r13     # 0xd2870
movl 0xc(%rsp), %r14d
movq %r12, %rdi
callq 0x8a2de
leaq 0x1b209(%rip), %rdi     # 0xa5fb8
movsd 0x18(%rsp), %xmm0
callq 0x3403a
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
leaq 0x1b1f5(%rip), %rdi     # 0xa5fbe
callq 0x3403a
movq (%r13), %rdi
callq 0x13765
movl $0x3fd, %edi            # imm = 0x3FD
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4d19