// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x68(%rdi), %rax
movsbl 0x900(%rax), %ecx
movq %rcx, 0x18(%rsp)
movsbl 0x901(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsi), %edi
callq 0x6a57
movq %rax, 0x28(%rbx)
movl 0xc(%rbx), %eax
movq 0x58(%r14), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
cmpq %rdx, %rax
je 0xd87b
movq 0xb8(%rbx), %rsi
movslq (%rsi,%rdx,4), %rsi
cmpb $0x0, (%rcx,%rsi)
jle 0xd876
movq 0x10(%rbx), %rsi
andq $0x0, (%rsi,%rdx,8)
incq %rdx
jmp 0xd857
movq 0x18(%rsp), %rcx
leal 0x1(%rcx), %eax
imull %ecx, %eax
movl %eax, 0x10(%rsp)
xorl %esi, %esi
testb %cl, %cl
movl $0x0, %ebp
cmovgl %ecx, %ebp
movl 0xc(%rsp), %eax
testb %al, %al
movl $0x0, %r13d
cmovgl %eax, %r13d
movq 0x68(%r14), %rdi
movl $0xff, %eax
andl %eax, %ebp
movl %ecx, %r15d
andl %eax, %r15d
andl %eax, %r13d
movl %ecx, %eax
movq %rax, 0x48(%rsp)
negq %r13
movslq 0x8f8(%rdi), %rax
cmpq %rax, %rsi
jge 0xdaeb
movq 0x880(%rdi), %rax
movzbl (%rax,%rsi), %r12d
movq %rsi, 0x20(%rsp)
leaq 0x50(%rsp), %rdx
leaq 0xb(%rsp), %rcx
callq 0x6a19
movsbl 0xb(%rsp), %eax
movq 0x58(%r14), %rdx
movq 0x50(%rsp), %r10
xorl %esi, %esi
testl %eax, %eax
movl $0x0, %edi
cmovgl %eax, %edi
xorl %ecx, %ecx
cmpq %rsi, %rdi
je 0xd92d
movslq (%r10,%rsi,4), %r8
xorl %r9d, %r9d
cmpb $0x0, (%rdx,%r8)
setg %r9b
addl %r9d, %ecx
incq %rsi
jmp 0xd910
movq %rdi, 0x38(%rsp)
movq 0x68(%r14), %rdi
cmpl %eax, %ecx
movq 0x20(%rsp), %rsi
movsd 0x300b(%rip), %xmm1     # 0x10950
jne 0xdae3
movl 0x38(%r14), %eax
movl %eax, 0x14(%rsp)
negq %r12
xorl %ecx, %ecx
movq %rdi, 0x30(%rsp)
movq %r10, 0x28(%rsp)
cmpq 0x38(%rsp), %rcx
je 0xdae3
movq 0x8b0(%rdi), %rax
movslq (%rax,%rsi,4), %rax
movslq %ecx, %r8
addq %rax, %r8
movq %rcx, 0x40(%rsp)
movl (%r10,%rcx,4), %r9d
imull 0x14(%rsp), %r9d
movq 0x98(%r14), %rdx
movq 0xb0(%r14), %rax
xorl %r10d, %r10d
cmpq %r10, %rbp
je 0xd9e4
cmpl $-0x1, (%rax,%r8,4)
jne 0xd9df
movq (%rdx,%r8,8), %rdi
movsd (%rdi,%r10,8), %xmm0
movq 0x10(%rbx), %rdi
leal (%r9,%r10), %r11d
movslq %r11d, %r11
addsd (%rdi,%r11,8), %xmm0
movsd %xmm0, (%rdi,%r11,8)
movq 0x28(%rbx), %rdi
movsd (%rdi,%r11,8), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rdi,%r11,8)
incq %r10
jmp 0xd99e
movq 0x18(%rsp), %rax
leal (%rax,%r9), %r10d
xorl %r11d, %r11d
cmpq %rbp, %r11
je 0xda53
movq 0x98(%r14), %rax
movq 0xa0(%r14), %rdx
xorl %edi, %edi
cmpq %rdi, %r15
je 0xda49
movq (%rdx,%r8,8), %rcx
movq (%rcx,%r11,8), %rcx
movsd (%rcx,%rdi,8), %xmm0
movq 0x10(%rbx), %rcx
leal (%r10,%rdi), %esi
movslq %esi, %rsi
addsd (%rcx,%rsi,8), %xmm0
movsd %xmm0, (%rcx,%rsi,8)
movq (%rax,%r8,8), %rcx
movsd (%rcx,%r11,8), %xmm0
movq 0x28(%rbx), %rcx
addsd (%rcx,%rsi,8), %xmm0
movsd %xmm0, (%rcx,%rsi,8)
incq %rdi
jmp 0xda05
incq %r11
addq 0x48(%rsp), %r10
jmp 0xd9f0
addl 0x10(%rsp), %r9d
xorl %r10d, %r10d
movl 0xc(%rsp), %edi
cmpq %rbp, %r10
je 0xdac7
movslq %r9d, %r11
shlq $0x3, %r11
movq 0x98(%r14), %rdx
xorl %eax, %eax
cmpq %rax, %r13
je 0xdabf
movapd %xmm1, %xmm0
cmpq %rax, %r12
je 0xda86
xorpd %xmm0, %xmm0
movq (%rdx,%r8,8), %rcx
mulsd (%rcx,%r10,8), %xmm0
movq 0x10(%rbx), %rsi
addsd (%rsi,%r11), %xmm0
movsd %xmm0, (%rsi,%r11)
movsd (%rcx,%r10,8), %xmm0
movq 0x28(%rbx), %rcx
addsd (%rcx,%r11), %xmm0
movsd %xmm0, (%rcx,%r11)
decq %rax
addq $0x8, %r11
jmp 0xda74
incq %r10
addl %edi, %r9d
jmp 0xda5f
movq 0x40(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %r10
jmp 0xd962
incq %rsi
jmp 0xd8c4
xorl %r15d, %r15d
movslq 0xc(%rbx), %rax
cmpq %rax, %r15
jge 0xdb34
movq 0x58(%r14), %rax
movq 0xb8(%rbx), %rcx
movslq (%rcx,%r15,4), %rcx
cmpb $0x0, (%rax,%rcx)
jle 0xdb2f
movq 0x28(%rbx), %rax
movsd (%rax,%r15,8), %xmm0
callq 0x55a1
movq 0x10(%rbx), %rax
movsd (%rax,%r15,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax,%r15,8)
incq %r15
jmp 0xdaee
movq 0x28(%rbx), %rdi
callq 0x3230
andq $0x0, 0x28(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop