// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movsd 0x3b8ca(%rip), %xmm0    # 0xe2010
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
leaq 0x5f0e4(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1bc(%rcx), %eax
jge 0xa6a94
leaq 0x5f0ce(%rip), %rax     # 0x105840
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x340, %rcx, %rcx      # imm = 0x340
movl 0x30f08(%rax,%rcx), %eax
subl $0x2, %eax
jne 0xa6a84
jmp 0xa6792
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq -0x28(%rbp), %rdx
callq 0xdc050
movq %rax, -0x38(%rbp)
movl $0x0, -0x20(%rbp)
movsd 0x3b853(%rip), %xmm0    # 0xe2008
movsd %xmm0, -0x48(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x8(%rbp), %rdi
callq 0x44600
movsd %xmm0, -0x80(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x8(%rbp), %rdi
movsd -0x80(%rbp), %xmm0
callq 0x44690
movsd %xmm0, -0x90(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0xa6a7b
movq -0x38(%rbp), %rax
movslq -0x20(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jne 0xa683a
jp 0xa683a
jmp 0xa68e5
movsd -0x48(%rbp), %xmm0
movsd 0x3b7c1(%rip), %xmm1    # 0xe2008
ucomisd %xmm1, %xmm0
jne 0xa6854
jp 0xa6854
jmp 0xa68db
movsd -0x78(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm1
movsd -0x60(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xa68d9
movsd -0x78(%rbp), %xmm1
cvtsi2sdl -0x2c(%rbp), %xmm0
divsd %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
cvtsi2sdl -0x30(%rbp), %xmm2
divsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0xa68a4
movq -0x18(%rbp), %rax
movb $0x3c, (%rax)
jmp 0xa68ab
movq -0x18(%rbp), %rax
movb $0x3e, (%rax)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movsd -0x48(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd 0x3c3d3(%rip), %xmm1    # 0xe2ca0
divsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0xa68db
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x20(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdi
callq 0x43580
movsd %xmm0, -0x50(%rbp)
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x68(%rbp), %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm2
movsd -0x50(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x80(%rbp), %xmm0
cvtsi2sdl -0x30(%rbp), %xmm2
movsd -0x50(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movl -0x30(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x78(%rbp), %xmm0
movsd -0x70(%rbp), %xmm1
subsd -0x68(%rbp), %xmm1
movsd -0x70(%rbp), %xmm2
subsd -0x68(%rbp), %xmm2
mulsd %xmm2, %xmm1
cvtsi2sdl -0x2c(%rbp), %xmm2
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x50(%rbp), %xmm0
subsd -0x68(%rbp), %xmm0
movsd -0x50(%rbp), %xmm2
subsd -0x68(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movsd -0x90(%rbp), %xmm0
movsd -0x88(%rbp), %xmm1
subsd -0x80(%rbp), %xmm1
movsd -0x88(%rbp), %xmm2
subsd -0x80(%rbp), %xmm2
mulsd %xmm2, %xmm1
movl -0x30(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm2
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd -0x50(%rbp), %xmm0
subsd -0x88(%rbp), %xmm0
movsd -0x50(%rbp), %xmm2
subsd -0x88(%rbp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xa67fb
movq -0x38(%rbp), %rdi
callq 0x52e80
jmp 0xa6a86
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xa6752
movl -0x24(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq