// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %rbx
movsbl 0x8b9(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xe8(%rsi), %r12d
movq 0xf0(%rsi), %r13
movl %r12d, %esi
movq %r13, %rdx
callq 0x11908
movq %r15, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x11c5e
movq %r15, 0x10(%rsp)
movq %r15, %rdi
movq %r12, 0x18(%rsp)
movl %r12d, %esi
movq %r13, 0x70(%rsp)
movq %r13, %rdx
callq 0x11e40
cmpq $0x0, 0x10(%r14)
movq %r14, (%rsp)
je 0x17c95
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4130
movq (%rsp), %rcx
movq %rax, %r12
jmp 0x17c9b
movq %r14, %rcx
xorl %r12d, %r12d
movzbl %bl, %r15d
cmpq $0x0, 0x18(%rcx)
je 0x17cc6
movl %ebx, %edi
movl %ebx, %esi
callq 0xbb36
movq %rax, 0x48(%rsp)
movl %ebx, %edi
movl %ebx, %esi
callq 0xbb36
movq (%rsp), %rcx
movq %rax, %r13
jmp 0x17cd2
movq $0x0, 0x48(%rsp)
xorl %r13d, %r13d
movl 0xc(%rsp), %ebp
movzbl %bpl, %r14d
cmpq $0x0, 0x20(%rcx)
movq %r12, 0x28(%rsp)
movq %rbx, 0x50(%rsp)
je 0x17d09
movl %ebx, %edi
movl %ebp, %esi
callq 0xbb36
movq %rax, 0x40(%rsp)
movl %ebx, %edi
movl %ebp, %esi
callq 0xbb36
movq %rax, 0x38(%rsp)
jmp 0x17d1b
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
cmpl $0x0, 0x18(%rsp)
jle 0x17ecd
cvtsi2sdl 0x18(%rsp), %xmm0
movq 0x10(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x68(%rsp)
xorl %ecx, %ecx
movsd 0x1bba9(%rip), %xmm1    # 0x338f0
movq 0x70(%rsp), %rax
movq %rcx, 0x58(%rsp)
movq (%rax,%rcx,8), %rdx
cmpl $0x0, 0x4(%rdx)
jne 0x17eba
movq (%rsp), %rax
cmpq $0x0, 0x10(%rax)
movq 0x28(%rsp), %rsi
je 0x17d98
testb %r15b, %r15b
jle 0x17d98
movq 0x38(%rdx), %rax
movq (%rax), %rax
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm2
divsd %xmm0, %xmm2
addsd (%rsi,%rcx,8), %xmm2
movsd %xmm2, (%rsi,%rcx,8)
incq %rcx
cmpq %rcx, %r15
jne 0x17d7d
movl (%rdx), %esi
cmpl $0x2, %esi
jl 0x17eba
decl %esi
movq 0x68(%rsp), %rax
movq 0x870(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x30(%rsp)
xorl %r9d, %r9d
testb %r15b, %r15b
jle 0x17eae
testb %r14b, %r14b
setle %al
movq 0x30(%rsp), %rcx
movslq (%rcx,%r9,4), %rcx
movq 0x20(%rsp), %rdi
movzbl (%rdi,%rcx), %r10d
movq (%rsp), %rcx
cmpq $0x0, 0x20(%rcx)
movq 0x18(%rcx), %r11
sete %bl
orb %al, %bl
xorl %ebp, %ebp
testq %r11, %r11
je 0x17e4d
movq 0x38(%rdx), %rdi
movq 0x40(%rdx), %rax
movq (%rax,%r9,8), %rax
movq (%rax,%rbp,8), %rax
movq 0x48(%rsp), %rcx
movq (%rcx,%rbp,8), %rcx
movq (%rdi,%r9,8), %rdi
movq (%r13,%rbp,8), %r8
xorl %r12d, %r12d
movsd (%rax,%r12,8), %xmm2
addsd (%rcx,%r12,8), %xmm2
movsd %xmm2, (%rcx,%r12,8)
movsd (%rdi,%rbp,8), %xmm2
addsd (%r8,%r12,8), %xmm2
movsd %xmm2, (%r8,%r12,8)
incq %r12
cmpq %r12, %r15
jne 0x17e22
testb %bl, %bl
jne 0x17ea2
movq 0x38(%rdx), %rax
movq (%rax,%r9,8), %rax
movq 0x40(%rsp), %rcx
movq (%rcx,%rbp,8), %rdi
movq 0x38(%rsp), %rcx
movq (%rcx,%rbp,8), %r8
xorl %ecx, %ecx
movapd %xmm1, %xmm2
cmpq %rcx, %r10
je 0x17e7a
xorpd %xmm2, %xmm2
mulsd (%rax,%rbp,8), %xmm2
addsd (%rdi,%rcx,8), %xmm2
movsd %xmm2, (%rdi,%rcx,8)
movsd (%rax,%rbp,8), %xmm2
addsd (%r8,%rcx,8), %xmm2
movsd %xmm2, (%r8,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x17e6d
incq %rbp
cmpq %r15, %rbp
jne 0x17df8
incq %r9
cmpq %rsi, %r9
jne 0x17dc2
movq 0x58(%rsp), %rcx
incq %rcx
cmpq 0x18(%rsp), %rcx
jne 0x17d47
testb %r15b, %r15b
movq (%rsp), %rdx
jle 0x17ffd
movslq 0x50(%rsp), %rax
movzbl 0xc(%rsp), %ebx
xorl %ebp, %ebp
movq %r13, 0x60(%rsp)
movq %rax, 0x30(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x10(%rdx), %rax
testq %rax, %rax
je 0x17f0d
movq 0x28(%rsp), %rcx
movsd (%rcx,%rbp,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
cmpq $0x0, 0x18(%rdx)
movq 0x48(%rsp), %rbx
movq 0x60(%rsp), %r13
je 0x17f6a
xorl %r12d, %r12d
movq (%rbx,%rbp,8), %rax
movsd (%rax,%r12,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq (%r13,%rbp,8), %rax
movsd (%rax,%r12,8), %xmm0
callq 0x8df8
movq (%rsp), %rdx
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x18(%rdx), %rax
movq (%rax,%rbp,8), %rax
movsd %xmm1, (%rax,%r12,8)
movq 0x30(%rsp), %rax
incq %r12
cmpq %r12, %rax
jne 0x17f21
cmpq $0x0, 0x20(%rdx)
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rcx
movq 0x18(%rsp), %rbx
je 0x17fe7
testb %r14b, %r14b
jle 0x17fe7
xorl %r12d, %r12d
movq %rbp, 0x58(%rsp)
movq %rax, %r13
movq (%rax,%rbp,8), %rax
movsd (%rax,%r12,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq (%rcx,%rbp,8), %rax
movsd (%rax,%r12,8), %xmm0
movq %rbx, %rbp
movq %rcx, %rbx
callq 0x8df8
movq %rbx, %rcx
movq %rbp, %rbx
movq 0x58(%rsp), %rbp
movq (%rsp), %rdx
movsd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x20(%rdx), %rax
movq (%rax,%rbp,8), %rax
movsd %xmm1, (%rax,%r12,8)
movq %r13, %rax
incq %r12
cmpq %r12, %rbx
jne 0x17f8d
incq %rbp
movq 0x30(%rsp), %rax
cmpq %rax, %rbp
movq 0x60(%rsp), %r13
jne 0x17ef5
movq 0x10(%rdx), %rdi
movq 0x10(%rsp), %rax
cmpb $0x0, 0x80(%rax)
je 0x180d2
testq %rdi, %rdi
movq 0x50(%rsp), %r12
movq 0x28(%rsp), %rbp
je 0x1803b
movq 0x10(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x68(%rax), %rdx
movl %r12d, %ecx
callq 0x7997
movq (%rsp), %rdx
testb %r12b, %r12b
jle 0x18138
xorl %ebx, %ebx
xorl %r15d, %r15d
movq 0x18(%rdx), %rax
testq %rax, %rax
je 0x18086
movq (%rax,%r15,8), %rdi
movq 0x10(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq 0x58(%rcx), %rax
cmoveq %rbx, %rax
movq (%rax,%r15,8), %rsi
movq 0x70(%rcx), %rax
cmoveq %rbx, %rax
movq (%rax,%r15,8), %rdx
movl %r12d, %ecx
callq 0x7997
movq (%rsp), %rdx
movq 0x20(%rdx), %rax
testq %rax, %rax
je 0x180c4
movq (%rax,%r15,8), %rdi
movq 0x10(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq 0x60(%rcx), %rax
cmoveq %rbx, %rax
movq (%rax,%r15,8), %rsi
movq 0x78(%rcx), %rax
cmoveq %rbx, %rax
movq (%rax,%r15,8), %rdx
movsbl %r14b, %ecx
callq 0x7997
movq (%rsp), %rdx
incq %r15
cmpq %r15, %r12
jne 0x18049
jmp 0x18138
testq %rdi, %rdi
movq 0x50(%rsp), %r12
movq 0x28(%rsp), %rbp
je 0x180ee
movsbl %r15b, %esi
callq 0x6836
movq (%rsp), %rdx
testb %r12b, %r12b
movl 0xc(%rsp), %r15d
jle 0x18138
movslq %r12d, %rbx
xorl %r14d, %r14d
movq 0x18(%rdx), %rax
testq %rax, %rax
je 0x18117
movq (%rax,%r14,8), %rdi
movl %r12d, %esi
callq 0x6836
movq (%rsp), %rdx
movq 0x20(%rdx), %rax
testq %rax, %rax
je 0x18130
movq (%rax,%r14,8), %rdi
movl %r15d, %esi
callq 0x6836
movq (%rsp), %rdx
incq %r14
cmpq %r14, %rbx
jne 0x180fe
movq (%rsp), %rax
movl 0xe8(%rax), %esi
movq (%rsp), %rax
movq 0xf0(%rax), %rdx
movq 0x10(%rsp), %rdi
callq 0x11908
movq (%rsp), %rcx
movslq 0xe8(%rcx), %rax
testq %rax, %rax
jle 0x18195
movq 0xf0(%rcx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
xorpd %xmm1, %xmm1
jne 0x18187
movsd 0x50(%rsi), %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x18174
jmp 0x18199
xorpd %xmm0, %xmm0
testq %rbp, %rbp
movsd %xmm0, (%rsp)
je 0x181b0
movq %rbp, %rdi
callq 0x42a0
movsd (%rsp), %xmm0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x181c7
movl %r12d, %esi
callq 0xa5dd
movsd (%rsp), %xmm0
testq %r13, %r13
je 0x181dc
movq %r13, %rdi
movl %r12d, %esi
callq 0xa5dd
movsd (%rsp), %xmm0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x181f3
movl %r12d, %esi
callq 0xa5dd
movsd (%rsp), %xmm0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x1820a
movl %r12d, %esi
callq 0xa5dd
movsd (%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop