// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movl %edx, %ebp
movq %rsi, 0x58(%rsp)
movq %rdi, 0x48(%rsp)
leaq 0x60f92(%rip), %r15     # 0xaa4dd
movl $0x1c, %edi
movq %r15, %rsi
movl $0x4, %edx
callq 0x3a0e1
movq %rax, %rbx
movl %ebp, 0x38(%rsp)
movslq %ebp, %r12
movslq %r13d, %rax
imulq %r12, %rax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rdi
movq %r15, %rsi
movl $0x5, %edx
callq 0x3a0e1
movq %rax, %r15
xorl %eax, %eax
testl %r12d, %r12d
jle 0x495ec
leaq 0x10(%r15), %rcx
movl %r13d, %edx
negl %edx
movl $0x1, %esi
movl 0x38(%rsp), %r12d
testl %r13d, %r13d
jle 0x495de
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 0x495b6
leal 0x1(%rsi), %edi
cmpl %r12d, %esi
movl %edi, %esi
jne 0x495a0
cltq
jmp 0x495f1
movl 0x38(%rsp), %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 0x60ec7(%rip), %r8      # 0xaa4dd
leaq -0xf042(%rip), %rax     # 0x3a5db
cmoveq 0x8a9b3(%rip), %rax     # 0xd3fd8
movq %rax, 0x50(%rsp)
movl $0x8, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x1f4, %ecx            # imm = 0x1F4
movl $0x14, %r9d
callq 0x3a3ac
movq %rax, %r14
movl %r13d, 0x3c(%rsp)
cmpl $0x2, (%rbp)
movl $0x0, %eax
jl 0x49669
movl 0x8(%rbp), %eax
subl %r13d, %eax
movl %eax, (%rbx)
movl $0x1, %eax
cmpl $0x2, 0x4(%rbp)
jl 0x4967b
movl 0x8(%rbp), %ecx
decl %ecx
movl %eax, %edx
incl %eax
movl %ecx, (%rbx,%rdx,4)
movl 0x8(%rbp), %edx
leal 0x1(%rax), %ecx
movl %eax, %esi
movl %edx, (%rbx,%rsi,4)
cmpl %r13d, 0x4(%rbp)
jge 0x49698
incl %edx
addl $0x2, %eax
movl %ecx, %ecx
movl %edx, (%rbx,%rcx,4)
movl %eax, %ecx
cmpl %r12d, (%rbp)
jge 0x496ab
movl 0x8(%rbp), %eax
addl %r13d, %eax
movl %ecx, %edx
incl %ecx
movl %eax, (%rbx,%rdx,4)
movl %ecx, %r12d
movl $0x4, %edx
movq %rbx, %rdi
movq %r12, %rsi
movq 0x50(%rsp), %rcx
callq 0x4240
movl 0x8(%rbp), %ecx
xorl %r13d, %r13d
movslq (%rbx,%r13,4), %rax
cmpl %ecx, %eax
je 0x49774
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 0x49829
movl 0x8(%rbp), %ecx
movslq (%rbx,%r13,4), %rdx
cmpl %edx, %ecx
setl %sil
cmpl $0x1, %eax
sete %dil
testb %dil, %sil
jne 0x49784
cmpl %edx, %ecx
setg %sil
testl %eax, %eax
sete %al
andb %sil, %al
cmpb $0x1, %al
je 0x49784
incq %r13
cmpq %r13, %r12
jne 0x496c9
movb $0x1, %al
jmp 0x4978e
leaq (%rdx,%rdx,4), %rax
leaq (%r15,%rax,4), %rbp
xorl %eax, %eax
cmpl $0x0, 0xc(%rbp)
movl 0x3c(%rsp), %r13d
movl 0x38(%rsp), %r12d
jne 0x497ad
movl $0x1, 0xc(%rbp)
testb %al, %al
je 0x49651
movq %rbx, %rdi
callq 0x3a180
movq 0x40(%rsp), %rsi
movl $0x0, (%rsi)
imull %r12d, %r13d
testl %r13d, %r13d
jle 0x497ed
movl %r13d, %eax
shlq $0x2, %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpl $0x0, 0x10(%r15,%rcx)
je 0x497e4
incl %edx
movl %edx, (%rsi)
addq $0x14, %rcx
cmpq %rcx, %rax
jne 0x497d8
movl 0x10(%rbp), %eax
movq 0x48(%rsp), %rbx
movl %eax, 0x10(%rbx)
movups (%rbp), %xmm0
movups %xmm0, (%rbx)
movq %r15, %rdi
callq 0x3a180
movq %r14, %rdi
movl $0x2, %esi
callq 0x3a14b
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq