// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/hmm.c
// Function: calc_bwd
void calc_bwd(Hmmptr hmm, Instanceptr traindata, int seqlen, double **beta, double *scale)
{
 /*!Last Changed 13.03.2007*/
 int i, j, t;
 double sumvalue, obslik;
 double *temp1D2;
 Instanceptr inst;
 /*initialization t = T - 1*/
 for (i = 0; i < hmm->statecount; i++)
   beta[seqlen - 1][i] = 1;  
 /*recursion t=T-2:0*/
 reverse_instancelist(&(traindata->sublist));
 inst = traindata->sublist; /* listeyi ters cevirmek lazim*/
 inst = inst->next;
 t = seqlen - 2; 
 while (t >= 0)
  {
   for (i = 0; i < hmm->statecount; i++)
    {
     temp1D2 = multiply_two_arrays_by_value(beta[t + 1], hmm->states[i].a_i_dot, hmm->statecount);
     for (j = 0; j < hmm->statecount; j++)
      {
       obslik = calc_obslik(hmm, inst, j);
       temp1D2[j] *= obslik;
      }
     sumvalue = sum_of_list_double(temp1D2, hmm->statecount);
     beta[t][i] = sumvalue;
     safe_free(temp1D2);
    }
   t--;
   inst = inst->next;
   if (!inst)
     inst = traindata;
  }
 reverse_instancelist(&(traindata->sublist)); 
 if (scale)
  {
   for (i = 0; i < seqlen; i++)
		   for (j = 0; j < hmm->statecount; j++)
			    beta[i][j] /= scale[i];
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r13
movl %edx, %ebx
movq %rsi, 0x10(%rsp)
movq %rdi, %r12
movslq 0x8(%rdi), %rdx
testq %rdx, %rdx
jle 0x2b574
movslq %ebx, %rax
movq -0x8(%r13,%rax,8), %rax
leal 0x1(%rdx), %ecx
decq %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %ecx
shlq $0x4, %rcx
movdqa 0x7af47(%rip), %xmm1    # 0xa6440
xorl %edx, %edx
movdqa 0x7af4d(%rip), %xmm2    # 0xa6450
pxor %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
pcmpeqd %xmm3, %xmm3
movdqa 0x7af73(%rip), %xmm4    # 0xa6490
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edi
notl %edi
testb $0x1, %dil
je 0x2b553
movq %rsi, (%rax,%rdx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edi
testb $0x1, %dil
je 0x2b567
movq %rsi, 0x8(%rax,%rdx)
paddq %xmm4, %xmm1
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x2b51d
movq %r8, 0x18(%rsp)
movq 0x10(%rsp), %rax
leaq 0x28(%rax), %rdi
movq %rdi, (%rsp)
callq 0x30623
movq %rbx, 0x8(%rsp)
cmpl $0x2, %ebx
jl 0x2b667
movq 0x8(%rsp), %rax
leal -0x2(%rax), %ebx
movq (%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %r15
movl 0x8(%r12), %edx
testl %edx, %edx
jle 0x2b64a
xorl %r14d, %r14d
movq %rbx, 0x20(%rsp)
movq 0x8(%r13,%rbx,8), %rdi
movq (%r12), %rax
movq %r14, %rcx
shlq $0x5, %rcx
movq 0x8(%rax,%rcx), %rsi
callq 0x3843d
movq %rax, %rbp
movl 0x8(%r12), %esi
testl %esi, %esi
jle 0x2b61e
movq %r13, %rbx
xorl %r13d, %r13d
movq %r12, %rdi
movq %r15, %rsi
movl %r13d, %edx
callq 0x2a5c0
mulsd (%rbp,%r13,8), %xmm0
movsd %xmm0, (%rbp,%r13,8)
incq %r13
movslq 0x8(%r12), %rsi
cmpq %rsi, %r13
jl 0x2b5ed
movq %rbx, %r13
movq 0x20(%rsp), %rbx
movq %rbp, %rdi
callq 0x38677
movq (%r13,%rbx,8), %rax
movsd %xmm0, (%rax,%r14,8)
movq %rbp, %rdi
callq 0x3a180
incq %r14
movslq 0x8(%r12), %rdx
cmpq %rdx, %r14
jl 0x2b5c1
leaq -0x1(%rbx), %rax
movq 0x10(%r15), %r15
testq %r15, %r15
cmoveq 0x10(%rsp), %r15
testq %rbx, %rbx
movq %rax, %rbx
jg 0x2b5b1
movq (%rsp), %rdi
callq 0x30623
movq 0x18(%rsp), %r8
testq %r8, %r8
sete %al
movq 0x8(%rsp), %rdx
testl %edx, %edx
setle %cl
orb %al, %cl
jne 0x2b6bd
movl 0x8(%r12), %eax
movl %edx, %ecx
xorl %edx, %edx
testl %eax, %eax
jle 0x2b6b5
movq (%r13,%rdx,8), %rsi
xorl %edi, %edi
movsd (%rsi,%rdi,8), %xmm0
divsd (%r8,%rdx,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x2b69d
incq %rdx
cmpq %rcx, %rdx
jne 0x2b692
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq