// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/hmm.c
// Function: calc_fwd
void calc_fwd(Hmmptr hmm, Instanceptr traindata, double **alpha, double *loglikelihood, double *scale)
{
 /*!Last Changed 13.03.2007*/
 int i, j, t;
 double sumvalue, obslik;
 double *temp1D2;
 Instanceptr inst = traindata;
 /*initialization t=0*/
 for (i = 0; i < hmm->statecount; i++)
  { 
   obslik = calc_obslik(hmm, inst, i);
   alpha[0][i] = hmm->states[i].pi * obslik;
   if (scale)
     scale[0] += alpha[0][i];
  }  
 /*recursion t=1:T-1*/
 t = 1;
 inst = inst->sublist;
 while (inst)
  {
   for (j = 0; j < hmm->statecount; j++)
    {
     temp1D2 = multiply_two_arrays_by_value(alpha[t - 1], hmm->states[j].a_dot_j, hmm->statecount);
     sumvalue = sum_of_list_double(temp1D2, hmm->statecount);
     obslik = calc_obslik(hmm, inst, j);
     alpha[t][j] = sumvalue * obslik;
     if (scale)
       scale[t] += alpha[t][j];
     safe_free(temp1D2);
    }
   inst = inst->next;
   t++;
  }
 if (scale)
  {
  	for (i=0;i<t;i++)
		   for (j = 0; j < hmm->statecount; j++)
		    	alpha[i][j] /= scale[i];
   temp1D2 = log_of_array(scale, hmm->statecount);
   *loglikelihood = sum_of_list_double(temp1D2, hmm->statecount);
   safe_free(temp1D2);
  }
 else
   *loglikelihood = safelog(sum_of_list_double(alpha[t - 1], hmm->statecount));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %r14
movq %rcx, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r13
movq %rdi, %r8
cmpl $0x0, 0x8(%rdi)
movq %rdi, 0x20(%rsp)
jle 0x2ab63
movl $0x10, %ebx
xorl %ebp, %ebp
movq %r8, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x29dde
movq 0x20(%rsp), %r8
movq (%r8), %rax
mulsd (%rax,%rbx), %xmm0
movq 0x8(%rsp), %rax
movq (%rax), %rax
movsd %xmm0, (%rax,%rbp,8)
testq %r14, %r14
je 0x2ab53
addsd (%r14), %xmm0
movsd %xmm0, (%r14)
incq %rbp
movslq 0x8(%r8), %rax
addq $0x20, %rbx
cmpq %rax, %rbp
jl 0x2ab1d
movq 0x28(%r13), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2ac37
movl $0x1, %r12d
movq %r14, 0x28(%rsp)
movq 0x8(%rsp), %r15
movl 0x8(%r8), %edx
testl %edx, %edx
jle 0x2ac1b
xorl %r13d, %r13d
xorl %ebp, %ebp
movq -0x8(%r15,%r12,8), %rdi
movq (%r8), %rax
movq (%rax,%r13), %rsi
movq %r12, %r14
movq %r8, %r12
callq 0x3792d
movq %rax, %rbx
movl 0x8(%r12), %esi
movq %rax, %rdi
callq 0x37ae1
movsd %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %r14, %r12
movq 0x10(%rsp), %rsi
movl %ebp, %edx
callq 0x29dde
mulsd 0x30(%rsp), %xmm0
movq (%r15,%r14,8), %rax
movq 0x28(%rsp), %r14
movsd %xmm0, (%rax,%rbp,8)
testq %r14, %r14
je 0x2abfa
addsd (%r14,%r12,8), %xmm0
movsd %xmm0, (%r14,%r12,8)
movq %rbx, %rdi
callq 0x39194
incq %rbp
movq 0x20(%rsp), %r8
movslq 0x8(%r8), %rdx
addq $0x20, %r13
cmpq %rdx, %rbp
jl 0x2ab96
incq %r12
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x2ab85
jmp 0x2ac3d
movl $0x1, %r12d
testq %r14, %r14
je 0x2acb7
movl 0x8(%r8), %eax
movl %r12d, %ecx
xorl %edx, %edx
testl %eax, %eax
jle 0x2ac72
movq 0x8(%rsp), %rsi
movq (%rsi,%rdx,8), %rsi
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm0
divsd (%r14,%rdx,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x2ac5a
incq %rdx
cmpq %rcx, %rdx
jne 0x2ac4b
movl 0x8(%r8), %esi
movq %r14, %rdi
movq %r8, %r14
callq 0x3788b
movq %rax, %rbx
movl 0x8(%r14), %esi
movq %rax, %rdi
callq 0x37ae1
movq 0x18(%rsp), %rax
movsd %xmm0, (%rax)
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x39194
movl %r12d, %eax
movq 0x8(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movl 0x8(%r8), %esi
callq 0x37ae1
callq 0x38ea9
movq 0x18(%rsp), %rax
movsd %xmm0, (%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq