// File: /workspace/github_c_makefile_star2_O1/olcaytaner[P]Isell/hmm.c
// Function: baumwelch
double baumwelch(Hmmptr hmm, Instanceptr traindata, int numiteration)
{
 /*!Last Changed 26.03.2007*/
 int t, s, i, j, iter = 0, numsequence;
 double ll = 0.0, llseq;
 double *accumA, *accumB, **accumMix;
 int componentcount;
 Instanceptr inst;
 Hmmptr hmmiter, hmmtemp; 
 accumA = safemalloc(sizeof(double) * hmm->statecount, "baumwelch", 9);
 accumB = safemalloc(sizeof(double) * hmm->statecount, "baumwelch", 10);
 componentcount = get_component_count(hmm->states[0], hmm->statetype);
 if (componentcount > 0)
   accumMix = (double **) safemalloc_2d(sizeof(double*), sizeof(double), hmm->statecount, componentcount,"baumwelch", 15);
 hmmiter = alloc_hmm(hmm->statecount, hmm->statetype, componentcount, hmm->hmmtype);
 hmmtemp = alloc_hmm(hmm->statecount, hmm->statetype, componentcount, hmm->hmmtype);
 do{
   init_hmm(hmmtemp);
   ll = 0.0;
   t = 0;
   init_array(accumA, hmm->statecount);
   init_array(accumB, hmm->statecount);
   inst = traindata;
   while (inst)
    { 
     copy_hmm(hmmiter, hmm);
     llseq = iterate_em_hmm(hmmiter, inst, accumA, accumB, accumMix);
	    sum_hmm(hmmtemp, hmmiter);
     ll = ll + llseq;
     t++;
     inst = inst->next;
    }
   numsequence = t;
   for (s = 0; s < hmmtemp->statecount; s++)
    {
     hmmtemp->states[s].pi /= numsequence;
     divide_array(hmmtemp->states[s].a_i_dot, accumA[s], hmmtemp->statecount);
     if (componentcount > 0)
       update_state_model(hmmtemp->states[s].b, accumB[s], accumMix[s], hmmtemp->statetype);
     else
       update_state_model(hmmtemp->states[s].b, accumB[s], NULL, hmmtemp->statetype);
    }
   for (i = 0; i < hmm->statecount; i++)
     for (j = 0; j < hmm->statecount; j++)
       hmmtemp->states[j].a_dot_j[i] = hmmtemp->states[i].a_i_dot[j];
   copy_hmm(hmm, hmmtemp);
   iter++;
 }while (iter < numiteration);
 free_hmm(hmmiter);
 free_hmm(hmmtemp);
 safe_free(accumA);
 safe_free(accumB);
 if (componentcount > 0)
   free_2d((void **)accumMix, hmm->statecount);
 return ll;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %r13
movslq 0x8(%rdi), %rdi
shlq $0x3, %rdi
leaq 0x7a39a(%rip), %rbx     # 0xa560b
movq %rbx, %rsi
movl $0x9, %edx
callq 0x390f5
movq %rax, 0x10(%rsp)
movslq 0x8(%r13), %rdi
shlq $0x3, %rdi
movq %rbx, %rsi
movl $0xa, %edx
callq 0x390f5
movq %rax, %r12
movl 0xc(%r13), %eax
cmpl $0x4, %eax
je 0x2b2ac
xorl %r14d, %r14d
cmpl $0x2, %eax
jne 0x2b2b8
movq (%r13), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %r14d
testl %r14d, %r14d
jle 0x2b2e7
movl 0x8(%r13), %edx
leaq 0x7a343(%rip), %r8      # 0xa560b
movl $0x8, %edi
movl $0x8, %esi
movl %r14d, %ecx
movl $0xf, %r9d
callq 0x393b5
movq %rax, 0x20(%rsp)
jmp 0x2b2e7
movq 0x10(%rsp), %rbx
movl 0x8(%r13), %edi
movl 0xc(%r13), %esi
movl 0x10(%r13), %ecx
movl %r14d, %edx
callq 0x279db
movq %rax, 0x30(%rsp)
movl 0x8(%r13), %edi
movl 0xc(%r13), %esi
movl 0x10(%r13), %ecx
movl %r14d, %edx
callq 0x279db
movq %rax, %rbp
movl 0x8(%rsp), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
movl %eax, 0x2c(%rsp)
xorl %eax, %eax
movq %r13, 0x40(%rsp)
movq %r15, 0x48(%rsp)
movl %r14d, 0x28(%rsp)
movq %r12, 0x38(%rsp)
movq %rax, 0x50(%rsp)
movq %rbp, %rdi
callq 0x2834d
movl 0x8(%r13), %esi
movq %rbx, %rdi
callq 0x37879
movl 0x8(%r13), %esi
movq %r12, %rdi
callq 0x37879
xorpd %xmm0, %xmm0
testq %r15, %r15
je 0x2b403
xorl %r13d, %r13d
movq %r15, %rbx
movq 0x10(%rsp), %r12
movq 0x20(%rsp), %r14
movq 0x30(%rsp), %r15
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x28504
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq 0x38(%rsp), %rcx
movq %r14, %r8
callq 0x2a1b2
movsd %xmm0, 0x18(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x28768
movsd 0x8(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
incl %r13d
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x2b38c
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x48(%rsp), %r15
movq 0x38(%rsp), %r12
movl 0x28(%rsp), %r14d
jmp 0x2b40f
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0x18(%rsp)
movl 0x8(%rbp), %esi
testl %esi, %esi
jle 0x2b47e
xorl %ebx, %ebx
xorl %r13d, %r13d
movq (%rbp), %rax
movsd 0x10(%rax,%rbx,4), %xmm0
divsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x10(%rax,%rbx,4)
movq 0x8(%rax,%rbx,4), %rdi
movq 0x10(%rsp), %rax
movsd (%rax,%rbx), %xmm0
callq 0x3745c
testl %r14d, %r14d
jle 0x2b455
movq 0x20(%rsp), %rax
movq (%rax,%rbx), %rsi
jmp 0x2b457
xorl %esi, %esi
movq (%rbp), %rax
movq 0x18(%rax,%rbx,4), %rdi
movsd (%r12,%rbx), %xmm0
movl 0xc(%rbp), %edx
callq 0x29645
incq %r13
movslq 0x8(%rbp), %rsi
addq $0x8, %rbx
cmpq %rsi, %r13
jl 0x2b41b
movq 0x40(%rsp), %r13
movslq 0x8(%r13), %rax
testq %rax, %rax
movq 0x10(%rsp), %rbx
jle 0x2b4cf
movq (%rbp), %rcx
leaq (,%rax,8), %rdx
xorl %esi, %esi
movq %rsi, %rdi
shlq $0x5, %rdi
movq 0x8(%rcx,%rdi), %rdi
xorl %r8d, %r8d
movsd (%rdi,%r8), %xmm0
movq (%rcx,%r8,4), %r9
movsd %xmm0, (%r9,%rsi,8)
addq $0x8, %r8
cmpq %r8, %rdx
jne 0x2b4ae
incq %rsi
cmpq %rax, %rsi
jne 0x2b49f
movq %r13, %rdi
movq %rbp, %rsi
callq 0x28504
movq 0x50(%rsp), %rax
incl %eax
cmpl 0x2c(%rsp), %eax
jne 0x2b345
movq 0x30(%rsp), %rdi
callq 0x2787b
movq %rbp, %rdi
callq 0x2787b
movq %rbx, %rdi
callq 0x39194
movq %r12, %rdi
callq 0x39194
testl %r14d, %r14d
jle 0x2b520
movl 0x8(%r13), %esi
movq 0x20(%rsp), %rdi
callq 0x3915f
movsd 0x8(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq