// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/regressiontree.c
// Function: best_hybrid_regression_tree_split_crossvalidation
int best_hybrid_regression_tree_split_crossvalidation(Regressionnodeptr node, Regtree_parameterptr param)
{
	/*!Last Changed 24.03.2005*/
 int hybridspace = get_parameter_i("hybridspace");
 FILE* errorfiles[3];
	int i, k, trainsize, testsize, bestmodel = 0, *models, dummy, dummy2, testtype;
	Instanceptr train = NULL, test = NULL;
	char **files, st[READLINELENGTH] = "-", dummy3, pst[READLINELENGTH], buffer[READLINELENGTH];
	double error;
 files = safemalloc(hybridspace * sizeof(char *), "best_hybrid_regression_tree_split_crossvalidation", 5);
 strcpy(buffer, get_parameter_s("tempdirectory"));
 strcat(buffer, "/univariate.txt");
 files[0] = strcopy(files[0], buffer);
 strcpy(buffer, get_parameter_s("tempdirectory"));
 strcat(buffer, "/linear.txt");
 files[1] = strcopy(files[1], buffer);
 if (hybridspace == 3)
  {
   strcpy(buffer, get_parameter_s("tempdirectory"));
   strcat(buffer, "/quadratic.txt");
   files[2] = strcopy(files[2], buffer);
  }
 for (i = 0; i < hybridspace; i++)
  {
   errorfiles[i] = fopen(files[i], "w");
   if (!errorfiles[i])
     return 0;
  }
	for (i = 0; i < 5; i++)
	 {
		 divide_instancelist(&(node->instances), &train, &test, 2);
			trainsize = data_size(train);
			testsize = data_size(test);
			if (trainsize <= 1 || testsize <= 1)
			 {
  			node->instances = restore_instancelist(node->instances, train);
	  		merge_instancelist(&(node->instances), test);	
					bestmodel = 1;
				 break;
			 }
   for (k = 0; k < hybridspace; k++)
    {
     switch (k)
      {
       case MODEL_UNI:error = regression_tree_model_selection_error(&train, &test, UNIVARIATE, param->leaftype);
                      break;
       case MODEL_LIN:error = regression_tree_model_selection_error(&train, &test, LINEAR, param->leaftype);
                      break;
       case MODEL_QUA:error = regression_tree_model_selection_error(&train, &test, QUADRATIC, param->leaftype);
                      break;
      }
     set_precision(pst, NULL, "\n");
     fprintf(errorfiles[k], pst, error / (testsize + 0.0));
    }
   for (k = 0; k < hybridspace; k++)
    {
     switch (k)
      {
       case MODEL_UNI:error = regression_tree_model_selection_error(&test, &train, UNIVARIATE, param->leaftype);
                      break;
       case MODEL_LIN:error = regression_tree_model_selection_error(&test, &train, LINEAR, param->leaftype);
                      break;
       case MODEL_QUA:error = regression_tree_model_selection_error(&test, &train, QUADRATIC, param->leaftype);
                      break;
      }
     set_precision(pst, NULL, "\n");
     fprintf(errorfiles[k], pst, error / (trainsize + 0.0));
    }
			node->instances = restore_instancelist(node->instances, train);
			merge_instancelist(&(node->instances), test);	
			train = NULL;
			test = NULL;
	 }
 for (i = 0; i < hybridspace; i++)
   fclose(errorfiles[i]);
 if (bestmodel)
	 {
		 free_2d((void**)files, hybridspace);
   if (param->leaftype == CONSTANTLEAF)
     node->featureno = best_regression_feature(node->instances, &(node->split), &dummy3);
	  else
		   node->featureno = best_regression_feature_linear_leaves(node->instances, &(node->split), &dummy3);
			if (node->featureno == LEAF_NODE)
				 return 0;
			return 1;
	 }
 testtype = get_parameter_l("testtype");
 if (in_list(testtype, 9, FTEST, PAIREDT5X2, COMBINEDT5X2, PERMUTATIONTEST, PAIREDPERMUTATIONTEST, SIGNTEST, RANKSUMTEST, SIGNEDRANKTEST, NOTEST))
   models = multiple_comparison(files, hybridspace, testtype, st, &dummy, &dummy2, param->correctiontype);
 else
   models = multiple_comparison(files, hybridspace, FTEST, st, &dummy, &dummy2, param->correctiontype);
	switch (models[0])
	 {
	  case 1:if (param->leaftype == CONSTANTLEAF)
            return best_regression_feature(node->instances, &(node->split), &dummy3);
	         else
		          return best_regression_feature_linear_leaves(node->instances, &(node->split), &dummy3);
										break;
			case 2:find_linear_regression_split(node->instances, &(node->leftcenter), &(node->rightcenter));
  				    node->featureno = LINEAR;
				      break;
			case 3:find_quadratic_regression_split(node->instances, &(node->leftcenter), &(node->rightcenter));
  				    node->featureno = QUADRATIC;
				      break;
	 }
	safe_free(models);
	free_2d((void**)files, hybridspace);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x16e3e8, %rsp         # imm = 0x16E3E8
movq %rsi, 0x18(%rsp)
movq %rdi, %r13
leaq 0x2f958(%rip), %rdi     # 0xa5c20
callq 0x5341c
movl %eax, %ebp
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x10(%rsp)
leaq 0x7a1a0(%rsp), %rbx
movl $0x7a120, %edx          # imm = 0x7A120
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4050
movb $0x2d, (%rbx)
movslq %ebp, %rdi
shlq $0x3, %rdi
leaq 0x368ae(%rip), %rsi     # 0xacbb1
movl $0x5, %edx
callq 0x390f5
movq %rax, %r14
leaq 0x310a5(%rip), %r15     # 0xa73bc
movq %r15, %rdi
callq 0x5356b
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movupd 0x31583(%rip), %xmm0    # 0xa78c5
movupd %xmm0, 0x80(%rsp,%rax)
movq (%r14), %rdi
movq %rbx, %rsi
callq 0x3a404
movq %rax, (%r14)
movq %r15, %rdi
callq 0x5356b
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movabsq $0x2e7261656e696c2f, %rcx # imm = 0x2E7261656E696C2F
movq %rcx, 0x80(%rsp,%rax)
movl $0x747874, 0x88(%rsp,%rax) # imm = 0x747874
movq 0x8(%r14), %rdi
movq %rbx, %rsi
callq 0x3a404
movq %rax, 0x8(%r14)
movl %ebp, 0x4(%rsp)
cmpl $0x3, %ebp
movq %r14, %rbp
jne 0x76408
leaq 0x31008(%rip), %rdi     # 0xa73bc
callq 0x5356b
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movabsq $0x746172646175712f, %rcx # imm = 0x746172646175712F
movq %rcx, 0x80(%rsp,%rax)
movabsq $0x7478742e636974, %rcx # imm = 0x7478742E636974
movq %rcx, 0x87(%rsp,%rax)
movq 0x10(%rbp), %rdi
movq %rbx, %rsi
callq 0x3a404
movq %rax, 0x10(%rbp)
movl 0x4(%rsp), %eax
movl %eax, %r12d
testl %eax, %eax
jle 0x76442
xorl %ebx, %ebx
leaq 0x32485(%rip), %r15     # 0xa88a1
xorl %r14d, %r14d
movq (%rbp,%r14,8), %rdi
movq %r15, %rsi
callq 0x40f0
movq %rax, 0x60(%rsp,%r14,8)
testq %rax, %rax
je 0x767fe
incq %r14
cmpq %r14, %r12
jne 0x7641f
movq %r13, %r14
addq $0x20, %r13
leaq 0x8(%rsp), %rbx
leaq 0x10(%rsp), %r15
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl $0x2, %ecx
callq 0x30746
movq (%rbx), %rdi
callq 0x2fa14
movl %eax, %ebx
movq (%r15), %rdi
callq 0x2fa14
cmpl $0x2, %ebx
setl %cl
cmpl $0x2, %eax
setl %dl
xorl %r15d, %r15d
orb %cl, %dl
jne 0x76643
movl %ebx, 0x24(%rsp)
movq %rbp, 0x40(%rsp)
movq %r14, 0x48(%rsp)
leaq 0x2fcc7(%rip), %rbp     # 0xa616b
leaq 0xf42c0(%rsp), %r15
xorl %r14d, %r14d
movq %r13, 0x50(%rsp)
movl %r14d, %ecx
movq %rcx, 0x28(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x76538
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x366bd(%rip), %r14     # 0xacb94
xorl %r13d, %r13d
leaq 0x10(%rsp), %rbx
cmpq $0x2, %r13
ja 0x76502
movl (%r14), %edx
movq 0x18(%rsp), %rax
movl (%rax), %ecx
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x75ff7
movsd %xmm0, 0x38(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x39529
movq 0x60(%rsp,%r13,8), %rdi
movsd 0x38(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
movq %r15, %rsi
movb $0x1, %al
callq 0x42e0
incq %r13
addq $0x4, %r14
cmpq %r13, %r12
jne 0x764df
jmp 0x7653d
leaq 0x10(%rsp), %rbx
cmpl $0x0, 0x4(%rsp)
jle 0x765b4
xorps %xmm0, %xmm0
cvtsi2sdl 0x24(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x3663a(%rip), %r14     # 0xacb94
xorl %r13d, %r13d
cmpq $0x2, %r13
ja 0x76580
movl (%r14), %edx
movq 0x18(%rsp), %rax
movl (%rax), %ecx
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x75ff7
movsd %xmm0, 0x38(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x39529
movq 0x60(%rsp,%r13,8), %rdi
movsd 0x38(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
movq %r15, %rsi
movb $0x1, %al
callq 0x42e0
incq %r13
addq $0x4, %r14
cmpq %r13, %r12
jne 0x7655d
movq 0x50(%rsp), %r13
movq (%r13), %rdi
movq 0x8(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r13)
movq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x2fd59
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
leal 0x1(%rax), %r14d
cmpl $0x5, %r14d
je 0x76664
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
movq %rbx, %rdx
movl $0x2, %ecx
callq 0x30746
movq 0x8(%rsp), %rdi
callq 0x2fa14
movl %eax, %ebx
movq 0x10(%rsp), %rdi
callq 0x2fa14
movl %ebx, 0x24(%rsp)
cmpl $0x2, %ebx
jl 0x76630
cmpl $0x2, %eax
jge 0x764b4
cmpl $0x4, 0x28(%rsp)
setae %r15b
movq 0x48(%rsp), %r14
movq 0x40(%rsp), %rbp
movq (%r13), %rdi
movq 0x8(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r13)
movq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x2fd59
jmp 0x76677
cmpl $0x4, 0x28(%rsp)
setae %r15b
movq 0x48(%rsp), %r14
movq 0x40(%rsp), %rbp
cmpl $0x0, 0x4(%rsp)
jle 0x76692
xorl %ebx, %ebx
movq 0x60(%rsp,%rbx,8), %rdi
callq 0x42a0
incq %rbx
cmpq %rbx, %r12
jne 0x76680
testb %r15b, %r15b
je 0x76761
movq %r14, %r13
leaq 0x30d5c(%rip), %rdi     # 0xa7401
callq 0x535f7
movl %eax, %ebx
subq $0x8, %rsp
movq %rbp, %r14
movl $0x3, %ebp
movl %eax, %edi
movl $0x9, %esi
movl $0x3, %edx
movl $0xa, %ecx
movl $0xc, %r8d
movl $0xf, %r9d
xorl %eax, %eax
pushq $0x13
pushq $0x7
pushq $0x6
pushq $0xe
pushq $0x10
callq 0x39a80
addq $0x30, %rsp
testl %eax, %eax
movq 0x18(%rsp), %rax
movl 0x1c(%rax), %eax
cmovel %ebp, %ebx
subq $0x8, %rsp
leaq 0x7a1a8(%rsp), %rcx
leaq 0x64(%rsp), %r8
leaq 0x60(%rsp), %r9
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
pushq %rax
callq 0x8c4bc
addq $0x10, %rsp
movq %rax, %rbx
movl (%rax), %eax
cmpl $0x3, %eax
je 0x767bc
cmpl $0x2, %eax
je 0x7679e
cmpl $0x1, %eax
jne 0x767dc
leaq 0x50(%r13), %rsi
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
movq 0x20(%r13), %rdi
leaq 0x3(%rsp), %rdx
je 0x767f7
callq 0x74e0c
jmp 0x767fc
movq %rbp, %rdi
movl 0x4(%rsp), %esi
callq 0x3915f
leaq 0x50(%r14), %rsi
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
movq 0x20(%r14), %rdi
leaq 0x3(%rsp), %rdx
je 0x7678b
callq 0x74e0c
jmp 0x76790
callq 0x753a6
movl %eax, 0x68(%r14)
xorl %ebx, %ebx
cmpl $-0x1, %eax
setne %bl
jmp 0x767fe
movq 0x20(%r13), %rdi
leaq 0x80(%r13), %rsi
leaq 0x88(%r13), %rdx
callq 0x761d7
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x767d8
movq 0x20(%r13), %rdi
leaq 0x80(%r13), %rsi
leaq 0x88(%r13), %rdx
callq 0x7621d
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
movl %eax, 0x68(%r13)
movq %rbx, %rdi
callq 0x39194
movq %r14, %rdi
movl 0x4(%rsp), %esi
callq 0x3915f
movl $0x1, %ebx
jmp 0x767fe
callq 0x753a6
movl %eax, %ebx
movl %ebx, %eax
addq $0x16e3e8, %rsp         # imm = 0x16E3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq