// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/svmtree.c
// Function: find_best_svmtree_feature
int find_best_svmtree_feature(Instanceptr instances, double* split, int positive, double C)
{
 /*!Last Changed 03.07.2009*/
 Instanceptr tmp;
 int counts[2][MAXCLASSES];
 int i, j, t, count = data_size(instances), bestfeature = -1, negativecount, positivecount, splitcount;
 double* splits = safemalloc(count * sizeof(double), "find_best_svmtree_feature", 2);
 Splitpointptr splitpoints = safemalloc(count * sizeof(Splitpoint), "find_best_svmtree_feature", 3);
 double fvalue, bestvalue, bestfsplit, besterror = +INT_MAX, error;
 for (i = 0; i < current_dataset->featurecount; i++)
   if (current_dataset->features[i].type != CLASSDEF)
    {
     for (j = 0, tmp = instances; j < count; j++, tmp = tmp->next)
      {
       if (give_classno(tmp) == positive)
         t = -1;
       else
         t = +1;
       splits[j] = (C - t * tmp->values[i].floatvalue) / t;
      }
     qsort(splits, count, sizeof(double), sort_function_double_ascending);
     if (splits[0] == splits[count - 1])
       continue;
     splitcount = 1;
     splitpoints[0].x = splits[0];
     splitpoints[0].count = 1;
     for (j = 1; j < count; j++)
       if (splitpoints[splitcount - 1].x != splits[j])
        {
         splitpoints[splitcount].x = splits[j];
         splitpoints[splitcount].count = 1;
         splitcount++;
        }
       else
         splitpoints[splitcount - 1].count++;
     bestvalue = +INT_MAX;
     fvalue = 0.0;
     negativecount = 0;
     positivecount = 0;
     for (j = splitcount - 1; j > 0; j--)
       if (splitpoints[j].x > 0)
        {
         positivecount += splitpoints[j].count;
         fvalue += (splitpoints[j].x - splitpoints[0].x) * splitpoints[j].count;
        }
       else
         break;
     for (j = 0; j < splitcount - 1; j++)
      {
       if (fvalue < bestvalue)
        {
         bestvalue = fvalue;
         bestfsplit = -(splitpoints[j].x + splitpoints[j + 1].x) / 2;
        }
       if (splitpoints[j].x < 0)
         negativecount += splitpoints[j].count;
       else
         positivecount -= splitpoints[j].count;
       fvalue += (negativecount - positivecount) * (splitpoints[j + 1].x - splitpoints[j].x);
      }
     find_counts_for_split(instances, i, bestfsplit, counts[0], counts[1], UNIVARIATE_CONDITION);
     error = error_of_split(counts, count);
     if (error < besterror)
      {
       bestfeature = i;
       *split = bestfsplit;
       besterror = error;
      }
    }
 safe_free(splits);
 safe_free(splitpoints);
 return bestfeature;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movsd %xmm0, 0x48(%rsp)
movl %edx, 0x1c(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
callq 0x2c3cc
movl %eax, %ebx
movslq %eax, %r15
leaq (,%r15,8), %rdi
leaq 0x18b70(%rip), %r14     # 0xa1c0a
pushq $0x2
popq %rdx
movq %r14, %rsi
callq 0x34f59
movq %rax, %r13
movq %r15, %rdi
shlq $0x4, %rdi
pushq $0x3
popq %rdx
movq %r14, %rsi
callq 0x34f59
movq %rax, %r14
testl %r15d, %r15d
movl $0x0, %r12d
movl %ebx, 0x18(%rsp)
cmovgl %ebx, %r12d
addq $0x8, %rax
movq %rax, 0x38(%rsp)
movsd 0xc439(%rip), %xmm0     # 0x95518
pushq $-0x1
popq %rax
movl %eax, 0x14(%rsp)
leaq 0x44783(%rip), %rdx     # 0xcd870
xorl %ebp, %ebp
movsd %xmm0, 0x20(%rsp)
movq %r15, 0x40(%rsp)
movq (%rdx), %rax
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %rbp
jge 0x8933b
imulq $0x340, %rbp, %rcx      # imm = 0x340
cmpl $0x3, 0x30f08(%rax,%rcx)
je 0x89333
movsd %xmm1, 0x8(%rsp)
xorl %ebx, %ebx
movq 0x28(%rsp), %r15
cmpq %rbx, %r12
je 0x8917b
movq %r15, %rdi
callq 0x2b47e
xorl %ecx, %ecx
cmpl 0x1c(%rsp), %eax
sete %cl
leaq 0x18a8c(%rip), %rax     # 0xa1bd8
movsd (%rax,%rcx,8), %xmm0
movq (%r15), %rax
movsd (%rax,%rbp,8), %xmm1
mulsd %xmm0, %xmm1
movsd 0x48(%rsp), %xmm2
subsd %xmm1, %xmm2
divsd %xmm0, %xmm2
movsd %xmm2, (%r13,%rbx,8)
incq %rbx
movq 0x10(%r15), %r15
jmp 0x8912f
movq %r13, %rdi
movq 0x40(%rsp), %r15
movq %r15, %rsi
pushq $0x8
popq %rdx
leaq -0x53dd1(%rip), %rcx    # 0x353bf
callq 0x4240
movsd (%r13), %xmm0
ucomisd -0x8(%r13,%r15,8), %xmm0
jne 0x891aa
jnp 0x89326
movsd %xmm0, (%r14)
movl $0x1, 0x8(%r14)
pushq $0x1
popq %rcx
pushq $0x1
popq %rax
xorl %r9d, %r9d
xorpd %xmm5, %xmm5
movsd 0xcc0c(%rip), %xmm6     # 0x95dd8
movsd 0x8(%rsp), %xmm0
cmpq %r15, %rcx
jge 0x8920f
movslq %eax, %rsi
shlq $0x4, %rsi
leaq (%r14,%rsi), %rdx
movsd -0x10(%r14,%rsi), %xmm1
movsd (%r13,%rcx,8), %xmm2
ucomisd %xmm2, %xmm1
jne 0x891f8
jnp 0x89207
movsd %xmm2, (%rdx)
movl $0x1, 0x8(%rdx)
incl %eax
jmp 0x8920a
incl -0x8(%rdx)
incq %rcx
jmp 0x891d2
decl %eax
movq %rax, %rdx
shlq $0x4, %rdx
addq 0x38(%rsp), %rdx
xorpd %xmm7, %xmm7
xorl %ecx, %ecx
movl %eax, %esi
testl %esi, %esi
jle 0x89251
movsd -0x8(%rdx), %xmm1
ucomisd %xmm5, %xmm1
jbe 0x89251
movl (%rdx), %edi
addl %edi, %ecx
subsd (%r14), %xmm1
cvtsi2sd %edi, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm7
addq $-0x10, %rdx
decl %esi
jmp 0x89225
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
shlq $0x4, %rax
xorl %esi, %esi
movsd 0xc2b2(%rip), %xmm1     # 0x95518
cmpq %rdx, %rax
je 0x892c7
ucomisd %xmm7, %xmm1
movsd (%r14,%rdx), %xmm2
jbe 0x8928a
movsd 0x10(%r14,%rdx), %xmm0
addsd %xmm2, %xmm0
mulsd %xmm6, %xmm0
movapd %xmm7, %xmm1
ucomisd %xmm2, %xmm5
movl 0x8(%r14,%rdx), %edi
movl $0x0, %r8d
cmoval %edi, %r8d
cmoval %r9d, %edi
addl %r8d, %esi
subl %edi, %ecx
movl %esi, %edi
subl %ecx, %edi
cvtsi2sd %edi, %xmm3
movsd 0x10(%r14,%rdx), %xmm4
subsd %xmm2, %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm7
addq $0x10, %rdx
jmp 0x89266
movq 0x28(%rsp), %rdi
movl %ebp, %esi
leaq 0x50(%rsp), %rbx
movq %rbx, %rdx
leaq 0x118(%rsp), %rcx
xorl %r8d, %r8d
movsd %xmm0, 0x8(%rsp)
callq 0x91c39
movq %rbx, %rdi
movl 0x18(%rsp), %esi
callq 0x1bb11
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x89326
movq 0x30(%rsp), %rax
movsd 0x8(%rsp), %xmm1
movsd %xmm1, (%rax)
movl %ebp, 0x14(%rsp)
movsd %xmm0, 0x20(%rsp)
leaq 0x4454c(%rip), %rdx     # 0xcd870
jmp 0x89333
leaq 0x44543(%rip), %rdx     # 0xcd870
movsd 0x8(%rsp), %xmm1
incq %rbp
jmp 0x890fa
movq %r13, %rdi
callq 0x34ff7
movq %r14, %rdi
callq 0x34ff7
movl 0x14(%rsp), %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq