// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/omnivariatetree.c
// Function: find_best_hybrid_ldt_split_crossvalidation
int find_best_hybrid_ldt_split_crossvalidation(Decisionnodeptr node, Multildt_parameterptr param)
{
 /*!Last Changed 22.11.2004*/
 int hybridspace = get_parameter_i("hybridspace");
 FILE* errorfiles[3];
 int i, j, k, datasize, trainsize, testsize, error, bestmodel = 0, *models, dummy, dummy2, testtype;
 Instanceptr train = NULL, test = NULL, traindata, testdata;
 char **files, st[READLINELENGTH] = "-", pst[READLINELENGTH], buffer[READLINELENGTH];
 files = safemalloc(hybridspace * sizeof(char *), "find_best_hybrid_ldt_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 (j = 0; j < 2; j++)
    {
     if (j == 0)
      {
       traindata = train;
       testdata = test;
       datasize = testsize;
      }
     else
      {
       traindata = test;
       testdata = train;
       datasize = trainsize;
      }
     for (k = 0; k < hybridspace; k++)
      {
       switch (k)
        {
         case MODEL_UNI:error = find_error_and_free_matrix(&traindata, testdata, UNIVARIATE, param->variance_explained);
                        break;
         case MODEL_LIN:error = find_error_and_free_matrix(&traindata, testdata, LINEAR, param->variance_explained);
                        break;
         case MODEL_QUA:error = find_error_and_free_matrix(&traindata, testdata, QUADRATIC, param->variance_explained);
                        break;
        }
       set_precision(pst, NULL, "\n");
       fprintf(errorfiles[k], pst, error / (datasize + 0.0));
      }
    }
   node->instances = restore_instancelist(node->instances, testdata);
   merge_instancelist(&(node->instances), traindata); 
   train = NULL;
   test = NULL;
  }
 for (i = 0; i < hybridspace; i++)
   fclose(errorfiles[i]);
 if (bestmodel)
  {
   free_2d((void**)files, hybridspace);
   node->featureno = find_best_ldt_feature_for_realized(node->instances, &(node->split));
   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:node->featureno = find_best_ldt_feature_for_realized(node->instances, &(node->split));
          break;
   case 2:node->lineard = find_best_multi_ldt_split(&(node->instances), &(node->w), &(node->w0), MULTIVARIATE_LINEAR, param->variance_explained);
          if (!node->lineard)
           {
            matrix_free(node->w);
            node->featureno = LEAF_NODE;
           }
          else
            node->featureno = LINEAR;
          break;
   case 3:node->quadraticd = find_best_multi_ldt_split(&(node->instances), &(node->w), &(node->w0), MULTIVARIATE_QUADRATIC, param->variance_explained);
          if (!node->quadraticd)
           {
            matrix_free(node->w);
            node->featureno = LEAF_NODE;
           }
          else
            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, 0x28(%rsp)
movq %rdi, %r13
leaq 0x4cc79(%rip), %rdi     # 0x98c18
callq 0x4cd35
andq $0x0, 0x20(%rsp)
movl %eax, %ebp
andq $0x0, 0x18(%rsp)
leaq 0x7a1b0(%rsp), %rbx
xorl %r12d, %r12d
movl $0x7a120, %edx          # imm = 0x7A120
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4050
movb $0x2d, (%rbx)
movslq %ebp, %rdi
shlq $0x3, %rdi
leaq 0x4e88d(%rip), %rsi     # 0x9a86a
pushq $0x5
popq %rdx
callq 0x34f59
movq %rax, %r15
leaq 0x4e39d(%rip), %r14     # 0x9a38c
movq %r14, %rdi
callq 0x4ce84
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movupd 0x4e87b(%rip), %xmm0    # 0x9a895
movupd %xmm0, 0x90(%rsp,%rax)
movq (%r15), %rdi
movq %rbx, %rsi
callq 0x3604c
movq %rax, (%r15)
movq %r14, %rdi
movq %r15, %r14
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(%r15), %rdi
movq %rbx, %rsi
callq 0x3604c
movq %rax, 0x8(%r15)
cmpl $0x3, %ebp
jne 0x4c0dc
leaq 0x4e304(%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(%r14), %rdi
movq %rbx, %rsi
callq 0x3604c
movq %rax, 0x10(%r14)
testl %ebp, %ebp
movl %ebp, %r15d
cmovlel %r12d, %r15d
leaq 0x4f7a5(%rip), %rbx     # 0x9b891
cmpq %r12, %r15
je 0x4c111
movq (%r14,%r12,8), %rdi
movq %rbx, %rsi
callq 0x40f0
movq %rax, 0x70(%rsp,%r12,8)
incq %r12
testq %rax, %rax
jne 0x4c0ec
xorl %eax, %eax
jmp 0x4c425
movq %r14, 0x30(%rsp)
movl %ebp, 0xc(%rsp)
movq %r13, 0x38(%rsp)
leaq 0x20(%r13), %rax
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
leaq 0xf42d0(%rsp), %rbp
cmpl $0x5, %ebx
je 0x4c273
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x18(%rsp), %rdx
pushq $0x2
popq %rcx
callq 0x2cfe5
movq 0x20(%rsp), %rdi
callq 0x2c3cc
movl %eax, %r13d
movq 0x18(%rsp), %rdi
callq 0x2c3cc
movl %r13d, 0x48(%rsp)
cmpl $0x2, %r13d
jl 0x4c278
cmpl $0x2, %eax
jl 0x4c278
movq %rbx, 0x58(%rsp)
xorl %ecx, %ecx
movl %eax, 0x44(%rsp)
cmpl $0x2, %ecx
je 0x4c23b
movl %ecx, 0x4c(%rsp)
testl %ecx, %ecx
movl 0x48(%rsp), %esi
cmovel %eax, %esi
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %rcx
movq %rcx, %rdx
cmoveq %r14, %rdx
movq %rdx, 0x50(%rsp)
cmoveq %rcx, %r14
cvtsi2sd %esi, %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x4e733(%rip), %r13     # 0x9a900
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x4c22c
cmpq $0x2, %rbx
ja 0x4c1f8
movl (%r13), %edx
movq 0x28(%rsp), %rax
movsd 0x20(%rax), %xmm0
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x4c437
movl %eax, %r12d
movq %rbp, %rdi
xorl %esi, %esi
leaq 0x4cf07(%rip), %rdx     # 0x9910b
callq 0x35305
movq 0x70(%rsp,%rbx,8), %rdi
cvtsi2sd %r12d, %xmm0
divsd 0x60(%rsp), %xmm0
movq %rbp, %rsi
movb $0x1, %al
callq 0x42e0
incq %rbx
addq $0x4, %r13
jmp 0x4c1cf
movl 0x4c(%rsp), %ecx
incl %ecx
movl 0x44(%rsp), %eax
jmp 0x4c18c
movq 0x10(%rsp), %rbx
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x2d2da
movq %rax, (%rbx)
movq 0x50(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c6e4
andq $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
movq 0x58(%rsp), %rbx
incl %ebx
jmp 0x4c132
movb $0x1, %bpl
jmp 0x4c2a1
cmpl $0x5, %ebx
setae %bpl
movq 0x10(%rsp), %rbx
movq (%rbx), %rdi
movq 0x20(%rsp), %rsi
callq 0x2d2da
movq %rax, (%rbx)
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c6e4
xorl %ebx, %ebx
movq 0x38(%rsp), %r14
cmpq %rbx, %r15
je 0x4c2bc
movq 0x70(%rsp,%rbx,8), %rdi
callq 0x42a0
incq %rbx
jmp 0x4c2a8
testb %bpl, %bpl
je 0x4c36d
leaq 0x4e105(%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 0x28(%rsp), %rax
movl 0x28(%rax), %eax
cmovel %ebp, %ebx
subq $0x8, %rsp
leaq 0x7a1b8(%rsp), %rcx
leaq 0x74(%rsp), %r8
leaq 0x70(%rsp), %r9
movq 0x38(%rsp), %rdi
movl 0x14(%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 0x4c3c5
cmpl $0x2, %eax
movq 0x38(%rsp), %r14
je 0x4c39b
cmpl $0x1, %eax
jne 0x4c40e
movq 0x20(%r14), %rdi
leaq 0x28(%r14), %rsi
callq 0x92b17
movl %eax, %ebp
jmp 0x4c406
movq 0x30(%rsp), %rdi
movl 0xc(%rsp), %esi
callq 0x34fc3
movq 0x20(%r14), %rdi
leaq 0x28(%r14), %rsi
callq 0x92b17
movl %eax, %ecx
movl %eax, 0x30(%r14)
xorl %eax, %eax
cmpl $-0x1, %ecx
setne %al
jmp 0x4c425
leaq 0x38(%r14), %rsi
leaq 0x48(%r14), %rdx
movq 0x28(%rsp), %rax
movsd 0x20(%rax), %xmm0
pushq $-0x2
popq %rbp
movq 0x10(%rsp), %rdi
movl %ebp, %ecx
callq 0x46e18
movl %eax, 0x90(%r14)
jmp 0x4c3f2
movq 0x38(%rsp), %r14
leaq 0x38(%r14), %rsi
leaq 0x48(%r14), %rdx
movq 0x28(%rsp), %rax
movsd 0x20(%rax), %xmm0
pushq $-0x3
popq %rbp
movq 0x10(%rsp), %rdi
movl %ebp, %ecx
callq 0x46e18
movl %eax, 0x94(%r14)
testl %eax, %eax
jne 0x4c406
movq 0x38(%r14), %rdi
movq 0x40(%r14), %rsi
callq 0x3929f
pushq $-0x1
popq %rbp
movl %ebp, 0x30(%r14)
movl 0xc(%rsp), %ebp
movq %rbx, %rdi
callq 0x34ff7
movq 0x30(%rsp), %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