// File: /workspace/github_c_makefile_star2_O3/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, 0x20(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r13
movq %rdi, %rcx
movl 0x8(%rdi), %edx
testl %edx, %edx
movq %rdi, 0x10(%rsp)
jle 0x2b32d
movl $0x10, %ebx
xorl %ebp, %ebp
movq %rcx, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x2a5c0
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
mulsd (%rax,%rbx), %xmm0
movq 0x8(%rsp), %rax
movq (%rax), %rax
movsd %xmm0, (%rax,%rbp,8)
testq %r14, %r14
je 0x2b31d
addsd (%r14), %xmm0
movsd %xmm0, (%r14)
incq %rbp
movslq 0x8(%rcx), %rdx
addq $0x20, %rbx
cmpq %rdx, %rbp
jl 0x2b2e7
movq 0x28(%r13), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x2b3fd
movl $0x1, %r12d
movq %r14, 0x28(%rsp)
movq 0x8(%rsp), %r15
testl %edx, %edx
jle 0x2b3e1
xorl %r13d, %r13d
xorl %ebp, %ebp
movq -0x8(%r15,%r12,8), %rdi
movq (%rcx), %rax
movq (%rax,%r13), %rsi
movq %r12, %r14
movq %rcx, %r12
callq 0x3843d
movq %rax, %rbx
movl 0x8(%r12), %esi
movq %rax, %rdi
callq 0x38677
movsd %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %r14, %r12
movq 0x18(%rsp), %rsi
movl %ebp, %edx
callq 0x2a5c0
mulsd 0x30(%rsp), %xmm0
movq (%r15,%r14,8), %rax
movq 0x28(%rsp), %r14
movsd %xmm0, (%rax,%rbp,8)
testq %r14, %r14
je 0x2b3c0
addsd (%r14,%r12,8), %xmm0
movsd %xmm0, (%r14,%r12,8)
movq %rbx, %rdi
callq 0x3a180
incq %rbp
movq 0x10(%rsp), %rcx
movslq 0x8(%rcx), %rdx
addq $0x20, %r13
cmpq %rdx, %rbp
jl 0x2b35c
incq %r12
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x18(%rsp)
testq %rax, %rax
jne 0x2b34f
jmp 0x2b403
movl $0x1, %r12d
movl %r12d, %eax
testq %r14, %r14
je 0x2b47d
movl %edx, %ecx
xorl %esi, %esi
testl %edx, %edx
jle 0x2b439
movq 0x8(%rsp), %rdi
movq (%rdi,%rsi,8), %rdi
xorl %r8d, %r8d
movsd (%rdi,%r8,8), %xmm0
divsd (%r14,%rsi,8), %xmm0
movsd %xmm0, (%rdi,%r8,8)
incq %r8
cmpq %r8, %rcx
jne 0x2b41f
incq %rsi
cmpq %rax, %rsi
jne 0x2b40f
movq %r14, %rdi
movl %edx, %esi
callq 0x3839b
movq %rax, %rbx
movq 0x10(%rsp), %rax
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0x38677
movq 0x20(%rsp), %rax
movsd %xmm0, (%rax)
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3a180
movq 0x8(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movl %edx, %esi
callq 0x38677
callq 0x39bec
movq 0x20(%rsp), %rax
movsd %xmm0, (%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq