// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %rbx
movsbl 0x8b9(%rax), %r12d
movzbl 0x8c8(%rax), %eax
movq %rax, 0x50(%rsp)
movsbl %al, %eax
movl %eax, 0x4(%rsp)
movl 0xd0(%rsi), %r13d
movq 0xd8(%rsi), %rbp
movl %r13d, %esi
movq %rbp, %rdx
callq 0x25910
movq %r15, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x25c5e
movq %r15, %rdi
movq %r13, 0x60(%rsp)
movl %r13d, %esi
movq %rbp, 0x90(%rsp)
movq %rbp, %rdx
callq 0x25e2e
cmpq $0x0, 0x10(%r14)
movq %r14, 0x8(%rsp)
je 0x29381
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4130
movq 0x8(%rsp), %r14
jmp 0x29383
xorl %eax, %eax
movq %rax, 0x58(%rsp)
movzbl %bl, %r13d
cmpq $0x0, 0x18(%r14)
movq %r15, 0x38(%rsp)
je 0x293c1
movl 0x4(%rsp), %ebp
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xcaae
movq %rax, 0x48(%rsp)
movl %ebp, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0xcaae
movq 0x8(%rsp), %r14
movq %rax, %rbp
jmp 0x293cc
movq $0x0, 0x48(%rsp)
xorl %ebp, %ebp
movzbl %r12b, %r15d
cmpq $0x0, 0x20(%r14)
movq %rbx, 0x18(%rsp)
movl %r12d, 0x7c(%rsp)
je 0x2940b
movl %r12d, %r14d
movl 0x4(%rsp), %r12d
movl %r12d, %edi
movl %ebx, %esi
movl %r14d, %edx
callq 0xcaae
movl %r12d, %edi
movq %rax, %r12
movl %ebx, %esi
movl %r14d, %edx
callq 0xcaae
movq %rax, %r14
jmp 0x29411
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %r12, 0x70(%rsp)
cmpl $0x0, 0x60(%rsp)
jle 0x2968c
cvtsi2sdl 0x60(%rsp), %xmm0
movq 0x38(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x30(%rsp)
xorl %ecx, %ecx
movsd 0xa4ae(%rip), %xmm1     # 0x338f0
movq 0x90(%rsp), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x4(%rax)
jne 0x29679
movq 0x8(%rsp), %rax
cmpq $0x0, 0x10(%rax)
movq 0x58(%rsp), %rcx
je 0x294a1
testb %r13b, %r13b
jle 0x294a1
movq 0x20(%rsp), %rax
movq 0x38(%rax), %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, %r13
jne 0x29486
movq 0x20(%rsp), %rax
movl (%rax), %eax
movq %rax, 0x68(%rsp)
cmpl $0x2, %eax
jl 0x29679
movq 0x68(%rsp), %rax
decl %eax
movq %rax, 0x68(%rsp)
movq 0x30(%rsp), %rax
movq 0x870(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x98(%rsp)
xorl %r9d, %r9d
testb %r13b, %r13b
jle 0x2966b
testb %r15b, %r15b
setle %al
movq 0x98(%rsp), %rcx
movslq (%rcx,%r9,4), %rsi
movq 0xa0(%rsp), %rcx
movzbl (%rcx,%rsi), %r10d
movq 0x8(%rsp), %rcx
cmpq $0x0, 0x20(%rcx)
movq 0x18(%rcx), %rcx
movq %rcx, 0x40(%rsp)
sete %cl
orb %al, %cl
movb %cl, 0x17(%rsp)
xorl %edi, %edi
cmpq $0x0, 0x40(%rsp)
movq 0x48(%rsp), %r12
movq %r13, %rdx
je 0x295ca
movq 0x30(%rsp), %rax
movq 0x8a8(%rax), %rsi
movq 0x8(%rsp), %rax
movq 0xd8(%rax), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movslq (%rax,%r9,4), %rax
movq 0x20(%rsp), %rcx
movq 0x38(%rcx), %rbx
movq 0x40(%rcx), %r8
movq (%r8,%r9,8), %r8
movq (%r8,%rdi,8), %r8
movq (%rbx,%r9,8), %rbx
xorl %r13d, %r13d
movsbq (%rsi,%rax), %rcx
movsd (%r8,%r13,8), %xmm2
movq (%r12,%rcx,8), %r11
movq (%r11,%rdi,8), %r11
addsd (%r11,%r13,8), %xmm2
movsd %xmm2, (%r11,%r13,8)
movsd (%rbx,%rdi,8), %xmm2
movq (%rbp,%rcx,8), %rcx
movq (%rcx,%rdi,8), %rcx
addsd (%rcx,%r13,8), %xmm2
movsd %xmm2, (%rcx,%r13,8)
incq %r13
cmpq %r13, %rdx
jne 0x29589
cmpb $0x0, 0x17(%rsp)
movq 0x70(%rsp), %r12
movq %rdx, %r13
jne 0x2965f
movq 0x30(%rsp), %rax
movq 0x8a8(%rax), %rbx
movq 0x8(%rsp), %rax
movq 0xd8(%rax), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movslq (%rax,%r9,4), %r8
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax,%r9,8), %rsi
xorl %eax, %eax
movapd %xmm1, %xmm2
cmpq %rax, %r10
je 0x29622
xorpd %xmm2, %xmm2
movsbq (%rbx,%r8), %rcx
movq (%r12,%rcx,8), %r11
movq (%r11,%rdi,8), %r11
mulsd (%rsi,%rdi,8), %xmm2
addsd (%r11,%rax,8), %xmm2
movsd %xmm2, (%r11,%rax,8)
movsd (%rsi,%rdi,8), %xmm2
movq (%r14,%rcx,8), %rcx
movq (%rcx,%rdi,8), %rcx
addsd (%rcx,%rax,8), %xmm2
movsd %xmm2, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x29615
incq %rdi
cmpq %r13, %rdi
jne 0x29530
incq %r9
cmpq 0x68(%rsp), %r9
jne 0x294ea
movq 0x28(%rsp), %rcx
incq %rcx
cmpq 0x60(%rsp), %rcx
jne 0x29442
movq %r13, 0x40(%rsp)
testb %r13b, %r13b
movq 0x8(%rsp), %rcx
movq 0x50(%rsp), %rsi
movq %r14, 0x80(%rsp)
jle 0x297f6
movslq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
movslq 0x7c(%rsp), %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x88(%rsp)
movq 0x10(%rcx), %rax
testq %rax, %rax
je 0x296e5
movq 0x58(%rsp), %rdx
movsd (%rdx,%r12,8), %xmm0
movsd %xmm0, (%rax,%r12,8)
cmpq $0x0, 0x18(%rcx)
je 0x29760
xorl %ebx, %ebx
testb %sil, %sil
movq 0x48(%rsp), %rbp
movq 0x88(%rsp), %r14
jle 0x29756
xorl %r13d, %r13d
movq (%rbp,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x28(%rsp)
movq (%r14,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
callq 0x8df8
movq 0x50(%rsp), %rsi
movq 0x8(%rsp), %rcx
movsd 0x28(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x18(%rcx), %rax
movq (%rax,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %r13
cmpq %r13, %rsi
jne 0x29703
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0x296ee
cmpq $0x0, 0x20(%rcx)
movq 0x80(%rsp), %r14
je 0x297e0
testb %r15b, %r15b
jle 0x297e0
xorl %ebx, %ebx
testb %sil, %sil
movq 0x70(%rsp), %rbp
jle 0x297d6
xorl %r13d, %r13d
movq (%rbp,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x28(%rsp)
movq (%r14,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
callq 0x8df8
movq 0x50(%rsp), %rsi
movq 0x8(%rsp), %rcx
movsd 0x28(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x20(%rcx), %rax
movq (%rax,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %r13
cmpq %r13, %rsi
jne 0x29783
incq %rbx
cmpq 0x30(%rsp), %rbx
jne 0x29776
incq %r12
cmpq 0x20(%rsp), %r12
movq 0x88(%rsp), %rbp
jne 0x296cb
movq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x1133a
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rdi
testb %al, %al
je 0x2983f
testq %rdi, %rdi
movq 0x18(%rsp), %rbx
je 0x298f5
cmpb $0x0, 0x80(%r15)
je 0x298e5
movq 0xa0(%r15), %rsi
movq 0xb8(%r15), %rdx
jmp 0x298e9
testq %rdi, %rdi
movq 0x18(%rsp), %rbx
movq 0x40(%rsp), %rax
je 0x2985b
movsbl %al, %esi
callq 0x6836
movq 0x8(%rsp), %rcx
testb %bl, %bl
jle 0x299f4
movslq 0x18(%rsp), %r14
movzbl 0x4(%rsp), %r15d
xorl %r12d, %r12d
movq 0x18(%rsp), %r13
cmpq $0x0, 0x18(%rcx)
je 0x298a7
cmpb $0x0, 0x4(%rsp)
jle 0x298a7
xorl %ebx, %ebx
movq 0x18(%rcx), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rdi
movl %r13d, %esi
callq 0x6836
movq 0x8(%rsp), %rcx
incq %rbx
cmpq %rbx, %r15
jne 0x29886
cmpq $0x0, 0x20(%rcx)
je 0x298d8
cmpb $0x0, 0x50(%rsp)
jle 0x298d8
xorl %ebx, %ebx
movq 0x20(%rcx), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rdi
movl %r13d, %esi
callq 0x6836
movq 0x8(%rsp), %rcx
incq %rbx
cmpq %rbx, %r15
jne 0x298b7
incq %r12
cmpq %r14, %r12
jne 0x29876
jmp 0x299f4
xorl %esi, %esi
xorl %edx, %edx
movl %ebx, %ecx
callq 0x7997
movq 0x8(%rsp), %rcx
testb %bl, %bl
jle 0x299f4
movzbl 0x4(%rsp), %r14d
xorl %edx, %edx
xorl %r15d, %r15d
movq 0x38(%rsp), %r12
cmpq $0x0, 0x18(%rcx)
movq 0x18(%rsp), %r13
je 0x29976
cmpb $0x0, 0x4(%rsp)
jle 0x29976
xorl %ebx, %ebx
movq 0x18(%rcx), %rax
cmpb $0x0, 0x80(%r12)
movq (%rax,%rbx,8), %rax
movq 0xa8(%r12), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r15,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r15,8), %rsi
movq 0xc0(%r12), %rax
cmoveq %rdx, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r15,8), %rdx
movl %r13d, %ecx
callq 0x7997
xorl %edx, %edx
movq 0x8(%rsp), %rcx
incq %rbx
cmpq %rbx, %r14
jne 0x29922
cmpq $0x0, 0x20(%rcx)
movq %r14, %r13
movq 0x40(%rsp), %r14
je 0x299e3
cmpb $0x0, 0x4(%rsp)
jle 0x299e3
xorl %ebx, %ebx
movq 0x20(%rcx), %rax
cmpb $0x0, 0x80(%r12)
movq (%rax,%rbx,8), %rax
movq 0xb0(%r12), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r15,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r15,8), %rsi
movq 0xc8(%r12), %rax
cmoveq %rdx, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r15,8), %rdx
movsbl %r14b, %ecx
callq 0x7997
xorl %edx, %edx
movq 0x8(%rsp), %rcx
incq %rbx
cmpq %rbx, %r13
jne 0x2998e
incq %r15
cmpq 0x18(%rsp), %r15
movq %r13, %r14
jne 0x2990d
movq %rcx, %r14
movl 0xd0(%rcx), %esi
movq 0xd8(%rcx), %rdx
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x25910
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq %rbx, %rdi
callq 0x114ae
movsd %xmm0, 0x8(%rsp)
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x29a3c
callq 0x42a0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
movq 0x18(%rsp), %rbx
movq 0x80(%rsp), %r14
movq 0x70(%rsp), %r15
je 0x29a63
movl 0x4(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
testq %rbp, %rbp
je 0x29a76
movq %rbp, %rdi
movl 0x4(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
testq %r15, %r15
je 0x29a89
movq %r15, %rdi
movl 0x4(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
testq %r14, %r14
je 0x29a9c
movq %r14, %rdi
movl 0x4(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
movsd 0x8(%rsp), %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq