// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/regressionrule.c
// Function: best_regression_condition_crossvalidation
void best_regression_condition_crossvalidation(Regressionconditionptr condition, Instanceptr* data, LEAF_TYPE leaftype, int correctiontype)
{
	/*!Last Changed 26.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] = "-", pst[READLINELENGTH], buffer[READLINELENGTH];
	double error;
 files = safemalloc(hybridspace * sizeof(char *), "best_regression_condition_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;
  }
	for (i = 0; i < 5; i++)
	 {
		 divide_instancelist(data, &train, &test, 2);
			trainsize = data_size(train);
			testsize = data_size(test);
			if (trainsize <= 1 || testsize <= 1)
			 {
  			*data = restore_instancelist(*data, train);
	  		merge_instancelist(data, test);	
					bestmodel = 1;
				 break;
			 }
   for (k = 0; k < hybridspace; k++)
    {
     switch (k)
      {
       case MODEL_UNI:error = regression_tree_model_selection_error(&train, &test, UNIVARIATE, leaftype);
                      break;
       case MODEL_LIN:error = regression_tree_model_selection_error(&train, &test, LINEAR, leaftype);
                      break;
       case MODEL_QUA:error = regression_tree_model_selection_error(&train, &test, QUADRATIC, 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, leaftype);
                      break;
       case MODEL_LIN:error = regression_tree_model_selection_error(&test, &train, LINEAR, leaftype);
                      break;
       case MODEL_QUA:error = regression_tree_model_selection_error(&test, &train, QUADRATIC, leaftype);
                      break;
      }
     set_precision(pst, NULL, "\n");
     fprintf(errorfiles[k], pst, error / (trainsize + 0.0));
    }
			*data = restore_instancelist(*data, train);
			merge_instancelist(data, test);	
			train = NULL;
			test = NULL;
	 }
 for (i = 0; i < hybridspace; i++)
   fclose(errorfiles[i]);
 if (bestmodel)
	 {
		 free_2d((void**)files, hybridspace);
   best_single_regression_condition(condition, data, UNIVARIATE, leaftype);
			return;
	 }
 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, correctiontype);
 else
   models = multiple_comparison(files, hybridspace, FTEST, st, &dummy, &dummy2, correctiontype);
	switch (models[0])
	 {
	  case 1:best_single_regression_condition(condition, data, UNIVARIATE, leaftype);
										break;
			case 2:best_single_regression_condition(condition, data, LINEAR, leaftype);
				      break;
			case 3:best_single_regression_condition(condition, data, QUADRATIC, leaftype);
				      break;
	 }
	safe_free(models);
	free_2d((void**)files, hybridspace);
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x16e3e8, %rsp         # imm = 0x16E3E8
movq %rcx, 0x40(%rsp)
movl %edx, 0x4(%rsp)
movq %rsi, %rbx
movq %rdi, 0x48(%rsp)
leaq 0x2d267(%rip), %rdi     # 0x98c18
callq 0x4cd35
andq $0x0, 0x8(%rsp)
movl %eax, %ebp
andq $0x0, 0x18(%rsp)
leaq 0x7a1a0(%rsp), %r14
xorl %r12d, %r12d
movl $0x7a120, %edx          # imm = 0x7A120
movq %r14, %rdi
xorl %esi, %esi
callq 0x4050
movb $0x2d, (%r14)
movslq %ebp, %rdi
shlq $0x3, %rdi
leaq 0x3411a(%rip), %rsi     # 0x9fb0a
pushq $0x5
popq %rdx
callq 0x34f59
movq %rax, %r13
leaq 0x2e98a(%rip), %r15     # 0x9a38c
movq %r15, %rdi
callq 0x4ce84
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x4270
movq %r14, %rdi
callq 0x4190
movupd 0x2ee68(%rip), %xmm0    # 0x9a895
movupd %xmm0, 0x80(%rsp,%rax)
movq (%r13), %rdi
movq %r14, %rsi
callq 0x3604c
movq %rax, (%r13)
movq %r15, %rdi
callq 0x4ce84
movq %r14, %rdi
movq %rax, %rsi
callq 0x4270
movq %r14, %rdi
callq 0x4190
movabsq $0x2e7261656e696c2f, %rcx # imm = 0x2E7261656E696C2F
movq %rcx, 0x80(%rsp,%rax)
movl $0x747874, 0x88(%rsp,%rax) # imm = 0x747874
movq 0x8(%r13), %rdi
movq %r14, %rsi
callq 0x3604c
movq %r13, 0x10(%rsp)
movq %rax, 0x8(%r13)
cmpl $0x3, %ebp
jne 0x6baf8
leaq 0x2e8ed(%rip), %rdi     # 0x9a38c
callq 0x4ce84
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x4270
movq %r14, %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(%rsp), %r15
movq 0x10(%r15), %rdi
movq %r14, %rsi
callq 0x3604c
movq %rax, 0x10(%r15)
testl %ebp, %ebp
movl %ebp, %r14d
cmovlel %r12d, %r14d
leaq 0x2fd89(%rip), %r15     # 0x9b891
cmpq %r12, %r14
je 0x6bb30
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rdi
movq %r15, %rsi
callq 0x40f0
movq %rax, 0x60(%rsp,%r12,8)
incq %r12
testq %rax, %rax
jne 0x6bb08
jmp 0x6bdd7
movl %ebp, 0x38(%rsp)
xorl %r12d, %r12d
leaq 0x18(%rsp), %r15
leaq 0x2d5c8(%rip), %rbp     # 0x9910b
leaq 0xf42c0(%rsp), %r13
movq %rbx, 0x20(%rsp)
cmpl $0x5, %r12d
je 0x6bcad
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movq %r15, %rdx
pushq $0x2
popq %rcx
callq 0x2cfe5
movq 0x8(%rsp), %rdi
callq 0x2c3cc
movl %eax, %ebx
movq 0x18(%rsp), %rdi
callq 0x2c3cc
movl %ebx, 0x3c(%rsp)
cmpl $0x2, %ebx
jl 0x6bcb2
cmpl $0x2, %eax
jl 0x6bcb2
movq %r12, 0x50(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x33ec1(%rip), %rbx     # 0x9fa70
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x6bc07
cmpq $0x2, %r12
ja 0x6bbd6
movl (%rbx), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movl 0x4(%rsp), %ecx
callq 0x6d701
movsd %xmm0, 0x30(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x35305
movq 0x60(%rsp,%r12,8), %rdi
movsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movq %r13, %rsi
movb $0x1, %al
callq 0x42e0
incq %r12
addq $0x4, %rbx
jmp 0x6bbb2
cvtsi2sdl 0x3c(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x33e56(%rip), %rbx     # 0x9fa70
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x6bc72
cmpq $0x2, %r12
ja 0x6bc41
movl (%rbx), %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
movl 0x4(%rsp), %ecx
callq 0x6d701
movsd %xmm0, 0x30(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x35305
movq 0x60(%rsp,%r12,8), %rdi
movsd 0x30(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movq %r13, %rsi
movb $0x1, %al
callq 0x42e0
incq %r12
addq $0x4, %rbx
jmp 0x6bc1d
movq 0x20(%rsp), %rbx
movq (%rbx), %rdi
movq 0x8(%rsp), %rsi
callq 0x2d2da
movq %rax, (%rbx)
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c6e4
andq $0x0, 0x8(%rsp)
andq $0x0, 0x18(%rsp)
movq 0x50(%rsp), %r12
incl %r12d
jmp 0x6bb50
movb $0x1, %bpl
jmp 0x6bcdc
cmpl $0x5, %r12d
setae %bpl
movq 0x20(%rsp), %rbx
movq (%rbx), %rdi
movq 0x8(%rsp), %rsi
callq 0x2d2da
movq %rax, (%rbx)
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c6e4
xorl %ebx, %ebx
movl 0x38(%rsp), %r15d
movq 0x48(%rsp), %r12
cmpq %rbx, %r14
je 0x6bcfc
movq 0x60(%rsp,%rbx,8), %rdi
callq 0x42a0
incq %rbx
jmp 0x6bce8
testb %bpl, %bpl
je 0x6bdb7
leaq 0x2e6c5(%rip), %rdi     # 0x9a3d1
callq 0x4cf11
movl %eax, %ebp
subq $0x8, %rsp
pushq $0x9
popq %rsi
pushq $0x3
popq %r14
pushq $0xa
popq %rcx
pushq $0xc
popq %r8
pushq $0xf
popq %r9
movl %eax, %edi
movl %r14d, %edx
movl $0x0, %eax
pushq $0x13
pushq $0x7
pushq $0x6
pushq $0xe
pushq $0x10
callq 0x35812
addq $0x30, %rsp
testl %eax, %eax
cmovel %r14d, %ebp
subq $0x8, %rsp
leaq 0x7a1a8(%rsp), %rcx
leaq 0x64(%rsp), %r8
leaq 0x60(%rsp), %r9
movq 0x18(%rsp), %rdi
movl %r15d, %esi
movl %ebp, %edx
pushq 0x48(%rsp)
callq 0x81133
popq %rcx
popq %rdx
movq %rax, %r14
movl (%rax), %eax
decl %eax
cmpl $0x2, %eax
movq 0x20(%rsp), %rsi
ja 0x6bda0
movl %eax, %eax
leaq 0x33cdf(%rip), %rcx     # 0x9fa70
movl (%rcx,%rax,4), %edx
movq %r12, %rdi
movl 0x4(%rsp), %ecx
callq 0x6b8fd
movq %r14, %rdi
callq 0x34ff7
movq 0x10(%rsp), %rdi
movl %r15d, %esi
callq 0x34fc3
jmp 0x6bdd7
movq 0x10(%rsp), %rdi
movl %r15d, %esi
callq 0x34fc3
movq %r12, %rdi
movq 0x20(%rsp), %rsi
xorl %edx, %edx
movl 0x4(%rsp), %ecx
callq 0x6b8fd
addq $0x16e3e8, %rsp         # imm = 0x16E3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq