// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::doBaumWelchStep(FitBitSlicedAB*)
NUMBER HMMProblemSlicedAB::doBaumWelchStep(FitBitSlicedAB *fb) {
	NCAT x;
    NPAR nS = this->p->nS, nO = this->p->nO, nZ = this->p->nZ;
	NPAR i,j,m, o, z;
	NDAT t;
    NUMBER ll;
    
    NCAT xndat = fb->xndat;
    struct data **x_data = fb->x_data;
    computeAlphaAndPOParam(xndat, x_data);
	computeBeta(xndat, x_data);
	computeXiGamma(xndat, x_data);
	

    NUMBER * b_PI = NULL;
	NUMBER *** b_A_num = NULL;
	NUMBER *** b_A_den = NULL;
	NUMBER *** b_B_num = NULL;
	NUMBER *** b_B_den = NULL;
//    NUMBER *** c_A     = NULL;  // A,B average across sequences
//    NUMBER *** c_B     = NULL;  // A,B average across sequences
    if(fb->pi != NULL)
        b_PI = init1D<NUMBER>((NDAT)nS);
    if(fb->A != NULL) {
        b_A_num = init3D<NUMBER>((NDAT)nZ, (NDAT)nS, (NDAT)nS);
        b_A_den = init3D<NUMBER>((NDAT)nZ, (NDAT)nS, (NDAT)nS);
//        c_A     = init3D<NUMBER>((NDAT)nZ, (NDAT)nS, (NDAT)nS);  // A,B average across sequences
    }
    if(fb->B != NULL) {
        b_B_num = init3D<NUMBER>((NDAT)nZ, (NDAT)nS, (NDAT)nO);
        b_B_den = init3D<NUMBER>((NDAT)nZ, (NDAT)nS, (NDAT)nO);
//        c_B     = init3D<NUMBER>((NDAT)nZ, (NDAT)nS, (NDAT)nO);  // A,B average across sequences
    }

    // compute sums PI

	for(x=0; x<xndat; x++) {
        if( x_data[x]->cnt!=0 ) continue;
        if(fb->pi != NULL) {
            for(i=0; i<nS; i++) {
                b_PI[i] += x_data[x]->gamma[0][i] / xndat;
            }
        }
		
		for(t=0;t<(x_data[x]->n-1);t++) {
            //			o = x_data[x]->obs[t];
            o = this->p->dat_obs[ x_data[x]->ix[t] ];//->get( x_data[x]->ix[t] );
			for(i=0; i<nS; i++) {
                if(fb->A != NULL) {
                    for(j=0; j<nS; j++){
                        z = this->p->dat_slice[ fb->x_data[x]->ix[t] ];
                        b_A_num[z][i][j] += x_data[x]->xi[t][i][j];
                        b_A_den[z][i][j] += x_data[x]->gamma[t][i];
                    }
                }
                if(fb->B != NULL) {
                    for(m=0; m<nO; m++) {
                        z = this->p->dat_slice[ fb->x_data[x]->ix[t] ];
                        b_B_num[z][i][m] += (m==o) * x_data[x]->gamma[t][i];
                        b_B_den[z][i][m] += x_data[x]->gamma[t][i];
                    }
                }
			}
		}
	} // for all groups within a skill
	// set params
	for(i=0; i<nS; i++) {
        if(fb->pi != NULL) fb->pi[i] = b_PI[i];
        if(fb->A != NULL) {
            for(j=0; j<nS; j++) {
                for(z=0; z<nZ; z++) {
                    fb->A[z][i][j] = b_A_num[z][i][j] / safe0num(b_A_den[z][i][j]);
                }
            }
        }
        if(fb->B != NULL) {
            for(m=0; m<nO; m++) {
                for(z=0; z<nZ; z++) {
                    fb->B[z][i][m] = b_B_num[z][i][m] / safe0num(b_B_den[z][i][m]);
                }
            }
        }
	}
    // scale
    if( !this->hasNon01Constraints() ) {
        if(fb->pi != NULL) projectsimplex(fb->pi, nS);
        for(i=0; i<nS; i++) {
            if(fb->A != NULL) {
                for(z=0; z<nZ; z++) {
                    projectsimplex(fb->A[z][i], nS);
                }
            }
            if(fb->B != NULL) {
                for(z=0; z<nZ; z++) {
                    projectsimplex(fb->B[z][i], nS);
                }
            }
        }
    } else {
        if(fb->pi != NULL) projectsimplexbounded(fb->pi, this->getLbPI(), this->getUbPI(), nS);
        for(i=0; i<nS; i++) {
            if(fb->A != NULL) {
                for(z=0; z<nZ; z++) {
                    projectsimplexbounded(fb->A[z][i], this->getLbA()[z][i], this->getUbA()[z][i], nS); // tag is currently fit slice
                }
            }
            if(fb->B != NULL) {
                for(z=0; z<nZ; z++) {
                    projectsimplexbounded(fb->B[z][i], this->getLbB()[z][i], this->getUbB()[z][i], nS);
                }
            }
        }
    }
    // compute LL
    computeAlphaAndPOParam(fb->xndat, fb->x_data);
    ll = HMMProblemSlicedAB::getSumLogPOPara(fb->xndat, fb->x_data);
    // free mem
    //    RecycleFitData(xndat, x_data, this->p);
	if(b_PI    != NULL) free(b_PI);
	if(b_A_num != NULL) free3D<NUMBER>(b_A_num, nZ, nS);
	if(b_A_den != NULL) free3D<NUMBER>(b_A_den, nZ, nS);
	if(b_B_num != NULL) free3D<NUMBER>(b_B_num, nZ, nS);
	if(b_B_den != NULL) free3D<NUMBER>(b_B_den, nZ, nS);
//    if(c_A     != NULL) free3D<NUMBER>(c_A, nZ, nS); // A,B average across sequences
//    if(c_B     != NULL) free3D<NUMBER>(c_B, nZ, nS); // A,B average across sequences
    return ll;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movq %rdi, %r12
movq 0x88(%rdi), %rax
movsbl 0x8b8(%rax), %ebx
movsbl 0x8b9(%rax), %r14d
movsbl 0x8c8(%rax), %eax
movl %eax, 0x1c(%rsp)
movl 0xd0(%rsi), %r13d
movq 0xd8(%rsi), %rbp
movl %r13d, %esi
movq %rbp, %rdx
callq 0x2012a
movq %r12, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x20414
movq %r12, %rdi
movq %r13, 0x10(%rsp)
movl %r13d, %esi
movq %rbp, 0x30(%rsp)
movq %rbp, %rdx
callq 0x2060a
cmpq $0x0, 0x10(%r15)
je 0x2354f
movl %ebx, %edi
callq 0x7dde
movq %rax, 0x38(%rsp)
jmp 0x23558
movq $0x0, 0x38(%rsp)
cmpq $0x0, 0x18(%r15)
movl %ebx, 0xc(%rsp)
movl 0x1c(%rsp), %ebp
movq %r12, 0x28(%rsp)
je 0x2358e
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xa0be
movq %rax, 0x50(%rsp)
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xa0be
movq %rax, 0x48(%rsp)
jmp 0x235a0
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
xorl %r13d, %r13d
cmpq $0x0, 0x20(%r15)
je 0x235d7
movl 0x1c(%rsp), %r12d
movl %r12d, %edi
movl 0xc(%rsp), %ebp
movl %ebp, %esi
movl %r14d, %edx
callq 0xa0be
movq %rax, %rbx
movl %r12d, %edi
movl %ebp, %esi
movl %r14d, %edx
callq 0xa0be
movq %rax, 0x40(%rsp)
jmp 0x235e6
xorl %ebx, %ebx
movq $0x0, 0x40(%rsp)
movl 0xc(%rsp), %ebp
movq 0x10(%rsp), %rcx
cvtsi2sd %ecx, %xmm0
testb %bpl, %bpl
movl $0x0, %eax
cmovgl %ebp, %eax
testb %r14b, %r14b
cmovlel %r13d, %r14d
testl %ecx, %ecx
cmovlel %r13d, %ecx
movq %rcx, 0x10(%rsp)
movl $0xff, %ecx
andl %ecx, %eax
movq %rax, 0x20(%rsp)
movl %ebp, %eax
andl %ecx, %eax
andl %ecx, %r14d
movsd 0x92c9(%rip), %xmm1     # 0x2c8f0
cmpq 0x10(%rsp), %r13
je 0x237ed
movq 0x30(%rsp), %rcx
movq (%rcx,%r13,8), %rdx
cmpl $0x0, 0x4(%rdx)
je 0x23646
incq %r13
jmp 0x23627
cmpq $0x0, 0x10(%r15)
je 0x2367a
xorl %esi, %esi
cmpq %rsi, 0x20(%rsp)
je 0x2367a
movq 0x38(%rdx), %rdi
movq (%rdi), %rdi
movsd (%rdi,%rsi,8), %xmm2
divsd %xmm0, %xmm2
movq 0x38(%rsp), %rcx
addsd (%rcx,%rsi,8), %xmm2
movsd %xmm2, (%rcx,%rsi,8)
incq %rsi
jmp 0x2364f
movl (%rdx), %esi
movq 0x28(%rsp), %rcx
movq 0x88(%rcx), %rdi
cmpl $0x2, %esi
pushq $0x1
popq %rcx
cmovll %ecx, %esi
decl %esi
xorl %r8d, %r8d
cmpq %rsi, %r8
je 0x23641
movq 0x870(%rdi), %r9
movq 0x10(%rdx), %r10
movslq (%r10,%r8,4), %r10
movzbl (%r9,%r10), %r9d
xorl %r10d, %r10d
cmpq 0x20(%rsp), %r10
je 0x237e5
cmpq $0x0, 0x18(%r15)
je 0x23757
xorl %r11d, %r11d
cmpq %r11, %rax
je 0x23757
movq 0x8a8(%rdi), %r12
movq 0xd8(%r15), %rbp
movq (%rbp,%r13,8), %rbp
movq 0x10(%rbp), %rbp
movslq (%rbp,%r8,4), %rbp
movsbq (%r12,%rbp), %r12
movq 0x40(%rdx), %rbp
movq (%rbp,%r8,8), %rbp
movq (%rbp,%r10,8), %rbp
movsd (%rbp,%r11,8), %xmm2
movq 0x50(%rsp), %rcx
movq (%rcx,%r12,8), %rbp
movq (%rbp,%r10,8), %rbp
addsd (%rbp,%r11,8), %xmm2
movsd %xmm2, (%rbp,%r11,8)
movq 0x38(%rdx), %rbp
movq (%rbp,%r8,8), %rbp
movsd (%rbp,%r10,8), %xmm2
movq 0x48(%rsp), %rcx
movq (%rcx,%r12,8), %r12
movq (%r12,%r10,8), %r12
addsd (%r12,%r11,8), %xmm2
movsd %xmm2, (%r12,%r11,8)
incq %r11
jmp 0x236cb
cmpq $0x0, 0x20(%r15)
je 0x237dd
xorl %r11d, %r11d
cmpq %r11, %r14
je 0x237dd
movapd %xmm1, %xmm2
cmpq %r11, %r9
je 0x23773
xorpd %xmm2, %xmm2
movq 0x8a8(%rdi), %r12
movq 0xd8(%r15), %rbp
movq (%rbp,%r13,8), %rbp
movq 0x10(%rbp), %rbp
movslq (%rbp,%r8,4), %rbp
movsbq (%r12,%rbp), %r12
movq 0x38(%rdx), %rbp
movq (%rbp,%r8,8), %rbp
movq (%rbx,%r12,8), %rcx
movq (%rcx,%r10,8), %rcx
mulsd (%rbp,%r10,8), %xmm2
addsd (%rcx,%r11,8), %xmm2
movsd %xmm2, (%rcx,%r11,8)
movsd (%rbp,%r10,8), %xmm2
movq 0x40(%rsp), %rcx
movq (%rcx,%r12,8), %rcx
movq (%rcx,%r10,8), %rcx
addsd (%rcx,%r11,8), %xmm2
movsd %xmm2, (%rcx,%r11,8)
incq %r11
jmp 0x23761
incq %r10
jmp 0x236b2
incq %r8
jmp 0x23696
xorl %r13d, %r13d
movl 0x1c(%rsp), %ecx
testb %cl, %cl
movl $0x0, %eax
cmovgl %ecx, %eax
movzbl %al, %eax
movq %rax, 0x10(%rsp)
cmpq 0x20(%rsp), %r13
je 0x23908
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x2382b
movq 0x38(%rsp), %rcx
movsd (%rcx,%r13,8), %xmm0
movsd %xmm0, (%rax,%r13,8)
cmpq $0x0, 0x18(%r15)
je 0x23899
xorl %ebp, %ebp
cmpq 0x20(%rsp), %rbp
je 0x23899
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x23894
movq 0x50(%rsp), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x48(%rsp), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm0
callq 0x67cb
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x18(%r15), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd %xmm1, (%rax,%rbp,8)
incq %r12
jmp 0x2383e
incq %rbp
jmp 0x23834
cmpq $0x0, 0x20(%r15)
je 0x23900
xorl %ebp, %ebp
cmpq %r14, %rbp
je 0x23900
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x238fb
movq (%rbx,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x40(%rsp), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%rbp,8), %xmm0
callq 0x67cb
movsd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x20(%r15), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd %xmm1, (%rax,%rbp,8)
incq %r12
jmp 0x238aa
incq %rbp
jmp 0x238a2
incq %r13
jmp 0x23806
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0xe1de
movq 0x10(%r15), %rdi
testb %al, %al
je 0x23948
testq %rdi, %rdi
movl 0xc(%rsp), %ecx
je 0x239c8
cmpb $0x0, 0x80(%r14)
je 0x239bf
movq 0xa0(%r14), %rsi
movq 0xb8(%r14), %rdx
jmp 0x239c3
testq %rdi, %rdi
movl 0xc(%rsp), %esi
je 0x23956
callq 0x5600
xorl %r14d, %r14d
movl 0xc(%rsp), %ebp
cmpq 0x20(%rsp), %r14
je 0x23a99
cmpq $0x0, 0x18(%r15)
je 0x23991
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x23991
movq 0x18(%r15), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
movl %ebp, %esi
callq 0x5600
incq %r12
jmp 0x23972
cmpq $0x0, 0x20(%r15)
je 0x239ba
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x239ba
movq 0x20(%r15), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r14,8), %rdi
movl %ebp, %esi
callq 0x5600
incq %r12
jmp 0x2399b
incq %r14
jmp 0x2395d
xorl %esi, %esi
xorl %edx, %edx
callq 0x5d80
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq 0x20(%rsp), %r13
je 0x23a99
cmpq $0x0, 0x18(%r15)
je 0x23a35
xorl %ebp, %ebp
cmpq %rbp, 0x10(%rsp)
je 0x23a35
movq 0x18(%r15), %rax
movq 0x28(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%rbp,8), %rax
movq 0xa8(%rdx), %rcx
cmoveq %r14, %rcx
movq (%rax,%r13,8), %rdi
movq (%rcx,%rbp,8), %rax
movq (%rax,%r13,8), %rsi
movq 0xc0(%rdx), %rax
cmoveq %r14, %rax
movq (%rax,%rbp,8), %rax
movq (%rax,%r13,8), %rdx
movl 0xc(%rsp), %ecx
callq 0x5d80
incq %rbp
jmp 0x239e2
cmpq $0x0, 0x20(%r15)
je 0x23a91
xorl %ebp, %ebp
cmpq %rbp, 0x10(%rsp)
je 0x23a91
movq 0x20(%r15), %rax
movq 0x28(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%rbp,8), %rax
movq 0xb0(%rdx), %rcx
cmoveq %r14, %rcx
movq (%rax,%r13,8), %rdi
movq (%rcx,%rbp,8), %rax
movq (%rax,%r13,8), %rsi
movq 0xc8(%rdx), %rax
cmoveq %r14, %rax
movq (%rax,%rbp,8), %rax
movq (%rax,%r13,8), %rdx
movl 0xc(%rsp), %ecx
callq 0x5d80
incq %rbp
jmp 0x23a3e
incq %r13
jmp 0x239ce
movl 0xd0(%r15), %esi
movq 0xd8(%r15), %rdx
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x2012a
movl 0xd0(%r15), %esi
movq 0xd8(%r15), %rdx
movq %r14, %rdi
callq 0xe400
movsd %xmm0, 0x10(%rsp)
movq 0x38(%rsp), %rdi
callq 0x4290
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
movl 0xc(%rsp), %ebp
movl 0x1c(%rsp), %r14d
je 0x23af7
movl %r14d, %esi
movl %ebp, %edx
callq 0xa064
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x23b0b
movl %r14d, %esi
movl %ebp, %edx
callq 0xa064
testq %rbx, %rbx
je 0x23b1d
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0xa064
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x23b31
movl %r14d, %esi
movl %ebp, %edx
callq 0xa064
movsd 0x10(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq