// File: /workspace/github_c_makefile_star2_O3/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, 0x20(%rsp)
movq %rdi, %r13
leaq 0x31003(%rip), %rdi     # 0xa8c98
callq 0x54920
movl %eax, %ebp
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%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 0x37fb9(%rip), %rsi     # 0xafc89
movl $0x5, %edx
callq 0x3a0e1
movq %rax, %r12
leaq 0x32778(%rip), %r15     # 0xaa45c
movq %r15, %rdi
callq 0x54a6f
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movupd 0x32c56(%rip), %xmm0    # 0xaa965
movupd %xmm0, 0x80(%rsp,%rax)
movq (%r12), %rdi
movq %rbx, %rsi
callq 0x3b494
movq %rax, (%r12)
movq %r15, %rdi
callq 0x54a6f
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(%r12), %rdi
movq %rbx, %rsi
callq 0x3b494
movq %rax, 0x8(%r12)
movl %ebp, %r14d
cmpl $0x3, %ebp
movq %r12, 0x48(%rsp)
jne 0x77dde
leaq 0x326d6(%rip), %rdi     # 0xaa45c
callq 0x54a6f
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(%r12), %rdi
movq %rbx, %rsi
callq 0x3b494
movq %rax, 0x10(%r12)
jmp 0x77de2
testl %ebp, %ebp
jle 0x77e21
xorl %ebx, %ebx
leaq 0x33b82(%rip), %r15     # 0xab96d
xorl %r12d, %r12d
movq 0x48(%rsp), %rax
movq (%rax,%r12,8), %rdi
movq %r15, %rsi
callq 0x40f0
movq %rax, 0x60(%rsp,%r12,8)
testq %rax, %rax
je 0x781d6
incq %r12
cmpq %r12, %r14
jne 0x77dee
movl %ebp, 0x1c(%rsp)
movb $0x1, %al
movl %eax, 0x4(%rsp)
jmp 0x77e2d
movl %ebp, 0x1c(%rsp)
movl $0x0, 0x4(%rsp)
movq %r13, %rbp
addq $0x20, %r13
leaq 0x10(%rsp), %rbx
leaq 0x8(%rsp), %r15
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl $0x2, %ecx
callq 0x31019
movq (%rbx), %rdi
callq 0x30309
movl %eax, %r12d
movq (%r15), %rdi
callq 0x30309
cmpl $0x2, %r12d
setl %cl
cmpl $0x2, %eax
setl %dl
xorl %r15d, %r15d
orb %cl, %dl
jne 0x7801f
movq %r13, 0x40(%rsp)
movq %rbp, 0x38(%rsp)
leaq 0x3130e(%rip), %rbp     # 0xa919b
leaq 0xf42c0(%rsp), %r15
xorl %ebx, %ebx
movl %ebx, %ecx
movq %rcx, 0x50(%rsp)
cmpb $0x0, 0x4(%rsp)
je 0x77f92
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x37daf(%rip), %r13     # 0xafc6c
xorl %ebx, %ebx
cmpq $0x2, %rbx
ja 0x77ee5
movl (%r13), %edx
movq 0x20(%rsp), %rax
movl (%rax), %ecx
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x779c4
movsd %xmm0, 0x30(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3a535
movq 0x60(%rsp,%rbx,8), %rdi
movsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movq %r15, %rsi
movb $0x1, %al
callq 0x42e0
incq %rbx
addq $0x4, %r13
cmpq %rbx, %r14
jne 0x77ebf
cmpb $0x0, 0x4(%rsp)
je 0x77f92
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x37d37(%rip), %r12     # 0xafc6c
xorl %r13d, %r13d
cmpq $0x2, %r13
ja 0x77f5e
movl (%r12), %edx
movq 0x20(%rsp), %rax
movl (%rax), %ecx
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x779c4
movsd %xmm0, 0x30(%rsp)
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3a535
movq 0x60(%rsp,%r13,8), %rdi
movsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movq %r15, %rsi
movb $0x1, %al
callq 0x42e0
incq %r13
addq $0x4, %r12
cmpq %r13, %r14
jne 0x77f38
movq 0x40(%rsp), %r12
movq (%r12), %rdi
movq 0x10(%rsp), %rsi
callq 0x31345
movq %rax, (%r12)
movq 0x8(%rsp), %rsi
movq %r12, %rdi
callq 0x30648
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %r13
leal 0x1(%r13), %ebx
cmpl $0x5, %ebx
je 0x78043
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movl $0x2, %ecx
callq 0x31019
movq 0x10(%rsp), %rdi
callq 0x30309
movl %eax, %r12d
movq 0x8(%rsp), %rdi
callq 0x30309
cmpl $0x2, %r12d
jl 0x7800d
cmpl $0x2, %eax
jge 0x77e97
cmpl $0x4, %r13d
setae %r15b
movq 0x38(%rsp), %rbp
movq 0x40(%rsp), %r13
movq (%r13), %rdi
movq 0x10(%rsp), %rsi
callq 0x31345
movq %rax, (%r13)
movq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x30648
movq %rbp, %r13
jmp 0x7804b
movb $0x1, %r15b
movq 0x38(%rsp), %r13
movl 0x1c(%rsp), %ebp
cmpb $0x0, 0x4(%rsp)
movq 0x48(%rsp), %r12
je 0x7806f
xorl %ebx, %ebx
movq 0x60(%rsp,%rbx,8), %rdi
callq 0x42a0
incq %rbx
cmpq %rbx, %r14
jne 0x7805d
testb %r15b, %r15b
je 0x7813d
leaq 0x32422(%rip), %rdi     # 0xaa4a1
callq 0x54afc
movl %eax, %ebx
subq $0x8, %rsp
movl %ebp, %r14d
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 0x3aa8c
addq $0x30, %rsp
testl %eax, %eax
movq 0x20(%rsp), %rax
movl 0x1c(%rax), %eax
cmovel %ebp, %ebx
movl %r14d, %ebp
subq $0x8, %rsp
leaq 0x7a1a8(%rsp), %rcx
leaq 0x64(%rsp), %r8
leaq 0x60(%rsp), %r9
movq %r12, %rdi
movl %r14d, %esi
movl %ebx, %edx
pushq %rax
callq 0x8ec4a
addq $0x10, %rsp
movq %rax, %rbx
movl (%rax), %eax
cmpl $0x3, %eax
je 0x78196
cmpl $0x2, %eax
je 0x78178
cmpl $0x1, %eax
jne 0x781b6
leaq 0x50(%r13), %rsi
movq 0x20(%rsp), %rax
cmpl $0x0, (%rax)
movq 0x20(%r13), %rdi
leaq 0x3(%rsp), %rdx
je 0x781cf
callq 0x766e3
jmp 0x781d4
movq %r12, %rdi
movl %ebp, %esi
callq 0x3a14b
leaq 0x50(%r13), %rsi
movq 0x20(%rsp), %rax
cmpl $0x0, (%rax)
movq 0x20(%r13), %rdi
leaq 0x3(%rsp), %rdx
je 0x78165
callq 0x766e3
jmp 0x7816a
callq 0x76c85
movl %eax, 0x68(%r13)
xorl %ebx, %ebx
cmpl $-0x1, %eax
setne %bl
jmp 0x781d6
movq 0x20(%r13), %rdi
leaq 0x80(%r13), %rsi
leaq 0x88(%r13), %rdx
callq 0x77ba4
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jmp 0x781b2
movq 0x20(%r13), %rdi
leaq 0x80(%r13), %rsi
leaq 0x88(%r13), %rdx
callq 0x77bea
movl $0xfffffffd, %eax       # imm = 0xFFFFFFFD
movl %eax, 0x68(%r13)
movq %rbx, %rdi
callq 0x3a180
movq %r12, %rdi
movl %ebp, %esi
callq 0x3a14b
movl $0x1, %ebx
jmp 0x781d6
callq 0x76c85
movl %eax, %ebx
movl %ebx, %eax
addq $0x16e3e8, %rsp         # imm = 0x16E3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq