// File: /workspace/github_c_makefile_star2_O2/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 $0x16e3f8, %rsp         # imm = 0x16E3F8
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
leaq 0x2b256(%rip), %rdi     # 0x98c18
callq 0x4cd35
andq $0x0, 0x10(%rsp)
movl %eax, %ebp
andq $0x0, 0x20(%rsp)
leaq 0x7a1b0(%rsp), %rbx
xorl %r13d, %r13d
movl $0x7a120, %edx          # imm = 0x7A120
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4050
movb $0x2d, (%rbx)
movslq %ebp, %rdi
shlq $0x3, %rdi
leaq 0x321a9(%rip), %rsi     # 0x9fba9
pushq $0x5
popq %rdx
callq 0x34f59
movq %rax, %r12
leaq 0x2c97a(%rip), %r15     # 0x9a38c
movq %r15, %rdi
callq 0x4ce84
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movupd 0x2ce58(%rip), %xmm0    # 0x9a895
movupd %xmm0, 0x90(%rsp,%rax)
movq (%r12), %rdi
movq %rbx, %rsi
callq 0x3604c
movq %rax, (%r12)
movq %r15, %rdi
movq %r12, %r15
callq 0x4ce84
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movabsq $0x2e7261656e696c2f, %rcx # imm = 0x2E7261656E696C2F
movq %rcx, 0x90(%rsp,%rax)
movl $0x747874, 0x98(%rsp,%rax) # imm = 0x747874
movq 0x8(%r12), %rdi
movq %rbx, %rsi
callq 0x3604c
movq %rax, 0x8(%r12)
cmpl $0x3, %ebp
jne 0x6db03
leaq 0x2c8dd(%rip), %rdi     # 0x9a38c
callq 0x4ce84
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movabsq $0x746172646175712f, %rcx # imm = 0x746172646175712F
movq %rcx, 0x90(%rsp,%rax)
movabsq $0x7478742e636974, %rcx # imm = 0x7478742E636974
movq %rcx, 0x97(%rsp,%rax)
movq 0x10(%r15), %rdi
movq %rbx, %rsi
callq 0x3604c
movq %rax, 0x10(%r15)
testl %ebp, %ebp
movl %ebp, %r12d
cmovlel %r13d, %r12d
leaq 0x2dd7e(%rip), %rbx     # 0x9b891
cmpq %r13, %r12
je 0x6db38
movq (%r15,%r13,8), %rdi
movq %rbx, %rsi
callq 0x40f0
movq %rax, 0x70(%rsp,%r13,8)
incq %r13
testq %rax, %rax
jne 0x6db13
xorl %eax, %eax
jmp 0x6de73
movq %r15, 0x58(%rsp)
movl %ebp, 0x2c(%rsp)
movq %r14, 0x30(%rsp)
addq $0x20, %r14
xorl %r13d, %r13d
leaq 0x20(%rsp), %rbx
leaq 0x2b5b2(%rip), %rbp     # 0x9910b
leaq 0xf42d0(%rsp), %r15
movq %r14, 0x50(%rsp)
cmpl $0x5, %r13d
je 0x6dcce
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movq %rbx, %rdx
pushq $0x2
popq %rcx
callq 0x2cfe5
movq 0x10(%rsp), %rdi
callq 0x2c3cc
movl %eax, %r14d
movq 0x20(%rsp), %rdi
callq 0x2c3cc
movl %r14d, 0x4c(%rsp)
cmpl $0x2, %r14d
jl 0x6dcd3
cmpl $0x2, %eax
jl 0x6dcd3
movq %r13, 0x60(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x31fc4(%rip), %r14     # 0x9fb8c
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x6dc24
cmpq $0x2, %r13
ja 0x6dbf3
movl (%r14), %edx
movq 0x18(%rsp), %rax
movl (%rax), %ecx
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x6d701
movsd %xmm0, 0x40(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x35305
movq 0x70(%rsp,%r13,8), %rdi
movsd 0x40(%rsp), %xmm0
divsd 0x38(%rsp), %xmm0
movq %r15, %rsi
movb $0x1, %al
callq 0x42e0
incq %r13
addq $0x4, %r14
jmp 0x6dbcb
cvtsi2sdl 0x4c(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x31f55(%rip), %r14     # 0x9fb8c
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x6dc93
cmpq $0x2, %r13
ja 0x6dc62
movl (%r14), %edx
movq 0x18(%rsp), %rax
movl (%rax), %ecx
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
callq 0x6d701
movsd %xmm0, 0x40(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x35305
movq 0x70(%rsp,%r13,8), %rdi
movsd 0x40(%rsp), %xmm0
divsd 0x38(%rsp), %xmm0
movq %r15, %rsi
movb $0x1, %al
callq 0x42e0
incq %r13
addq $0x4, %r14
jmp 0x6dc3a
movq 0x50(%rsp), %r14
movq (%r14), %rdi
movq 0x10(%rsp), %rsi
callq 0x2d2da
movq %rax, (%r14)
movq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x2c6e4
andq $0x0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq 0x60(%rsp), %r13
incl %r13d
jmp 0x6db66
movb $0x1, %bpl
jmp 0x6dcfd
cmpl $0x5, %r13d
setae %bpl
movq 0x50(%rsp), %rbx
movq (%rbx), %rdi
movq 0x10(%rsp), %rsi
callq 0x2d2da
movq %rax, (%rbx)
movq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c6e4
xorl %ebx, %ebx
movq 0x30(%rsp), %r14
movq 0x58(%rsp), %r15
cmpq %rbx, %r12
je 0x6dd1d
movq 0x70(%rsp,%rbx,8), %rdi
callq 0x42a0
incq %rbx
jmp 0x6dd09
testb %bpl, %bpl
je 0x6dde1
leaq 0x2c6a4(%rip), %rdi     # 0x9a3d1
callq 0x4cf11
movl %eax, %ebx
subq $0x8, %rsp
pushq $0x9
popq %rsi
pushq $0x3
popq %rbp
pushq $0xa
popq %rcx
pushq $0xc
popq %r8
pushq $0xf
popq %r9
movl %eax, %edi
movl %ebp, %edx
movl $0x0, %eax
pushq $0x13
pushq $0x7
pushq $0x6
pushq $0xe
pushq $0x10
callq 0x35812
addq $0x30, %rsp
testl %eax, %eax
movq 0x18(%rsp), %rax
movl 0x1c(%rax), %eax
cmovel %ebp, %ebx
subq $0x8, %rsp
leaq 0x7a1b8(%rsp), %rcx
leaq 0x74(%rsp), %r8
leaq 0x70(%rsp), %r9
movq %r15, %rdi
movl 0x34(%rsp), %ebp
movl %ebp, %esi
movl %ebx, %edx
pushq %rax
callq 0x81133
popq %rcx
popq %rdx
movq %rax, %rbx
movl (%rax), %eax
cmpl $0x3, %eax
je 0x6de3b
cmpl $0x2, %eax
movq 0x30(%rsp), %r14
je 0x6de20
cmpl $0x1, %eax
jne 0x6de5e
leaq 0x50(%r14), %rsi
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
movq 0x20(%r14), %rdi
leaq 0xf(%rsp), %rdx
je 0x6de85
callq 0x6c660
jmp 0x6de73
movq %r15, %rdi
movl 0x2c(%rsp), %esi
callq 0x34fc3
leaq 0x50(%r14), %rsi
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
movq 0x20(%r14), %rdi
leaq 0xf(%rsp), %rdx
je 0x6de0b
callq 0x6c660
jmp 0x6de10
callq 0x6cbb5
movl %eax, %ecx
movl %eax, 0x68(%r14)
xorl %eax, %eax
cmpl $-0x1, %ecx
setne %al
jmp 0x6de73
movq 0x20(%r14), %rdi
leaq 0x80(%r14), %rsi
leaq 0x88(%r14), %rdx
callq 0x6d8df
pushq $-0x2
jmp 0x6de59
movq 0x30(%rsp), %r14
movq 0x20(%r14), %rdi
leaq 0x80(%r14), %rsi
leaq 0x88(%r14), %rdx
callq 0x6d91e
pushq $-0x3
popq %rax
movl %eax, 0x68(%r14)
movq %rbx, %rdi
callq 0x34ff7
movq %r15, %rdi
movl %ebp, %esi
callq 0x34fc3
pushq $0x1
popq %rax
addq $0x16e3f8, %rsp         # imm = 0x16E3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6cbb5
jmp 0x6de73