// File: /workspace/github_cpp_makefile_star2_O2/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 $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 0x25848
movq %r12, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x25b32
movq %r12, 0x28(%rsp)
movq %r12, %rdi
movq %r13, 0x20(%rsp)
movl %r13d, %esi
movq %rbp, 0x10(%rsp)
movq %rbp, %rdx
callq 0x25d20
cmpq $0x0, 0x10(%r15)
je 0x28a1e
movl %ebx, %edi
callq 0x7dde
movq %rax, 0x38(%rsp)
jmp 0x28a27
movq $0x0, 0x38(%rsp)
cmpq $0x0, 0x18(%r15)
movl %ebx, 0xc(%rsp)
je 0x28a59
movl 0x1c(%rsp), %r12d
movl %r12d, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xa0be
movq %rax, %rbp
movl %r12d, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xa0be
movq %rax, 0x50(%rsp)
jmp 0x28a64
xorl %ebp, %ebp
movq $0x0, 0x50(%rsp)
xorl %r12d, %r12d
cmpq $0x0, 0x20(%r15)
je 0x28a92
movl 0xc(%rsp), %ebx
movl %ebx, %edi
movl %r14d, %esi
callq 0x9328
movq %rax, 0x40(%rsp)
movl %ebx, %edi
movl %r14d, %esi
callq 0x9328
movq %rax, 0x48(%rsp)
jmp 0x28aa8
movq $0x0, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movl 0xc(%rsp), %ebx
movq 0x20(%rsp), %rcx
cvtsi2sd %ecx, %xmm0
testb %bl, %bl
movl $0x0, %eax
cmovgl %ebx, %eax
testb %r14b, %r14b
cmovlel %r12d, %r14d
testl %ecx, %ecx
cmovlel %r12d, %ecx
movl $0xff, %edx
andl %edx, %eax
movq %rax, 0x20(%rsp)
movl %ebx, %eax
andl %edx, %eax
andl %edx, %r14d
movsd 0x3e0d(%rip), %xmm1     # 0x2c8f0
cmpq %rcx, %r12
je 0x28c82
movq 0x10(%rsp), %rdx
movq (%rdx,%r12,8), %rdx
cmpl $0x0, 0x4(%rdx)
je 0x28b00
incq %r12
jmp 0x28ae3
cmpq $0x0, 0x10(%r15)
je 0x28b34
xorl %esi, %esi
cmpq %rsi, 0x20(%rsp)
je 0x28b34
movq 0x38(%rdx), %rdi
movq (%rdi), %rdi
movsd (%rdi,%rsi,8), %xmm2
divsd %xmm0, %xmm2
movq 0x38(%rsp), %rdi
addsd (%rdi,%rsi,8), %xmm2
movsd %xmm2, (%rdi,%rsi,8)
incq %rsi
jmp 0x28b09
movl (%rdx), %esi
movq 0x28(%rsp), %rdi
movq 0x88(%rdi), %rdi
cmpl $0x2, %esi
pushq $0x1
popq %r8
cmovll %r8d, %esi
decl %esi
xorl %r8d, %r8d
cmpq %rsi, %r8
je 0x28afb
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 0x28c7a
cmpq $0x0, 0x18(%r15)
je 0x28c10
xorl %r11d, %r11d
cmpq %r11, %rax
je 0x28c10
movq 0x8a8(%rdi), %rbx
movq 0xd8(%r15), %r13
movq (%r13,%r12,8), %r13
movq 0x10(%r13), %r13
movslq (%r13,%r8,4), %r13
movsbq (%rbx,%r13), %rbx
movq 0x40(%rdx), %r13
movq (%r13,%r8,8), %r13
movq (%r13,%r10,8), %r13
movsd (%r13,%r11,8), %xmm2
movq (%rbp,%rbx,8), %r13
movq (%r13,%r10,8), %r13
addsd (%r13,%r11,8), %xmm2
movsd %xmm2, (%r13,%r11,8)
movq 0x38(%rdx), %r13
movq (%r13,%r8,8), %r13
movsd (%r13,%r10,8), %xmm2
movq 0x50(%rsp), %r13
movq (%r13,%rbx,8), %rbx
movq (%rbx,%r10,8), %rbx
addsd (%rbx,%r11,8), %xmm2
movsd %xmm2, (%rbx,%r11,8)
incq %r11
jmp 0x28b87
cmpq $0x0, 0x20(%r15)
je 0x28c72
xorl %r11d, %r11d
cmpq %r11, %r14
je 0x28c72
movapd %xmm1, %xmm2
cmpq %r11, %r9
je 0x28c2c
xorpd %xmm2, %xmm2
movq 0x38(%rdx), %rbx
movq (%rbx,%r8,8), %rbx
movq 0x40(%rsp), %r13
movq (%r13,%r10,8), %r13
mulsd (%rbx,%r10,8), %xmm2
addsd (%r13,%r11,8), %xmm2
movsd %xmm2, (%r13,%r11,8)
movsd (%rbx,%r10,8), %xmm2
movq 0x48(%rsp), %rbx
movq (%rbx,%r10,8), %rbx
addsd (%rbx,%r11,8), %xmm2
movsd %xmm2, (%rbx,%r11,8)
incq %r11
jmp 0x28c1a
incq %r10
jmp 0x28b6e
incq %r8
jmp 0x28b52
xorl %r12d, %r12d
movl 0x1c(%rsp), %ecx
testb %cl, %cl
movl $0x0, %eax
cmovgl %ecx, %eax
cltq
movq %rax, 0x30(%rsp)
cmpq 0x20(%rsp), %r12
je 0x28d85
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x28cbf
movq 0x38(%rsp), %rcx
movsd (%rcx,%r12,8), %xmm0
movsd %xmm0, (%rax,%r12,8)
cmpq $0x0, 0x18(%r15)
je 0x28d2c
xorl %ebx, %ebx
cmpq 0x30(%rsp), %rbx
je 0x28d2c
xorl %r13d, %r13d
cmpq %r13, 0x20(%rsp)
je 0x28d27
movq (%rbp,%rbx,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x50(%rsp), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r13,8), %xmm0
callq 0x67cb
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x18(%r15), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rax
movsd %xmm1, (%rax,%r13,8)
incq %r13
jmp 0x28cd2
incq %rbx
jmp 0x28cc8
cmpq $0x0, 0x20(%r15)
je 0x28d7d
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x28d7d
movq 0x40(%rsp), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x48(%rsp), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
callq 0x67cb
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x20(%r15), %rax
movq (%rax,%r12,8), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %rbx
jmp 0x28d35
incq %r12
jmp 0x28c9a
movq 0x28(%rsp), %rdi
callq 0xe1de
movq 0x10(%r15), %r14
testb %al, %al
je 0x28e8c
testq %r14, %r14
movl 0xc(%rsp), %ebx
je 0x28dcc
movq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0xe2a0
movq %rax, %r13
movq %r12, %rdi
callq 0xe2d6
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
movl %ebx, %ecx
callq 0x5d80
movzbl 0x30(%rsp), %r12d
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpq 0x20(%rsp), %r14
je 0x28ef0
cmpq $0x0, 0x18(%r15)
je 0x28e3d
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x28e3d
movq 0x18(%r15), %rax
movq 0x28(%rsp), %rdx
cmpb $0x0, 0x80(%rdx)
movq (%rax,%rbx,8), %rax
movq 0x98(%rdx), %rcx
cmoveq %r13, %rcx
movq (%rax,%r14,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r14,8), %rsi
movq 0xa0(%rdx), %rax
cmoveq %r13, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r14,8), %rdx
movl 0xc(%rsp), %ecx
callq 0x5d80
incq %rbx
jmp 0x28dec
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x28e84
movq (%rax,%r14,8), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0xe2c4
movq (%rax,%r14,8), %rax
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
callq 0xe2fa
movq (%rax,%r14,8), %rdx
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0xc(%rsp), %ecx
callq 0x5d80
incq %r14
jmp 0x28dd8
testq %r14, %r14
movl 0xc(%rsp), %esi
je 0x28e9d
movq %r14, %rdi
callq 0x5600
movzbl 0x30(%rsp), %r14d
xorl %r12d, %r12d
cmpq 0x20(%rsp), %r12
je 0x28ef0
cmpq $0x0, 0x18(%r15)
je 0x28ed5
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x28ed5
movq 0x18(%r15), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rdi
movl 0xc(%rsp), %esi
callq 0x5600
incq %rbx
jmp 0x28eb6
movq 0x20(%r15), %rax
testq %rax, %rax
je 0x28eeb
movq (%rax,%r12,8), %rdi
movl 0xc(%rsp), %esi
callq 0x5600
incq %r12
jmp 0x28ea6
movl 0xd0(%r15), %esi
movq 0xd8(%r15), %rdx
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x25848
movl 0xd0(%r15), %esi
movq 0xd8(%r15), %rdx
movq %rbx, %rdi
callq 0xe400
movsd %xmm0, 0x10(%rsp)
movq 0x38(%rsp), %rdi
callq 0x4290
testq %rbp, %rbp
movl 0xc(%rsp), %ebx
je 0x28f48
movq %rbp, %rdi
movl 0x1c(%rsp), %esi
movl %ebx, %edx
callq 0xa064
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
movq 0x40(%rsp), %r14
je 0x28f62
movl 0x1c(%rsp), %esi
movl %ebx, %edx
callq 0xa064
testq %r14, %r14
je 0x28f71
movq %r14, %rdi
movl %ebx, %esi
callq 0x7f2c
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x28f82
movl %ebx, %esi
callq 0x7f2c
movsd 0x10(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop