// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %r15
movq %rdx, 0x28(%rsp)
movq %rsi, %r12
movq %rdi, %rbp
movl 0x4(%rcx), %edi
movl $0x3, %esi
xorl %edx, %edx
movl $0x1, %ecx
movl $0x3, %r8d
xorl %eax, %eax
callq 0x39a80
movl %eax, 0x24(%rsp)
leaq 0xa6880(%rip), %r13     # 0xd2870
movq (%r13), %rax
cmpl $0x0, 0x10(%rax)
jle 0x2c03b
xorl %ebx, %ebx
movl (%r15), %ecx
movl 0x4(%r15), %esi
movq (%r12,%rbx,8), %r8
movl $0x2, %edi
movl $0x1, %edx
callq 0x27426
movq %rax, (%rbp,%rbx,8)
movq (%r12,%rbx,8), %rsi
movl 0x8(%r15), %edx
movq %rax, %rdi
callq 0x2b24a
incq %rbx
movq (%r13), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %rbx
jl 0x2bffc
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
callq 0x2c1a9
leaq 0x796fe(%rip), %rdi     # 0xa574d
movsd %xmm0, 0x30(%rsp)
movb $0x1, %al
callq 0x4030
movq %rbp, 0x10(%rsp)
movq %r12, 0x40(%rsp)
movq %r15, 0x38(%rsp)
movq (%r13), %rax
cmpl $0x0, 0x10(%rax)
movq %r15, %rdx
jle 0x2c163
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r14, %r13
movq (%rbp,%r15,8), %rdi
movq (%r12,%r15,8), %rcx
movl %ebx, %esi
callq 0x2bd8c
movq %rax, %r14
movq (%rbp,%r15,8), %r12
movq %rax, (%rbp,%r15,8)
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
callq 0x2c1a9
movsd %xmm0, 0x18(%rsp)
leaq 0x79699(%rip), %rdi     # 0xa575f
movb $0x1, %al
callq 0x4030
movsd 0x30(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x2c10f
testq %r13, %r13
je 0x2c0e8
movq %r13, %rdi
callq 0x2787b
leaq 0x79686(%rip), %rdi     # 0xa5775
movsd 0x18(%rsp), %xmm0
movb $0x1, %al
callq 0x4030
movl %r15d, 0xc(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0x2c11a
movq %r14, %rdi
callq 0x2787b
movq %r13, %r14
movq 0x10(%rsp), %rbp
movq %r12, (%rbp,%r15,8)
cmpl $0x0, 0x24(%rsp)
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %r12
jne 0x2c146
leal 0x1(%rbx), %eax
movq %r14, %r13
cmpl $0x2, %ebx
movl %eax, %ebx
jb 0x2c08f
incq %r15
leaq 0xa6720(%rip), %r13     # 0xd2870
movq (%r13), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %r15
jl 0x2c08a
jmp 0x2c16e
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movl 0xc(%rsp), %eax
cmpl $-0x1, %eax
je 0x2c19a
movq %rdx, %r15
movslq %eax, %rbx
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x2787b
movq 0x10(%rsp), %rbp
movq %r14, (%rbp,%rbx,8)
jmp 0x2c06b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq