// File: /workspace/github_c_makefile_star2_O3/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, %ebp
movq %rsi, %r15
movq %rdi, %r13
movslq 0x8(%rdi), %rdi
shlq $0x3, %rdi
leaq 0x7cb8b(%rip), %rbx     # 0xa8663
movq %rbx, %rsi
movl $0x9, %edx
callq 0x3a0e1
movq %rax, 0x8(%rsp)
movslq 0x8(%r13), %rdi
shlq $0x3, %rdi
movq %rbx, %rsi
movl $0xa, %edx
callq 0x3a0e1
movq %rax, %r12
movl 0xc(%r13), %esi
cmpl $0x4, %esi
movl %ebp, 0x10(%rsp)
je 0x2bb14
cmpl $0x2, %esi
jne 0x2bb56
movq (%r13), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %r14d
testl %r14d, %r14d
jle 0x2bb5d
movl 0x8(%r13), %edx
leaq 0x7cb33(%rip), %r8      # 0xa8663
movl $0x8, %edi
movl $0x8, %esi
movl %r14d, %ecx
movl $0xf, %r9d
callq 0x3a3ac
movq %rax, 0x20(%rsp)
movl 0xc(%r13), %esi
movb $0x1, %bpl
jmp 0x2bb5f
xorl %ebp, %ebp
xorl %r14d, %r14d
jmp 0x2bb5f
xorl %ebp, %ebp
movq 0x8(%rsp), %rbx
movl 0x8(%r13), %edi
movl 0x10(%r13), %ecx
movl %r14d, %edx
callq 0x280ea
movq %rax, 0x30(%rsp)
movl 0x8(%r13), %edi
movl 0xc(%r13), %esi
movl 0x10(%r13), %ecx
movl %r14d, %edx
callq 0x280ea
movq %rax, %r14
movl 0x10(%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 %ebp, 0x28(%rsp)
movq %r12, 0x38(%rsp)
movq %rax, 0x50(%rsp)
movq %r14, %rdi
callq 0x28a54
movl 0x8(%r13), %esi
movq %rbx, %rdi
callq 0x38389
movl 0x8(%r13), %esi
movq %r12, %rdi
callq 0x38389
xorpd %xmm0, %xmm0
testq %r15, %r15
je 0x2bc75
xorl %r13d, %r13d
movq %r15, %rbx
movq 0x8(%rsp), %r12
movq 0x20(%rsp), %rbp
movq 0x30(%rsp), %r15
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x28c08
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq 0x38(%rsp), %rcx
movq %rbp, %r8
callq 0x2a98f
movsd %xmm0, 0x18(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x28e64
movsd 0x10(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
incl %r13d
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x2bbff
movsd %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x48(%rsp), %r15
movq 0x38(%rsp), %r12
movl 0x28(%rsp), %ebp
jmp 0x2bc81
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movl 0x8(%r14), %esi
testl %esi, %esi
jle 0x2bcf0
xorl %ebx, %ebx
xorl %r13d, %r13d
movq (%r14), %rax
movsd 0x10(%rax,%rbx,4), %xmm0
divsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x10(%rax,%rbx,4)
movq 0x8(%rax,%rbx,4), %rdi
movq 0x8(%rsp), %rax
movsd (%rax,%rbx), %xmm0
callq 0x37f5a
testb %bpl, %bpl
je 0x2bcc7
movq 0x20(%rsp), %rax
movq (%rax,%rbx), %rsi
jmp 0x2bcc9
xorl %esi, %esi
movq (%r14), %rax
movq 0x18(%rax,%rbx,4), %rdi
movsd (%r12,%rbx), %xmm0
movl 0xc(%r14), %edx
callq 0x29e37
incq %r13
movslq 0x8(%r14), %rsi
addq $0x8, %rbx
cmpq %rsi, %r13
jl 0x2bc8e
movq 0x40(%rsp), %r13
movslq 0x8(%r13), %rax
testq %rax, %rax
movq 0x8(%rsp), %rbx
jle 0x2bd40
movq (%r14), %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 0x2bd1f
incq %rsi
cmpq %rax, %rsi
jne 0x2bd10
movq %r13, %rdi
movq %r14, %rsi
callq 0x28c08
movq 0x50(%rsp), %rax
incl %eax
cmpl 0x2c(%rsp), %eax
jne 0x2bbb8
movq 0x30(%rsp), %rdi
callq 0x27f8a
movq %r14, %rdi
callq 0x27f8a
movq %rbx, %rdi
callq 0x3a180
movq %r12, %rdi
callq 0x3a180
testb %bpl, %bpl
je 0x2bd91
movl 0x8(%r13), %esi
movq 0x20(%rsp), %rdi
callq 0x3a14b
movsd 0x10(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq