// File: /workspace/github_c_makefile_star2_O3/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 $0x1f8, %rsp            # imm = 0x1F8
movsd %xmm0, 0x58(%rsp)
movl %edx, %ebx
movq %rsi, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
callq 0x30309
movl %eax, %r14d
movslq %eax, %r15
leaq (,%r15,8), %rdi
leaq 0x1a418(%rip), %r13     # 0xb1cb2
movq %r13, %rsi
movl $0x2, %edx
callq 0x3a0e1
movq %rax, %r12
movq %r15, 0x48(%rsp)
movq %r15, %rdi
shlq $0x4, %rdi
movq %r13, %rsi
movl $0x3, %edx
callq 0x3a0e1
leaq 0x3dfa6(%rip), %rsi     # 0xd5870
movq (%rsi), %rdx
cmpl $0x0, 0x1bc(%rdx)
movq %rax, 0x20(%rsp)
jle 0x97bca
movl %ebx, 0x1c(%rsp)
movl %r14d, %ebx
addq $0x8, %rax
movq %rax, 0x38(%rsp)
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movsd 0xdc19(%rip), %xmm0     # 0xa5518
leaq 0x1a37a(%rip), %r15     # 0xb1c80
xorl %ebp, %ebp
movsd %xmm0, 0x28(%rsp)
movl %r14d, 0x18(%rsp)
movq %rbx, 0x50(%rsp)
imulq $0x340, %rbp, %rcx      # imm = 0x340
cmpl $0x3, 0x30f08(%rdx,%rcx)
je 0x97bb2
movsd %xmm0, 0x10(%rsp)
testl %r14d, %r14d
movq %r12, %r14
movl 0x1c(%rsp), %edx
movq %rbp, %rsi
movq %rbx, %r13
jle 0x979a1
xorl %ebx, %ebx
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
movl %edx, %r12d
movq %rsi, %r13
callq 0x2f33a
movq %r13, %rsi
movl %r12d, %edx
movsd 0x58(%rsp), %xmm2
xorl %ecx, %ecx
cmpl %r12d, %eax
sete %cl
movsd (%r15,%rcx,8), %xmm0
movq (%rbp), %rax
movsd (%rax,%r13,8), %xmm1
movq 0x50(%rsp), %r13
mulsd %xmm0, %xmm1
subsd %xmm1, %xmm2
divsd %xmm0, %xmm2
movsd %xmm2, (%r14,%rbx,8)
incq %rbx
movq 0x10(%rbp), %rbp
cmpq %rbx, %r13
jne 0x9794c
movq %rsi, %rbp
movl $0x8, %edx
movq %r14, %rdi
movq 0x48(%rsp), %rbx
movq %rbx, %rsi
leaq -0x5d3c8(%rip), %rcx    # 0x3a5f3
callq 0x4240
movsd (%r14), %xmm0
ucomisd -0x8(%r14,%rbx,8), %xmm0
movq %r14, %r12
jne 0x979ee
jp 0x979ee
movl 0x18(%rsp), %r14d
leaq 0x3de91(%rip), %rsi     # 0xd5870
leaq 0x1a29a(%rip), %r15     # 0xb1c80
movq %r13, %rbx
jmp 0x97bac
movq 0x20(%rsp), %r9
movsd %xmm0, (%r9)
movl $0x1, 0x8(%r9)
movl 0x18(%rsp), %r14d
cmpl $0x2, %r14d
movq %r13, %rbx
xorpd %xmm5, %xmm5
movsd 0x10(%rsp), %xmm0
jl 0x97b35
movl $0x1, %edx
movl $0x1, %eax
movslq %edx, %rsi
shlq $0x4, %rsi
leaq (%r9,%rsi), %rcx
movsd -0x10(%r9,%rsi), %xmm1
movsd (%r12,%rax,8), %xmm2
ucomisd %xmm2, %xmm1
jne 0x97a46
jnp 0x97a55
movsd %xmm2, (%rcx)
movl $0x1, 0x8(%rcx)
incl %edx
jmp 0x97a58
incl -0x8(%rcx)
incq %rax
cmpq %rax, %rbx
jne 0x97a26
cmpl $0x2, %edx
jl 0x97b35
leal -0x1(%rdx), %eax
movl %eax, %eax
leaq 0x1(%rax), %rsi
shlq $0x4, %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax), %rdi
xorl %ecx, %ecx
xorpd %xmm6, %xmm6
movsd -0x8(%rdi), %xmm1
ucomisd %xmm5, %xmm1
jbe 0x97ab8
movl (%rdi), %r8d
subsd (%r9), %xmm1
addl %r8d, %ecx
xorps %xmm2, %xmm2
cvtsi2sd %r8d, %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm6
decq %rsi
addq $-0x10, %rdi
cmpq $0x1, %rsi
jg 0x97a85
cmpl $0x2, %edx
jl 0x97b35
xorl %edx, %edx
movsd 0xda51(%rip), %xmm1     # 0xa5518
xorl %esi, %esi
ucomisd %xmm6, %xmm1
movsd (%r9,%rdx), %xmm2
jbe 0x97aec
movsd 0x10(%r9,%rdx), %xmm0
addsd %xmm2, %xmm0
mulsd 0xe2e8(%rip), %xmm0     # 0xa5dd0
movapd %xmm6, %xmm1
ucomisd %xmm2, %xmm5
movl 0x8(%r9,%rdx), %edi
movl $0x0, %r8d
cmoval %edi, %r8d
movl $0x0, %r10d
cmoval %r10d, %edi
addl %r8d, %esi
subl %edi, %ecx
movl %esi, %edi
subl %ecx, %edi
xorps %xmm3, %xmm3
cvtsi2sd %edi, %xmm3
movsd 0x10(%r9,%rdx), %xmm4
subsd %xmm2, %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm6
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x97ac9
movsd %xmm0, 0x10(%rsp)
movq 0x30(%rsp), %rdi
movl %ebp, %esi
leaq 0x60(%rsp), %r15
movq %r15, %rdx
leaq 0x128(%rsp), %rcx
xorl %r8d, %r8d
callq 0xa11fa
movq %r15, %rdi
movl %r14d, %esi
callq 0x1e071
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x97b9e
movq 0x40(%rsp), %rax
movsd 0x10(%rsp), %xmm1
movsd %xmm1, (%rax)
movl %ebp, 0xc(%rsp)
movsd %xmm0, 0x28(%rsp)
leaq 0x3dcdf(%rip), %rsi     # 0xd5870
leaq 0x1a0e8(%rip), %r15     # 0xb1c80
movapd %xmm1, %xmm0
jmp 0x97bb2
leaq 0x3dccb(%rip), %rsi     # 0xd5870
leaq 0x1a0d4(%rip), %r15     # 0xb1c80
movsd 0x10(%rsp), %xmm0
incq %rbp
movq (%rsi), %rdx
movslq 0x1bc(%rdx), %rcx
cmpq %rcx, %rbp
jl 0x97918
jmp 0x97bd2
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq %r12, %rdi
callq 0x3a180
movq 0x20(%rsp), %rdi
callq 0x3a180
movl 0xc(%rsp), %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq