// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x88(%rdi), %rax
movsbq 0x8b8(%rax), %rbx
movsbl 0x8b9(%rax), %ebp
movl 0xe8(%rsi), %r12d
movq 0xf0(%rsi), %r13
movl %r12d, %esi
movq %r13, %rdx
callq 0x11a86
movq %r15, %rdi
movl %r12d, %esi
movq %r13, %rdx
callq 0x11dac
movq %r15, 0x18(%rsp)
movq %r15, %rdi
movq %r12, 0x48(%rsp)
movl %r12d, %esi
movq %r13, 0x78(%rsp)
movq %r13, %rdx
callq 0x11fb6
cmpq $0x0, 0x10(%r14)
movq %r14, 0x8(%rsp)
je 0x17e95
movl $0x8, %esi
movq %rbx, %rdi
callq 0x4130
movq 0x8(%rsp), %rcx
jmp 0x17e9a
movq %r14, %rcx
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movzbl %bl, %r14d
cmpq $0x0, 0x18(%rcx)
je 0x17ec9
movl %ebx, %edi
movl %ebx, %esi
callq 0xbb5c
movq %rax, %r12
movl %ebx, %edi
movl %ebx, %esi
callq 0xbb5c
movq 0x8(%rsp), %rcx
movq %rax, %r13
jmp 0x17ecf
xorl %r12d, %r12d
xorl %r13d, %r13d
movzbl %bpl, %r15d
cmpq $0x0, 0x20(%rcx)
movq %rbx, 0x28(%rsp)
movl %ebp, 0x24(%rsp)
je 0x17f01
movl %ebx, %edi
movl %ebp, %esi
callq 0xbb5c
movq %rax, 0x40(%rsp)
movl %ebx, %edi
movl %ebp, %esi
callq 0xbb5c
movq %rax, 0x38(%rsp)
jmp 0x17f13
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
cmpl $0x0, 0x48(%rsp)
jle 0x180d5
cvtsi2sdl 0x48(%rsp), %xmm0
movq 0x18(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x60(%rsp)
xorl %ecx, %ecx
movsd 0x1c9b1(%rip), %xmm1    # 0x348f0
movq 0x78(%rsp), %rax
movq %rcx, 0x80(%rsp)
movq (%rax,%rcx,8), %rdx
cmpl $0x0, 0x4(%rdx)
jne 0x180bf
movq 0x8(%rsp), %rax
cmpq $0x0, 0x10(%rax)
movq 0x30(%rsp), %rsi
je 0x17f94
testb %r14b, %r14b
jle 0x17f94
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, %r14
jne 0x17f79
movl (%rdx), %eax
movq %rax, 0x10(%rsp)
cmpl $0x2, %eax
jl 0x180bf
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x10(%rsp)
movq 0x60(%rsp), %rax
movq 0x870(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x50(%rsp)
xorl %r9d, %r9d
testb %r14b, %r14b
jle 0x180b1
testb %r15b, %r15b
setle %al
movq 0x50(%rsp), %rcx
movslq (%rcx,%r9,4), %rcx
movq 0x58(%rsp), %rsi
movzbl (%rsi,%rcx), %r10d
movq 0x8(%rsp), %rcx
cmpq $0x0, 0x20(%rcx)
movq 0x18(%rcx), %r11
sete %bl
orb %al, %bl
xorl %ebp, %ebp
testq %r11, %r11
je 0x18050
movq 0x38(%rdx), %rsi
movq 0x40(%rdx), %rax
movq (%rax,%r9,8), %rax
movq (%rax,%rbp,8), %rax
movq (%r12,%rbp,8), %rcx
movq (%rsi,%r9,8), %rdi
movq (%r13,%rbp,8), %r8
xorl %esi, %esi
movsd (%rax,%rsi,8), %xmm2
addsd (%rcx,%rsi,8), %xmm2
movsd %xmm2, (%rcx,%rsi,8)
movsd (%rdi,%rbp,8), %xmm2
addsd (%r8,%rsi,8), %xmm2
movsd %xmm2, (%r8,%rsi,8)
incq %rsi
cmpq %rsi, %r14
jne 0x18028
testb %bl, %bl
jne 0x180a5
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 0x1807d
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, %r15
jne 0x18070
incq %rbp
cmpq %r14, %rbp
jne 0x18004
incq %r9
cmpq 0x10(%rsp), %r9
jne 0x17fcd
movq 0x80(%rsp), %rcx
incq %rcx
cmpq 0x48(%rsp), %rcx
jne 0x17f3f
cmpb $0x0, 0x28(%rsp)
movq 0x8(%rsp), %rdx
jle 0x181e4
movslq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
movzbl 0x24(%rsp), %eax
xorl %ebp, %ebp
movq %r12, 0x70(%rsp)
movq %r13, 0x68(%rsp)
movq %rax, 0x58(%rsp)
movq 0x10(%rdx), %rax
testq %rax, %rax
je 0x1811d
movq 0x30(%rsp), %rcx
movsd (%rcx,%rbp,8), %xmm0
movsd %xmm0, (%rax,%rbp,8)
cmpq $0x0, 0x18(%rdx)
je 0x18168
xorl %ebx, %ebx
movq (%r12,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%r13,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm0
callq 0x8d88
movq 0x8(%rsp), %rdx
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x18(%rdx), %rax
movq (%rax,%rbp,8), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %rbx
cmpq %rbx, %r14
jne 0x18126
cmpq $0x0, 0x20(%rdx)
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %r12
je 0x181cc
testb %r15b, %r15b
jle 0x181cc
xorl %ebx, %ebx
movq (%rcx,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%r12,%rbp,8), %rax
movsd (%rax,%rbx,8), %xmm0
movq %rcx, %r13
callq 0x8d88
movq %r13, %rcx
movq 0x8(%rsp), %rdx
movsd 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movq 0x20(%rdx), %rax
movq (%rax,%rbp,8), %rax
movsd %xmm1, (%rax,%rbx,8)
movq 0x58(%rsp), %rax
incq %rbx
cmpq %rbx, %rax
jne 0x18180
incq %rbp
cmpq 0x50(%rsp), %rbp
movq 0x70(%rsp), %r12
movq 0x68(%rsp), %r13
jne 0x18105
movq 0x10(%rdx), %rdi
movq 0x18(%rsp), %rax
cmpb $0x0, 0x80(%rax)
je 0x182b7
testq %rdi, %rdi
movq 0x28(%rsp), %rbp
je 0x1821f
movq 0x18(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x68(%rax), %rdx
movsbl %r14b, %ecx
callq 0x79f5
movq 0x8(%rsp), %rdx
testb %r14b, %r14b
jle 0x18318
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x18(%rdx), %rax
testq %rax, %rax
je 0x1826a
movq (%rax,%r14,8), %rdi
movq 0x18(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq 0x58(%rcx), %rax
cmoveq %rbx, %rax
movq (%rax,%r14,8), %rsi
movq 0x70(%rcx), %rax
cmoveq %rbx, %rax
movq (%rax,%r14,8), %rdx
movl %ebp, %ecx
callq 0x79f5
movq 0x8(%rsp), %rdx
movq 0x20(%rdx), %rax
testq %rax, %rax
je 0x182a9
movq (%rax,%r14,8), %rdi
movq 0x18(%rsp), %rcx
cmpb $0x0, 0x80(%rcx)
movq 0x60(%rcx), %rax
cmoveq %rbx, %rax
movq (%rax,%r14,8), %rsi
movq 0x78(%rcx), %rax
cmoveq %rbx, %rax
movq (%rax,%r14,8), %rdx
movsbl %r15b, %ecx
callq 0x79f5
movq 0x8(%rsp), %rdx
incq %r14
cmpq %r14, %rbp
jne 0x1822d
jmp 0x18318
testq %rdi, %rdi
movq 0x28(%rsp), %rbp
je 0x182cf
movsbl %r14b, %esi
callq 0x692d
movq 0x8(%rsp), %rdx
testb %bpl, %bpl
jle 0x18318
movslq %ebp, %rbx
xorl %r15d, %r15d
movq 0x18(%rdx), %rax
testq %rax, %rax
je 0x182f5
movq (%rax,%r15,8), %rdi
movsbl %r14b, %esi
callq 0x692d
movq 0x8(%rsp), %rdx
movq 0x20(%rdx), %rax
testq %rax, %rax
je 0x18310
movq (%rax,%r15,8), %rdi
movl 0x24(%rsp), %esi
callq 0x692d
movq 0x8(%rsp), %rdx
incq %r15
cmpq %r15, %rbx
jne 0x182da
movl 0xe8(%rdx), %esi
movq 0x8(%rsp), %rax
movq 0xf0(%rax), %rdx
movq 0x18(%rsp), %rdi
callq 0x11a86
movq 0x8(%rsp), %rcx
movslq 0xe8(%rcx), %rax
testq %rax, %rax
jle 0x18378
movq 0xf0(%rcx), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq 0x30(%rsp), %rdi
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x4(%rsi)
xorpd %xmm1, %xmm1
jne 0x1836a
movsd 0x50(%rsi), %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x18357
jmp 0x18381
xorpd %xmm0, %xmm0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
movsd %xmm0, 0x8(%rsp)
je 0x18397
callq 0x42a0
movsd 0x8(%rsp), %xmm0
testq %r12, %r12
je 0x183ac
movq %r12, %rdi
movl %ebp, %esi
callq 0xa5e0
movsd 0x8(%rsp), %xmm0
testq %r13, %r13
je 0x183c1
movq %r13, %rdi
movl %ebp, %esi
callq 0xa5e0
movsd 0x8(%rsp), %xmm0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x183d8
movl %ebp, %esi
callq 0xa5e0
movsd 0x8(%rsp), %xmm0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x183ef
movl %ebp, %esi
callq 0xa5e0
movsd 0x8(%rsp), %xmm0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop