// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movl %edx, 0x34(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %r13
movslq 0x8(%rdi), %rdi
shlq $0x3, %rdi
leaq 0x70683(%rip), %rbx     # 0x985e3
pushq $0x9
popq %rdx
movq %rbx, %rsi
callq 0x34f59
movq %rax, 0x10(%rsp)
movslq 0x8(%r13), %rdi
shlq $0x3, %rdi
pushq $0xa
popq %rdx
movq %rbx, %rsi
callq 0x34f59
movq %rax, %r12
movl 0xc(%r13), %esi
cmpl $0x4, %esi
je 0x27f94
cmpl $0x2, %esi
jne 0x27fd3
movq (%r13), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %r14d
testl %r14d, %r14d
jle 0x27fe0
movl 0x8(%r13), %edx
leaq 0x70633(%rip), %r8      # 0x985e3
pushq $0x8
popq %rdi
pushq $0xf
popq %r9
movq %rdi, %rsi
movl %r14d, %ecx
callq 0x351c3
movq %rax, 0x18(%rsp)
movl 0xc(%r13), %esi
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x27fe8
movl $0x0, 0xc(%rsp)
xorl %r14d, %r14d
jmp 0x27fe8
movl $0x0, 0xc(%rsp)
movl 0x8(%r13), %edi
movl 0x10(%r13), %ecx
movl %r14d, %edx
callq 0x24b6e
movq %rax, %rbp
movl 0x8(%r13), %edi
movl 0xc(%r13), %esi
movl 0x10(%r13), %ecx
movl %r14d, %edx
callq 0x24b6e
movq %rax, %r14
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movq %r14, %rdi
callq 0x25381
movl 0x8(%r13), %esi
movq 0x10(%rsp), %rdi
callq 0x339dc
movq %r13, %r15
movl 0x8(%r13), %esi
movq %r12, %rdi
callq 0x339dc
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x38(%rsp), %rbx
xorl %r13d, %r13d
testq %rbx, %rbx
je 0x280a3
movq %rbp, %rdi
movq %r15, %rsi
callq 0x25518
movq %rbp, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
movq 0x18(%rsp), %r8
callq 0x26f6e
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2574b
movsd 0x28(%rsp), %xmm0
addsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
incl %r13d
movq 0x10(%rbx), %rbx
jmp 0x2804f
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %ebx, %ebx
xorl %r13d, %r13d
movslq 0x8(%r14), %rsi
cmpq %rsi, %r13
jge 0x28119
movq (%r14), %rax
movsd 0x10(%rax,%rbx,4), %xmm0
divsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x10(%rax,%rbx,4)
movq 0x8(%rax,%rbx,4), %rdi
movq 0x10(%rsp), %rax
movsd (%rax,%rbx), %xmm0
callq 0x33606
cmpb $0x0, 0xc(%rsp)
je 0x280f7
movq 0x18(%rsp), %rax
movq (%rax,%rbx), %rsi
jmp 0x280f9
xorl %esi, %esi
movq (%r14), %rax
movq 0x18(%rax,%rbx,4), %rdi
movsd (%r12,%rbx), %xmm0
movl 0xc(%r14), %edx
callq 0x26518
incq %r13
addq $0x8, %rbx
jmp 0x280b3
movq %r15, %r13
movl 0x8(%r15), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
shlq $0x3, %rax
xorl %edx, %edx
cmpq %rcx, %rdx
je 0x28166
movq %rdx, %rsi
shlq $0x5, %rsi
xorl %edi, %edi
cmpq %rdi, %rax
je 0x28161
movq (%r14), %r8
movq 0x8(%r8,%rsi), %r9
movsd (%r9,%rdi), %xmm0
movq (%r8,%rdi,4), %r8
movsd %xmm0, (%r8,%rdx,8)
addq $0x8, %rdi
jmp 0x2813e
incq %rdx
jmp 0x28130
movq %r13, %rdi
movq %r14, %rsi
callq 0x25518
movq 0x40(%rsp), %rax
incl %eax
cmpl 0x34(%rsp), %eax
jl 0x28014
movq %rbp, %rdi
callq 0x24a32
movq %r14, %rdi
callq 0x24a32
movq 0x10(%rsp), %rdi
callq 0x34ff7
movq %r12, %rdi
callq 0x34ff7
cmpb $0x0, 0xc(%rsp)
je 0x281b9
movl 0x8(%r13), %esi
movq 0x18(%rsp), %rdi
callq 0x34fc3
movsd 0x28(%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq