// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x68(%rbp)
movq 0x88(%rdi), %rax
movb 0x8b8(%rax), %al
movb %al, -0x15(%rbp)
movq 0x88(%rdi), %rax
movb 0x8b9(%rax), %al
movb %al, -0x16(%rbp)
movq -0x10(%rbp), %rax
movl 0xe8(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x2c(%rbp), %esi
movq -0x38(%rbp), %rdx
callq 0x1b0c0
movq -0x68(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x38(%rbp), %rdx
callq 0x1b690
movq -0x68(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x38(%rbp), %rdx
callq 0x1b9b0
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x24f83
movsbl -0x15(%rbp), %edi
callq 0xc410
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x24fb0
movsbl -0x15(%rbp), %edi
movsbl -0x15(%rbp), %esi
callq 0xf580
movq %rax, -0x48(%rbp)
movsbl -0x15(%rbp), %edi
movsbl -0x15(%rbp), %esi
callq 0xf580
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x24fdd
movsbl -0x15(%rbp), %edi
movsbl -0x16(%rbp), %esi
callq 0xf580
movq %rax, -0x58(%rbp)
movsbl -0x15(%rbp), %edi
movsbl -0x16(%rbp), %esi
callq 0xf580
movq %rax, -0x60(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x25280
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $0x0, 0x4(%rax)
je 0x25007
jmp 0x25272
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x25067
movb $0x0, -0x17(%rbp)
movsbl -0x17(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x25065
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rax
movq (%rax), %rax
movsbq -0x17(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
cvtsi2sdl -0x2c(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x40(%rbp), %rax
movsbq -0x17(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x17(%rbp), %al
addb $0x1, %al
movb %al, -0x17(%rbp)
jmp 0x25016
jmp 0x25067
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x38(%rbp), %rcx
movslq -0x14(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x25270
movq -0x68(%rbp), %rax
movq 0x88(%rax), %rax
movq 0x870(%rax), %rax
movq -0x38(%rbp), %rcx
movslq -0x14(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x10(%rcx), %rcx
movslq -0x20(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %al
movb %al, -0x1a(%rbp)
movb $0x0, -0x17(%rbp)
movsbl -0x17(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x25260
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x25185
movb $0x0, -0x18(%rbp)
movsbl -0x18(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x25183
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x40(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x48(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x18(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x17(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x50(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x18(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x18(%rbp), %al
addb $0x1, %al
movb %al, -0x18(%rbp)
jmp 0x250e1
jmp 0x25185
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x25251
movb $0x0, -0x19(%rbp)
movsbl -0x19(%rbp), %eax
movsbl -0x16(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2524f
movsbl -0x19(%rbp), %eax
movsbl -0x1a(%rbp), %ecx
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
cvtsi2sd %eax, %xmm0
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x17(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x58(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x38(%rax), %rax
movslq -0x20(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x17(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x60(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movb -0x19(%rbp), %al
addb $0x1, %al
movb %al, -0x19(%rbp)
jmp 0x25198
jmp 0x25251
jmp 0x25253
movb -0x17(%rbp), %al
addb $0x1, %al
movb %al, -0x17(%rbp)
jmp 0x250be
jmp 0x25262
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2506e
jmp 0x25272
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x24fe4
movb $0x0, -0x17(%rbp)
movsbl -0x17(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x253da
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x252bf
movq -0x40(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movsbq -0x17(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x25345
movb $0x0, -0x18(%rbp)
movsbl -0x18(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x25343
movq -0x48(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
movq -0x50(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x18(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x9a40
movaps %xmm0, %xmm1
movsd -0x70(%rbp), %xmm0
divsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x18(%rbp), %al
addb $0x1, %al
movb %al, -0x18(%rbp)
jmp 0x252ce
jmp 0x25345
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x253cb
movb $0x0, -0x19(%rbp)
movsbl -0x19(%rbp), %eax
movsbl -0x16(%rbp), %ecx
cmpl %ecx, %eax
jge 0x253c9
movq -0x58(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x60(%rbp), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x9a40
movaps %xmm0, %xmm1
movsd -0x78(%rbp), %xmm0
divsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x19(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movb -0x19(%rbp), %al
addb $0x1, %al
movb %al, -0x19(%rbp)
jmp 0x25354
jmp 0x253cb
jmp 0x253cd
movb -0x17(%rbp), %al
addb $0x1, %al
movb %al, -0x17(%rbp)
jmp 0x25284
movq -0x68(%rbp), %rdi
callq 0x1a4e0
testb $0x1, %al
jne 0x25472
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x25407
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movsbl -0x15(%rbp), %esi
callq 0x7980
movb $0x0, -0x17(%rbp)
movsbl -0x17(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x2546d
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x2543c
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movsbl -0x15(%rbp), %esi
callq 0x7980
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x25461
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movsbl -0x16(%rbp), %esi
callq 0x7980
jmp 0x25463
movb -0x17(%rbp), %al
addb $0x1, %al
movb %al, -0x17(%rbp)
jmp 0x2540b
jmp 0x255a8
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x254b9
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
callq 0x1a6b0
movq -0x68(%rbp), %rdi
movq %rax, -0x80(%rbp)
callq 0x1a770
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq %rax, %rdx
movsbl -0x15(%rbp), %ecx
callq 0x87e0
movb $0x0, -0x17(%rbp)
movsbl -0x17(%rbp), %eax
movsbl -0x15(%rbp), %ecx
cmpl %ecx, %eax
jge 0x255a6
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x25532
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
callq 0x1a6f0
movq -0x68(%rbp), %rdi
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x90(%rbp)
callq 0x1a7b0
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movsbl -0x15(%rbp), %ecx
callq 0x87e0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x25597
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
callq 0x1a730
movq -0x68(%rbp), %rdi
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa0(%rbp)
callq 0x1a7f0
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movsbq -0x17(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movsbl -0x16(%rbp), %ecx
callq 0x87e0
jmp 0x25599
movb -0x17(%rbp), %al
addb $0x1, %al
movb %al, -0x17(%rbp)
jmp 0x254bd
jmp 0x255a8
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xe8(%rax), %esi
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rdx
callq 0x1b0c0
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xe8(%rax), %esi
movq -0x10(%rbp), %rax
movq 0xf0(%rax), %rdx
callq 0x1a9f0
movsd %xmm0, -0x28(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x255f9
movq -0x40(%rbp), %rdi
callq 0x5380
cmpq $0x0, -0x48(%rbp)
je 0x2560d
movq -0x48(%rbp), %rdi
movsbl -0x15(%rbp), %esi
callq 0xc7f0
cmpq $0x0, -0x50(%rbp)
je 0x25621
movq -0x50(%rbp), %rdi
movsbl -0x15(%rbp), %esi
callq 0xc7f0
cmpq $0x0, -0x58(%rbp)
je 0x25635
movq -0x58(%rbp), %rdi
movsbl -0x15(%rbp), %esi
callq 0xc7f0
cmpq $0x0, -0x60(%rbp)
je 0x25649
movq -0x60(%rbp), %rdi
movsbl -0x15(%rbp), %esi
callq 0xc7f0
movsd -0x28(%rbp), %xmm0
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)