// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %rbx
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %r15
movsbl 0x8b9(%rax), %r12d
movzbl 0x8c8(%rax), %eax
movq %rax, 0x30(%rsp)
movsbl %al, %eax
movl %eax, 0x14(%rsp)
movl 0xd0(%rsi), %r13d
movq 0xd8(%rsi), %rbp
movl %r13d, %esi
movq %rbp, %rdx
callq 0x2bfa8
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x2c2c6
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
movq %r13, 0x68(%rsp)
movl %r13d, %esi
movq %rbp, 0x90(%rsp)
movq %rbp, %rdx
callq 0x2c4d0
cmpq $0x0, 0x10(%r14)
movq %r14, 0x8(%rsp)
je 0x2f8d8
movl $0x8, %esi
movq %r15, %rdi
callq 0x4130
movq 0x8(%rsp), %rcx
jmp 0x2f8dd
movq %r14, %rcx
xorl %eax, %eax
movq %rax, 0x60(%rsp)
movzbl %r15b, %r14d
cmpq $0x0, 0x18(%rcx)
je 0x2f91a
movl 0x14(%rsp), %ebx
movl %ebx, %edi
movl %r15d, %esi
movl %r15d, %edx
callq 0xcaae
movq %rax, 0x58(%rsp)
movl %ebx, %edi
movl %r15d, %esi
movl %r15d, %edx
callq 0xcaae
movq 0x8(%rsp), %rcx
movq %rax, %rbp
jmp 0x2f925
movq $0x0, 0x58(%rsp)
xorl %ebp, %ebp
movl %r12d, %ebx
movzbl %bl, %r12d
cmpq $0x0, 0x20(%rcx)
movq %r15, 0x20(%rsp)
movl %ebx, 0x7c(%rsp)
je 0x2f95c
movl %r15d, %edi
movl %ebx, %esi
callq 0xbb5c
movq %rax, 0x38(%rsp)
movl %r15d, %edi
movl %ebx, %esi
callq 0xbb5c
movq %rax, 0x40(%rsp)
jmp 0x2f96e
movq $0x0, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
cmpl $0x0, 0x68(%rsp)
movq %r12, 0x88(%rsp)
jle 0x2fb96
cvtsi2sdl 0x68(%rsp), %xmm0
movq 0x28(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x48(%rsp)
xorl %ecx, %ecx
movsd 0x4f4e(%rip), %xmm1     # 0x348f0
movq 0x90(%rsp), %rax
movq %rcx, 0x50(%rsp)
movq (%rax,%rcx,8), %rdx
cmpl $0x0, 0x4(%rdx)
jne 0x2fb83
movq 0x8(%rsp), %rax
cmpq $0x0, 0x10(%rax)
movq 0x60(%rsp), %rcx
je 0x2f9f7
testb %r14b, %r14b
jle 0x2f9f7
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, %r14
jne 0x2f9dc
movl (%rdx), %eax
movq %rax, 0x70(%rsp)
cmpl $0x2, %eax
jl 0x2fb83
movq 0x70(%rsp), %rax
decl %eax
movq %rax, 0x70(%rsp)
movq 0x48(%rsp), %rax
movq 0x870(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x98(%rsp)
xorl %r9d, %r9d
testb %r14b, %r14b
jle 0x2fb75
testb %r12b, %r12b
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, 0x18(%rsp)
sete %bl
orb %al, %bl
xorl %edi, %edi
cmpq $0x0, 0x18(%rsp)
movq 0x58(%rsp), %r12
je 0x2fb0a
movq 0x48(%rsp), %rax
movq 0x8a8(%rax), %r15
movq 0x8(%rsp), %rax
movq 0xd8(%rax), %rax
movq 0x50(%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,%rdi,8), %rax
movq (%rsi,%r9,8), %rsi
xorl %r13d, %r13d
movsbq (%r15,%r8), %rcx
movsd (%rax,%r13,8), %xmm2
movq (%r12,%rcx,8), %r11
movq (%r11,%rdi,8), %r11
addsd (%r11,%r13,8), %xmm2
movsd %xmm2, (%r11,%r13,8)
movsd (%rsi,%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, %r14
jne 0x2fac9
testb %bl, %bl
movq 0x88(%rsp), %r12
jne 0x2fb69
movq 0x38(%rdx), %rax
movq (%rax,%r9,8), %r15
movq 0x38(%rsp), %rax
movq (%rax,%rdi,8), %r8
movq 0x40(%rsp), %rax
movq (%rax,%rdi,8), %rsi
xorl %eax, %eax
movapd %xmm1, %xmm2
cmpq %rax, %r10
je 0x2fb3f
xorpd %xmm2, %xmm2
mulsd (%r15,%rdi,8), %xmm2
addsd (%r8,%rax,8), %xmm2
movsd %xmm2, (%r8,%rax,8)
movsd (%r15,%rdi,8), %xmm2
addsd (%rsi,%rax,8), %xmm2
movsd %xmm2, (%rsi,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x2fb32
incq %rdi
cmpq %r14, %rdi
jne 0x2fa78
incq %r9
cmpq 0x70(%rsp), %r9
jne 0x2fa36
movq 0x50(%rsp), %rcx
incq %rcx
cmpq 0x68(%rsp), %rcx
jne 0x2f9a2
cmpb $0x0, 0x20(%rsp)
movq 0x8(%rsp), %rcx
jle 0x2fcd9
movslq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
movzbl 0x7c(%rsp), %eax
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x80(%rsp)
movq 0x10(%rcx), %rax
testq %rax, %rax
je 0x2fbdf
movq 0x60(%rsp), %rdx
movsd (%rdx,%r12,8), %xmm0
movsd %xmm0, (%rax,%r12,8)
cmpq $0x0, 0x18(%rcx)
je 0x2fc5b
cmpb $0x0, 0x30(%rsp)
jle 0x2fc5b
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x58(%rsp), %rbp
movq 0x80(%rsp), %rbx
movq (%rbp,%r15,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq (%rbx,%r15,8), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r13,8), %xmm0
callq 0x8d88
movq 0x8(%rsp), %rcx
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x18(%rcx), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%r12,8), %rax
movsd %xmm1, (%rax,%r13,8)
incq %r13
cmpq %r13, %r14
jne 0x2fc00
incq %r15
cmpq 0x30(%rsp), %r15
jne 0x2fbf0
cmpq $0x0, 0x20(%rcx)
movq 0x40(%rsp), %rbx
movq 0x38(%rsp), %r13
movq 0x48(%rsp), %rbp
je 0x2fcc3
cmpb $0x0, 0x88(%rsp)
jle 0x2fcc3
xorl %r15d, %r15d
movq (%r13,%r12,8), %rax
movsd (%rax,%r15,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movq (%rbx,%r12,8), %rax
movsd (%rax,%r15,8), %xmm0
callq 0x8d88
movq 0x8(%rsp), %rcx
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x20(%rcx), %rax
movq (%rax,%r12,8), %rax
movsd %xmm1, (%rax,%r15,8)
incq %r15
cmpq %r15, %rbp
jne 0x2fc7e
incq %r12
cmpq 0x50(%rsp), %r12
movq 0x80(%rsp), %rbp
jne 0x2fbc5
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x114a8
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %r12
testb %al, %al
je 0x2fdff
testq %r12, %r12
movq 0x20(%rsp), %rbx
je 0x2fd29
movq %r15, %rdi
callq 0x11516
movq %rax, %r13
movq %r15, %rdi
callq 0x1154c
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
movl %ebx, %ecx
callq 0x79f5
movq 0x8(%rsp), %rcx
testb %r14b, %r14b
jle 0x2fe88
movzbl 0x14(%rsp), %r14d
xorl %edx, %edx
xorl %r12d, %r12d
movq 0x20(%rsp), %r13
cmpq $0x0, 0x18(%rcx)
movq 0x28(%rsp), %rbx
je 0x2fda8
cmpb $0x0, 0x14(%rsp)
jle 0x2fda8
xorl %r15d, %r15d
movq 0x18(%rcx), %rax
cmpb $0x0, 0x80(%rbx)
movq (%rax,%r15,8), %rax
movq 0x98(%rbx), %rcx
cmoveq %rdx, %rcx
movq (%rax,%r12,8), %rdi
movq (%rcx,%r15,8), %rax
movq (%rax,%r12,8), %rsi
movq 0xa0(%rbx), %rax
cmoveq %rdx, %rax
movq (%rax,%r15,8), %rax
movq (%rax,%r12,8), %rdx
movl %r13d, %ecx
callq 0x79f5
xorl %edx, %edx
movq 0x8(%rsp), %rcx
incq %r15
cmpq %r15, %r14
jne 0x2fd58
movq 0x20(%rcx), %rax
testq %rax, %rax
je 0x2fdee
movq (%rax,%r12,8), %rbx
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x1153a
movq (%rax,%r12,8), %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0x11570
movq (%rax,%r12,8), %rdx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movl %r13d, %ecx
callq 0x79f5
xorl %edx, %edx
movq 0x8(%rsp), %rcx
incq %r12
cmpq %r13, %r12
jne 0x2fd42
jmp 0x2fe88
testq %r12, %r12
movq 0x20(%rsp), %rbx
je 0x2fe1a
movsbl %r14b, %esi
movq %r12, %rdi
callq 0x692d
movq 0x8(%rsp), %rcx
testb %bl, %bl
movq 0x30(%rsp), %rdx
jle 0x2fe88
movslq 0x20(%rsp), %rbx
xorl %r12d, %r12d
cmpq $0x0, 0x18(%rcx)
je 0x2fe60
testb %dl, %dl
jle 0x2fe60
xorl %r15d, %r15d
movq 0x18(%rcx), %rax
movq (%rax,%r15,8), %rax
movq (%rax,%r12,8), %rdi
movsbl %r14b, %esi
callq 0x692d
movq 0x30(%rsp), %rdx
movq 0x8(%rsp), %rcx
incq %r15
cmpq %r15, %rdx
jne 0x2fe39
movq 0x20(%rcx), %rax
testq %rax, %rax
je 0x2fe80
movq (%rax,%r12,8), %rdi
movsbl %r14b, %esi
callq 0x692d
movq 0x30(%rsp), %rdx
movq 0x8(%rsp), %rcx
incq %r12
cmpq %rbx, %r12
jne 0x2fe2b
movq %rcx, %r14
movl 0xd0(%rcx), %esi
movq 0xd8(%rcx), %rdx
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x2bfa8
movl 0xd0(%r14), %esi
movq 0xd8(%r14), %rdx
movq %rbx, %rdi
callq 0x1161c
movsd %xmm0, 0x18(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x2fed0
callq 0x42a0
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
movq 0x20(%rsp), %rbx
je 0x2feea
movl 0x14(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
testq %rbp, %rbp
je 0x2fefd
movq %rbp, %rdi
movl 0x14(%rsp), %esi
movl %ebx, %edx
callq 0xca4f
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x2ff0e
movl %ebx, %esi
callq 0xa5e0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2ff1f
movl %ebx, %esi
callq 0xa5e0
movsd 0x18(%rsp), %xmm0
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop