// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
callq 0x1164c
movq (%r14), %rax
movq %r14, %rdi
callq *0x60(%rax)
testl %ebp, %ebp
jle 0x11d9b
movq 0x88(%r14), %rax
movzbl 0x8b8(%rax), %r12d
movl %ebp, %r15d
xorl %ebp, %ebp
xorl %eax, %eax
movq %r15, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq (%rbx,%rbp,8), %rcx
cmpl $0x0, 0x4(%rcx)
jne 0x11d8d
addl (%rcx), %eax
movl %eax, 0x14(%rsp)
movq (%rbx,%rbp,8), %rax
movl (%rax), %ecx
testl %ecx, %ecx
jle 0x11d11
xorl %r13d, %r13d
movq 0x88(%r14), %rcx
movq 0x870(%rcx), %rcx
movq 0x10(%rax), %rdx
movslq (%rdx,%r13,4), %rdx
movb (%rcx,%rdx), %cl
movl %edx, 0x8(%rax)
movb %cl, 0x3(%rsp)
movsbl %cl, %eax
movl %eax, 0x4(%rsp)
testq %r13, %r13
je 0x11bf7
testb %r12b, %r12b
jle 0x11c83
xorl %r15d, %r15d
xorl %r12d, %r12d
movq (%rbx,%rbp,8), %rsi
movq 0x28(%rsi), %rax
movq -0x8(%rax,%r13,8), %rax
movsd (%rax,%r12,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq (%r14), %rax
movsbl %r15b, %ecx
movsbl %r12b, %edx
movq %r14, %rdi
callq *0x18(%rax)
movq (%rbx,%rbp,8), %rax
movq 0x28(%rax), %rax
movq (%rax,%r13,8), %rax
mulsd 0x8(%rsp), %xmm0
addsd (%rax,%r15,8), %xmm0
movsd %xmm0, (%rax,%r15,8)
incq %r12
cmpq %r12, 0x18(%rsp)
jne 0x11b36
movq 0x18(%rsp), %r12
movsd 0x22d5b(%rip), %xmm0    # 0x348f0
cmpb $0x0, 0x3(%rsp)
js 0x11bb1
movq (%rbx,%rbp,8), %rsi
movq (%r14), %rax
movsbl %r15b, %edx
movq %r14, %rdi
movl 0x4(%rsp), %ecx
callq *0x20(%rax)
movq (%rbx,%rbp,8), %rax
movq 0x28(%rax), %rcx
movq (%rcx,%r13,8), %rcx
mulsd (%rcx,%r15,8), %xmm0
movsd %xmm0, (%rcx,%r15,8)
movq 0x88(%r14), %rcx
cmpb $0x1, 0x31(%rcx)
jne 0x11be6
movq 0x20(%rax), %rax
addsd (%rax,%r13,8), %xmm0
movsd %xmm0, (%rax,%r13,8)
incq %r15
cmpq %r12, %r15
jne 0x11b33
jmp 0x11c83
testb %r12b, %r12b
jle 0x11c83
xorl %r15d, %r15d
movq (%rbx,%rbp,8), %rsi
movq (%r14), %rax
movsbl %r15b, %edx
movq %r14, %rdi
callq *0x10(%rax)
movapd %xmm0, %xmm1
movsd 0x22cd0(%rip), %xmm0    # 0x348f0
cmpb $0x0, 0x3(%rsp)
js 0x11c48
movq (%rbx,%rbp,8), %rsi
movq (%r14), %rax
movsbl %r15b, %edx
movq %r14, %rdi
movl 0x4(%rsp), %ecx
movsd %xmm1, 0x8(%rsp)
callq *0x20(%rax)
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq (%rbx,%rbp,8), %rax
movq 0x28(%rax), %rcx
movq (%rcx,%r13,8), %rcx
movsd %xmm1, (%rcx,%r15,8)
movq 0x88(%r14), %rcx
cmpb $0x1, 0x31(%rcx)
jne 0x11c7b
movq 0x20(%rax), %rax
addsd (%rax,%r13,8), %xmm1
movsd %xmm1, (%rax,%r13,8)
incq %r15
cmpq %r15, %r12
jne 0x11c03
movq 0x88(%r14), %rax
movb 0x31(%rax), %al
cmpb $0x1, %al
jne 0x11cf9
movq (%rbx,%rbp,8), %rdx
movq 0x20(%rdx), %rcx
movsd 0x22c4f(%rip), %xmm0    # 0x348f0
divsd (%rcx,%r13,8), %xmm0
movsd %xmm0, (%rcx,%r13,8)
testb %r12b, %r12b
jle 0x11cd4
movq 0x28(%rdx), %rdx
movq (%rdx,%r13,8), %rdx
xorl %esi, %esi
movsd (%rcx,%r13,8), %xmm0
mulsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %r12
jne 0x11cbc
cmpb $0x1, %al
jne 0x11cf9
movq (%rbx,%rbp,8), %rax
movq 0x20(%rax), %rax
movsd (%rax,%r13,8), %xmm0
callq 0x41b0
movq (%rbx,%rbp,8), %rax
addsd 0x50(%rax), %xmm0
movsd %xmm0, 0x50(%rax)
incq %r13
movq (%rbx,%rbp,8), %rax
movslq (%rax), %rcx
cmpq %rcx, %r13
jl 0x11af7
movq 0x20(%rsp), %r15
movq 0x88(%r14), %rdx
cmpb $0x1, 0x31(%rdx)
jne 0x11d37
movsd 0x50(%rax), %xmm0
xorpd 0x22c15(%rip), %xmm0    # 0x34940
callq 0x4440
movl $0x48, %eax
jmp 0x11d80
movq $0x0, 0x48(%rax)
testb %r12b, %r12b
jle 0x11d69
movq 0x28(%rax), %rdx
movslq %ecx, %rcx
movq -0x8(%rdx,%rcx,8), %rcx
movsd 0x48(%rax), %xmm0
xorl %edx, %edx
addsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, 0x48(%rax)
incq %rdx
cmpq %rdx, %r12
jne 0x11d57
movsd 0x48(%rax), %xmm0
callq 0x8dcd
xorpd 0x22bc5(%rip), %xmm0    # 0x34940
movl $0x50, %eax
movq (%rbx,%rbp,8), %rcx
movsd %xmm0, (%rcx,%rax)
movl 0x14(%rsp), %eax
incq %rbp
cmpq %r15, %rbp
jne 0x11ad2
jmp 0x11d9d
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq