// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq 0x68(%rdi), %rcx
movsbl 0x900(%rcx), %eax
movq %rax, 0x10(%rsp)
movq %rcx, %r15
movsbq 0x901(%rcx), %rax
movq %rax, 0x30(%rsp)
movslq 0xc(%rsi), %r12
movl $0x8, %esi
movq %r12, %rdi
callq 0x2110
movq %rax, 0x28(%rbx)
testq %r12, %r12
jle 0xf1ab
movq 0x58(%r14), %rax
movq 0xb8(%rbx), %rcx
xorl %edx, %edx
movslq (%rcx,%rdx,4), %rsi
cmpb $0x0, (%rax,%rsi)
jle 0xf1a3
movq 0x10(%rbx), %rsi
movq $0x0, (%rsi,%rdx,8)
incq %rdx
cmpq %rdx, %r12
jne 0xf18d
cmpl $0x0, 0x8f8(%r15)
movq %r14, 0x28(%rsp)
jle 0xf464
movq %r15, %rdi
movq 0x10(%rsp), %rax
movzbl %al, %r15d
movq 0x30(%rsp), %rcx
movzbl %cl, %r12d
leal 0x1(%rax), %edx
imull %eax, %edx
movl %edx, 0x20(%rsp)
movl %eax, %r13d
leaq (,%rcx,8), %rax
movq %rax, 0x70(%rsp)
xorl %esi, %esi
movq 0x880(%rdi), %rax
movzbl (%rax,%rsi), %ebp
movq %rsi, 0x8(%rsp)
leaq 0x38(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x7797
movsbl 0x7(%rsp), %eax
testl %eax, %eax
jle 0xf444
movq 0x58(%r14), %rdx
movq 0x38(%rsp), %rsi
movl %eax, %edi
xorl %r8d, %r8d
xorl %ecx, %ecx
movslq (%rsi,%r8,4), %r9
xorl %r10d, %r10d
cmpb $0x0, (%rdx,%r9)
setg %r10b
addl %r10d, %ecx
incq %r8
cmpq %r8, %rdi
jne 0xf22b
movq 0x68(%r14), %rdi
testb %al, %al
movsd 0x36fc(%rip), %xmm1     # 0x12950
jle 0xf448
cmpl %eax, %ecx
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rsi
jne 0xf44d
movq 0x38(%rsp), %rcx
movq %rcx, 0x58(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x8b0(%rdi), %rcx
movslq (%rcx,%rsi,4), %rcx
movq %rcx, 0x50(%rsp)
movl 0x38(%r14), %ecx
movl %ecx, 0x24(%rsp)
movq 0xb0(%r14), %rcx
movq %rcx, 0x48(%rsp)
cltq
movq %rax, 0x68(%rsp)
movq $0x0, 0x18(%rsp)
testb %r8b, %r8b
jle 0xf425
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r9
movq 0x58(%rsp), %rax
movl (%rax,%rcx,4), %r10d
imull 0x24(%rsp), %r10d
movq 0x48(%rsp), %rax
movl (%rax,%r9,4), %eax
movq 0x98(%r14), %rcx
movslq %r10d, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
cmpl $-0x1, %eax
jne 0xf322
movq (%rcx,%r9,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, %r15
jne 0xf2ee
testb %r15b, %r15b
jle 0xf425
movq %r10, 0x60(%rsp)
leal (%r10,%r8), %esi
movq 0x98(%r14), %rdx
movq 0xa0(%r14), %rax
movq (%rax,%r9,8), %rax
movq 0x10(%rbx), %r11
movq 0x28(%rbx), %rcx
movq (%rdx,%r9,8), %rdx
movl %esi, %esi
xorl %edi, %edi
movq (%rax,%rdi,8), %r14
xorl %r10d, %r10d
movsd (%r14,%r10,8), %xmm0
leal (%rsi,%r10), %r8d
movslq %r8d, %r8
addsd (%r11,%r8,8), %xmm0
movsd %xmm0, (%r11,%r8,8)
movsd (%rdx,%rdi,8), %xmm0
addsd (%rcx,%r8,8), %xmm0
movsd %xmm0, (%rcx,%r8,8)
incq %r10
cmpq %r10, %r15
jne 0xf365
incq %rdi
addq %r13, %rsi
cmpq %r15, %rdi
jne 0xf35e
testb %r15b, %r15b
movq 0x28(%rsp), %r14
movq 0x10(%rsp), %r8
movq 0x60(%rsp), %rax
jle 0xf425
addl 0x20(%rsp), %eax
movq 0x98(%r14), %r11
movslq %eax, %r10
shlq $0x3, %r10
xorl %eax, %eax
cmpb $0x0, 0x30(%rsp)
jle 0xf418
movq (%r11,%r9,8), %rcx
movq 0x10(%rbx), %rdx
addq %r10, %rdx
movq 0x28(%rbx), %rsi
addq %r10, %rsi
xorl %edi, %edi
movapd %xmm1, %xmm0
cmpq %rdi, %rbp
je 0xf3f2
xorpd %xmm0, %xmm0
mulsd (%rcx,%rax,8), %xmm0
addsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rdx,%rdi,8)
movsd (%rcx,%rax,8), %xmm0
addsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %r12
jne 0xf3e5
incq %rax
addq 0x70(%rsp), %r10
cmpq %r15, %rax
jne 0xf3ca
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq 0x68(%rsp), %rcx
jb 0xf2af
movq 0x40(%rsp), %rdi
jmp 0xf448
movq 0x68(%r14), %rdi
movq 0x8(%rsp), %rsi
incq %rsi
movslq 0x8f8(%rdi), %rax
cmpq %rax, %rsi
jl 0xf1ef
movl 0xc(%rbx), %r12d
testl %r12d, %r12d
jle 0xf4b8
xorl %r14d, %r14d
movq 0x28(%rsp), %rax
movq 0x58(%rax), %rax
movq 0xb8(%rbx), %rcx
movslq (%rcx,%r14,4), %rcx
cmpb $0x0, (%rax,%rcx)
jle 0xf4ad
movq 0x28(%rbx), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x6158
movq 0x10(%rbx), %rax
movsd (%rax,%r14,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%r14,8)
movl 0xc(%rbx), %r12d
incq %r14
movslq %r12d, %rax
cmpq %rax, %r14
jl 0xf46c
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