// File: /workspace/github_c_makefile_star2_O1/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, 0x50(%rsp)
movl %edx, 0x4(%rsp)
movq %rsi, %r12
movq %rdi, 0x48(%rsp)
leaq 0x31c13(%rip), %rdi     # 0xa5c20
callq 0x5341c
movl %eax, %ebx
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x10(%rsp)
leaq 0x7a1a0(%rsp), %r14
movl $0x7a120, %edx          # imm = 0x7A120
movq %r14, %rdi
xorl %esi, %esi
callq 0x4050
movb $0x2d, (%r14)
movslq %ebx, %rdi
shlq $0x3, %rdi
leaq 0x38ac9(%rip), %rsi     # 0xacb12
movl $0x5, %edx
callq 0x390f5
movq %rax, %rbp
leaq 0x3335f(%rip), %r15     # 0xa73bc
movq %r15, %rdi
callq 0x5356b
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x4270
movq %r14, %rdi
callq 0x4190
movupd 0x3383d(%rip), %xmm0    # 0xa78c5
movupd %xmm0, 0x80(%rsp,%rax)
movq (%rbp), %rdi
movq %r14, %rsi
callq 0x3a404
movq %rax, (%rbp)
movq %r15, %rdi
callq 0x5356b
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(%rbp), %rdi
movq %r14, %rsi
callq 0x3a404
movq %rax, 0x8(%rbp)
movl %ebx, (%rsp)
cmpl $0x3, %ebx
jne 0x7414c
leaq 0x332c4(%rip), %rdi     # 0xa73bc
callq 0x5356b
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(%rbp), %rdi
movq %r14, %rsi
callq 0x3a404
movq %rax, 0x10(%rbp)
movl (%rsp), %eax
movl %eax, %r14d
testl %eax, %eax
jle 0x74182
xorl %ebx, %ebx
leaq 0x34742(%rip), %r15     # 0xa88a1
movq (%rbp,%rbx,8), %rdi
movq %r15, %rsi
callq 0x40f0
movq %rax, 0x60(%rsp,%rbx,8)
testq %rax, %rax
je 0x744a9
incq %rbx
cmpq %rbx, %r14
jne 0x7415f
movq %r12, 0x20(%rsp)
leaq 0x8(%rsp), %r12
leaq 0x10(%rsp), %r15
movq 0x20(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movl $0x2, %ecx
callq 0x30746
movq (%r12), %rdi
movq 0x20(%rsp), %r12
callq 0x2fa14
movl %eax, %ebx
movq (%r15), %rdi
callq 0x2fa14
cmpl $0x2, %ebx
setl %cl
cmpl $0x2, %eax
setl %dl
xorl %r13d, %r13d
orb %cl, %dl
jne 0x74375
movl %ebx, 0x1c(%rsp)
movq %rbp, 0x40(%rsp)
leaq 0x31f86(%rip), %rbp     # 0xa616b
leaq 0xf42c0(%rsp), %r13
xorl %ebx, %ebx
movl %ebx, %ecx
movq %rcx, 0x28(%rsp)
cmpl $0x0, (%rsp)
jle 0x7426d
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x38868(%rip), %rbx     # 0xaca78
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
cmpq $0x2, %r12
ja 0x74237
movl (%rbx), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movl 0x4(%rsp), %ecx
callq 0x75ff7
movsd %xmm0, 0x38(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x39529
movq 0x60(%rsp,%r12,8), %rdi
movsd 0x38(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
movq %r13, %rsi
movb $0x1, %al
callq 0x42e0
incq %r12
addq $0x4, %rbx
cmpq %r12, %r14
jne 0x74218
jmp 0x74272
leaq 0x10(%rsp), %r15
cmpl $0x0, (%rsp)
jle 0x742e4
xorps %xmm0, %xmm0
cvtsi2sdl 0x1c(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x387ea(%rip), %rbx     # 0xaca78
xorl %r12d, %r12d
cmpq $0x2, %r12
ja 0x742b0
movl (%rbx), %edx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
movl 0x4(%rsp), %ecx
callq 0x75ff7
movsd %xmm0, 0x38(%rsp)
movq %r13, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x39529
movq 0x60(%rsp,%r12,8), %rdi
movsd 0x38(%rsp), %xmm0
divsd 0x30(%rsp), %xmm0
movq %r13, %rsi
movb $0x1, %al
callq 0x42e0
incq %r12
addq $0x4, %rbx
cmpq %r12, %r14
jne 0x74291
movq 0x20(%rsp), %r12
movq (%r12), %rdi
movq 0x8(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r12)
movq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x2fd59
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
leal 0x1(%rax), %ebx
cmpl $0x5, %ebx
je 0x7439b
movq %r15, %rdx
movq %r12, %r15
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
movl $0x2, %ecx
callq 0x30746
movq 0x8(%rsp), %rdi
callq 0x2fa14
movl %eax, %r12d
movq 0x10(%rsp), %rdi
callq 0x2fa14
movl %r12d, 0x1c(%rsp)
cmpl $0x2, %r12d
jl 0x74364
cmpl $0x2, %eax
jge 0x741ef
cmpl $0x4, 0x28(%rsp)
setae %r13b
movq 0x40(%rsp), %rbp
movq %r15, %r12
movq (%r12), %rdi
movq 0x8(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r12)
movq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x2fd59
movq 0x48(%rsp), %r15
jmp 0x743ae
cmpl $0x4, 0x28(%rsp)
setae %r13b
movq 0x48(%rsp), %r15
movq 0x40(%rsp), %rbp
cmpl $0x0, (%rsp)
jle 0x743c8
xorl %ebx, %ebx
movq 0x60(%rsp,%rbx,8), %rdi
callq 0x42a0
incq %rbx
cmpq %rbx, %r14
jne 0x743b6
testb %r13b, %r13b
je 0x7448d
leaq 0x33029(%rip), %rdi     # 0xa7401
callq 0x535f7
movq %rbp, %r14
movl %eax, %ebp
subq $0x8, %rsp
movl $0x3, %ebx
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
cmovel %ebx, %ebp
subq $0x8, %rsp
leaq 0x7a1a8(%rsp), %rcx
leaq 0x64(%rsp), %r8
leaq 0x60(%rsp), %r9
movq %r14, %rbx
movq %r14, %rdi
movl 0x8(%rsp), %esi
movl %ebp, %edx
pushq 0x58(%rsp)
callq 0x8c4bc
addq $0x10, %rsp
movq %rax, %r14
movl (%rax), %eax
decl %eax
cmpl $0x2, %eax
ja 0x74478
movl %eax, %eax
leaq 0x38612(%rip), %rcx     # 0xaca78
movl (%rcx,%rax,4), %edx
movq %r15, %rdi
movq %r12, %rsi
movl 0x4(%rsp), %ecx
callq 0x73f3c
movq %r14, %rdi
callq 0x39194
movq %rbx, %rdi
movl (%rsp), %esi
callq 0x3915f
jmp 0x744a9
movq %rbp, %rdi
movl (%rsp), %esi
callq 0x3915f
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
movl 0x4(%rsp), %ecx
callq 0x73f3c
addq $0x16e3e8, %rsp         # imm = 0x16E3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq