// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/regressiontree.c
// Function: best_regression_feature_linear_leaves
int best_regression_feature_linear_leaves(Instanceptr instances, double* split, char* comparison)
{
	/*!Last Changed 28.02.2005*/
 int bestfeature = -1, i, size, k;
 Instanceptr sorted;
	matrix w, X, r;
 double value, valuebefore, mse, bestmse = +INT_MAX;
	double lefterror, 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;
                  while (k < size)
                   {
                    value = sorted[k].values[i].floatvalue;
                    if (value != valuebefore)
                     {
                      if (valuebefore != -INT_MAX)
                       {
                        create_regression_matrices_for_array_of_instances(sorted, 0, k - 1, &X, &r);
      		                w = multivariate_regression(X, r);
																								matrix_free(X);
																								matrix_free(r);
				                    lefterror = variance_of_linear_regression_for_array_of_instances(w, sorted, 0, k - 1, LINEARLEAF);
                        matrix_free(w);
                        create_regression_matrices_for_array_of_instances(sorted, k, size - 1, &X, &r);
      		                w = multivariate_regression(X, r);
																								matrix_free(X);
																								matrix_free(r);
				                    righterror = variance_of_linear_regression_for_array_of_instances(w, sorted, k, size - 1, LINEARLEAF);
                        matrix_free(w);
																							 mse = lefterror + righterror;
																								if (mse < bestmse)
																								 {
																									 if ((lefterror / k) < (righterror / (size - k)))
																											 *comparison = '<';
																										else
																											 *comparison = '>';
																									 bestmse = mse;
																										bestfeature = i;
                          *split = (valuebefore + value) / 2;
																								 }
                       }
                      valuebefore = value;
                     }
                    k++;
                   }
                  safe_free(sorted);
                  break;
    }
 return bestfeature;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdi, 0x70(%rsp)
leaq 0x5f163(%rip), %rax     # 0xd5870
movq (%rax), %rax
cmpl $0x0, 0x1bc(%rax)
jle 0x76a10
movsd 0x2edf3(%rip), %xmm0    # 0xa5518
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %ebx, %ebx
imulq $0x340, %rbx, %rcx      # imm = 0x340
cmpl $0x2, 0x30f08(%rax,%rcx)
jne 0x769fb
movsd %xmm0, 0x8(%rsp)
movq 0x70(%rsp), %rdi
movl %ebx, %esi
movq %rsp, %rdx
callq 0xa0055
movq %rax, %r15
movl (%rsp), %eax
testl %eax, %eax
jle 0x769d7
xorl %esi, %esi
movq %r15, %r12
xorl %r14d, %r14d
movsd 0x2ed99(%rip), %xmm2    # 0xa5510
movsd 0x8(%rsp), %xmm0
movq %rbx, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movq (%r12), %rcx
movsd (%rcx,%rbx,8), %xmm1
ucomisd %xmm2, %xmm1
jne 0x7679c
jnp 0x76994
ucomisd 0x2ed6c(%rip), %xmm2    # 0xa5510
jne 0x767ac
jnp 0x7699e
movsd %xmm2, 0x88(%rsp)
movsd %xmm1, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
leal -0x1(%r14), %edx
movl %edx, 0x60(%rsp)
movq %r15, %rdi
xorl %esi, %esi
leaq 0x50(%rsp), %rcx
leaq 0x40(%rsp), %r8
movsd %xmm0, 0x8(%rsp)
callq 0x76a2e
movq 0x50(%rsp), %rbp
movq 0x58(%rsp), %r13
movq 0x40(%rsp), %r15
movq 0x48(%rsp), %rbx
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x76b4b
movq %rax, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3ed51
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3ed51
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
movq 0x20(%rsp), %rdx
xorl %ecx, %ecx
movl 0x60(%rsp), %r8d
movl $0x1, %r9d
callq 0x76c00
movapd %xmm0, 0x60(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3ed51
movl (%rsp), %edx
decl %edx
movq 0x20(%rsp), %rdi
movl %r14d, %esi
leaq 0x50(%rsp), %rcx
leaq 0x40(%rsp), %r8
callq 0x76a2e
movq 0x50(%rsp), %rbx
movq 0x58(%rsp), %r15
movq 0x40(%rsp), %rbp
movq 0x48(%rsp), %r13
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x76b4b
movq %rax, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq 0x20(%rsp), %r15
callq 0x3ed51
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3ed51
movl (%rsp), %r8d
decl %r8d
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
movl %r14d, %ecx
movl $0x1, %r9d
callq 0x76c00
movapd %xmm0, 0x90(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3ed51
movapd 0x90(%rsp), %xmm4
movapd 0x60(%rsp), %xmm2
movsd 0x8(%rsp), %xmm0
movapd %xmm2, %xmm3
addsd %xmm4, %xmm3
ucomisd %xmm3, %xmm0
movl (%rsp), %eax
jbe 0x769a4
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movq 0x38(%rsp), %rsi
leal (%rsi,%rax), %ecx
cvtsi2sd %ecx, %xmm1
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
unpcklpd %xmm4, %xmm2            # xmm2 = xmm2[0],xmm4[0]
divpd %xmm0, %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
ucomisd %xmm2, %xmm0
setbe %cl
addb %cl, %cl
orb $0x3c, %cl
movq 0x80(%rsp), %rdx
movb %cl, (%rdx)
movsd 0x30(%rsp), %xmm1
movsd 0x88(%rsp), %xmm0
addsd %xmm1, %xmm0
mulsd 0x2f398(%rip), %xmm0    # 0xa5d18
movq 0x78(%rsp), %rcx
movsd %xmm0, (%rcx)
movq 0x28(%rsp), %rbx
movl %ebx, 0x4(%rsp)
jmp 0x769b8
movapd %xmm0, %xmm3
movapd %xmm2, %xmm1
jmp 0x769b8
movapd %xmm0, %xmm3
jmp 0x769b8
movapd %xmm0, %xmm3
movq 0x28(%rsp), %rbx
movq 0x38(%rsp), %rsi
movsd 0x30(%rsp), %xmm1
incq %r14
movslq %eax, %rcx
decl %esi
addq $0x30, %r12
movapd %xmm1, %xmm2
movapd %xmm3, %xmm0
cmpq %rcx, %r14
jl 0x76787
jmp 0x769dd
movsd 0x8(%rsp), %xmm3
movsd %xmm3, 0x8(%rsp)
movq %r15, %rdi
callq 0x3a180
leaq 0x5ee7e(%rip), %rax     # 0xd5870
movq (%rax), %rax
movsd 0x8(%rsp), %xmm0
incq %rbx
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %rbx
jl 0x7672f
jmp 0x76a18
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl 0x4(%rsp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq