// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblem.cpp
// Function: HMMProblem::computeAlphaAndPOParam(int, data**)
NDAT HMMProblem::computeAlphaAndPOParam(NCAT xndat, struct data** x_data) {
    //    NUMBER mult_c, old_pOparam, neg_sum_log_c;
	initAlpha(xndat, x_data);

    preprocessglobal(); // nothing for BKT, computes Elo values for Elo-based

    NPAR nS = this->p->nS;
    NDAT  ndat = 0;
//    int parallel_now = this->p->parallel==2; //PAR
//    #pragma omp parallel for schedule(dynamic) if(parallel_now) reduction(+:ndat) //PAR
	for(NCAT x=0; x<xndat; x++) {
        NDAT t;
        NPAR i, j, o;
		if( x_data[x]->cnt!=0 ) continue; // ... and the thing has not been computed yet (e.g. from group to skill)
        ndat += x_data[x]->n; // reduction'ed
		for(t=0; t<x_data[x]->n; t++) {
//			o = x_data[x]->obs[t];
			o = this->p->dat_obs[ x_data[x]->ix[t] ];//->get( x_data[x]->ix[t] );
			x_data[x]->t = x_data[x]->ix[t]; // statefullness
            if(t==0) { // it's alpha(1,i)
                // compute \alpha_1(i) = \pi_i b_i(o_1)
				for(i=0; i<nS; i++) {
					x_data[x]->alpha[t][i] = getPI(x_data[x],i) * ((o<0)?1:getB(x_data[x],i,o)); // if observatiob unknown use 1
                    if(this->p->scaled==1) x_data[x]->c[t] += x_data[x]->alpha[t][i];
                }
			} else { // it's alpha(t,i)
				// compute \alpha_{t}(i) = b_j(o_{t})\sum_{j=1}^N{\alpha_{t-1}(j) a_{ji}}
				for(i=0; i<nS; i++) {
					for(j=0; j<nS; j++) {
						x_data[x]->alpha[t][i] += x_data[x]->alpha[t-1][j] * getA(x_data[x],j,i);
					}
					x_data[x]->alpha[t][i] *= ((o<0)?1:getB(x_data[x],i,o)); // if observatiob unknown use 1
//                    if( x_data[x]->alpha[t][i] < 0 || x_data[x]->alpha[t][i] > 1)
//                        fprintf(stderr, "ERROR! alpha value is not within [0, 1] range!\n");
                    if(this->p->scaled==1) x_data[x]->c[t] += x_data[x]->alpha[t][i];
				}
			}
            // scale \alpha_{t}(i) - same for t=1 or otherwise
            if(this->p->scaled==1) {
                x_data[x]->c[t] = 1/x_data[x]->c[t];//safe0num();
                for(i=0; i<nS; i++) x_data[x]->alpha[t][i] *= x_data[x]->c[t];
            }
            
            if(this->p->scaled==1)  x_data[x]->loglik += log(x_data[x]->c[t]);
		} // for all observations within skill-group
        if(this->p->scaled==1)  x_data[x]->p_O_param = exp( -x_data[x]->loglik );
        else {
            x_data[x]->p_O_param = 0; // 0 for non-scaled
            for(i=0; i<nS; i++) x_data[x]->p_O_param += x_data[x]->alpha[x_data[x]->n-1][i];
//            if( x_data[x]->p_O_param < 0 || x_data[x]->p_O_param >1 )
//                fprintf(stderr, "ERROR! p(O|param) not within [0, 1] range!\n");
            x_data[x]->loglik = -safelog(x_data[x]->p_O_param);
//            if( x_data[x]->loglik < 0)
//                fprintf(stderr, "ERROR! -log( p(O|param) ) is negative!\n");
        }
	} // for all groups in skill
    return ndat; //TODO, figure out a diff way to sum it, and not multiple times
    // especially for parallel version
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbx
movl %esi, %r14d
movq %rdi, %r15
callq 0xe42e
movq (%r15), %rax
movq %r15, %rdi
callq *0x60(%rax)
movq 0x88(%r15), %rax
movzbl 0x8b8(%rax), %eax
xorl %ebp, %ebp
testb %al, %al
movl $0x0, %r13d
movq %rax, 0x30(%rsp)
cmovgl %eax, %r13d
testl %r14d, %r14d
cmovlel %ebp, %r14d
xorl %r12d, %r12d
movq %r14, 0x20(%rsp)
movq %r13, 0x28(%rsp)
cmpq %r14, %rbp
je 0xea8b
movq (%rbx,%rbp,8), %rsi
cmpl $0x0, 0x4(%rsi)
jne 0xea83
movl %r12d, 0x8(%rsp)
movl (%rsi), %eax
movl %eax, 0x1c(%rsp)
xorl %r12d, %r12d
cltq
cmpq %rax, %r12
jge 0xea05
movq 0x88(%r15), %rax
movq 0x870(%rax), %rcx
movq 0x10(%rsi), %rdx
movslq (%rdx,%r12,4), %rdx
movb (%rcx,%rdx), %cl
movl %edx, 0x8(%rsi)
movb %cl, 0x7(%rsp)
movsbl %cl, %ecx
movl %ecx, 0xc(%rsp)
xorl %r14d, %r14d
testq %r12, %r12
je 0xe9a1
cmpq %r13, %r14
je 0xe9a6
movq %rbx, %r13
xorl %ebx, %ebx
cmpq %rbx, 0x30(%rsp)
je 0xe8ca
movq 0x28(%rsi), %rax
movq -0x8(%rax,%r12,8), %rax
movsd (%rax,%rbx,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%r15), %rax
movsbl %r14b, %ecx
movsbl %bl, %edx
movq %r15, %rdi
callq *0x18(%rax)
movq (%r13,%rbp,8), %rsi
movq 0x28(%rsi), %rax
movq (%rax,%r12,8), %rax
mulsd 0x10(%rsp), %xmm0
addsd (%rax,%r14,8), %xmm0
movsd %xmm0, (%rax,%r14,8)
incq %rbx
jmp 0xe87b
movsd 0x1e01e(%rip), %xmm0    # 0x2c8f0
cmpb $0x0, 0x7(%rsp)
movq %r13, %rbx
js 0xe8f1
movq (%r15), %rax
movsbl %r14b, %edx
movq %r15, %rdi
movl 0xc(%rsp), %ecx
callq *0x20(%rax)
movq (%rbx,%rbp,8), %rsi
movq 0x28(%rsi), %rax
movq (%rax,%r12,8), %rax
mulsd (%rax,%r14,8), %xmm0
movsd %xmm0, (%rax,%r14,8)
movq 0x88(%r15), %rax
cmpb $0x1, 0x31(%rax)
movq 0x28(%rsp), %r13
jne 0xe927
movq 0x20(%rsi), %rcx
addsd (%rcx,%r12,8), %xmm0
movsd %xmm0, (%rcx,%r12,8)
incq %r14
jmp 0xe86d
movq (%r15), %rax
movsbl %r14b, %edx
movq %r15, %rdi
callq *0x10(%rax)
movapd %xmm0, %xmm1
movsd 0x1dfa8(%rip), %xmm0    # 0x2c8f0
cmpb $0x0, 0x7(%rsp)
js 0xe970
movq (%rbx,%rbp,8), %rsi
movq (%r15), %rax
movsbl %r14b, %edx
movq %r15, %rdi
movl 0xc(%rsp), %ecx
movsd %xmm1, 0x10(%rsp)
callq *0x20(%rax)
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq (%rbx,%rbp,8), %rsi
movq 0x28(%rsi), %rax
movq (%rax), %rax
movsd %xmm1, (%rax,%r14,8)
movq 0x88(%r15), %rax
cmpb $0x1, 0x31(%rax)
jne 0xe99e
movq 0x20(%rsi), %rcx
addsd (%rcx), %xmm1
movsd %xmm1, (%rcx)
incq %r14
cmpq %r14, %r13
jne 0xe92f
cmpb $0x1, 0x31(%rax)
jne 0xe9fb
movq 0x20(%rsi), %rax
movsd 0x1df38(%rip), %xmm0    # 0x2c8f0
divsd (%rax,%r12,8), %xmm0
movsd %xmm0, (%rax,%r12,8)
xorl %ecx, %ecx
movsd (%rax,%r12,8), %xmm0
cmpq %rcx, %r13
je 0xe9e8
movq 0x28(%rsi), %rdx
movq (%rdx,%r12,8), %rdx
mulsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0xe9c6
callq 0x41b0
movq (%rbx,%rbp,8), %rsi
addsd 0x50(%rsi), %xmm0
movsd %xmm0, 0x50(%rsi)
incq %r12
movl (%rsi), %eax
jmp 0xe82f
movq 0x88(%r15), %rcx
cmpb $0x1, 0x31(%rcx)
jne 0xea33
movsd 0x50(%rsi), %xmm0
xorpd 0x1df21(%rip), %xmm0    # 0x2c940
callq 0x4460
pushq $0x48
popq %rax
movq 0x20(%rsp), %r14
movl 0x8(%rsp), %r12d
jmp 0xea75
andq $0x0, 0x48(%rsi)
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq 0x20(%rsp), %r14
movl 0x8(%rsp), %r12d
cmpq %rcx, %r13
je 0xea65
movq 0x28(%rsi), %rdx
movq -0x8(%rdx,%rax,8), %rdx
addsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, 0x48(%rsi)
incq %rcx
jmp 0xea48
callq 0x67ff
xorpd 0x1dece(%rip), %xmm0    # 0x2c940
pushq $0x50
popq %rax
addl 0x1c(%rsp), %r12d
movq (%rbx,%rbp,8), %rcx
movsd %xmm0, (%rcx,%rax)
incq %rbp
jmp 0xe80a
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop