// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/hmm.c
// Function: viterbi
int* viterbi(Hmmptr hmm, Instanceptr traindata)
{
 /*!Last Changed 25.02.2007 updated for multivariate data*/
 /*!Last Changed 22.01.2007*/
 double **gamma;
 int **phi, *qs;
 int i, j, t, maxindex, seqlen;
 double maxvalue, gamma_initial, obslik;
 double *temp1D2, *temp1D3;
 Instanceptr inst = traindata;
 seqlen = frame_count(traindata);
 gamma = (double **) safecalloc_2d(sizeof(double*), sizeof(double), seqlen, hmm->statecount, "viterbi", 7);
 phi = (int **) safemalloc_2d(sizeof(int*), sizeof(int), seqlen, hmm->statecount, "viterbi", 8);
 qs = (int *) safemalloc(seqlen * sizeof(int), "viterbi", 9);
 for (i = 0; i < hmm->statecount; i++)
  {
   obslik = calc_obslik(hmm, inst, i);
   gamma_initial = safelog(hmm->states[i].pi) + safelog(obslik);
   gamma[0][i] = gamma_initial;
   phi[0][i] = 0;
  }
 t = 1; 
 inst = inst->sublist;
 while (inst)
  {
   for (j = 0; j < hmm->statecount; j++)
    {
     temp1D2 = log_of_array(hmm->states[j].a_dot_j, hmm->statecount);
     temp1D3 = sum_of_two_arrays(gamma[t - 1], temp1D2, hmm->statecount);
     maxindex = findMaxOutput(temp1D3, hmm->statecount);
     maxvalue = temp1D3[maxindex];
     if (maxvalue == 0)
       maxindex = 0;
     obslik = calc_obslik(hmm, inst, j);
     gamma[t][j] = maxvalue + safelog(obslik);
     phi[t][j] = maxindex + 1;
     safe_free(temp1D2);
     safe_free(temp1D3);
    }
   inst = inst->next;
   t++;
  }
 qs[seqlen - 1] = findMaxOutput(gamma[seqlen - 1], hmm->statecount);
 qs[seqlen - 1]++;
 for (i = seqlen - 2; i >= 0; i--)
   qs[i] = phi[i + 1][qs[i + 1] - 1];
 free_2d((void**) gamma, seqlen);
 free_2d((void**) phi, seqlen);
 return qs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r12
movq %rdi, %r13
movq %rsi, %rdi
callq 0x2e3d4
movl %eax, %r14d
movl 0x8(%r13), %ecx
leaq 0x7b695(%rip), %rbx     # 0xa55e6
movl $0x8, %edi
movl $0x8, %esi
movl %eax, %edx
movq %rbx, %r8
movl $0x7, %r9d
callq 0x39245
movq %rax, %r15
movl 0x8(%r13), %ecx
movl $0x8, %edi
movl $0x4, %esi
movl %r14d, %edx
movq %rbx, %r8
movl $0x8, %r9d
callq 0x393b5
movq %rax, %rbp
movq %r14, 0x38(%rsp)
movslq %r14d, %rax
movq %rax, 0x30(%rsp)
leaq (,%rax,4), %rdi
movq %rbx, %rsi
movl $0x9, %edx
callq 0x390f5
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x8(%r13)
jle 0x2a022
movl $0x10, %r14d
xorl %ebx, %ebx
movq %r13, %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x29dde
movsd %xmm0, 0x18(%rsp)
movq (%r13), %rax
movsd (%rax,%r14), %xmm0
callq 0x38ea9
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x38ea9
addsd 0x10(%rsp), %xmm0
movq (%r15), %rax
movsd %xmm0, (%rax,%rbx,8)
movq (%rbp), %rax
movl $0x0, (%rax,%rbx,4)
incq %rbx
movslq 0x8(%r13), %rax
addq $0x20, %r14
cmpq %rax, %rbx
jl 0x29fc6
movq %r15, 0x8(%rsp)
movq 0x28(%r12), %rax
testq %rax, %rax
je 0x2a13e
movl $0x1, %ecx
movq %rbp, 0x40(%rsp)
movq %rax, 0x20(%rsp)
movl 0x8(%r13), %esi
testl %esi, %esi
movq 0x8(%rsp), %rbx
movq %r13, %r14
movq %rcx, %r15
jle 0x2a123
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%r14), %rax
movq (%rax,%r12), %rdi
callq 0x3788b
movq %rax, 0x18(%rsp)
movq -0x8(%rbx,%r15,8), %rdi
movl 0x8(%r14), %edx
movq %rax, %rsi
callq 0x37b6d
movq %r15, %rbx
movq %rax, %r15
movl 0x8(%r14), %esi
movq %rax, %rdi
callq 0x37852
movslq %eax, %rbp
movsd (%r15,%rbp,8), %xmm0
movsd %xmm0, 0x10(%rsp)
incl %ebp
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movl %r13d, %edx
callq 0x29dde
callq 0x38ea9
xorpd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm2
ucomisd %xmm2, %xmm1
addsd %xmm2, %xmm0
movq 0x8(%rsp), %rax
movq (%rax,%rbx,8), %rax
movsd %xmm0, (%rax,%r13,8)
movl $0x1, %eax
cmovnel %ebp, %eax
cmovpl %ebp, %eax
movq 0x40(%rsp), %rbp
movq (%rbp,%rbx,8), %rcx
movl %eax, (%rcx,%r13,4)
movq 0x18(%rsp), %rdi
callq 0x39194
movq %r15, %rdi
movq %rbx, %r15
movq 0x8(%rsp), %rbx
callq 0x39194
incq %r13
movslq 0x8(%r14), %rsi
addq $0x20, %r12
cmpq %rsi, %r13
jl 0x2a061
incq %r15
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
movq %r14, %r13
movq %r15, %rcx
jne 0x2a03f
movq 0x8(%rsp), %rbx
movq 0x30(%rsp), %r14
movq -0x8(%rbx,%r14,8), %rdi
movl 0x8(%r13), %esi
callq 0x37852
incl %eax
movq 0x28(%rsp), %r15
movl %eax, -0x4(%r15,%r14,4)
cmpl $0x2, %r14d
movq 0x38(%rsp), %rdx
jl 0x2a18a
addl $-0x2, %edx
incq %rdx
movq (%rbp,%rdx,8), %rax
movslq (%r15,%rdx,4), %rcx
movl -0x4(%rax,%rcx,4), %eax
movl %eax, -0x4(%r15,%rdx,4)
decq %rdx
jg 0x2a173
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3915f
movq %rbp, %rdi
movl %r14d, %esi
callq 0x3915f
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq