// File: /workspace/github_c_makefile_star2_O3/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 %rcx, %r14
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movl 0x4(%rcx), %edi
movl $0x3, %esi
xorl %edx, %edx
movl $0x1, %ecx
movl $0x3, %r8d
xorl %eax, %eax
callq 0x3aa8c
movl %eax, 0x14(%rsp)
leaq 0xa8fce(%rip), %r13     # 0xd5870
movq (%r13), %rax
cmpl $0x0, 0x10(%rax)
jle 0x2c8ec
xorl %ebx, %ebx
movl (%r14), %ecx
movl 0x4(%r14), %esi
movq (%r15,%rbx,8), %r8
movl $0x2, %edi
movl $0x1, %edx
callq 0x27b0a
movq %rax, (%r12,%rbx,8)
movq (%r15,%rbx,8), %rsi
movl 0x8(%r14), %edx
movq %rax, %rdi
callq 0x2bab3
incq %rbx
movq (%r13), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %rbx
jl 0x2c8ae
movq %r15, 0x28(%rsp)
movq %r14, 0x30(%rsp)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0x2ca49
leaq 0x7be9b(%rip), %rdi     # 0xa87a5
movsd %xmm0, 0x20(%rsp)
movb $0x1, %al
callq 0x4030
movq (%r13), %rax
cmpl $0x0, 0x10(%rax)
jle 0x2ca3a
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorl %ebx, %ebx
xorl %r15d, %r15d
movq (%r12,%rbx,8), %r14
movq %r15, %r13
xorl %ebp, %ebp
movq 0x28(%rsp), %rax
movq (%rax,%rbx,8), %rcx
movq %r14, %rdi
movl %ebp, %esi
movq 0x30(%rsp), %rdx
callq 0x2c63e
movq %rax, %r15
movq (%r12,%rbx,8), %r14
movq %rax, (%r12,%rbx,8)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
callq 0x2ca49
movsd %xmm0, 0x8(%rsp)
leaq 0x7be3f(%rip), %rdi     # 0xa87b7
movb $0x1, %al
callq 0x4030
movsd 0x20(%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x2c9c0
testq %r13, %r13
je 0x2c99a
movq %r13, %rdi
callq 0x27f8a
leaq 0x7be2c(%rip), %rdi     # 0xa87cd
movsd 0x8(%rsp), %xmm0
movb $0x1, %al
callq 0x4030
movl %ebx, 0x4(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x2c9cb
movq %r15, %rdi
callq 0x27f8a
movq %r13, %r15
movq %r14, (%r12,%rbx,8)
cmpl $0x0, 0x14(%rsp)
jne 0x2c9e7
leal 0x1(%rbp), %eax
movq %r15, %r13
cmpl $0x2, %ebp
movl %eax, %ebp
jb 0x2c93b
incq %rbx
leaq 0xa8e7f(%rip), %r14     # 0xd5870
movq (%r14), %rax
movslq 0x10(%rax), %rax
cmpq %rax, %rbx
jl 0x2c932
cmpl $-0x1, 0x4(%rsp)
je 0x2ca3a
movslq 0x4(%rsp), %rbx
movq (%r12,%rbx,8), %rdi
callq 0x27f8a
movq %r15, (%r12,%rbx,8)
movq (%r14), %rax
cmpl $0x0, 0x10(%rax)
movl $0x0, %ebx
movl $0x0, %r15d
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
jg 0x2c932
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq