// File: /workspace/github_c_makefile_star2_O2/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, %r12
movq %rcx, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %r13
movq %rdi, %rcx
pushq $0x10
popq %rbx
xorl %ebp, %ebp
movq %rdi, 0x10(%rsp)
movslq 0x8(%rcx), %rdx
cmpq %rdx, %rbp
jge 0x2788e
movq %rcx, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x26c02
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
mulsd (%rax,%rbx), %xmm0
movq 0x8(%rsp), %rax
movq (%rax), %rax
movsd %xmm0, (%rax,%rbp,8)
testq %r12, %r12
je 0x27885
addsd (%r12), %xmm0
movsd %xmm0, (%r12)
incq %rbp
addq $0x20, %rbx
jmp 0x27844
addq $0x28, %r13
pushq $0x1
popq %r14
movq %r12, 0x20(%rsp)
movq (%r13), %r13
testq %r13, %r13
je 0x2795e
xorl %r15d, %r15d
xorl %ebp, %ebp
movslq %edx, %rax
cmpq %rax, %rbp
jge 0x27952
movq 0x8(%rsp), %r12
movq -0x8(%r12,%r14,8), %rdi
movq (%rcx), %rax
movq (%rax,%r15,4), %rsi
movq %rbp, 0x30(%rsp)
movq %r15, %rbp
movq %r14, %r15
movq %r13, %r14
movq %rcx, %r13
callq 0x33a93
movq %rax, %rbx
movl 0x8(%r13), %esi
movq %rax, %rdi
callq 0x33c3e
movsd %xmm0, 0x28(%rsp)
movq %r13, %rdi
movq %r14, %r13
movq %r15, %r14
movq %rbp, %r15
movq 0x30(%rsp), %rbp
movq %r13, %rsi
movl %ebp, %edx
callq 0x26c02
mulsd 0x28(%rsp), %xmm0
movq (%r12,%r14,8), %rax
movq 0x20(%rsp), %r12
movsd %xmm0, (%rax,%r15)
testq %r12, %r12
je 0x27936
addsd (%r12,%r14,8), %xmm0
movsd %xmm0, (%r12,%r14,8)
movq %rbx, %rdi
callq 0x34ff7
incq %rbp
movq 0x10(%rsp), %rcx
movl 0x8(%rcx), %edx
addq $0x8, %r15
jmp 0x278ad
addq $0x10, %r13
incq %r14
jmp 0x2789b
testq %r12, %r12
je 0x279df
xorl %eax, %eax
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
movl %r14d, %esi
cmpq %rsi, %rax
je 0x279a3
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x2799e
movq 0x8(%rsp), %r8
movq (%r8,%rax,8), %r8
movsd (%r8,%rdi,8), %xmm0
divsd (%r12,%rax,8), %xmm0
movsd %xmm0, (%r8,%rdi,8)
incq %rdi
jmp 0x27979
incq %rax
jmp 0x27972
movq %r12, %rdi
movl %edx, %esi
callq 0x339f3
movq %rax, %rbx
movq 0x10(%rsp), %rax
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0x33c3e
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 0x34ff7
movl %r14d, %eax
movq 0x8(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movl %edx, %esi
callq 0x33c3e
callq 0x34d87
movq 0x18(%rsp), %rax
movsd %xmm0, (%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq