// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/model.c
// Function: hmm_model_simulation_multitest
Hmm_model hmm_model_simulation_multitest(char* simulationdirectory, int maxstates, int maxmixture, int* tries)
{
 /*!Last Changed 10.01.2009*/
 char **filenames, directory[500], tmpst[500];
 int filecount, i, j, k, m, c, *model, *models_searched, current_best_index;
 Hmm_modelptr bestmodel;
 Hmm_modelptr models;
 Hmm_model result;
 models = safemalloc(sizeof(Hmm_model) * maxstates * maxmixture, "hmm_model_simulation_multitest", 4);
 models_searched = safemalloc(sizeof(int) * MAX_MODEL_SEARCH, "hmm_model_simulation_multitest", 4);
 k = 0;
 for (i = 1; i <= maxstates; i++)
   for (j = 1; j <= maxmixture; j++)
    {
     models[k].states = i;
     models[k].mixture = j;
     models[k].index = k;
     models[k].selected = BOOLEAN_FALSE;
     models[k].tried = BOOLEAN_FALSE;
     k++;
    }
 bestmodel = &(models[0]);
 bestmodel->selected = BOOLEAN_TRUE;
 filenames = (char **) safemalloc_2d(sizeof(char *), sizeof(char), MAX_MODEL_SEARCH, 500, "hmm_model_simulation_multitest", 3);
 sprintf(directory,"%s", simulationdirectory);
 while (BOOLEAN_TRUE)
  {
   filecount = 0;
   /*Remove one state*/
   if (bestmodel->states > 1)
     hmm_add_candidate_model(filenames, directory, bestmodel->index - maxmixture, models, models_searched, &filecount);
   /*Remove one mixture*/
   if (bestmodel->mixture > 1)
     hmm_add_candidate_model(filenames, directory, bestmodel->index - 1, models, models_searched, &filecount);
   /*Add best model to comparison*/
   hmm_add_candidate_model(filenames, directory, bestmodel->index, models, models_searched, &filecount);
   /*Add one mixture*/
   if (bestmodel->mixture < maxmixture)
     hmm_add_candidate_model(filenames, directory, bestmodel->index + 1, models, models_searched, &filecount);
   /*Add one state*/
   if (bestmodel->states < maxstates)
     hmm_add_candidate_model(filenames, directory, bestmodel->index + maxmixture, models, models_searched, &filecount);
   sprintf(tmpst,"-");
   model = multiple_comparison(filenames, filecount, get_parameter_l("testtype"), tmpst, &m, &c, get_parameter_l("correctiontype"));
   current_best_index = models_searched[model[0] - 1]; 
   if (current_best_index == bestmodel->index)
    {
     safe_free(model);
     break;
    }
   else
    {
     if (models[current_best_index].selected)
       break;
     bestmodel = &(models[current_best_index]);
     bestmodel->selected = BOOLEAN_TRUE;
    }
   safe_free(model);
  }
 *tries = 0;
 for (i = 0; i < maxstates * maxmixture; i++)
   if (models[i].tried)
     (*tries)++;
 result = *bestmodel;
 free_2d((void**)filenames, MAX_MODEL_SEARCH);
 safe_free(models);
 safe_free(models_searched);
 return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %r8, 0x28(%rsp)
movl %ecx, %r15d
movl %edx, %r12d
movq %rsi, %r13
movq %rdi, 0x30(%rsp)
movslq %edx, %r14
movslq %ecx, %rax
imulq %r14, %rax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rdi
leaq 0x6188b(%rip), %rbx     # 0xaa482
movq %rbx, %rsi
movl $0x4, %edx
callq 0x3a0e1
movq %rax, 0x20(%rsp)
movl $0x1c, %edi
movq %rbx, %rsi
movl %r12d, %ebx
movl $0x4, %edx
callq 0x3a0e1
movq %rax, %r12
testl %r14d, %r14d
movl %r15d, %r14d
jle 0x48c85
movq 0x20(%rsp), %rax
addq $0x10, %rax
movl %r14d, %ecx
negl %ecx
xorl %edx, %edx
movl $0x1, %esi
testl %r14d, %r14d
jle 0x48c7c
movslq %edx, %rdi
leaq (%rdi,%rdi,4), %rdi
leaq (%rax,%rdi,4), %rdi
movl $0x1, %r8d
movl %esi, -0x10(%rdi)
movl %r8d, -0xc(%rdi)
movl %edx, -0x8(%rdi)
movq $0x0, -0x4(%rdi)
addq $0x14, %rdi
incl %edx
leal (%rcx,%r8), %r9d
incl %r9d
incl %r8d
cmpl $0x1, %r9d
jne 0x48c54
leal 0x1(%rsi), %edi
cmpl %ebx, %esi
movl %edi, %esi
jne 0x48c3e
movq 0x20(%rsp), %r15
movl $0x1, 0xc(%r15)
leaq 0x617e9(%rip), %r8      # 0xaa482
movl $0x8, %edi
movl $0x1, %esi
movl $0x7, %edx
movl $0x1f4, %ecx            # imm = 0x1F4
movl $0x3, %r9d
callq 0x3a3ac
movq %rax, 0x10(%rsp)
leaq 0x240(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %r13
callq 0x4270
movl %ebx, 0x1c(%rsp)
movl $0x0, 0xc(%rsp)
cmpl $0x2, (%r15)
jl 0x48d04
movl 0x8(%r15), %edx
subl %r14d, %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
movq %r12, %r8
leaq 0xc(%rsp), %r9
callq 0x48b4f
cmpl $0x2, 0x4(%r15)
jl 0x48d29
movl 0x8(%r15), %edx
decl %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
movq %r12, %r8
leaq 0xc(%rsp), %r9
callq 0x48b4f
movl 0x8(%r15), %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
movq %r12, %r8
leaq 0xc(%rsp), %r9
callq 0x48b4f
cmpl %r14d, 0x4(%r15)
jge 0x48d69
movl 0x8(%r15), %edx
incl %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
movq %r12, %r8
leaq 0xc(%rsp), %r9
callq 0x48b4f
cmpl %ebx, (%r15)
jge 0x48d8d
movl 0x8(%r15), %edx
addl %r14d, %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rcx
movq %r12, %r8
leaq 0xc(%rsp), %r9
callq 0x48b4f
movw $0x2d, 0x40(%rsp)
movq %r12, %rbx
movl 0xc(%rsp), %r12d
leaq 0x616fe(%rip), %rdi     # 0xaa4a1
callq 0x54afc
movl %eax, %r13d
leaq 0x5cb86(%rip), %rdi     # 0xa5938
callq 0x54afc
movl %eax, (%rsp)
movq 0x10(%rsp), %rdi
movl %r12d, %esi
movq %rbx, %r12
movl %r13d, %edx
leaq 0x40(%rsp), %rcx
leaq 0x3c(%rsp), %r8
leaq 0x38(%rsp), %r9
callq 0x8ec4a
movslq (%rax), %rcx
movslq -0x4(%rbx,%rcx,4), %rcx
cmpl 0x8(%r15), %ecx
je 0x48e1f
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%rsp), %r13
cmpl $0x0, 0xc(%r13,%rcx,4)
movl 0x1c(%rsp), %ebx
jne 0x48e30
leaq (,%rcx,4), %r15
addq %r13, %r15
movl $0x1, 0xc(%r15)
movq %rax, %rdi
callq 0x3a180
jmp 0x48cd7
movq %rax, %rdi
callq 0x3a180
movl 0x1c(%rsp), %ebx
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rsi
movl $0x0, (%rsi)
imull %ebx, %r14d
testl %r14d, %r14d
jle 0x48e68
movl %r14d, %eax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x0, 0x10(%r13,%rcx)
je 0x48e5f
incl %edx
movl %edx, (%rsi)
addq $0x14, %rcx
cmpq %rcx, %rax
jne 0x48e53
movl 0x10(%r15), %eax
movq 0x30(%rsp), %r14
movl %eax, 0x10(%r14)
movups (%r15), %xmm0
movups %xmm0, (%r14)
movq 0x10(%rsp), %rdi
movl $0x7, %esi
callq 0x3a14b
movq %r13, %rdi
callq 0x3a180
movq %r12, %rdi
callq 0x3a180
movq %r14, %rax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq