// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movq %rsi, %r15
movq %rdi, %r14
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, 0xc(%rsp)
movl 0xd0(%rsi), %r13d
movq 0xd8(%rsi), %rbp
movl %r13d, %esi
movq %rbp, %rdx
callq 0x25d92
movq %r14, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x260b0
movq %r14, 0x40(%rsp)
movq %r14, %rdi
movq %r13, 0x60(%rsp)
movl %r13d, %esi
movq %rbp, 0x98(%rsp)
movq %rbp, %rdx
callq 0x262ae
cmpq $0x0, 0x10(%r15)
movq %r15, 0x10(%rsp)
je 0x298a8
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4130
movq 0x10(%rsp), %r15
jmp 0x298aa
xorl %eax, %eax
movq %rax, 0x58(%rsp)
movzbl %bl, %r13d
cmpq $0x0, 0x18(%r15)
je 0x298e3
movl 0xc(%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 0x10(%rsp), %r15
movq %rax, %rbp
jmp 0x298ee
movq $0x0, 0x48(%rsp)
xorl %ebp, %ebp
movzbl %r12b, %r14d
cmpq $0x0, 0x20(%r15)
movq %rbx, 0x20(%rsp)
movl %r12d, 0x7c(%rsp)
je 0x2992d
movl %r12d, %r15d
movl 0xc(%rsp), %r12d
movl %r12d, %edi
movl %ebx, %esi
movl %r15d, %edx
callq 0xcaae
movl %r12d, %edi
movq %rax, %r12
movl %ebx, %esi
movl %r15d, %edx
callq 0xcaae
movq %rax, %r15
jmp 0x29933
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r12, 0x70(%rsp)
cmpl $0x0, 0x60(%rsp)
jle 0x29bb4
cvtsi2sdl 0x60(%rsp), %xmm0
movq 0x40(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x38(%rsp)
xorl %ecx, %ecx
movsd 0xaf8c(%rip), %xmm1     # 0x348f0
movq 0x98(%rsp), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0x28(%rsp)
cmpl $0x0, 0x4(%rax)
jne 0x29ba1
movq 0x10(%rsp), %rax
cmpq $0x0, 0x10(%rax)
movq 0x58(%rsp), %rcx
je 0x299c3
testb %r13b, %r13b
jle 0x299c3
movq 0x28(%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 0x299a8
movq 0x28(%rsp), %rax
movl (%rax), %eax
movq %rax, 0x68(%rsp)
cmpl $0x2, %eax
jl 0x29ba1
movq 0x68(%rsp), %rax
decl %eax
movq %rax, 0x68(%rsp)
movq 0x38(%rsp), %rax
movq 0x870(%rax), %rax
movq %rax, 0xa8(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0xa0(%rsp)
xorl %r9d, %r9d
testb %r13b, %r13b
jle 0x29b93
testb %r14b, %r14b
setle %al
movq 0xa0(%rsp), %rcx
movslq (%rcx,%r9,4), %rsi
movq 0xa8(%rsp), %rcx
movzbl (%rcx,%rsi), %r10d
movq 0x10(%rsp), %rcx
cmpq $0x0, 0x20(%rcx)
movq 0x18(%rcx), %rcx
movq %rcx, 0xb0(%rsp)
sete %cl
orb %al, %cl
movb %cl, 0x1f(%rsp)
xorl %edi, %edi
cmpq $0x0, 0xb0(%rsp)
movq 0x48(%rsp), %r12
movq %r13, %rdx
je 0x29af2
movq 0x38(%rsp), %rax
movq 0x8a8(%rax), %rsi
movq 0x10(%rsp), %rax
movq 0xd8(%rax), %rax
movq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movslq (%rax,%r9,4), %rax
movq 0x28(%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 0x29ab1
cmpb $0x0, 0x1f(%rsp)
movq 0x70(%rsp), %r12
movq %rdx, %r13
jne 0x29b87
movq 0x38(%rsp), %rax
movq 0x8a8(%rax), %rbx
movq 0x10(%rsp), %rax
movq 0xd8(%rax), %rax
movq 0x30(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rax
movslq (%rax,%r9,4), %r8
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rax
movq (%rax,%r9,8), %rsi
xorl %eax, %eax
movapd %xmm1, %xmm2
cmpq %rax, %r10
je 0x29b4a
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 (%r15,%rcx,8), %rcx
movq (%rcx,%rdi,8), %rcx
addsd (%rcx,%rax,8), %xmm2
movsd %xmm2, (%rcx,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x29b3d
incq %rdi
cmpq %r13, %rdi
jne 0x29a55
incq %r9
cmpq 0x68(%rsp), %r9
jne 0x29a0c
movq 0x30(%rsp), %rcx
incq %rcx
cmpq 0x60(%rsp), %rcx
jne 0x29964
cmpb $0x0, 0x20(%rsp)
movq 0x10(%rsp), %rcx
movq 0x50(%rsp), %rsi
movq %r15, 0x80(%rsp)
jle 0x29d2e
movslq 0x20(%rsp), %rax
movq %rax, 0x38(%rsp)
movslq 0x7c(%rsp), %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x90(%rsp)
movq %r13, 0x88(%rsp)
movq 0x10(%rcx), %rax
testq %rax, %rax
je 0x29c12
movq 0x58(%rsp), %rdx
movsd (%rdx,%r12,8), %xmm0
movsd %xmm0, (%rax,%r12,8)
cmpq $0x0, 0x18(%rcx)
je 0x29c90
xorl %ebx, %ebx
testb %sil, %sil
movq 0x48(%rsp), %rbp
movq 0x90(%rsp), %r15
jle 0x29c83
xorl %r13d, %r13d
movq (%rbp,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movq (%r15,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
callq 0x8d88
movq 0x50(%rsp), %rsi
movq 0x10(%rsp), %rcx
movsd 0x30(%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 0x29c30
incq %rbx
cmpq 0x88(%rsp), %rbx
jne 0x29c1b
cmpq $0x0, 0x20(%rcx)
movq 0x80(%rsp), %r15
je 0x29d10
testb %r14b, %r14b
jle 0x29d10
xorl %ebx, %ebx
testb %sil, %sil
movq 0x70(%rsp), %rbp
jle 0x29d06
xorl %r13d, %r13d
movq (%rbp,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x30(%rsp)
movq (%r15,%r13,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
callq 0x8d88
movq 0x50(%rsp), %rsi
movq 0x10(%rsp), %rcx
movsd 0x30(%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 0x29cb3
incq %rbx
cmpq 0x28(%rsp), %rbx
jne 0x29ca6
incq %r12
cmpq 0x38(%rsp), %r12
movq 0x90(%rsp), %rbp
movq 0x88(%rsp), %r13
jne 0x29bf8
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x114a8
movq 0x10(%rsp), %rdx
movq 0x10(%rdx), %rdi
testb %al, %al
je 0x29d76
testq %rdi, %rdi
movq 0x20(%rsp), %rcx
je 0x29e23
cmpb $0x0, 0x80(%rbx)
je 0x29e15
movq 0xa0(%rbx), %rsi
movq 0xb8(%rbx), %rdx
jmp 0x29e19
testq %rdi, %rdi
movq 0x20(%rsp), %rbx
je 0x29d8e
movsbl %r13b, %esi
callq 0x692d
movq 0x10(%rsp), %rdx
testb %bl, %bl
jle 0x29f12
movslq 0x20(%rsp), %r15
movzbl 0xc(%rsp), %r12d
xorl %r14d, %r14d
cmpq $0x0, 0x18(%rdx)
je 0x29dd6
cmpb $0x0, 0x50(%rsp)
jle 0x29dd6
xorl %ebx, %ebx
movq 0x18(%rdx), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r14,8), %rdi
movsbl %r13b, %esi
callq 0x692d
movq 0x10(%rsp), %rdx
incq %rbx
cmpq %rbx, %r12
jne 0x29db4
cmpq $0x0, 0x20(%rdx)
je 0x29e08
cmpb $0x0, 0xc(%rsp)
jle 0x29e08
xorl %ebx, %ebx
movq 0x20(%rdx), %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r14,8), %rdi
movsbl %r13b, %esi
callq 0x692d
movq 0x10(%rsp), %rdx
incq %rbx
cmpq %rbx, %r12
jne 0x29de6
incq %r14
cmpq %r15, %r14
jne 0x29da4
jmp 0x29f12
xorl %esi, %esi
xorl %edx, %edx
callq 0x79f5
movq 0x10(%rsp), %rdx
testb %r13b, %r13b
jle 0x29f12
movzbl 0xc(%rsp), %r15d
xorl %r8d, %r8d
xorl %r12d, %r12d
movq 0x20(%rsp), %r13
movq 0x40(%rsp), %r14
cmpq $0x0, 0x18(%rdx)
je 0x29ea4
cmpb $0x0, 0xc(%rsp)
jle 0x29ea4
xorl %ebx, %ebx
movq 0x18(%rdx), %rax
cmpb $0x0, 0x80(%r14)
movq (%rax,%rbx,8), %rax
movq 0xa8(%r14), %rcx
cmoveq %r8, %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r12,8), %rsi
movq 0xc0(%r14), %rax
cmoveq %r8, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rdx
movl %r13d, %ecx
callq 0x79f5
xorl %r8d, %r8d
movq 0x10(%rsp), %rdx
incq %rbx
cmpq %rbx, %r15
jne 0x29e52
cmpq $0x0, 0x20(%rdx)
je 0x29f06
cmpb $0x0, 0xc(%rsp)
jle 0x29f06
xorl %ebx, %ebx
movq 0x20(%rdx), %rax
cmpb $0x0, 0x80(%r14)
movq (%rax,%rbx,8), %rax
movq 0xb0(%r14), %rcx
cmoveq %r8, %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%rbx,8), %rax
movq (%rax,%r12,8), %rsi
movq 0xc8(%r14), %rax
cmoveq %r8, %rax
movq (%rax,%rbx,8), %rax
movq (%rax,%r12,8), %rdx
movl %r13d, %ecx
callq 0x79f5
xorl %r8d, %r8d
movq 0x10(%rsp), %rdx
incq %rbx
cmpq %rbx, %r15
jne 0x29eb4
incq %r12
cmpq %r13, %r12
jne 0x29e42
movq %rdx, %r14
movl 0xd0(%rdx), %esi
movq 0xd8(%rdx), %rdx
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x25d92
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq %rbx, %rdi
callq 0x1161c
movsd %xmm0, 0x10(%rsp)
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x29f5a
callq 0x42a0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
movq 0x20(%rsp), %rbx
movq 0x80(%rsp), %r14
movq 0x70(%rsp), %r15
je 0x29f81
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
testq %rbp, %rbp
je 0x29f94
movq %rbp, %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
testq %r15, %r15
je 0x29fa7
movq %r15, %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
testq %r14, %r14
je 0x29fba
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
movsd 0x10(%rsp), %xmm0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq