// File: /workspace/github_c_makefile_star2_O2/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, 0x68(%rsp)
movq %rsi, %r14
movq %rdi, 0x58(%rsp)
leaq 0x60c96(%rip), %rax     # 0xcd870
movq (%rax), %rax
movsd 0x28933(%rip), %xmm0    # 0x95518
pushq $-0x1
popq %rbp
xorl %r12d, %r12d
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %r12
jge 0x6cdf8
imulq $0x340, %r12, %rcx      # imm = 0x340
cmpl $0x2, 0x30f08(%rax,%rcx)
jne 0x6cdf0
movq 0x58(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
leaq 0xc(%rsp), %rdx
movsd %xmm0, 0x10(%rsp)
callq 0x90c1a
movq %rax, %r13
movl 0xc(%rsp), %r15d
movq %rbx, %rdi
callq 0x2c08f
movapd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
callq 0x2c0e3
movsd 0x10(%rsp), %xmm2
xorpd %xmm3, %xmm3
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
xorpd %xmm1, %xmm1
unpcklpd 0x40(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
movq %r13, 0x60(%rsp)
xorl %ebx, %ebx
movsd 0x288a0(%rip), %xmm0    # 0x95510
movslq 0xc(%rsp), %rax
cmpq %rax, %rbx
jge 0x6cdd0
movq (%r13), %rax
movsd (%rax,%r12,8), %xmm4
ucomisd %xmm0, %xmm4
cvtsi2sd %r15d, %xmm5
movapd %xmm3, 0x10(%rsp)
movapd %xmm1, 0x40(%rsp)
movapd %xmm5, 0x70(%rsp)
jne 0x6cca7
jnp 0x6cd11
ucomisd 0x28861(%rip), %xmm0    # 0x95510
movsd %xmm4, 0x28(%rsp)
jne 0x6ccb9
jnp 0x6cd17
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
addsd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
jbe 0x6cd17
movapd %xmm1, 0x30(%rsp)
cvtsi2sd %ebx, %xmm1
unpcklpd %xmm5, %xmm1            # xmm1 = xmm1[0],xmm5[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 0x68(%rsp), %rcx
movb %al, (%rcx)
addsd %xmm4, %xmm0
mulsd 0x29019(%rip), %xmm0    # 0x95d20
movsd %xmm0, (%r14)
movl %r12d, %ebp
jmp 0x6cd1d
movsd %xmm0, 0x28(%rsp)
movapd %xmm2, 0x30(%rsp)
movq %r13, %rdi
callq 0x2b4a0
cvtsi2sd %ebx, %xmm2
incq %rbx
cvtsi2sd %ebx, %xmm3
movapd %xmm0, %xmm4
movapd 0x29494(%rip), %xmm7    # 0x961d0
xorpd %xmm7, %xmm4
decl %r15d
cvtsi2sd %r15d, %xmm5
movapd %xmm2, %xmm1
unpcklpd 0x70(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
movapd %xmm0, %xmm6
unpcklpd %xmm4, %xmm6            # xmm6 = xmm6[0],xmm4[0]
movapd 0x40(%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 0x10(%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
addq $0x30, %r13
movsd 0x28(%rsp), %xmm0
movapd 0x30(%rsp), %xmm2
jmp 0x6cc70
movq 0x60(%rsp), %rdi
movsd %xmm2, 0x10(%rsp)
callq 0x34ff7
movsd 0x10(%rsp), %xmm0
leaq 0x60a83(%rip), %rax     # 0xcd870
movq (%rax), %rax
incq %r12
jmp 0x6cbeb
movl %ebp, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq