// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl $0x0, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movq -0x8(%rbp), %rax
movslq 0x8(%rax), %rdi
shlq $0x3, %rdi
leaq 0xa75fb(%rip), %rsi     # 0xe5203
movl $0x9, %edx
callq 0x52d70
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movslq 0x8(%rax), %rdi
shlq $0x3, %rdi
leaq 0xa75da(%rip), %rsi     # 0xe5203
movl $0xa, %edx
callq 0x52d70
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movl 0xc(%rcx), %edi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x3b1b0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jle 0x3dc9d
movq -0x8(%rbp), %rax
movl 0x8(%rax), %edx
movl -0x5c(%rbp), %ecx
movl $0x8, %esi
leaq 0xa7578(%rip), %r8      # 0xe5203
movl $0xf, %r9d
movq %rsi, %rdi
callq 0x53120
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x8(%rbp), %rax
movl 0xc(%rax), %esi
movl -0x5c(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0x10(%rax), %ecx
callq 0x38920
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x8(%rbp), %rax
movl 0xc(%rax), %esi
movl -0x5c(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0x10(%rax), %ecx
callq 0x38920
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x39710
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x18(%rbp)
movq -0x48(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x50650
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x50650
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x3dd84
movq -0x70(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x399d0
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq -0x58(%rbp), %r8
callq 0x3c590
movsd %xmm0, -0x40(%rbp)
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x39de0
movsd -0x38(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x68(%rbp)
jmp 0x3dd1f
movl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x3de7b
cvtsi2sdl -0x2c(%rbp), %xmm1
movq -0x78(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movsd 0x10(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rax)
movq -0x78(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x48(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x78(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x4ff60
cmpl $0x0, -0x5c(%rbp)
jle 0x3de38
movq -0x78(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x58(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x78(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x3b500
jmp 0x3de6b
movq -0x78(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x78(%rbp), %rax
movl 0xc(%rax), %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x3b500
jmp 0x3de6d
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x3dd91
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x3def6
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x3dee9
movq -0x78(%rbp), %rax
movq (%rax), %rax
movslq -0x20(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movslq -0x24(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x78(%rbp), %rax
movq (%rax), %rax
movslq -0x24(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0x20(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3de95
jmp 0x3deeb
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x3de82
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x399d0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x14(%rbp), %eax
jl 0x3dcdf
movq -0x70(%rbp), %rdi
callq 0x386d0
movq -0x78(%rbp), %rdi
callq 0x386d0
movq -0x48(%rbp), %rdi
callq 0x52e80
movq -0x50(%rbp), %rdi
callq 0x52e80
cmpl $0x0, -0x5c(%rbp)
jle 0x3df52
movq -0x58(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %esi
callq 0x52e20
movsd -0x38(%rbp), %xmm0
addq $0xa0, %rsp
popq %rbp
retq