// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/regressiontree.c
// Function: best_regression_feature
int best_regression_feature(Instanceptr instances, double* split, char* comparison)
{
 /*!Last Changed 09.12.2004*/
 int i, k, bestfeature = -1, size, leftcount, rightcount;
 Instanceptr sorted;
 double value, valuebefore, regvalue, mse, bestmse = +INT_MAX;
	double leftmean, leftmeanbefore, lefterror;
	double rightmean, rightmeanbefore, righterror;
 for (i = 0; i < current_dataset->featurecount;i++)
   switch (current_dataset->features[i].type)
    {
     case REAL   :sorted = sort_instances(instances,i,&size);
                  k = 0;
                  valuebefore = -INT_MAX;
																		leftcount = 0;
																		rightcount = size;
																		leftmean = 0;
																		rightmean = find_mean_of_regression_value(instances);
																		lefterror = 0;
																		righterror = find_variance_of_regression_value(instances, rightmean);
                  while (k < size)
                   {
                    value = sorted[k].values[i].floatvalue;
                    if (value != valuebefore)
                     {
                      if (valuebefore != -INT_MAX)
                       {
																							 mse = lefterror + righterror;
																								if (mse < bestmse)
																								 {
																									 if ((lefterror / leftcount) < (righterror / rightcount))
																											 *comparison = '<';
																										else
																											 *comparison = '>';
																									 bestmse = mse;
																										bestfeature = i;
                          *split = (valuebefore + value) / 2;
																								 }
                       }
                      valuebefore = value;
                     }
                    regvalue = give_regressionvalue(&(sorted[k]));
																				leftmeanbefore = leftmean;
																				rightmeanbefore = rightmean;
																				leftmean = (leftmean * leftcount + regvalue) / (leftcount + 1);
																				rightmean = (rightmean * rightcount - regvalue) / (rightcount - 1);
																				lefterror = lefterror + (leftmeanbefore - leftmean) * (leftmeanbefore - leftmean) * leftcount + (regvalue - leftmean) * (regvalue - leftmean);
																				righterror = righterror - (rightmeanbefore - rightmean) * (rightmeanbefore - rightmean) * (rightcount - 1) - (regvalue - rightmeanbefore) * (regvalue - rightmeanbefore);
																				leftcount++;
																				rightcount--;
                    k++;
                   }
                  safe_free(sorted);
                  break;
    }
 return bestfeature;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, 0x58(%rsp)
movq %rdi, 0x48(%rsp)
leaq 0x5ebc9(%rip), %rax     # 0xd5870
movq (%rax), %rax
cmpl $0x0, 0x1bc(%rax)
jle 0x76ef2
movq %rsi, %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movsd 0x2e851(%rip), %xmm0    # 0xa5518
xorl %r12d, %r12d
imulq $0x340, %r12, %rcx      # imm = 0x340
cmpl $0x2, 0x30f08(%rax,%rcx)
jne 0x76edd
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
leaq 0xc(%rsp), %rdx
callq 0xa0055
movq %rax, %r13
movl 0xc(%rsp), %r15d
movq %rbx, %rdi
callq 0x2ffe1
movq %rbx, %rdi
movapd %xmm0, 0x30(%rsp)
callq 0x3002f
cmpl $0x0, 0xc(%rsp)
movq %r13, 0x50(%rsp)
jle 0x76eb8
xorpd %xmm1, %xmm1
unpcklpd 0x30(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
xorpd %xmm3, %xmm3
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
xorl %ebx, %ebx
movsd 0x2e7cc(%rip), %xmm0    # 0xa5510
movsd 0x10(%rsp), %xmm2
movq (%r13), %rax
movsd (%rax,%r12,8), %xmm5
ucomisd %xmm0, %xmm5
xorps %xmm6, %xmm6
cvtsi2sd %r15d, %xmm6
movapd %xmm3, 0x30(%rsp)
movapd %xmm1, 0x70(%rsp)
movapd %xmm6, 0x60(%rsp)
jne 0x76d76
jnp 0x76de3
ucomisd 0x2e792(%rip), %xmm0    # 0xa5510
movsd %xmm5, 0x28(%rsp)
jne 0x76d88
jnp 0x76de9
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
ucomisd %xmm4, %xmm2
jbe 0x76de9
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
unpcklpd %xmm6, %xmm1            # xmm1 = xmm1[0],xmm6[0]
movapd %xmm3, %xmm2
divpd %xmm1, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
ucomisd %xmm2, %xmm1
setbe %al
addb %al, %al
orb $0x3c, %al
movq 0x58(%rsp), %rcx
movb %al, (%rcx)
addsd %xmm5, %xmm0
mulsd 0x2ef45(%rip), %xmm0    # 0xa5d18
movsd %xmm0, (%r14)
movl %r12d, %ebp
movapd %xmm4, 0x10(%rsp)
jmp 0x76def
movsd %xmm0, 0x28(%rsp)
movapd %xmm2, 0x10(%rsp)
movq %r13, %rdi
callq 0x2f35e
xorps %xmm2, %xmm2
cvtsi2sd %ebx, %xmm2
incq %rbx
xorps %xmm3, %xmm3
cvtsi2sd %ebx, %xmm3
movapd %xmm0, %xmm4
decl %r15d
xorps %xmm5, %xmm5
cvtsi2sd %r15d, %xmm5
movapd 0x2f3b1(%rip), %xmm7    # 0xa61d0
xorpd %xmm7, %xmm4
movapd %xmm2, %xmm1
unpcklpd 0x60(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
movapd %xmm0, %xmm6
unpcklpd %xmm4, %xmm6            # xmm6 = xmm6[0],xmm4[0]
movapd 0x70(%rsp), %xmm8
mulpd %xmm8, %xmm1
addpd %xmm6, %xmm1
unpcklpd %xmm5, %xmm3            # xmm3 = xmm3[0],xmm5[0]
divpd %xmm3, %xmm1
movapd %xmm8, %xmm3
subpd %xmm1, %xmm3
movapd %xmm3, %xmm4
xorpd %xmm7, %xmm4
movsd %xmm3, %xmm4            # xmm4 = xmm3[0],xmm4[1]
mulpd %xmm3, %xmm4
unpcklpd %xmm5, %xmm2            # xmm2 = xmm2[0],xmm5[0]
mulpd %xmm4, %xmm2
addpd 0x30(%rsp), %xmm2
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movsd %xmm1, %xmm8            # xmm8 = xmm1[0],xmm8[1]
subpd %xmm8, %xmm0
movapd %xmm0, %xmm3
xorpd %xmm7, %xmm3
movsd %xmm0, %xmm3            # xmm3 = xmm0[0],xmm3[1]
mulpd %xmm0, %xmm3
addpd %xmm2, %xmm3
movslq 0xc(%rsp), %rax
addq $0x30, %r13
movaps 0x10(%rsp), %xmm4
movaps %xmm4, %xmm2
movsd 0x28(%rsp), %xmm0
cmpq %rax, %rbx
jl 0x76d4a
jmp 0x76ebe
movsd 0x10(%rsp), %xmm4
movaps %xmm4, 0x10(%rsp)
movq 0x50(%rsp), %rdi
callq 0x3a180
leaq 0x5e99c(%rip), %rax     # 0xd5870
movq (%rax), %rax
movapd 0x10(%rsp), %xmm0
incq %r12
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %r12
jl 0x76cca
jmp 0x76ef7
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq