// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movl %r9d, 0x48(%rsp)
movq %r8, 0x50(%rsp)
movl %ecx, %ebp
movl %edx, %r14d
movq %rsi, 0x68(%rsp)
movq %rdi, 0x58(%rsp)
leaq 0x57d1c(%rip), %r15     # 0x9a40d
pushq $0x1c
popq %rdi
pushq $0x4
popq %rdx
movq %r15, %rsi
callq 0x34f59
movq %rax, %rbx
movslq %r14d, %r12
movslq %ebp, %r13
movq %r12, %rax
imulq %r13, %rax
imulq $0x14, %rax, %rdi
pushq $0x5
popq %rdx
movq %r15, %rsi
callq 0x34f59
movq %rax, %r15
xorl %ecx, %ecx
testl %r13d, %r13d
movl $0x0, %eax
movq %rbp, 0x40(%rsp)
cmovgl %ebp, %eax
testl %r12d, %r12d
movl $0x0, %edx
movl %r14d, 0x3c(%rsp)
cmovgl %r14d, %edx
incl %edx
leaq 0x10(%r15), %rsi
pushq $0x1
popq %rdi
movl %eax, %r8d
movslq %ecx, %r10
imulq $0x14, %r10, %r9
cmpl %edx, %edi
je 0x4279b
movslq %r8d, %r11
subq %r10, %r11
addq %rsi, %r9
leaq (%rcx,%rax), %r10
xorl %r14d, %r14d
cmpq %r14, %r11
je 0x42791
leaq 0x1(%r14), %r12
movl %edi, -0x10(%r9)
movl %r12d, -0xc(%r9)
addl %ecx, %r14d
movl %r14d, -0x8(%r9)
andq $0x0, -0x4(%r9)
addq $0x14, %r9
movq %r12, %r14
jmp 0x4276b
incl %edi
addl %eax, %r8d
movq %r10, %rcx
jmp 0x42750
cmpl $0x0, 0x48(%rsp)
leaq -0x14(%r15,%r9), %r12
cmoveq %r15, %r12
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0xc(%r12)
leaq 0x57c4e(%rip), %r8      # 0x9a40d
leaq -0xd41d(%rip), %rax     # 0x353a9
cmoveq 0x8980a(%rip), %rax     # 0xcbfd8
movq %rax, 0x60(%rsp)
pushq $0x8
popq %rdi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x14
popq %r9
movl $0x1f4, %ecx            # imm = 0x1F4
callq 0x351c3
movq %rax, 0x48(%rsp)
pushq $0x1
popq %r13
testl %r13d, %r13d
je 0x42978
xorl %eax, %eax
cmpl $0x2, (%r12)
jl 0x42813
movl 0x8(%r12), %eax
subl 0x40(%rsp), %eax
movl %eax, (%rbx)
pushq $0x1
popq %rax
cmpl $0x2, 0x4(%r12)
jl 0x42829
movl 0x8(%r12), %ecx
decl %ecx
movl %eax, %edx
incl %eax
movl %ecx, (%rbx,%rdx,4)
movl 0x8(%r12), %edx
leal 0x1(%rax), %ecx
movl %eax, %esi
movl %edx, (%rbx,%rsi,4)
movq 0x40(%rsp), %rsi
cmpl %esi, 0x4(%r12)
jge 0x4284e
incl %edx
addl $0x2, %eax
movl %ecx, %ecx
movl %edx, (%rbx,%rcx,4)
movl %eax, %ecx
movl 0x3c(%rsp), %eax
cmpl %eax, (%r12)
jge 0x42868
movl 0x8(%r12), %eax
addl 0x40(%rsp), %eax
movl %ecx, %edx
incl %ecx
movl %eax, (%rbx,%rdx,4)
movl %ecx, %ebp
movq %rbx, %rdi
movq %rbp, %rsi
pushq $0x4
popq %rdx
movq 0x60(%rsp), %rcx
callq 0x4240
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq %r14, %rbp
je 0x4294d
movslq (%rbx,%r14,4), %rax
cmpl 0x8(%r12), %eax
je 0x42945
imulq $0x14, %rax, %rax
movl $0x1, 0x10(%r15,%rax)
movslq (%rbx,%r14,4), %rax
imulq $0x14, %rax, %rax
movl 0x10(%r12), %ecx
movl %ecx, 0xa0(%rsp)
movups (%r12), %xmm0
movaps %xmm0, 0x90(%rsp)
movl 0x10(%r15,%rax), %ecx
movl %ecx, 0x80(%rsp)
movups (%r15,%rax), %xmm0
movaps %xmm0, 0x70(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0x28(%rsp)
movaps 0x70(%rsp), %xmm0
movups %xmm0, 0x18(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0x10(%rsp)
movaps 0x90(%rsp), %xmm0
movups %xmm0, (%rsp)
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x429f5
movl 0x8(%r12), %edx
movslq (%rbx,%r14,4), %rcx
cmpl %ecx, %edx
setl %sil
cmpl $0x1, %eax
sete %dil
testb %dil, %sil
jne 0x42951
cmpl %ecx, %edx
setg %dl
testl %eax, %eax
sete %al
andb %dl, %al
cmpb $0x1, %al
je 0x42951
incq %r14
jmp 0x42880
xorl %eax, %eax
jmp 0x4295b
imulq $0x14, %rcx, %r12
addq %r15, %r12
pushq $0x1
popq %rax
cmpl $0x0, 0xc(%r12)
jne 0x427f3
movl $0x1, 0xc(%r12)
movl %eax, %r13d
jmp 0x427f3
movq %rbx, %rdi
callq 0x34ff7
movq 0x50(%rsp), %rsi
andl $0x0, (%rsi)
movq 0x40(%rsp), %rcx
imull 0x3c(%rsp), %ecx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
imulq $0x14, %rcx, %rcx
xorl %edx, %edx
cmpq %rax, %rcx
je 0x429b6
cmpl $0x0, 0x10(%r15,%rax)
je 0x429b0
incl %edx
movl %edx, (%rsi)
addq $0x14, %rax
jmp 0x4299f
movl 0x10(%r12), %eax
movq 0x58(%rsp), %rbx
movl %eax, 0x10(%rbx)
movups (%r12), %xmm0
movups %xmm0, (%rbx)
movq %r15, %rdi
callq 0x34ff7
pushq $0x2
popq %rsi
movq 0x48(%rsp), %rdi
callq 0x34fc3
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq