// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/HMMProblemSlicedAB.cpp
// Function: HMMProblemSlicedAB::computeAlphaAndPOParam(int, data**)
NDAT HMMProblemSlicedAB::computeAlphaAndPOParam(NCAT xndat, struct data** x_data) {
    //    NUMBER mult_c, old_pOparam, neg_sum_log_c;
	initAlpha(xndat, x_data);
    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]; // global state pointer
			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(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];
            x_data[x]->loglik = -safelog(x_data[x]->p_O_param);
        }
	} // 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, %r13
movl %esi, %r14d
movq %rdi, %r15
callq 0xe42e
movq 0x88(%r15), %rax
movzbl 0x8b8(%rax), %eax
xorl %ebp, %ebp
testb %al, %al
movl $0x0, %ebx
movq %rax, 0x30(%rsp)
cmovgl %eax, %ebx
testl %r14d, %r14d
cmovlel %ebp, %r14d
xorl %r12d, %r12d
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
cmpq %r14, %rbp
je 0x20402
movq (%r13,%rbp,8), %rsi
cmpl $0x0, 0x4(%rsi)
jne 0x203fa
movl %r12d, 0x8(%rsp)
movl (%rsi), %eax
movl %eax, 0x1c(%rsp)
xorl %r12d, %r12d
cltq
cmpq %rax, %r12
jge 0x2037b
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 0x20316
cmpq %rbx, %r14
je 0x2031b
movq %r13, %rbx
xorl %r13d, %r13d
cmpq %r13, 0x30(%rsp)
je 0x2023c
movq 0x28(%rsi), %rax
movq -0x8(%rax,%r12,8), %rax
movsd (%rax,%r13,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%r15), %rax
movsbl %r14b, %ecx
movsbl %r13b, %edx
movq %r15, %rdi
callq *0x18(%rax)
movq (%rbx,%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 %r13
jmp 0x201ec
movsd 0xc6ac(%rip), %xmm0     # 0x2c8f0
cmpb $0x0, 0x7(%rsp)
movq %rbx, %r13
js 0x20264
movq (%r15), %rax
movsbl %r14b, %edx
movq %r15, %rdi
movl 0xc(%rsp), %ecx
callq *0x20(%rax)
movq (%r13,%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), %rbx
jne 0x2029a
movq 0x20(%rsi), %rcx
addsd (%rcx,%r12,8), %xmm0
movsd %xmm0, (%rcx,%r12,8)
incq %r14
jmp 0x201dd
movq (%r15), %rax
movsbl %r14b, %edx
movq %r15, %rdi
callq *0x10(%rax)
movapd %xmm0, %xmm1
movsd 0xc635(%rip), %xmm0     # 0x2c8f0
cmpb $0x0, 0x7(%rsp)
js 0x202e4
movq (%r13,%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 (%r13,%rbp,8), %rsi
movq 0x28(%rsi), %rax
movq (%rax), %rax
movsd %xmm1, (%rax,%r14,8)
movq 0x88(%r15), %rax
cmpb $0x1, 0x31(%rax)
jne 0x20313
movq 0x20(%rsi), %rcx
addsd (%rcx), %xmm1
movsd %xmm1, (%rcx)
incq %r14
cmpq %r14, %rbx
jne 0x202a2
cmpb $0x1, 0x31(%rax)
jne 0x20371
movq 0x20(%rsi), %rax
movsd 0xc5c3(%rip), %xmm0     # 0x2c8f0
divsd (%rax,%r12,8), %xmm0
movsd %xmm0, (%rax,%r12,8)
xorl %ecx, %ecx
movsd (%rax,%r12,8), %xmm0
cmpq %rcx, %rbx
je 0x2035d
movq 0x28(%rsi), %rdx
movq (%rdx,%r12,8), %rdx
mulsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0x2033b
callq 0x41b0
movq (%r13,%rbp,8), %rsi
addsd 0x50(%rsi), %xmm0
movsd %xmm0, 0x50(%rsi)
incq %r12
movl (%rsi), %eax
jmp 0x2019f
movq 0x88(%r15), %rcx
cmpb $0x1, 0x31(%rcx)
jne 0x203a9
movsd 0x50(%rsi), %xmm0
xorpd 0xc5ab(%rip), %xmm0     # 0x2c940
callq 0x4460
pushq $0x48
popq %rax
movq 0x20(%rsp), %r14
movl 0x8(%rsp), %r12d
jmp 0x203eb
andq $0x0, 0x48(%rsi)
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq 0x20(%rsp), %r14
movl 0x8(%rsp), %r12d
cmpq %rcx, %rbx
je 0x203db
movq 0x28(%rsi), %rdx
movq -0x8(%rdx,%rax,8), %rdx
addsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, 0x48(%rsi)
incq %rcx
jmp 0x203be
callq 0x67ff
xorpd 0xc558(%rip), %xmm0     # 0x2c940
pushq $0x50
popq %rax
addl 0x1c(%rsp), %r12d
movq (%r13,%rbp,8), %rcx
movsd %xmm0, (%rcx,%rax)
incq %rbp
jmp 0x20179
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq