// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblemSt.cpp
// Function: HMMProblemSt::doBaumWelchStep(FitBitSt*)
void HMMProblemSt::doBaumWelchStep(FitBitSt *fb) {
	NCAT *ar, k, l;
    NPAR nS = this->task->nS, nO = this->task->nO;
	NPAR i,j,m, o, n;
	NDAT t, tt, offset;
    
    // use PARAM for new parameter numerator value, use PARAMcopy for new parameter denominator value
    fb->PARAMcopy = init1D<NUMBER>(fb->nPARAM);
    //toZero1D(fb->PARAM, fb->nPARAM); // ONLY ZERO ACTIVE
    for(l=0; l<fb->nPARAM; l++)
        if( this->active_set_block[ fb->param_blocks[l] ]>0 )
            fb->PARAM[l] = 0;
    
    // compute numerators in PARAM and denominators in PARAMcopy
    for(t=0; t<this->task->N; t++) {
        o = this->task->dat_obs[t]; // observation y in the data is 1-right, 0-wrong; HMM-Scalable assumes 1-right, 2-wrong, with -1 taken out, so 0-right, 1-wrong
        // grab skill array (if exists)
        getSkillsAtRow(this->task, t, &ar, &n);

        NCAT n_active = 0;
        for(int l=0; l<n; l++) n_active+=(this->active_set_block[ar[l]]>0);
                
        for(l=0; l<n && n_active==n/**/; l++) {
            k = ar[l];
            tt = l + this->task->dat_skill_rix[t]; // tt – index into stacked skill array
            
            // pi
            offset = k*this->skill1_n;
            for(i=0; i<nS; i++) {
                if(this->backward_ix[tt]==-1) { // first row of skill's sequence.
                    fb->PARAM[offset+i] += this->gamma[tt][i]; // num
                    fb->PARAMcopy[offset+i]++ ;// den -- count first rows
                }
            }
            // A
            offset = k*this->skill1_n + nS;
            for(i=0; i<nS; i++) {
                for(j=0; j<nS; j++) {
                    fb->PARAM[offset+i*nS+j] += this->xi[tt][i][j];
                    fb->PARAMcopy[offset+i*nS+j] += this->gamma[tt][i];
                }
            }
            // B
            offset = k*this->skill1_n + (1+nS)*nS;
            for(i=0; i<nS; i++) {
                for(m=0; m<nO; m++) {
                    fb->PARAM[offset+i*nO+m] += (m==o) * this->gamma[tt][i];
                    fb->PARAMcopy[offset+i*nO+m] += this->gamma[tt][i];
                }
            }
        }// for all skills
        
    }// for all rows
    
    // divide numerators and denominators where needed
    for(l=0; l<fb->nPARAM; l++) { //
        if( this->active_set_block[ fb->param_blocks[l] ]>0 ) {
            fb->PARAM[l] /= safe0num( fb->PARAMcopy[l] );
        }
    }// all parameters

    // recycle
    free(fb->PARAMcopy);
    fb->PARAMcopy = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
movq 0x68(%rdi), %rax
movsbl 0x900(%rax), %ecx
movq %rcx, 0x10(%rsp)
movsbq 0x901(%rax), %rbp
movslq 0xc(%rsi), %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x2110
movq %rax, 0x28(%rbx)
testq %r15, %r15
jle 0xef88
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rax
movq 0xb8(%rbx), %rcx
xorl %edx, %edx
movslq (%rcx,%rdx,4), %rsi
cmpb $0x0, (%rax,%rsi)
jle 0xef80
movq 0x10(%rbx), %rsi
movq $0x0, (%rsi,%rdx,8)
incq %rdx
cmpq %rdx, %r15
jne 0xef6a
movq 0x8(%rsp), %rax
movq 0x68(%rax), %rdi
cmpl $0x0, 0x8f8(%rdi)
jle 0xf25b
movq 0x10(%rsp), %rax
movzbl %al, %r13d
movzbl %bpl, %r15d
leal 0x1(%rax), %ecx
imull %eax, %ecx
movl %ecx, 0x24(%rsp)
movl %eax, %eax
movq %rax, 0x70(%rsp)
leaq (,%rbp,8), %r14
xorl %esi, %esi
movq %rbp, 0x48(%rsp)
movq 0x880(%rdi), %rax
movzbl (%rax,%rsi), %r12d
movq %rsi, 0x30(%rsp)
leaq 0x38(%rsp), %rdx
leaq 0x3(%rsp), %rcx
callq 0x76ed
movsbl 0x3(%rsp), %eax
movl $0x0, %r9d
movl %eax, 0x4(%rsp)
testl %eax, %eax
jle 0xf02f
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rax
movq 0x38(%rsp), %rcx
movl 0x4(%rsp), %edx
xorl %esi, %esi
xorl %r9d, %r9d
movslq (%rcx,%rsi,4), %rdi
xorl %r8d, %r8d
cmpb $0x0, (%rax,%rdi)
setg %r8b
addl %r8d, %r9d
incq %rsi
cmpq %rsi, %rdx
jne 0xf015
cmpb $0x0, 0x4(%rsp)
movsd 0x2914(%rip), %xmm1     # 0x11950
jle 0xf23a
cmpl 0x4(%rsp), %r9d
jne 0xf23a
movq 0x38(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x8(%rsp), %rcx
movq 0x68(%rcx), %rax
movq 0x8b0(%rax), %rax
movq 0x30(%rsp), %rdx
movslq (%rax,%rdx,4), %rax
movq %rax, 0x58(%rsp)
movl 0x38(%rcx), %eax
movl %eax, 0x28(%rsp)
movq 0xb0(%rcx), %rax
movq %rax, 0x40(%rsp)
movslq 0x4(%rsp), %rax
movq %rax, 0x50(%rsp)
xorl %ecx, %ecx
movl %r9d, 0x2c(%rsp)
movq 0x58(%rsp), %rax
leaq (%rcx,%rax), %r11
movq 0x60(%rsp), %rax
movq %rcx, 0x68(%rsp)
movl (%rax,%rcx,4), %eax
imull 0x28(%rsp), %eax
movq %rax, 0x18(%rsp)
cmpb $0x0, 0x10(%rsp)
jle 0xf11c
movq 0x40(%rsp), %rax
movl (%rax,%r11,4), %eax
movq 0x8(%rsp), %rcx
movq 0x98(%rcx), %rcx
movslq 0x18(%rsp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
cmpl $-0x1, %eax
jne 0xf114
movq (%rcx,%r11,8), %rdi
movsd (%rdi,%rsi,8), %xmm0
movq 0x10(%rbx), %rdi
addq %rdx, %rdi
addsd (%rdi,%rsi,8), %xmm0
movsd %xmm0, (%rdi,%rsi,8)
movq 0x28(%rbx), %rdi
addq %rdx, %rdi
movsd (%rdi,%rsi,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
cmpq %rsi, %r13
jne 0xf0e0
testb %r13b, %r13b
jle 0xf197
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
addl %eax, %ecx
movq 0x8(%rsp), %rdx
movq 0x98(%rdx), %rax
movq 0xa0(%rdx), %rdx
movq (%rdx,%r11,8), %rdx
movq 0x10(%rbx), %rsi
movq 0x28(%rbx), %rdi
movq (%rax,%r11,8), %rax
movl %ecx, %r9d
xorl %ecx, %ecx
movq (%rdx,%rcx,8), %r10
xorl %r8d, %r8d
movsd (%r10,%r8,8), %xmm0
leal (%r9,%r8), %ebp
movslq %ebp, %rbp
addsd (%rsi,%rbp,8), %xmm0
movsd %xmm0, (%rsi,%rbp,8)
movsd (%rax,%rcx,8), %xmm0
addsd (%rdi,%rbp,8), %xmm0
movsd %xmm0, (%rdi,%rbp,8)
incq %r8
cmpq %r8, %r13
jne 0xf15c
incq %rcx
addq 0x70(%rsp), %r9
cmpq %r13, %rcx
jne 0xf155
testb %r13b, %r13b
movq 0x48(%rsp), %rbp
jle 0xf21b
movq 0x18(%rsp), %rcx
addl 0x24(%rsp), %ecx
movq 0x8(%rsp), %rax
movq 0x98(%rax), %rdx
movslq %ecx, %r8
shlq $0x3, %r8
xorl %esi, %esi
testb %bpl, %bpl
jle 0xf210
movq (%rdx,%r11,8), %rax
movq 0x10(%rbx), %rcx
addq %r8, %rcx
movq 0x28(%rbx), %rdi
addq %r8, %rdi
xorl %r9d, %r9d
movapd %xmm1, %xmm0
cmpq %r9, %r12
je 0xf1e6
xorpd %xmm0, %xmm0
mulsd (%rax,%rsi,8), %xmm0
addsd (%rcx,%r9,8), %xmm0
movsd %xmm0, (%rcx,%r9,8)
movsd (%rax,%rsi,8), %xmm0
addsd (%rdi,%r9,8), %xmm0
movsd %xmm0, (%rdi,%r9,8)
incq %r9
cmpq %r9, %r15
jne 0xf1d9
incq %rsi
addq %r14, %r8
cmpq %r13, %rsi
jne 0xf1bf
movq 0x68(%rsp), %rcx
incq %rcx
cmpq 0x50(%rsp), %rcx
movl 0x2c(%rsp), %r9d
jae 0xf23a
cmpl 0x4(%rsp), %r9d
je 0xf099
movq 0x30(%rsp), %rsi
incq %rsi
movq 0x8(%rsp), %rax
movq 0x68(%rax), %rdi
movslq 0x8f8(%rdi), %rax
cmpq %rax, %rsi
jl 0xefcb
cmpl $0x0, 0xc(%rbx)
jle 0xf2ad
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rax
movq 0xb8(%rbx), %rcx
movslq (%rcx,%r14,4), %rcx
cmpb $0x0, (%rax,%rcx)
jle 0xf2a1
movq 0x28(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x6046
movq 0x10(%rbx), %rax
movsd (%rax,%r14,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%r14,8)
incq %r14
movslq 0xc(%rbx), %rax
cmpq %rax, %r14
jl 0xf264
movq 0x28(%rbx), %rdi
callq 0x2240
movq $0x0, 0x28(%rbx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop