// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/hmm.c
// Function: best_hmm_for_together
void best_hmm_for_together(Hmmptr* model, Instanceptr* traindata, Instanceptr cvdata, Hmm_parameterptr p)
{
 /*!Last Changed 14.04.2007*/
 Hmmptr candidate, besthmm, tmp;
 int i, j, bestone = 0, operatorcount;
 double besterror, error;
 if (in_list(p->statetype, 3, STATE_DISCRETE, STATE_GAUSSIAN, STATE_DIRICHLET))
   operatorcount = 1;
 else
   operatorcount = 3;
 for (i = 0; i < current_dataset->classno; i++)
  {
   model[i] = random_hmm(2, p->statetype, 1, p->hmmtype, traindata[i]);
   baumwelch(model[i], traindata[i], p->iteration);
  }
 besterror = hmm_error_rate(model, cvdata);
 printf("Start error:%.6f\n", besterror);
 while (bestone != NO_CHANGE)
  {
   bestone = NO_CHANGE;
   besthmm = NULL;
   for (i = 0; i < current_dataset->classno; i++)
     for (j = 0; j < operatorcount; j++)
      {
       candidate = apply_operator(model[i], j, p, traindata[i]);
       tmp = model[i];
       model[i] = candidate;
       error = hmm_error_rate(model, cvdata);
       printf("Candidate error:%.6f\n", error);
       if (error < besterror)
        {
         if (besthmm)
           free_hmm(besthmm);
         besthmm = candidate;
         besterror = error;
         bestone = i;
         printf("Best error:%.6f\n", besterror);
        }
       else
         free_hmm(candidate);
       model[i] = tmp;
      }
   if (bestone != NO_CHANGE)
    {
     free_hmm(model[bestone]);
     model[bestone] = besthmm;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
movq %rcx, 0x28(%rsp)
movl 0x4(%rcx), %edi
xorl %r14d, %r14d
pushq $0x1
popq %rbp
pushq $0x3
popq %rsi
xorl %edx, %edx
movl %ebp, %ecx
movl %esi, %r8d
xorl %eax, %eax
callq 0x35812
movl %eax, 0x8(%rsp)
pushq $0x2
popq %r13
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %rbx
leaq 0xa4c6b(%rip), %rax     # 0xcd870
movq (%rax), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %r14
jge 0x28c3f
movl (%r15), %ecx
movl 0x4(%r15), %esi
movq (%rbx,%r14,8), %r8
movl %r13d, %edi
movl %ebp, %edx
callq 0x2460f
movq %rax, (%r12,%r14,8)
movq (%rbx,%r14,8), %rsi
movl 0x8(%r15), %edx
movq %rax, %rdi
callq 0x27f37
incq %r14
jmp 0x28bfe
xorl %eax, %eax
cmpl $0x0, 0x8(%rsp)
sete %al
leal 0x1(,%rax,2), %eax
movl %eax, 0x14(%rsp)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0x28d8f
leaq 0x6fabd(%rip), %rdi     # 0x98725
movsd %xmm0, 0x30(%rsp)
movb $0x1, %al
callq 0x4030
xorl %r14d, %r14d
testb $0x1, %r14b
jne 0x28d80
xorl %r15d, %r15d
pushq $-0x1
popq %rax
movl %eax, 0x10(%rsp)
xorl %ebx, %ebx
leaq 0xa4bdb(%rip), %rax     # 0xcd870
movq (%rax), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %r15
jge 0x28d5a
xorl %r13d, %r13d
cmpl %r13d, 0x14(%rsp)
je 0x28d52
movq (%r12,%r15,8), %rdi
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rcx
movl %r13d, %esi
movq 0x28(%rsp), %rdx
callq 0x289aa
movq %rax, %rbp
movq (%r12,%r15,8), %r14
movq %rax, (%r12,%r15,8)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0x28d8f
movsd %xmm0, 0x8(%rsp)
leaq 0x6fa45(%rip), %rdi     # 0x98737
movb $0x1, %al
callq 0x4030
movsd 0x30(%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x28d3e
testq %rbx, %rbx
je 0x28d14
movq %rbx, %rdi
callq 0x24a32
leaq 0x6fa32(%rip), %rdi     # 0x9874d
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x4030
movl %r15d, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq %rbp, %rbx
jmp 0x28d46
movq %rbp, %rdi
callq 0x24a32
movq %r14, (%r12,%r15,8)
incl %r13d
jmp 0x28ca8
incq %r15
jmp 0x28c8e
movl 0x10(%rsp), %eax
cmpl $-0x1, %eax
sete %r14b
je 0x28c78
movslq %eax, %r15
movq (%r12,%r15,8), %rdi
callq 0x24a32
movq %rbx, (%r12,%r15,8)
jmp 0x28c78
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq