// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rsi, %r12
movq %rdi, %r13
movq %rsi, %rdi
callq 0x2ec88
movl %eax, %r14d
movl 0x8(%r13), %ecx
leaq 0x7df0b(%rip), %rbx     # 0xa863e
movl $0x8, %edi
movl $0x8, %esi
movl %eax, %edx
movq %rbx, %r8
movl $0x7, %r9d
callq 0x3a231
movq %rax, %r15
movl 0x8(%r13), %ecx
movl $0x8, %edi
movl $0x4, %esi
movl %r14d, %edx
movq %rbx, %r8
movl $0x8, %r9d
callq 0x3a3ac
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 0x3a0e1
movq %rax, 0x28(%rsp)
movl 0x8(%r13), %esi
testl %esi, %esi
jle 0x2a805
movl $0x10, %r14d
xorl %ebx, %ebx
movq %r13, %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x2a5c0
movsd %xmm0, 0x8(%rsp)
movq (%r13), %rax
movsd (%rax,%r14), %xmm0
callq 0x39bec
movsd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x39bec
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), %rsi
addq $0x20, %r14
cmpq %rsi, %rbx
jl 0x2a7a9
movq %r15, 0x8(%rsp)
movq 0x28(%r12), %rax
testq %rax, %rax
je 0x2a91f
movq %r13, 0x40(%rsp)
movl $0x1, %ecx
movq %rbp, 0x48(%rsp)
movq 0x40(%rsp), %r14
testl %esi, %esi
movq %rax, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
jle 0x2a905
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%r14), %rax
movq (%rax,%r12), %rdi
callq 0x3839b
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %rcx
movq -0x8(%rcx,%rbx,8), %rdi
movl 0x8(%r14), %edx
movq %rax, %rsi
callq 0x38792
movq %rax, %r15
movl 0x8(%r14), %esi
movq %rax, %rdi
callq 0x38362
movslq %eax, %rbp
movsd (%r15,%rbp,8), %xmm0
movsd %xmm0, 0x50(%rsp)
incl %ebp
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movl %r13d, %edx
callq 0x2a5c0
callq 0x39bec
xorpd %xmm1, %xmm1
movsd 0x50(%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 0x48(%rsp), %rbp
movq (%rbp,%rbx,8), %rcx
movl %eax, (%rcx,%r13,4)
movq 0x10(%rsp), %rdi
callq 0x3a180
movq %r15, %rdi
callq 0x3a180
incq %r13
movslq 0x8(%r14), %rsi
addq $0x20, %r12
cmpq %rsi, %r13
jl 0x2a844
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x2a82c
movq 0x8(%rsp), %rbx
movq 0x30(%rsp), %r14
movq -0x8(%rbx,%r14,8), %rdi
callq 0x38362
incl %eax
movq 0x28(%rsp), %r15
movl %eax, -0x4(%r15,%r14,4)
cmpl $0x2, %r14d
movq 0x38(%rsp), %rdx
jl 0x2a967
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
jne 0x2a950
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3a14b
movq %rbp, %rdi
movl %r14d, %esi
callq 0x3a14b
movq %r15, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq