// File: /workspace/github_cpp_makefile_star2_O3/myudelson[P]hmm-scalable/HMMProblemSlicedA.cpp
// Function: HMMProblemSlicedA::doBaumWelchStep(FitBitSlicedA*)
NUMBER HMMProblemSlicedA::doBaumWelchStep(FitBitSlicedA *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 = init2D<NUMBER>((NDAT)nS, (NDAT)nO);
        b_B_den = init2D<NUMBER>((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++) {
                        b_B_num[i][m] += (m==o) * x_data[x]->gamma[t][i];
                        b_B_den[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(z=0; z<nZ; z++) {
                for(j=0; j<nS; j++) {
                    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++) {
                fb->B[i][m] = b_B_num[i][m] / safe0num(b_B_den[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) projectsimplex(fb->B[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) projectsimplexbounded(fb->B[i], this->getLbB()[i], this->getUbB()[i], nS);
        }
    }
    // compute LL
    computeAlphaAndPOParam(fb->xndat, fb->x_data);
    ll = HMMProblemSlicedA::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) free2D<NUMBER>(b_B_num, nS);
	if(b_B_den != NULL) free2D<NUMBER>(b_B_den, 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 $0xa8, %rsp
movq %rsi, %r15
movq %rdi, %rbp
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %rbx
movsbl 0x8b9(%rax), %r14d
movzbl 0x8c8(%rax), %eax
movq %rax, 0x30(%rsp)
movsbl %al, %eax
movl %eax, 0xc(%rsp)
movl 0xd0(%rsi), %r12d
movq 0xd8(%rsi), %r13
movl %r12d, %esi
movq %r13, %rdx
callq 0x2bba0
movq %rbp, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x2beee
movq %rbp, 0x28(%rsp)
movq %rbp, %rdi
movq %r12, 0x60(%rsp)
movl %r12d, %esi
movq %r13, 0x90(%rsp)
movq %r13, %rdx
callq 0x2c0d0
cmpq $0x0, 0x10(%r15)
movq %r15, (%rsp)
je 0x2f4ac
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4130
movq (%rsp), %r15
jmp 0x2f4ae
xorl %eax, %eax
movq %rax, 0x58(%rsp)
movzbl %bl, %eax
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x18(%r15)
movq %rbx, 0x10(%rsp)
je 0x2f4ed
movl 0xc(%rsp), %ebp
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xcaae
movq %rax, %r13
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xcaae
movq (%rsp), %r15
movq %rax, %rbp
jmp 0x2f4f2
xorl %r13d, %r13d
xorl %ebp, %ebp
movzbl %r14b, %r12d
cmpq $0x0, 0x20(%r15)
movl %r14d, 0x74(%rsp)
je 0x2f527
movq 0x10(%rsp), %rbx
movl %ebx, %edi
movl %r14d, %esi
callq 0xbb36
movq %rax, 0x40(%rsp)
movl %ebx, %edi
movl %r14d, %esi
callq 0xbb36
movq %rax, 0x48(%rsp)
jmp 0x2f539
movq $0x0, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq 0x20(%rsp), %r11
cmpl $0x0, 0x60(%rsp)
jle 0x2f756
cvtsi2sdl 0x60(%rsp), %xmm0
movq 0x28(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x50(%rsp)
xorl %ecx, %ecx
movsd 0x4386(%rip), %xmm1     # 0x338f0
movq 0x90(%rsp), %rax
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %rdx
cmpl $0x0, 0x4(%rdx)
jne 0x2f743
movq (%rsp), %rax
cmpq $0x0, 0x10(%rax)
movq 0x58(%rsp), %rcx
je 0x2f5be
testb %r11b, %r11b
jle 0x2f5be
movq 0x38(%rdx), %rax
movq (%rax), %rax
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm2
divsd %xmm0, %xmm2
addsd (%rcx,%rsi,8), %xmm2
movsd %xmm2, (%rcx,%rsi,8)
incq %rsi
cmpq %rsi, %r11
jne 0x2f5a3
movl (%rdx), %eax
movq %rax, 0x68(%rsp)
cmpl $0x2, %eax
jl 0x2f743
movq 0x68(%rsp), %rax
decl %eax
movq %rax, 0x68(%rsp)
movq 0x50(%rsp), %rax
movq 0x870(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x98(%rsp)
xorl %r9d, %r9d
testb %r11b, %r11b
jle 0x2f735
testb %r12b, %r12b
setle %al
movq 0x98(%rsp), %rcx
movslq (%rcx,%r9,4), %rsi
movq 0xa0(%rsp), %rcx
movzbl (%rcx,%rsi), %r10d
movq (%rsp), %rcx
cmpq $0x0, 0x20(%rcx)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rsp)
sete %bl
orb %al, %bl
xorl %r15d, %r15d
cmpq $0x0, 0x18(%rsp)
je 0x2f6d2
movq 0x50(%rsp), %rax
movq 0x8a8(%rax), %rdi
movq (%rsp), %rax
movq 0xd8(%rax), %rax
movq 0x38(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movslq (%rax,%r9,4), %r8
movq 0x38(%rdx), %rsi
movq 0x40(%rdx), %rax
movq (%rax,%r9,8), %rax
movq (%rax,%r15,8), %rax
movq (%rsi,%r9,8), %rsi
xorl %r14d, %r14d
movsbq (%rdi,%r8), %rcx
movsd (%rax,%r14,8), %xmm2
movq (%r13,%rcx,8), %r11
movq (%r11,%r15,8), %r11
addsd (%r11,%r14,8), %xmm2
movsd %xmm2, (%r11,%r14,8)
movq 0x20(%rsp), %r11
movsd (%rsi,%r15,8), %xmm2
movq (%rbp,%rcx,8), %rcx
movq (%rcx,%r15,8), %rcx
addsd (%rcx,%r14,8), %xmm2
movsd %xmm2, (%rcx,%r14,8)
incq %r14
cmpq %r14, %r11
jne 0x2f68a
testb %bl, %bl
jne 0x2f729
movq 0x38(%rdx), %rax
movq (%rax,%r9,8), %rdi
movq 0x40(%rsp), %rax
movq (%rax,%r15,8), %r8
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rsi
xorl %eax, %eax
movapd %xmm1, %xmm2
cmpq %rax, %r10
je 0x2f6ff
xorpd %xmm2, %xmm2
mulsd (%rdi,%r15,8), %xmm2
addsd (%r8,%rax,8), %xmm2
movsd %xmm2, (%r8,%rax,8)
movsd (%rdi,%r15,8), %xmm2
addsd (%rsi,%rax,8), %xmm2
movsd %xmm2, (%rsi,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x2f6f2
incq %r15
cmpq %r11, %r15
jne 0x2f63f
incq %r9
cmpq 0x68(%rsp), %r9
jne 0x2f5fd
movq 0x38(%rsp), %rcx
incq %rcx
cmpq 0x60(%rsp), %rcx
jne 0x2f56a
testb %r11b, %r11b
movq (%rsp), %rcx
jle 0x2f8ac
movslq 0x10(%rsp), %rax
movzbl 0x74(%rsp), %edx
movq %rdx, 0x50(%rsp)
xorl %r15d, %r15d
movq %rax, 0x38(%rsp)
movq %r13, 0x80(%rsp)
movq %rbp, 0x78(%rsp)
movq %r12, 0x88(%rsp)
movq 0x10(%rcx), %rax
testq %rax, %rax
je 0x2f7a9
movq 0x58(%rsp), %rdx
movsd (%rdx,%r15,8), %xmm0
movsd %xmm0, (%rax,%r15,8)
cmpq $0x0, 0x18(%rcx)
je 0x2f828
cmpb $0x0, 0x30(%rsp)
jle 0x2f828
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x38(%rsp), %r13
movq 0x80(%rsp), %rbp
movq 0x78(%rsp), %r12
movq (%rbp,%rbx,8), %rax
movq (%rax,%r15,8), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq (%r12,%rbx,8), %rax
movq (%rax,%r15,8), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x8df8
movq (%rsp), %rcx
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x18(%rcx), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r15,8), %rax
movsd %xmm1, (%rax,%r14,8)
incq %r14
cmpq %r14, %r13
jne 0x2f7ce
incq %rbx
cmpq 0x30(%rsp), %rbx
jne 0x2f7b9
cmpq $0x0, 0x20(%rcx)
movq 0x48(%rsp), %rbp
movq 0x40(%rsp), %r14
movq 0x88(%rsp), %r12
movq 0x50(%rsp), %r13
je 0x2f88e
testb %r12b, %r12b
jle 0x2f88e
xorl %ebx, %ebx
movq (%r14,%r15,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq (%rbp,%r15,8), %rax
movsd (%rax,%rbx,8), %xmm0
callq 0x8df8
movq (%rsp), %rcx
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x20(%rcx), %rax
movq (%rax,%r15,8), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %rbx
cmpq %rbx, %r13
jne 0x2f84d
incq %r15
movq 0x38(%rsp), %rax
cmpq %rax, %r15
movq 0x80(%rsp), %r13
movq 0x78(%rsp), %rbp
jne 0x2f78f
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x1133a
movq (%rsp), %rcx
movq 0x10(%rcx), %r15
testb %al, %al
je 0x2f9d0
testq %r15, %r15
movq 0x10(%rsp), %rbx
je 0x2f8fa
movq %r14, %rdi
callq 0x113a8
movq %rax, %r12
movq %r14, %rdi
callq 0x113de
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
movl %ebx, %ecx
callq 0x7997
movq (%rsp), %rcx
testb %bl, %bl
jle 0x2fa5a
movzbl 0xc(%rsp), %eax
movq %rax, 0x18(%rsp)
xorl %edx, %edx
xorl %r12d, %r12d
cmpq $0x0, 0x18(%rcx)
movq 0x10(%rsp), %r14
movq 0x28(%rsp), %r15
je 0x2f97d
cmpb $0x0, 0xc(%rsp)
jle 0x2f97d
xorl %ebx, %ebx
movq 0x18(%rcx), %rax
cmpb $0x0, 0x80(%r15)
movq (%rax,%rbx,8), %rax
movq 0x98(%r15), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r12,8), %rsi
movq 0xa0(%r15), %rax
cmoveq %rdx, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rdx
movl %r14d, %ecx
callq 0x7997
xorl %edx, %edx
movq (%rsp), %rcx
incq %rbx
cmpq %rbx, 0x18(%rsp)
jne 0x2f92b
movq 0x20(%rcx), %rax
testq %rax, %rax
je 0x2f9bd
movq (%rax,%r12,8), %r14
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x113cc
movq (%rax,%r12,8), %r15
movq %rbx, %rdi
callq 0x11402
movq (%rax,%r12,8), %rdx
movsbl 0x20(%rsp), %ecx
movq %r14, %rdi
movq %r15, %rsi
callq 0x7997
xorl %edx, %edx
movq (%rsp), %rcx
incq %r12
cmpq 0x10(%rsp), %r12
jne 0x2f911
jmp 0x2fa5a
testq %r15, %r15
movq 0x10(%rsp), %rbx
je 0x2f9eb
movsbl 0x20(%rsp), %esi
movq %r15, %rdi
callq 0x6836
movq (%rsp), %rcx
testb %bl, %bl
movq 0x30(%rsp), %rdx
jle 0x2fa5a
movslq 0x10(%rsp), %rbx
xorl %r14d, %r14d
movq 0x10(%rsp), %r12
cmpq $0x0, 0x18(%rcx)
je 0x2fa34
testb %dl, %dl
jle 0x2fa34
xorl %r15d, %r15d
movq 0x18(%rcx), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%r14,8), %rdi
movl %r12d, %esi
callq 0x6836
movq 0x30(%rsp), %rdx
movq (%rsp), %rcx
incq %r15
cmpq %r15, %rdx
jne 0x2fa0f
movq 0x20(%rcx), %rax
testq %rax, %rax
je 0x2fa52
movq (%rax,%r14,8), %rdi
movl %r12d, %esi
callq 0x6836
movq 0x30(%rsp), %rdx
movq (%rsp), %rcx
incq %r14
cmpq %rbx, %r14
jne 0x2fa01
movq %rcx, %r14
movl 0xd0(%rcx), %esi
movq 0xd8(%rcx), %rdx
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x2bba0
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq %rbx, %rdi
callq 0x114ae
movsd %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x2faa2
callq 0x42a0
testq %r13, %r13
movq 0x10(%rsp), %rbx
je 0x2faba
movq %r13, %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
testq %rbp, %rbp
je 0x2facd
movq %rbp, %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2fade
movl %ebx, %esi
callq 0xa5dd
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x2faef
movl %ebx, %esi
callq 0xa5dd
movsd 0x20(%rsp), %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop