// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rdx, 0x30(%rsp)
movq %rdi, 0x20(%rsp)
leaq 0x5d4ab(%rip), %rdx     # 0xd2870
movq (%rdx), %rax
cmpl $0x0, 0x1bc(%rax)
jle 0x755f2
movq %rsi, %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movsd 0x2d133(%rip), %xmm0    # 0xa2518
xorl %r12d, %r12d
imulq $0x340, %r12, %rcx      # imm = 0x340
cmpl $0x2, 0x30f08(%rax,%rcx)
jne 0x755da
movsd %xmm0, (%rsp)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
leaq 0xc(%rsp), %rdx
callq 0x9d053
movq %rax, %r13
movl 0xc(%rsp), %r15d
movq %rbx, %rdi
callq 0x2f6a1
movq %rbx, %rdi
movsd %xmm0, 0x18(%rsp)
callq 0x2f6f9
cmpl $0x0, 0xc(%rsp)
movq %r13, 0x28(%rsp)
jle 0x755ba
movapd %xmm0, %xmm3
xorpd %xmm2, %xmm2
xorl %ebx, %ebx
xorps %xmm4, %xmm4
movsd 0x2d0b6(%rip), %xmm0    # 0xa2510
movsd (%rsp), %xmm1
movq (%r13), %rax
movsd (%rax,%r12,8), %xmm5
ucomisd %xmm0, %xmm5
xorps %xmm7, %xmm7
cvtsi2sd %r15d, %xmm7
movsd %xmm3, 0x50(%rsp)
movsd %xmm2, 0x48(%rsp)
movsd %xmm4, 0x40(%rsp)
movsd %xmm7, 0x38(%rsp)
jne 0x75491
jnp 0x754ed
ucomisd 0x2d077(%rip), %xmm0    # 0xa2510
movsd %xmm5, 0x10(%rsp)
jne 0x754a3
jnp 0x754f3
movapd %xmm2, %xmm6
addsd %xmm3, %xmm6
ucomisd %xmm6, %xmm1
jbe 0x754f3
xorps %xmm1, %xmm1
cvtsi2sd %ebx, %xmm1
divsd %xmm1, %xmm2
divsd %xmm7, %xmm3
ucomisd %xmm2, %xmm3
setbe %al
addb %al, %al
orb $0x3c, %al
movq 0x30(%rsp), %rcx
movb %al, (%rcx)
addsd %xmm5, %xmm0
mulsd 0x2d83a(%rip), %xmm0    # 0xa2d18
movsd %xmm0, (%r14)
movl %r12d, %ebp
movsd %xmm6, (%rsp)
jmp 0x754f8
movsd %xmm0, 0x10(%rsp)
movsd %xmm1, (%rsp)
movq %r13, %rdi
callq 0x2eaee
xorps %xmm2, %xmm2
cvtsi2sd %ebx, %xmm2
movsd 0x40(%rsp), %xmm5
movapd %xmm5, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
incq %rbx
xorps %xmm3, %xmm3
cvtsi2sd %ebx, %xmm3
divsd %xmm3, %xmm1
movsd 0x18(%rsp), %xmm4
movsd 0x38(%rsp), %xmm6
mulsd %xmm4, %xmm6
decl %r15d
xorps %xmm3, %xmm3
cvtsi2sd %r15d, %xmm3
subsd %xmm0, %xmm6
divsd %xmm3, %xmm6
subsd %xmm1, %xmm5
mulsd %xmm5, %xmm5
mulsd %xmm2, %xmm5
addsd 0x48(%rsp), %xmm5
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
mulsd %xmm2, %xmm2
addsd %xmm5, %xmm2
subsd %xmm4, %xmm0
subsd %xmm6, %xmm4
mulsd %xmm4, %xmm4
mulsd %xmm3, %xmm4
movsd 0x50(%rsp), %xmm3
subsd %xmm4, %xmm3
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm3
movslq 0xc(%rsp), %rax
addq $0x30, %r13
movsd %xmm6, 0x18(%rsp)
movapd %xmm1, %xmm4
movsd (%rsp), %xmm5
movaps %xmm5, %xmm1
movsd 0x10(%rsp), %xmm0
cmpq %rax, %rbx
jl 0x7545f
jmp 0x755bf
movsd (%rsp), %xmm5
movsd %xmm5, (%rsp)
movq 0x28(%rsp), %rdi
callq 0x39194
movsd (%rsp), %xmm0
leaq 0x5d296(%rip), %rdx     # 0xd2870
incq %r12
movq (%rdx), %rax
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %r12
jl 0x753e8
jmp 0x755f7
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq