// File: /workspace/github_c_makefile_star2_O2/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 $0xb8, %rsp
movq %rdx, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movq %rdi, 0x80(%rsp)
leaq 0x611e0(%rip), %rax     # 0xcd870
movq (%rax), %rax
pushq $-0x1
popq %rcx
movl %ecx, 0x1c(%rsp)
movsd 0x28e76(%rip), %xmm0    # 0x95518
xorl %ebx, %ebx
movslq 0x1bc(%rax), %rcx
cmpq %rcx, %rbx
jge 0x6c97f
imulq $0x340, %rbx, %rcx      # imm = 0x340
cmpl $0x2, 0x30f08(%rax,%rcx)
jne 0x6c977
movq 0x80(%rsp), %rdi
movl %ebx, %esi
leaq 0xc(%rsp), %rdx
movsd %xmm0, 0x10(%rsp)
callq 0x90c1a
movsd 0x10(%rsp), %xmm1
xorl %edx, %edx
movq %rax, 0x20(%rsp)
movq %rax, %rcx
xorl %r14d, %r14d
movsd 0x28e12(%rip), %xmm3    # 0x95510
movq %rbx, 0x28(%rsp)
movslq 0xc(%rsp), %rax
cmpq %rax, %r14
jge 0x6c957
movq (%rcx), %rax
movsd (%rax,%rbx,8), %xmm2
ucomisd %xmm3, %xmm2
jne 0x6c725
jnp 0x6c918
ucomisd 0x28de3(%rip), %xmm3    # 0x95510
jne 0x6c735
jnp 0x6c922
movsd %xmm3, 0x98(%rsp)
movsd %xmm2, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
leal -0x1(%r14), %edx
movl %edx, 0x70(%rsp)
movq 0x20(%rsp), %rdi
xorl %esi, %esi
leaq 0x60(%rsp), %rcx
leaq 0x50(%rsp), %r8
movsd %xmm1, 0x10(%rsp)
callq 0x6c995
movq 0x60(%rsp), %r13
movq 0x68(%rsp), %r15
movq 0x50(%rsp), %r12
movq %r14, 0x48(%rsp)
movq 0x58(%rsp), %r14
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x6ca8d
movq %rax, %rbp
movq %rdx, %rbx
movq %r13, %rdi
movq %r15, %rsi
callq 0x3929f
movq %r12, %rdi
movq %r14, %rsi
callq 0x3929f
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdx
xorl %ecx, %ecx
movl 0x70(%rsp), %r8d
pushq $0x1
popq %r9
callq 0x6cb42
movapd %xmm0, 0x70(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3929f
movl 0xc(%rsp), %edx
decl %edx
movq %r14, %rdi
movq 0x48(%rsp), %rsi
leaq 0x60(%rsp), %rcx
leaq 0x50(%rsp), %r8
callq 0x6c995
movq 0x60(%rsp), %rbx
movq 0x68(%rsp), %r14
movq 0x50(%rsp), %r15
movq 0x58(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x6ca8d
movq %rax, %r13
movq %rdx, %rbp
movq %rbx, %rdi
movq %r14, %rsi
movq 0x48(%rsp), %r14
callq 0x3929f
movq %r15, %rdi
movq %r12, %rsi
callq 0x3929f
movl 0xc(%rsp), %r8d
decl %r8d
movq %r13, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
movl %r14d, %ecx
pushq $0x1
popq %r9
callq 0x6cb42
movapd %xmm0, 0xa0(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3929f
movapd 0xa0(%rsp), %xmm4
movapd 0x70(%rsp), %xmm3
movsd 0x10(%rsp), %xmm1
movapd %xmm3, %xmm0
addsd %xmm4, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x6c928
cvtsi2sd %r14d, %xmm1
movl 0xc(%rsp), %eax
movq 0x40(%rsp), %rdx
addl %edx, %eax
cvtsi2sd %eax, %xmm2
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
unpcklpd %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0]
divpd %xmm1, %xmm3
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
ucomisd %xmm3, %xmm1
setbe %al
addb %al, %al
orb $0x3c, %al
movq 0x90(%rsp), %rcx
movb %al, (%rcx)
movsd 0x30(%rsp), %xmm2
movsd 0x98(%rsp), %xmm1
addsd %xmm2, %xmm1
mulsd 0x29424(%rip), %xmm1    # 0x95d20
movq 0x88(%rsp), %rax
movsd %xmm1, (%rax)
movq 0x28(%rsp), %rbx
movl %ebx, 0x1c(%rsp)
movq 0x38(%rsp), %rcx
jmp 0x6c941
movapd %xmm1, %xmm0
movapd %xmm3, %xmm2
jmp 0x6c941
movapd %xmm1, %xmm0
jmp 0x6c941
movapd %xmm1, %xmm0
movq 0x28(%rsp), %rbx
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rcx
movsd 0x30(%rsp), %xmm2
incq %r14
addq $0x30, %rcx
decl %edx
movapd %xmm0, %xmm1
movapd %xmm2, %xmm3
jmp 0x6c703
movq 0x20(%rsp), %rdi
movsd %xmm1, 0x10(%rsp)
callq 0x34ff7
movsd 0x10(%rsp), %xmm0
leaq 0x60efc(%rip), %rax     # 0xcd870
movq (%rax), %rax
incq %rbx
jmp 0x6c6a4
movl 0x1c(%rsp), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq