// File: /workspace/github_c_makefile_star2_O3/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, 0x30(%rsp)
movq %rdi, %r13
leaq 0x55261(%rip), %rdi     # 0xa8c98
callq 0x54920
movl %eax, %r15d
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, (%rsp)
leaq 0x7a1b0(%rsp), %rbx
movl $0x7a120, %edx          # imm = 0x7A120
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4050
movb $0x2d, (%rbx)
movslq %r15d, %rdi
shlq $0x3, %rdi
leaq 0x56ec8(%rip), %rsi     # 0xaa93a
movl $0x5, %edx
callq 0x3a0e1
movq %rax, %rbp
leaq 0x569d6(%rip), %r14     # 0xaa45c
movq %r14, %rdi
callq 0x54a6f
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movupd 0x56eb4(%rip), %xmm0    # 0xaa965
movupd %xmm0, 0x90(%rsp,%rax)
movq (%rbp), %rdi
movq %rbx, %rsi
callq 0x3b494
movq %rax, (%rbp)
movq %r14, %rdi
callq 0x54a6f
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(%rbp), %rdi
movq %rbx, %rsi
callq 0x3b494
movq %rax, 0x8(%rbp)
movl %r15d, %r14d
cmpl $0x3, %r15d
movl %r15d, 0x8(%rsp)
jne 0x53b7d
leaq 0x56935(%rip), %rdi     # 0xaa45c
callq 0x54a6f
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(%rbp), %rdi
movq %rbx, %rsi
callq 0x3b494
movq %rax, 0x10(%rbp)
jmp 0x53b82
testl %r15d, %r15d
jle 0x53bb9
xorl %ebx, %ebx
leaq 0x57de2(%rip), %r12     # 0xab96d
xorl %r15d, %r15d
movq (%rbp,%r15,8), %rdi
movq %r12, %rsi
callq 0x40f0
movq %rax, 0x70(%rsp,%r15,8)
testq %rax, %rax
je 0x53f5c
incq %r15
cmpq %r15, %r14
jne 0x53b8e
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x53bc1
movl $0x0, 0xc(%rsp)
leaq 0x20(%r13), %r15
leaq 0x10(%rsp), %rbx
movq %rsp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
movl $0x2, %ecx
callq 0x31019
movq (%rbx), %rdi
callq 0x30309
movl %eax, %r12d
movq %rsp, %rax
movq (%rax), %rdi
movq %r13, %rbx
callq 0x30309
movl %r12d, 0x1c(%rsp)
cmpl $0x2, %r12d
setl %sil
cmpl $0x2, %eax
setl %cl
xorl %r13d, %r13d
orb %sil, %cl
movl 0x8(%rsp), %r12d
movq %rbx, 0x20(%rsp)
movq %r15, 0x28(%rsp)
jne 0x53d8c
movq %rbp, 0x40(%rsp)
leaq 0xf42d0(%rsp), %rbp
xorl %ebx, %ebx
movl %eax, 0x38(%rsp)
movl %ebx, %eax
movq %rax, 0x50(%rsp)
movb $0x1, %dl
movq 0x10(%rsp), %rcx
movq (%rsp), %r15
testb $0x1, %dl
movq %r15, %rax
cmovneq %rcx, %rax
movq %rax, 0x48(%rsp)
movq %rcx, 0x58(%rsp)
cmoveq %rcx, %r15
cmpb $0x0, 0xc(%rsp)
movl %edx, 0x3c(%rsp)
je 0x53ceb
testb $0x1, %dl
movl 0x1c(%rsp), %eax
cmovnel 0x38(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x56d44(%rip), %rbx     # 0xaa9d0
xorl %r13d, %r13d
cmpq $0x2, %r13
ja 0x53cb1
movl (%rbx), %edx
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm0
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x53f70
movl %eax, %r12d
movq %rbp, %rdi
xorl %esi, %esi
leaq 0x554de(%rip), %rdx     # 0xa919b
callq 0x3a535
movq 0x70(%rsp,%r13,8), %rdi
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd 0x60(%rsp), %xmm0
movq %rbp, %rsi
movb $0x1, %al
callq 0x42e0
incq %r13
addq $0x4, %rbx
cmpq %r13, %r14
jne 0x53c8f
testb $0x1, 0x3c(%rsp)
movl $0x0, %edx
jne 0x53c40
movq 0x28(%rsp), %r15
movq (%r15), %rdi
movq 0x58(%rsp), %rsi
callq 0x31345
movq %rax, (%r15)
movq 0x48(%rsp), %rsi
movq %r15, %rdi
callq 0x30648
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %rax, (%rsp)
movq 0x50(%rsp), %r13
leal 0x1(%r13), %ebx
cmpl $0x5, %ebx
je 0x53daa
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
movq %rsp, %rdx
movl $0x2, %ecx
callq 0x31019
movq 0x10(%rsp), %rdi
callq 0x30309
movl %eax, %r15d
movq (%rsp), %rdi
callq 0x30309
movl %r15d, 0x1c(%rsp)
cmpl $0x2, %r15d
movq 0x28(%rsp), %r15
jl 0x53d7a
cmpl $0x2, %eax
jge 0x53c33
cmpl $0x4, %r13d
setae %r13b
movl 0x8(%rsp), %r12d
movq 0x40(%rsp), %rbp
movq (%r15), %rdi
movq 0x10(%rsp), %rsi
callq 0x31345
movq %rax, (%r15)
movq (%rsp), %rsi
movq %r15, %rdi
callq 0x30648
jmp 0x53db7
movb $0x1, %r13b
movl 0x8(%rsp), %r12d
movq 0x40(%rsp), %rbp
cmpb $0x0, 0xc(%rsp)
je 0x53dd2
xorl %ebx, %ebx
movq 0x70(%rsp,%rbx,8), %rdi
callq 0x42a0
incq %rbx
cmpq %rbx, %r14
jne 0x53dc0
testb %r13b, %r13b
je 0x53e94
leaq 0x566bf(%rip), %rdi     # 0xaa4a1
callq 0x54afc
movl %eax, %ebx
subq $0x8, %rsp
movq %rbp, %r15
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 0x30(%rsp), %rax
movl 0x28(%rax), %eax
cmovel %ebp, %ebx
movq %r15, %r14
subq $0x8, %rsp
leaq 0x7a1b8(%rsp), %rcx
leaq 0x74(%rsp), %r8
leaq 0x70(%rsp), %r9
movq %r15, %rdi
movl %r12d, %esi
movl %ebx, %edx
pushq %rax
callq 0x8ec4a
addq $0x10, %rsp
movq %rax, %rbx
movl (%rax), %eax
cmpl $0x3, %eax
je 0x53ef5
cmpl $0x2, %eax
je 0x53ec1
cmpl $0x1, %eax
jne 0x53f44
movq 0x20(%rsp), %r14
movq 0x20(%r14), %rdi
leaq 0x28(%r14), %rsi
callq 0xa211d
movl %eax, %ebp
jmp 0x53f3d
movq %rbp, %rdi
movl %r12d, %esi
callq 0x3a14b
movq 0x20(%rsp), %rbx
movq 0x20(%rbx), %rdi
leaq 0x28(%rbx), %rsi
callq 0xa211d
movl %eax, 0x30(%rbx)
xorl %ebx, %ebx
cmpl $-0x1, %eax
setne %bl
jmp 0x53f5c
movq 0x20(%rsp), %r14
leaq 0x38(%r14), %rsi
leaq 0x48(%r14), %rdx
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm0
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movq 0x28(%rsp), %rdi
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0x4e089
movl %eax, 0x90(%r14)
jmp 0x53f27
movq 0x20(%rsp), %r14
leaq 0x38(%r14), %rsi
leaq 0x48(%r14), %rdx
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm0
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
movq 0x28(%rsp), %rdi
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
callq 0x4e089
movl %eax, 0x94(%r14)
testl %eax, %eax
jne 0x53f3d
movq 0x38(%r14), %rdi
movq 0x40(%r14), %rsi
callq 0x3ed51
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, 0x30(%r14)
movq %r15, %r14
movq %rbx, %rdi
callq 0x3a180
movq %r14, %rdi
movl %r12d, %esi
callq 0x3a14b
movl $0x1, %ebx
movl %ebx, %eax
addq $0x16e3f8, %rsp         # imm = 0x16E3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq