// File: /workspace/github_c_makefile_star2_O2/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, %r13
movq %rdi, %rbp
movq %rsi, %rdi
callq 0x2adc0
movl %eax, %r12d
movl 0x8(%rbp), %ecx
leaq 0x7188f(%rip), %rbx     # 0x985be
pushq $0x8
popq %r15
pushq $0x7
popq %r9
movq %r15, %rdi
movq %r15, %rsi
movl %eax, %edx
movq %rbx, %r8
callq 0x3509c
movq %rax, (%rsp)
movl 0x8(%rbp), %ecx
pushq $0x4
popq %rsi
pushq $0x8
popq %r9
movq %r15, %rdi
movl %r12d, %edx
movq %rbx, %r8
callq 0x351c3
movq %rax, 0x8(%rsp)
movslq %r12d, %rdi
shlq $0x2, %rdi
pushq $0x9
popq %rdx
movq %rbx, %rsi
callq 0x34f59
movq %rax, 0x30(%rsp)
pushq $0x10
popq %r14
xorl %ebx, %ebx
movslq 0x8(%rbp), %rsi
cmpq %rsi, %rbx
jge 0x26de8
movq %rbp, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0x26c02
movsd %xmm0, 0x10(%rsp)
movq (%rbp), %rax
movsd (%rax,%r14), %xmm0
callq 0x34d87
movsd %xmm0, 0x18(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x34d87
addsd 0x18(%rsp), %xmm0
movq (%rsp), %rax
movq (%rax), %rax
movsd %xmm0, (%rax,%rbx,8)
movq 0x8(%rsp), %rax
movq (%rax), %rax
andl $0x0, (%rax,%rbx,4)
incq %rbx
addq $0x20, %r14
jmp 0x26d85
movq %r12, 0x38(%rsp)
addq $0x28, %r13
pushq $0x1
popq %rax
movq %rax, 0x20(%rsp)
movq (%r13), %r13
testq %r13, %r13
je 0x26ef4
movq %r13, 0x28(%rsp)
xorl %r14d, %r14d
xorl %ecx, %ecx
movslq %esi, %rax
cmpq %rax, %rcx
jge 0x26ee1
movq (%rbp), %rax
movq (%rax,%r14,8), %rdi
movq %rcx, 0x10(%rsp)
callq 0x339f3
movq %rax, 0x18(%rsp)
movq (%rsp), %r13
movq 0x20(%rsp), %r12
movq -0x8(%r13,%r12,8), %rdi
movl 0x8(%rbp), %edx
movq %rax, %rsi
callq 0x33cc7
movq %rax, %r15
movl 0x8(%rbp), %esi
movq %rax, %rdi
callq 0x339b7
movq %rbp, %rbx
movslq %eax, %rbp
movsd (%r15,%rbp,8), %xmm0
movsd %xmm0, 0x40(%rsp)
incl %ebp
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x26c02
callq 0x34d87
xorpd %xmm1, %xmm1
movsd 0x40(%rsp), %xmm2
ucomisd %xmm2, %xmm1
addsd %xmm2, %xmm0
movq (%r13,%r12,8), %rax
movsd %xmm0, (%rax,%r14,2)
pushq $0x1
popq %rax
cmovnel %ebp, %eax
cmovpl %ebp, %eax
movq %rbx, %rbp
movq 0x8(%rsp), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, (%rcx,%r14)
movq 0x18(%rsp), %rdi
callq 0x34ff7
movq %r15, %rdi
callq 0x34ff7
movq 0x10(%rsp), %rcx
incq %rcx
movl 0x8(%rbx), %esi
addq $0x4, %r14
jmp 0x26e10
movq 0x28(%rsp), %r13
addq $0x10, %r13
incq 0x20(%rsp)
jmp 0x26df9
movq 0x38(%rsp), %r15
leal -0x1(%r15), %ebx
movslq %ebx, %r14
movq (%rsp), %rax
movq (%rax,%r14,8), %rdi
callq 0x339b7
incl %eax
movq 0x30(%rsp), %rcx
movl %eax, (%rcx,%r14,4)
movq %rcx, %r14
movl %r15d, %eax
addl $-0x2, %eax
testl %eax, %eax
js 0x26f43
movq 0x8(%rsp), %rcx
movq (%rcx,%rbx,8), %rcx
movslq (%r14,%rbx,4), %rdx
movl -0x4(%rcx,%rdx,4), %ecx
movl %eax, %edx
movl %ecx, (%r14,%rdx,4)
decl %eax
decq %rbx
jmp 0x26f21
movq (%rsp), %rdi
movl %r15d, %esi
callq 0x34fc3
movq 0x8(%rsp), %rdi
movl %r15d, %esi
callq 0x34fc3
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq