// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
callq 0x42ac0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0x8(%rax), %ecx
movl $0x8, %esi
leaq 0xa8f61(%rip), %r8      # 0xe51de
movl $0x7, %r9d
movq %rsi, %rdi
callq 0x52fa0
movq %rax, -0x18(%rbp)
movl -0x3c(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0x8(%rax), %ecx
movl $0x8, %edi
movl $0x4, %esi
leaq 0xa8f34(%rip), %r8      # 0xe51de
movl $0x8, %r9d
callq 0x53120
movq %rax, -0x20(%rbp)
movslq -0x3c(%rbp), %rdi
shlq $0x2, %rdi
leaq 0xa8f16(%rip), %rsi     # 0xe51de
movl $0x9, %edx
callq 0x52d70
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x3c373
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x3c030
movsd %xmm0, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movsd 0x10(%rax), %xmm0
callq 0x52b10
movsd %xmm0, -0x78(%rbp)
movsd -0x58(%rbp), %xmm0
callq 0x52b10
movaps %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
movq -0x18(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3c2dd
movl $0x1, -0x34(%rbp)
movq -0x70(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
je 0x3c4cb
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x3c4b1
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x30(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x50690
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
movq -0x60(%rbp), %rsi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %edx
callq 0x50c60
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x505f0
movl %eax, -0x38(%rbp)
movq -0x68(%rbp), %rax
movslq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3c42e
jp 0x3c42e
movl $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x30(%rbp), %edx
callq 0x3c030
movsd %xmm0, -0x58(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movsd -0x58(%rbp), %xmm0
callq 0x52b10
movaps %xmm0, %xmm1
movsd -0x80(%rbp), %xmm0
addsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x38(%rbp), %edx
addl $0x1, %edx
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x60(%rbp), %rdi
callq 0x52e80
movq -0x68(%rbp), %rdi
callq 0x52e80
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x3c398
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x70(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x3c386
movq -0x18(%rbp), %rax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x505f0
movl %eax, %edx
movq -0x28(%rbp), %rax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
movl -0x3c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
subl $0x2, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jl 0x3c55f
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq -0x28(%rbp), %rcx
movl -0x2c(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3c519
movq -0x18(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x52e20
movq -0x20(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x52e20
movq -0x28(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)