// File: /workspace/github_c_makefile_star2_O3/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 0x241f7(%rip), %rdi     # 0xb14b9
movl %r13d, %esi
movq %r12, %rdx
movq %r15, %rcx
movl $0x2, %r8d
callq 0x156a2
leaq 0x48593(%rip), %r13     # 0xd5870
movq %rax, (%r13)
movq %rax, %rdi
callq 0x14fef
movq (%r13), %rdx
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %ecx
callq 0x8d07c
movq %rax, %r12
testq %rax, %rax
je 0x8d31c
xorl %eax, %eax
movq %r12, %rcx
addl 0x4(%rcx), %eax
movq 0x20(%rcx), %rcx
testq %rcx, %rcx
jne 0x8d307
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
jmp 0x8d320
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r12, %rdi
movl $0x1, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x8cb52
movq %r12, %rdi
movsd %xmm0, 0x18(%rsp)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x8ce48
movsd %xmm0, 0x10(%rsp)
testq %r12, %r12
je 0x8d4e9
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r12, %r14
movl (%r14), %edx
movq %r12, %rdi
movl $0x1, %esi
callq 0x8cb52
movl (%r14), %esi
movq %r12, %rdi
callq 0x8ce48
subsd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x4(%r14), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%r14)
ucomisd 0x18961(%rip), %xmm0    # 0xa5d08
jbe 0x8d3d1
divsd 0x20(%rsp), %xmm1
xorl %eax, %eax
xorl %ecx, %ecx
movsd 0x1893d(%rip), %xmm0    # 0xa5cf8
ucomisd %xmm1, %xmm0
seta %al
setbe %cl
addl %eax, %r15d
movl %ecx, 0x8(%r14)
incl %r13d
jmp 0x8d3d9
movl $0x0, 0x8(%r14)
movq 0x20(%r14), %r14
testq %r14, %r14
jne 0x8d36b
testl %r13d, %r13d
je 0x8d4f6
testl %r15d, %r15d
leaq 0x4847b(%rip), %r13     # 0xd5870
movl 0xc(%rsp), %r14d
je 0x8d502
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x8d46d
movq %rax, %rcx
movq 0x20(%rax), %rax
movq 0x28(%rcx), %rdx
testq %rdx, %rdx
je 0x8d465
movsd 0x10(%rcx), %xmm0
movq %rax, %rsi
ucomisd 0x10(%rdx), %xmm0
jbe 0x8d465
movq 0x28(%rdx), %r8
movq %rcx, %rdi
testq %r8, %r8
je 0x8d43c
movq %rcx, 0x20(%r8)
movq %r12, %rdi
testq %rsi, %rsi
je 0x8d445
movq %rdx, 0x28(%rsi)
movq %rcx, 0x28(%rdx)
movq %rsi, 0x20(%rdx)
movq %r8, 0x28(%rcx)
movq %rdx, 0x20(%rcx)
movq %rdx, %rsi
movq %r8, %rdx
movq %rdi, %r12
testq %r8, %r8
jne 0x8d422
jmp 0x8d468
movq %r12, %rdi
movq %rdi, %r12
jmp 0x8d405
movq %r12, %rax
movq %rax, %r15
movq 0x20(%rax), %rax
testq %rax, %rax
jne 0x8d470
incl %r14d
testq %r12, %r12
je 0x8d4f1
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 0x8d554
testl %eax, %eax
jne 0x8d4b9
movq 0x20(%r14), %r14
testq %r14, %r14
jne 0x8d48c
jmp 0x8d4fd
movq %r12, %rdi
movl $0x1, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x8cb52
movq %r12, %rdi
movsd %xmm0, 0x18(%rsp)
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x8ce48
movsd %xmm0, 0x10(%rsp)
jmp 0x8d362
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x8d502
xorl %r12d, %r12d
jmp 0x8d502
leaq 0x48373(%rip), %r13     # 0xd5870
movl 0xc(%rsp), %r14d
movq %r12, %rdi
callq 0x8ca56
leaq 0x1bad7(%rip), %rdi     # 0xa8fe8
movsd 0x18(%rsp), %xmm0
callq 0x34a17
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
leaq 0x1bac3(%rip), %rdi     # 0xa8fee
callq 0x34a17
movq (%r13), %rdi
callq 0x1376b
movl $0x3fd, %edi            # imm = 0x3FD
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4cff