// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::doBaumWelchStep(FitBit*)
NUMBER HMMProblem::doBaumWelchStep(FitBit *fb) {
	NCAT x;
    NPAR nS = this->p->nS, nO = this->p->nO;
	NPAR i,j,m, o;
	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 = init2D<NUMBER>((NDAT)nS, (NDAT)nS);
        b_A_den = init2D<NUMBER>((NDAT)nS, (NDAT)nS);
//        c_A     = init2D<NUMBER>((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     = init2D<NUMBER>((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;
        
//        toZero2D(b_A_num, (NDAT)nS, (NDAT)nS); // A,B average across sequences
//        toZero2D(b_A_den, (NDAT)nS, (NDAT)nS); // A,B average across sequences
//        toZero2D(b_B_num, (NDAT)nS, (NDAT)nO); // A,B average across sequences
//        toZero2D(b_B_den, (NDAT)nS, (NDAT)nO); // A,B average across sequences
        
        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++){
                        b_A_num[i][j] += x_data[x]->xi[t][i][j];
                        b_A_den[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];
                    }
                }
			}
		}
//        if(fb->A != NULL)  // A,B average across sequences
//            for(j=0; j<nS; j++)  // A,B average across sequences
//                c_A[i][j] += b_A_num[i][j] / safe0num(b_A_den[i][j]);  // A,B average across sequences
//        if(fb->B != NULL)  // A,B average across sequences
//            for(m=0; m<nO; m++)  // A,B average across sequences
//                c_B[i][m] = b_B_num[i][m] / safe0num(b_B_den[i][m]);  // A,B average across sequences
	} // 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++) {
                fb->A[i][j] = b_A_num[i][j] / safe0num(b_A_den[i][j]);
//                fb->A[i][j] = c_A[i][j] / xndat; // A,B average across sequences
            }
        }
        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]);
//                fb->B[i][m] = c_B[i][m] / xndat; // A,B average across sequences
            }
        }
	}
    // scale
    if( !this->hasNon01Constraints()) {
        if(fb->pi != NULL) {
            projectsimplex(fb->pi, nS);
        }
        for(i=0; i<nS; i++) {
            if(fb->A != NULL) {
                projectsimplex(fb->A[i], nS);
            }
            if(fb->B != NULL) {
                projectsimplex(fb->B[i], nO);
            }
        }
    } else {
        if(fb->pi != NULL) {
            projectsimplexbounded(fb->pi, this->getLbPI(), this->getUbPI(), nS);
        }
        for(i=0; i<nS; i++) {
            if(fb->A != NULL) {
                projectsimplexbounded(fb->A[i], this->getLbA()[i], this->getUbA()[i], nS);
            }
            if(fb->B != NULL) {
                projectsimplexbounded(fb->B[i], this->getLbB()[i], this->getUbB()[i], nO);
            }
        }
    }
    
//    NUMBER sumP=0, sumA=0, sumB=0;
//    for(i=0; i<nS; i++) {
//        sumP += fb->pi[i];
//        sumA=0;
//        for(j=0; j<nS; j++)
//            sumA += fb->A[i][j];
//        if(fabs(sumA-1)>SAFETY) {
//            int a = 0;
//        }
//        sumB=0;
//        for(m=0; m<nO; m++)
//            sumB += fb->B[i][m];
//        if(fabs(sumB-1)>SAFETY) {
//            int a = 0;
//        }
//    }
//    if(fabs(sumP-1)>SAFETY) {
//        int a = 0;
//    }
    
    // compute LL
    computeAlphaAndPOParam(fb->xndat, fb->x_data);
    ll = HMMProblem::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) free2D<NUMBER>(b_A_num, nS);
	if(b_A_den != NULL) free2D<NUMBER>(b_A_den, 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) free2D<NUMBER>(c_A, nS); // A,B average across sequences
//    if(c_B     != NULL) free2D<NUMBER>(c_B, nS); // A,B average across sequences
    return ll;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movq 0x88(%rdi), %rax
movsbl 0x8b8(%rax), %r14d
movsbl 0x8b9(%rax), %r12d
movl 0xe8(%rsi), %r13d
movq 0xf0(%rsi), %rbp
movl %r13d, %esi
movq %rbp, %rdx
callq 0xe7b0
movq %r15, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0xea9e
movq %r15, 0x18(%rsp)
movq %r15, %rdi
movq %r13, 0x10(%rsp)
movl %r13d, %esi
movq %rbp, 0x40(%rsp)
movq %rbp, %rdx
callq 0xec8c
movq %rbx, (%rsp)
cmpq $0x0, 0x10(%rbx)
je 0x1407d
movl %r14d, %edi
callq 0x7dde
movq %rax, 0x30(%rsp)
jmp 0x14086
movq $0x0, 0x30(%rsp)
movq (%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x140af
movl %r14d, %edi
movl %r14d, %esi
callq 0x9328
movq %rax, %r13
movl %r14d, %edi
movl %r14d, %esi
callq 0x9328
movq %rax, %rbp
jmp 0x140b4
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %esi, %esi
movq (%rsp), %rax
cmpq $0x0, 0x20(%rax)
je 0x140e1
movl %r14d, %edi
movl %r12d, %esi
callq 0x9328
movq %rax, %r15
movl %r14d, %edi
movl %r12d, %esi
callq 0x9328
xorl %esi, %esi
movq %rax, %rbx
jmp 0x140e6
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x10(%rsp), %rcx
cvtsi2sd %ecx, %xmm0
testb %r14b, %r14b
movl $0x0, %edx
cmovgl %r14d, %edx
testb %r12b, %r12b
movl %r12d, %eax
movl $0x0, %r12d
movl %eax, 0x2c(%rsp)
cmovgl %eax, %r12d
testl %ecx, %ecx
cmovlel %esi, %ecx
movq %rcx, 0x10(%rsp)
movl $0xff, %ecx
andl %ecx, %edx
movq %rdx, 0x20(%rsp)
movl %r14d, 0xc(%rsp)
movl %r14d, %eax
andl %ecx, %eax
andl %ecx, %r12d
movsd 0x187b6(%rip), %xmm1    # 0x2c8f0
cmpq 0x10(%rsp), %rsi
je 0x142a1
movq 0x40(%rsp), %rcx
movq %rsi, 0x38(%rsp)
movq (%rcx,%rsi,8), %rdx
cmpl $0x0, 0x4(%rdx)
je 0x14163
movq 0x38(%rsp), %rsi
incq %rsi
jmp 0x1413a
movq (%rsp), %rcx
cmpq $0x0, 0x10(%rcx)
je 0x1419b
xorl %esi, %esi
cmpq %rsi, 0x20(%rsp)
je 0x1419b
movq 0x38(%rdx), %rdi
movq (%rdi), %rdi
movsd (%rdi,%rsi,8), %xmm2
divsd %xmm0, %xmm2
movq 0x30(%rsp), %rcx
addsd (%rcx,%rsi,8), %xmm2
movsd %xmm2, (%rcx,%rsi,8)
incq %rsi
jmp 0x14170
movl (%rdx), %esi
movq 0x18(%rsp), %rcx
movq 0x88(%rcx), %rdi
cmpl $0x2, %esi
pushq $0x1
popq %rcx
cmovll %ecx, %esi
decl %esi
xorl %r8d, %r8d
cmpq %rsi, %r8
je 0x14159
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 0x14299
movq (%rsp), %rcx
cmpq $0x0, 0x18(%rcx)
je 0x14238
xorl %r11d, %r11d
cmpq %r11, %rax
je 0x14238
movq 0x40(%rdx), %r14
movq (%r14,%r8,8), %r14
movq (%r14,%r10,8), %r14
movsd (%r14,%r11,8), %xmm2
movq (%r13,%r10,8), %r14
addsd (%r14,%r11,8), %xmm2
movsd %xmm2, (%r14,%r11,8)
movq 0x38(%rdx), %r14
movq (%r14,%r8,8), %r14
movsd (%r14,%r10,8), %xmm2
movq (%rbp,%r10,8), %r14
addsd (%r14,%r11,8), %xmm2
movsd %xmm2, (%r14,%r11,8)
incq %r11
jmp 0x141ec
movq (%rsp), %rcx
cmpq $0x0, 0x20(%rcx)
je 0x14291
xorl %r11d, %r11d
cmpq %r11, %r12
je 0x14291
movapd %xmm1, %xmm2
cmpq %r11, %r9
je 0x14258
xorpd %xmm2, %xmm2
movq 0x38(%rdx), %r14
movq (%r14,%r8,8), %r14
movq (%r15,%r10,8), %rcx
mulsd (%r14,%r10,8), %xmm2
addsd (%rcx,%r11,8), %xmm2
movsd %xmm2, (%rcx,%r11,8)
movsd (%r14,%r10,8), %xmm2
movq (%rbx,%r10,8), %rcx
addsd (%rcx,%r11,8), %xmm2
movsd %xmm2, (%rcx,%r11,8)
incq %r11
jmp 0x14246
incq %r10
jmp 0x141d3
incq %r8
jmp 0x141b7
xorl %edx, %edx
cmpq 0x20(%rsp), %rdx
je 0x1438b
movq (%rsp), %rax
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x142ca
movq 0x30(%rsp), %rcx
movsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
movq (%rsp), %rax
cmpq $0x0, 0x18(%rax)
movq %rdx, 0x38(%rsp)
je 0x1432b
xorl %r14d, %r14d
cmpq %r14, 0x20(%rsp)
je 0x1432b
movq (%r13,%rdx,8), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%rbp,%rdx,8), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x67cb
movq 0x38(%rsp), %rdx
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movq (%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rdx,8), %rax
movsd %xmm1, (%rax,%r14,8)
incq %r14
jmp 0x142dd
movq (%rsp), %rax
cmpq $0x0, 0x20(%rax)
je 0x14383
xorl %r14d, %r14d
cmpq %r14, %r12
je 0x14383
movq (%r15,%rdx,8), %rax
movsd (%rax,%r14,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%rbx,%rdx,8), %rax
movsd (%rax,%r14,8), %xmm0
callq 0x67cb
movq 0x38(%rsp), %rdx
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movq (%rsp), %rax
movq 0x20(%rax), %rax
movq (%rax,%rdx,8), %rax
movsd %xmm1, (%rax,%r14,8)
incq %r14
jmp 0x14339
incq %rdx
jmp 0x142a3
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x18(%rsp), %rax
cmpb $0x0, 0x80(%rax)
je 0x14455
testq %rdi, %rdi
movl 0xc(%rsp), %ecx
je 0x143c0
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x68(%rax), %rdx
callq 0x5d80
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpq %r12, 0x20(%rsp)
je 0x144a6
movq (%rsp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x1440f
movq (%rax,%r12,8), %rdi
movq 0x18(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq 0x58(%rcx), %rax
cmoveq %r14, %rax
movq (%rax,%r12,8), %rsi
movq 0x70(%rcx), %rax
cmoveq %r14, %rax
movq (%rax,%r12,8), %rdx
movl 0xc(%rsp), %ecx
callq 0x5d80
movq (%rsp), %rax
movq 0x20(%rax), %rax
testq %rax, %rax
je 0x1444d
movq (%rax,%r12,8), %rdi
movq 0x18(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq 0x60(%rcx), %rax
cmoveq %r14, %rax
movq (%rax,%r12,8), %rsi
movq 0x78(%rcx), %rax
cmoveq %r14, %rax
movq (%rax,%r12,8), %rdx
movl 0x2c(%rsp), %ecx
callq 0x5d80
incq %r12
jmp 0x143c6
testq %rdi, %rdi
movl 0xc(%rsp), %esi
je 0x14463
callq 0x5600
xorl %r12d, %r12d
cmpq %r12, 0x20(%rsp)
je 0x144a6
movq (%rsp), %rax
movq 0x18(%rax), %rax
testq %rax, %rax
je 0x14487
movq (%rax,%r12,8), %rdi
movl 0xc(%rsp), %esi
callq 0x5600
movq (%rsp), %rax
movq 0x20(%rax), %rax
testq %rax, %rax
je 0x144a1
movq (%rax,%r12,8), %rdi
movl 0x2c(%rsp), %esi
callq 0x5600
incq %r12
jmp 0x14466
movq (%rsp), %r14
movl 0xe8(%r14), %esi
movq 0xf0(%r14), %rdx
movq 0x18(%rsp), %rdi
callq 0xe7b0
movl 0xe8(%r14), %esi
movq 0xf0(%r14), %rdx
callq 0xe400
movsd %xmm0, (%rsp)
movq 0x30(%rsp), %rdi
callq 0x4290
testq %r13, %r13
movl 0xc(%rsp), %r14d
je 0x144f9
movq %r13, %rdi
movl %r14d, %esi
callq 0x7f2c
testq %rbp, %rbp
je 0x14509
movq %rbp, %rdi
movl %r14d, %esi
callq 0x7f2c
testq %r15, %r15
je 0x14519
movq %r15, %rdi
movl %r14d, %esi
callq 0x7f2c
testq %rbx, %rbx
je 0x14529
movq %rbx, %rdi
movl %r14d, %esi
callq 0x7f2c
movsd (%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop