// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/model.c
// Function: hmm_model_simulation_most
Hmm_model hmm_model_simulation_most(char* simulationdirectory, int maxstates, int maxmixture, int* tries, MODEL_SEARCH_DIRECTION_TYPE searchdirection)
{
 /*!Last Changed 10.01.2009*/
 char **two_file;
 int *list, modelcount, i, j, k, bestoftwo;
 BOOLEAN improved = BOOLEAN_TRUE;
 Hmm_modelptr bestmodel;
 Hmm_model result;
 Hmm_modelptr models;
 list = safemalloc(sizeof(int) * MAX_MODEL_SEARCH, "hmm_model_simulation_most", 4);
 models = safemalloc(sizeof(Hmm_model) * maxstates * maxmixture, "hmm_model_simulation_most", 5);
 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++;
    }
 if (searchdirection == FORWARD)
   bestmodel = &(models[0]);
 else
   bestmodel = &(models[k - 1]);
 bestmodel->tried = BOOLEAN_TRUE;
 bestmodel->selected = BOOLEAN_TRUE;
 two_file = (char**) safemalloc_2d(sizeof(char *), sizeof(char), 2, 500, "hmm_model_simulation_most", 20);
 while (improved)
  {
   modelcount = 0;
   /*Remove one state*/
   if (bestmodel->states > 1)
     list[modelcount++] = bestmodel->index - maxmixture;
   /*Remove one mixture*/
   if (bestmodel->mixture > 1)
     list[modelcount++] = bestmodel->index - 1;
   /*Add best model to comparison*/
   list[modelcount++] = bestmodel->index;
   /*Add one mixture*/
   if (bestmodel->mixture < maxmixture)
     list[modelcount++] = bestmodel->index + 1;
   /*Add one state*/
   if (bestmodel->states < maxstates)
     list[modelcount++] = bestmodel->index + maxmixture;
   if (searchdirection == FORWARD)
     qsort(list, modelcount, sizeof(int), sort_function_int_ascending);
   else
     qsort(list, modelcount, sizeof(int), sort_function_int_descending);
   improved = BOOLEAN_FALSE;
   for (i = 0; i < modelcount; i++)
     if (list[i] != bestmodel->index)
      {
       models[list[i]].tried = BOOLEAN_TRUE;
       bestoftwo = compare_two_hmm_models(two_file, simulationdirectory, *bestmodel, models[list[i]]);
       if ((bestmodel->index < list[i] && bestoftwo == 1) || (bestmodel->index > list[i] && bestoftwo == 0))
        {
         bestmodel = &(models[list[i]]);
         improved = BOOLEAN_TRUE;
         break;
        }
      }
   if (bestmodel->selected)
     improved = BOOLEAN_FALSE;
   else
     bestmodel->selected = BOOLEAN_TRUE;
  }
 safe_free(list);
 *tries = 0;
 for (i = 0; i < maxstates * maxmixture; i++)
   if (models[i].tried)
     (*tries)++;
 result = *bestmodel;
 safe_free(models);
 free_2d((void**)two_file, 2);
 return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %r9d, %r14d
movq %r8, 0x40(%rsp)
movl %ecx, %ebp
movl %edx, %r12d
movq %rsi, 0x58(%rsp)
movq %rdi, 0x48(%rsp)
leaq 0x5f2ca(%rip), %r15     # 0xa743d
movl $0x1c, %edi
movq %r15, %rsi
movl $0x4, %edx
callq 0x390f5
movq %rax, %rbx
movl %r12d, 0x38(%rsp)
movslq %r12d, %r12
movslq %ebp, %rax
imulq %r12, %rax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rdi
movq %r15, %rsi
movl $0x5, %edx
callq 0x390f5
movq %rax, %r15
xorl %eax, %eax
testl %r12d, %r12d
jle 0x48217
leaq 0x10(%r15), %rcx
movl %ebp, %edx
negl %edx
movl $0x1, %esi
movl %ebp, %r12d
movl 0x38(%rsp), %r10d
testl %r12d, %r12d
jle 0x48209
movslq %eax, %rdi
leaq (%rdi,%rdi,4), %rdi
leaq (%rcx,%rdi,4), %rdi
movl $0x1, %r8d
movl %esi, -0x10(%rdi)
movl %r8d, -0xc(%rdi)
movl %eax, -0x8(%rdi)
movq $0x0, -0x4(%rdi)
addq $0x14, %rdi
incl %eax
leal (%rdx,%r8), %r9d
incl %r9d
incl %r8d
cmpl $0x1, %r9d
jne 0x481e1
leal 0x1(%rsi), %edi
cmpl %r10d, %esi
movl %edi, %esi
jne 0x481cb
cltq
jmp 0x4821a
movl %ebp, %r12d
testl %r14d, %r14d
leaq (%rax,%rax,4), %rax
leaq -0x14(%r15,%rax,4), %rbp
cmoveq %r15, %rbp
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0xc(%rbp)
leaq 0x5f1fe(%rip), %r8      # 0xa743d
leaq -0xec77(%rip), %rax     # 0x395cf
cmoveq 0x88d8a(%rip), %rax     # 0xd0fd8
movq %rax, 0x50(%rsp)
movl $0x8, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x1f4, %ecx            # imm = 0x1F4
movl $0x14, %r9d
callq 0x393b5
movq %rax, %r14
movl %r12d, 0x3c(%rsp)
cmpl $0x2, (%rbp)
movl $0x0, %eax
jl 0x48292
movl 0x8(%rbp), %eax
subl %r12d, %eax
movl %eax, (%rbx)
movl $0x1, %eax
cmpl $0x2, 0x4(%rbp)
movl 0x38(%rsp), %esi
jl 0x482a8
movl 0x8(%rbp), %ecx
decl %ecx
movl %eax, %edx
incl %eax
movl %ecx, (%rbx,%rdx,4)
movl 0x8(%rbp), %ecx
leal 0x1(%rax), %r13d
movl %eax, %edx
movl %ecx, (%rbx,%rdx,4)
cmpl %r12d, 0x4(%rbp)
jge 0x482cb
movl 0x8(%rbp), %ecx
incl %ecx
addl $0x2, %eax
movl %r13d, %edx
movl %ecx, (%rbx,%rdx,4)
movl %eax, %r13d
cmpl %esi, (%rbp)
jge 0x482df
movl 0x8(%rbp), %eax
addl %r12d, %eax
movl %r13d, %ecx
incl %r13d
movl %eax, (%rbx,%rcx,4)
movl %r13d, %r12d
movl $0x4, %edx
movq %rbx, %rdi
movq %r12, %rsi
movq 0x50(%rsp), %rcx
callq 0x4240
movb $0x1, %al
testl %r13d, %r13d
jle 0x483c9
xorl %r13d, %r13d
movslq (%rbx,%r13,4), %rax
cmpl 0x8(%rbp), %eax
je 0x483af
leaq (%rax,%rax,4), %rax
movl $0x1, 0x10(%r15,%rax,4)
movslq (%rbx,%r13,4), %rax
leaq (%rax,%rax,4), %rax
movl 0x10(%rbp), %ecx
movl %ecx, 0x90(%rsp)
movups (%rbp), %xmm0
movaps %xmm0, 0x80(%rsp)
movl 0x10(%r15,%rax,4), %ecx
movl %ecx, 0x70(%rsp)
movups (%r15,%rax,4), %xmm0
movaps %xmm0, 0x60(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0x28(%rsp)
movaps 0x60(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x80(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq 0x58(%rsp), %rsi
callq 0x4845d
movl 0x8(%rbp), %edx
movslq (%rbx,%r13,4), %rcx
cmpl %ecx, %edx
setl %sil
cmpl $0x1, %eax
sete %dil
testb %dil, %sil
jne 0x483bf
cmpl %ecx, %edx
setg %dl
testl %eax, %eax
sete %al
andb %dl, %al
cmpb $0x1, %al
je 0x483bf
incq %r13
cmpq %r13, %r12
jne 0x48305
movb $0x1, %al
jmp 0x483c9
leaq (%rcx,%rcx,4), %rax
leaq (%r15,%rax,4), %rbp
xorl %eax, %eax
cmpl $0x0, 0xc(%rbp)
movl 0x3c(%rsp), %r12d
jne 0x483e3
movl $0x1, 0xc(%rbp)
testb %al, %al
je 0x4827a
movq %rbx, %rdi
callq 0x39194
movq 0x40(%rsp), %rdx
movl $0x0, (%rdx)
imull 0x38(%rsp), %r12d
testl %r12d, %r12d
jle 0x48421
movl %r12d, %eax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
cmpl $0x0, 0x10(%r15,%rcx)
je 0x48418
incl (%rdx)
addq $0x14, %rcx
cmpq %rcx, %rax
jne 0x4840e
movl 0x10(%rbp), %eax
movq 0x48(%rsp), %rbx
movl %eax, 0x10(%rbx)
movups (%rbp), %xmm0
movups %xmm0, (%rbx)
movq %r15, %rdi
callq 0x39194
movq %r14, %rdi
movl $0x2, %esi
callq 0x3915f
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq