// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x68(%rax), %rcx
movb 0x900(%rcx), %cl
movb %cl, -0x21(%rbp)
movq 0x68(%rax), %rax
movb 0x901(%rax), %al
movb %al, -0x22(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %edi
callq 0x9da0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x17051
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xb8(%rcx), %rcx
movslq -0x20(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jle 0x17044
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x20(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x17046
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x17000
movl $0x0, -0x2c(%rbp)
movq -0x48(%rbp), %rcx
movl -0x2c(%rbp), %eax
movq 0x68(%rcx), %rcx
cmpl 0x8f8(%rcx), %eax
jge 0x17402
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rcx
movq 0x880(%rcx), %rcx
movslq -0x2c(%rbp), %rdx
movb (%rcx,%rdx), %cl
movb %cl, -0x26(%rbp)
movq 0x68(%rax), %rdi
movl -0x2c(%rbp), %esi
leaq -0x18(%rbp), %rdx
leaq -0x27(%rbp), %rcx
callq 0x9ce0
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movsbl -0x27(%rbp), %ecx
cmpl %ecx, %eax
jge 0x170e9
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x18(%rbp), %rcx
movslq -0x3c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x170aa
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %ecx
movsbl -0x27(%rbp), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x49(%rbp)
jge 0x1710f
movl -0x38(%rbp), %eax
movsbl -0x27(%rbp), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0x1711b
jmp 0x173f2
movq -0x48(%rbp), %rcx
movq -0x18(%rbp), %rax
movslq -0x20(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movl %eax, -0x1c(%rbp)
movl -0x20(%rbp), %eax
movq 0x68(%rcx), %rdx
movq 0x8b0(%rdx), %rdx
movslq -0x2c(%rbp), %rsi
addl (%rdx,%rsi,4), %eax
movl %eax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
imull 0x38(%rcx), %eax
movl %eax, -0x34(%rbp)
movb $0x0, -0x23(%rbp)
movsbl -0x23(%rbp), %eax
movsbl -0x21(%rbp), %ecx
cmpl %ecx, %eax
jge 0x171e8
movq -0x48(%rbp), %rax
movq 0xb0(%rax), %rax
movslq -0x30(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x171d9
movq -0x48(%rbp), %rax
movq 0x98(%rax), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x23(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x34(%rbp), %ecx
movsbl -0x23(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x34(%rbp), %ecx
movsbl -0x23(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movsd 0x4871(%rip), %xmm0     # 0x1ba40
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x171db
movb -0x23(%rbp), %al
addb $0x1, %al
movb %al, -0x23(%rbp)
jmp 0x17153
movq -0x48(%rbp), %rcx
movl -0x1c(%rbp), %eax
imull 0x38(%rcx), %eax
movsbl -0x21(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movb $0x0, -0x23(%rbp)
movsbl -0x23(%rbp), %eax
movsbl -0x21(%rbp), %ecx
cmpl %ecx, %eax
jge 0x172d5
movb $0x0, -0x24(%rbp)
movsbl -0x24(%rbp), %eax
movsbl -0x21(%rbp), %ecx
cmpl %ecx, %eax
jge 0x172c6
movq -0x48(%rbp), %rax
movq 0xa0(%rax), %rcx
movslq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0x23(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0x24(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x34(%rbp), %edx
movsbl -0x23(%rbp), %esi
movsbl -0x21(%rbp), %edi
imull %edi, %esi
addl %esi, %edx
movsbl -0x24(%rbp), %esi
addl %esi, %edx
movslq %edx, %rdx
addsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movq 0x98(%rax), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x23(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x34(%rbp), %ecx
movsbl -0x23(%rbp), %edx
movsbl -0x21(%rbp), %esi
imull %esi, %edx
addl %edx, %ecx
movsbl -0x24(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x24(%rbp), %al
addb $0x1, %al
movb %al, -0x24(%rbp)
jmp 0x17214
jmp 0x172c8
movb -0x23(%rbp), %al
addb $0x1, %al
movb %al, -0x23(%rbp)
jmp 0x17200
movq -0x48(%rbp), %rcx
movl -0x1c(%rbp), %eax
imull 0x38(%rcx), %eax
movsbl -0x21(%rbp), %ecx
addl $0x1, %ecx
movsbl -0x21(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movb $0x0, -0x23(%rbp)
movsbl -0x23(%rbp), %eax
movsbl -0x21(%rbp), %ecx
cmpl %ecx, %eax
jge 0x173e2
movb $0x0, -0x25(%rbp)
movsbl -0x25(%rbp), %eax
movsbl -0x22(%rbp), %ecx
cmpl %ecx, %eax
jge 0x173d3
movq -0x48(%rbp), %rax
movsbl -0x25(%rbp), %ecx
movsbl -0x26(%rbp), %edx
cmpl %edx, %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
cvtsi2sd %ecx, %xmm0
movq 0x98(%rax), %rcx
movslq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movsbq -0x23(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x34(%rbp), %edx
movsbl -0x23(%rbp), %esi
movsbl -0x22(%rbp), %edi
imull %edi, %esi
addl %esi, %edx
movsbl -0x25(%rbp), %esi
addl %esi, %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movq 0x98(%rax), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x23(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x34(%rbp), %ecx
movsbl -0x23(%rbp), %edx
movsbl -0x22(%rbp), %esi
imull %esi, %edx
addl %edx, %ecx
movsbl -0x25(%rbp), %edx
addl %edx, %ecx
movslq %ecx, %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x25(%rbp), %al
addb $0x1, %al
movb %al, -0x25(%rbp)
jmp 0x1730b
jmp 0x173d5
movb -0x23(%rbp), %al
addb $0x1, %al
movb %al, -0x23(%rbp)
jmp 0x172f7
jmp 0x173e4
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x170f0
jmp 0x173f4
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x17058
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x17479
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0xb8(%rcx), %rcx
movslq -0x20(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jle 0x1746c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x75f0
movaps %xmm0, %xmm1
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x20(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x1746e
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x17409
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x3320
movq -0x10(%rbp), %rax
movq $0x0, 0x28(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)