// File: /workspace/github_c_makefile_star2_O1/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 0x536dd(%rip), %rdi     # 0xa5c20
callq 0x5341c
movl %eax, %ebp
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
leaq 0x7a1b0(%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 0x5531c(%rip), %rsi     # 0xa789a
movl $0x5, %edx
callq 0x390f5
movq %rax, %r15
leaq 0x54e2a(%rip), %r14     # 0xa73bc
movq %r14, %rdi
callq 0x5356b
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x4190
movupd 0x55308(%rip), %xmm0    # 0xa78c5
movupd %xmm0, 0x90(%rsp,%rax)
movq (%r15), %rdi
movq %rbx, %rsi
callq 0x3a404
movq %rax, (%r15)
movq %r14, %rdi
callq 0x5356b
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 0x3a404
movq %rax, 0x8(%r15)
movl %ebp, 0xc(%rsp)
cmpl $0x3, %ebp
movq %r15, %rbp
jne 0x52683
leaq 0x54d8d(%rip), %rdi     # 0xa73bc
callq 0x5356b
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 0x3a404
movq %rax, 0x10(%rbp)
movl 0xc(%rsp), %eax
movl %eax, %r15d
testl %eax, %eax
jle 0x526bd
xorl %ebx, %ebx
leaq 0x5620a(%rip), %r14     # 0xa88a1
xorl %r12d, %r12d
movq (%rbp,%r12,8), %rdi
movq %r14, %rsi
callq 0x40f0
movq %rax, 0x70(%rsp,%r12,8)
testq %rax, %rax
je 0x52a4b
incq %r12
cmpq %r12, %r15
jne 0x5269a
leaq 0x20(%r13), %r12
leaq 0x18(%rsp), %rbx
leaq 0x10(%rsp), %r14
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
movl $0x2, %ecx
callq 0x30746
movq (%rbx), %rdi
callq 0x2fa14
movl %eax, %ebx
movq (%r14), %rdi
callq 0x2fa14
movl %ebx, 0x24(%rsp)
cmpl $0x2, %ebx
setl %sil
cmpl $0x2, %eax
setl %cl
xorl %r14d, %r14d
orb %sil, %cl
movq %r12, 0x28(%rsp)
jne 0x52888
movq %rbp, 0x40(%rsp)
movq %r13, 0x48(%rsp)
leaq 0xf42d0(%rsp), %r14
xorl %ebx, %ebx
movl %eax, 0x3c(%rsp)
movl %ebx, %eax
movq %rax, 0x58(%rsp)
xorl %r13d, %r13d
testl %r13d, %r13d
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %rax
movq %rax, %rcx
cmoveq %rbp, %rcx
cmoveq %rax, %rbp
movq %rcx, 0x50(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x527e5
testl %r13d, %r13d
movl 0x24(%rsp), %eax
cmovel 0x3c(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x60(%rsp)
xorl %ebx, %ebx
cmpl $0x2, %ebx
je 0x52790
cmpl $0x1, %ebx
je 0x52789
testl %ebx, %ebx
jne 0x527af
movl %ebx, %edx
jmp 0x52795
movl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
jmp 0x52795
movl $0xfffffffd, %edx       # imm = 0xFFFFFFFD
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm0
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x52a5f
movl %eax, %r12d
movq %r14, %rdi
xorl %esi, %esi
leaq 0x539b0(%rip), %rdx     # 0xa616b
callq 0x39529
movq 0x70(%rsp,%rbx,8), %rdi
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd 0x60(%rsp), %xmm0
movq %r14, %rsi
movb $0x1, %al
callq 0x42e0
incq %rbx
cmpq %rbx, %r15
jne 0x52777
leal 0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
je 0x52734
movq 0x28(%rsp), %r13
movq (%r13), %rdi
movq %rbp, %rsi
callq 0x30ad5
movq %rax, (%r13)
movq 0x50(%rsp), %rsi
movq %r13, %rdi
callq 0x2fd59
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq 0x58(%rsp), %rbp
leal 0x1(%rbp), %ebx
cmpl $0x5, %ebx
je 0x528a9
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movl $0x2, %ecx
callq 0x30746
movq 0x18(%rsp), %rdi
callq 0x2fa14
movl %eax, %r13d
movq 0x10(%rsp), %rdi
callq 0x2fa14
movl %r13d, 0x24(%rsp)
cmpl $0x2, %r13d
jl 0x52872
cmpl $0x2, %eax
jge 0x52726
cmpl $0x4, %ebp
setae %r14b
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbp
movq 0x28(%rsp), %r12
movq (%r12), %rdi
movq 0x18(%rsp), %rsi
callq 0x30ad5
movq %rax, (%r12)
movq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x2fd59
jmp 0x528ba
cmpl $0x4, %ebp
setae %r14b
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbp
cmpl $0x0, 0xc(%rsp)
jle 0x528d5
xorl %ebx, %ebx
movq 0x70(%rsp,%rbx,8), %rdi
callq 0x42a0
incq %rbx
cmpq %rbx, %r15
jne 0x528c3
testb %r14b, %r14b
je 0x5298f
leaq 0x54b1c(%rip), %rdi     # 0xa7401
callq 0x535f7
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 0x39a80
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 0x14(%rsp), %esi
movl %ebx, %edx
pushq %rax
callq 0x8c4bc
addq $0x10, %rsp
movq %rax, %rbx
movl (%rax), %eax
cmpl $0x3, %eax
je 0x529e8
cmpl $0x2, %eax
je 0x529b9
cmpl $0x1, %eax
jne 0x52a32
movq 0x20(%r13), %rdi
leaq 0x28(%r13), %rsi
callq 0x9f13b
movl %eax, %ebp
jmp 0x52a2b
movq %rbp, %rdi
movl 0xc(%rsp), %esi
callq 0x3915f
movq 0x20(%r13), %rdi
leaq 0x28(%r13), %rsi
callq 0x9f13b
movl %eax, 0x30(%r13)
xorl %ebx, %ebx
cmpl $-0x1, %eax
setne %bl
jmp 0x52a4b
leaq 0x38(%r13), %rsi
leaq 0x48(%r13), %rdx
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm0
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movq 0x28(%rsp), %rdi
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0x4cbee
movl %eax, 0x90(%r13)
jmp 0x52a15
leaq 0x38(%r13), %rsi
leaq 0x48(%r13), %rdx
movq 0x30(%rsp), %rax
movsd 0x20(%rax), %xmm0
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
movq 0x28(%rsp), %rdi
movl $0xfffffffd, %ecx       # imm = 0xFFFFFFFD
callq 0x4cbee
movl %eax, 0x94(%r13)
testl %eax, %eax
jne 0x52a2b
movq 0x38(%r13), %rdi
movq 0x40(%r13), %rsi
callq 0x3dbad
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, 0x30(%r13)
movq %r15, %r14
movq %rbx, %rdi
callq 0x39194
movq %r14, %rdi
movl 0xc(%rsp), %esi
callq 0x3915f
movl $0x1, %ebx
movl %ebx, %eax
addq $0x16e3f8, %rsp         # imm = 0x16E3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq