// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movl %esi, %ebp
movq %rdi, %r14
movq %rdx, %rbx
callq 0x114de
movq (%r14), %rax
movq %r14, %rdi
callq *0x60(%rax)
testl %ebp, %ebp
jle 0x11c43
movq 0x88(%r14), %rax
movzbl 0x8b8(%rax), %r12d
movl %ebp, %eax
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
movq %rbx, %r8
movq %rax, 0x40(%rsp)
movq %rbx, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq (%r8,%r15,8), %rsi
cmpl $0x0, 0x4(%rsi)
jne 0x11c35
movl (%rsi), %eax
testl %eax, %eax
movl %eax, 0x2c(%rsp)
jle 0x11ba8
xorl %ebx, %ebx
movq %rbx, 0x10(%rsp)
movq 0x88(%r14), %rcx
movq 0x870(%rcx), %rax
movq 0x10(%rsi), %rdx
movslq (%rdx,%rbx,4), %rdx
movb (%rax,%rdx), %al
movl %edx, 0x8(%rsi)
movb %al, 0xb(%rsp)
movsbl %al, %eax
movl %eax, 0x1c(%rsp)
testq %rbx, %rbx
je 0x11aa7
testb %r12b, %r12b
jle 0x11b2a
movq 0x28(%rsi), %rax
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x10(%rsp), %rbx
movq -0x8(%rax,%rbx,8), %rax
movsd (%rax,%rbp,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movq (%r14), %rax
movsbl %r13b, %r12d
movsbl %bpl, %edx
movq %r14, %rdi
movl %r12d, %ecx
callq *0x18(%rax)
movq 0x38(%rsp), %rax
movq (%rax,%r15,8), %rsi
movq 0x28(%rsi), %rax
movq (%rax,%rbx,8), %rcx
mulsd 0x20(%rsp), %xmm0
addsd (%rcx,%r13,8), %xmm0
movsd %xmm0, (%rcx,%r13,8)
incq %rbp
cmpq %rbp, 0x30(%rsp)
jne 0x119cc
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rbx
cmpb $0x0, 0xb(%rsp)
js 0x11a5e
movq (%r14), %rax
movq %r14, %rdi
movl %r12d, %edx
movl 0x1c(%rsp), %ecx
callq *0x20(%rax)
movapd %xmm0, %xmm1
movq %rbx, %r8
movq (%rbx,%r15,8), %rsi
movq 0x28(%rsi), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
movsd (%rcx,%r13,8), %xmm0
jmp 0x11a69
movsd 0x21e8a(%rip), %xmm1    # 0x338f0
movq %rbx, %r8
mulsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%r13,8)
movq 0x88(%r14), %rcx
cmpb $0x1, 0x31(%rcx)
movq %rbp, %r12
jne 0x11a96
movq 0x20(%rsi), %rdx
movq 0x10(%rsp), %rdi
addsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rdx,%rdi,8)
incq %r13
cmpq %r12, %r13
jne 0x119c5
jmp 0x11b2a
testb %r12b, %r12b
jle 0x11b2a
xorl %ebx, %ebx
movq (%r14), %rax
movsbl %bl, %edx
movq %r14, %rdi
movq %r8, %r13
callq *0x10(%rax)
movapd %xmm0, %xmm1
movsd 0x21e27(%rip), %xmm0    # 0x338f0
cmpb $0x0, 0xb(%rsp)
js 0x11af1
movq (%r13,%r15,8), %rsi
movq (%r14), %rax
movsbl %bl, %edx
movq %r14, %rdi
movl 0x1c(%rsp), %ecx
movsd %xmm1, 0x20(%rsp)
callq *0x20(%rax)
movsd 0x20(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq (%r13,%r15,8), %rsi
movq 0x28(%rsi), %rax
movq (%rax), %rax
movsd %xmm1, (%rax,%rbx,8)
movq 0x88(%r14), %rcx
cmpb $0x1, 0x31(%rcx)
movq %r13, %r8
jne 0x11b22
movq 0x20(%rsi), %rax
addsd (%rax), %xmm1
movsd %xmm1, (%rax)
incq %rbx
cmpq %rbx, %r12
jne 0x11aae
cmpb $0x1, 0x31(%rcx)
jne 0x11b92
movq %r8, %r13
movq 0x20(%rsi), %rax
movsd 0x21db1(%rip), %xmm0    # 0x338f0
movq 0x10(%rsp), %rbx
divsd (%rax,%rbx,8), %xmm0
movsd %xmm0, (%rax,%rbx,8)
testb %r12b, %r12b
jle 0x11b79
movq 0x28(%rsi), %rcx
movq (%rcx,%rbx,8), %rcx
xorl %edx, %edx
movsd (%rax,%rbx,8), %xmm0
mulsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %r12
jne 0x11b5d
movsd (%rax,%rbx,8), %xmm0
callq 0x41b0
movq %r13, %r8
movq (%r13,%r15,8), %rsi
addsd 0x50(%rsi), %xmm0
movsd %xmm0, 0x50(%rsi)
jmp 0x11b97
movq 0x10(%rsp), %rbx
incq %rbx
movslq (%rsi), %rax
cmpq %rax, %rbx
jl 0x11980
jmp 0x11ba8
movq 0x88(%r14), %rcx
cmpb $0x1, 0x31(%rcx)
jne 0x11bd1
movq %r8, %rbx
movsd 0x50(%rsi), %xmm0
xorpd 0x21d7b(%rip), %xmm0    # 0x33940
callq 0x4440
movl $0x48, %eax
jmp 0x11c1c
movq $0x0, 0x48(%rsi)
movq %r8, %rbx
testb %r12b, %r12b
jle 0x11c06
movq 0x28(%rsi), %rcx
cltq
movq -0x8(%rcx,%rax,8), %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x48(%rsi)
incq %rcx
cmpq %rcx, %r12
jne 0x11bf2
jmp 0x11c0a
xorpd %xmm0, %xmm0
callq 0x8e3d
xorpd 0x21d29(%rip), %xmm0    # 0x33940
movl $0x50, %eax
movl 0x2c(%rsp), %ecx
addl %ecx, 0xc(%rsp)
movq %rbx, %r8
movq (%rbx,%r15,8), %rcx
movsd %xmm0, (%rcx,%rax)
movq 0x40(%rsp), %rax
incq %r15
cmpq %rax, %r15
jne 0x11962
jmp 0x11c4b
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq