// File: /workspace/github_cpp_makefile_star2_O1/myudelson[P]hmm-scalable/HMMProblemSt.cpp
// Function: HMMProblemSt::predict(double*, char const*, task*, HMMProblemSt**, signed char, signed char*)
void HMMProblemSt::predict(NUMBER* metrics, const char *filename,
                           //NDAT N, NPAR* dat_obs, NCAT *dat_group, NCAT *dat_skill, NCAT *dat_skill_stacked, NCAT *dat_skill_rcount, NDAT *dat_skill_rix,
                           struct task* task,
                           HMMProblemSt **hmms, NPAR nhmms, NPAR *hmm_idx) {
	NDAT t;
	NPAR i, /*o,*/ h, n, m;
	NPAR nS = /*hmms[0]->*/task->nS, nO = /*hmms[0]->*/task->nO;
    NCAT *ar, nK = /*hmms[0]->*/task->nK, nG = /*hmms[0]->*/task->nG;
	char f_multiskill = /*hmms[0]->*/task->multiskill;
	char f_update_known = /*hmms[0]->*/task->update_known;
	char f_update_unknown = /*hmms[0]->*/task->update_unknown;
	int f_predictions = /*hmms[0]->*/task->predictions;
	int f_metrics_target_obs = /*hmms[0]->*/task->metrics_target_obs;
	for(i=1; i<nhmms; i++) {
		if( nS != hmms[i]->task->nS || nO != hmms[i]->task->nO || nK != hmms[i]->task->nK ||
		   nG != hmms[i]->task->nG || // N != hmms[i]->task->N || N_null != hmms[i]->task->N_null || // all internal N's are different
		   f_multiskill != hmms[i]->task->multiskill ||
		   f_update_known != hmms[i]->task->update_known ||
		   f_update_unknown != hmms[i]->task->update_unknown ||
		   f_predictions != hmms[i]->task->predictions ||
		   f_metrics_target_obs != hmms[i]->task->metrics_target_obs) {
			fprintf(stderr,"Error! One of count variables (N, N_null, nS, nO, nK, nG) or flags (multiskill, predictions, metrics_target_obs, update_known, update_unknown) does not have the same value across multiple models\n");
			exit(1);
		}
	}
	
	// initialize and run
//    NUMBER ***predict_hmm = init1D<NUMBER**>(nhmms);
    NUMBER **metrics_hmm = init2D<NUMBER>(nhmms,6);
    NDAT N_all = 0;
    NDAT N_nnul_all = 0;
    NUMBER loglik_all = 0.0;
    NUMBER loglik_nnul_all = 0.0;
    NUMBER sse_all = 0.0;
    NUMBER sse_nnul_all = 0.0;
    NDAT ncorr_all = 0;
    NDAT ncorr_nnul_all = 0;
    for(NPAR h=0;h<nhmms;h++) {
//        predict_hmm[h] = init2D<NUMBER>(hmms[h]->task->N,nO);
        hmms[h]->computeAlphaAndPOParam(metrics_hmm[h]);
        N_all           += hmms[h]->task->N;
        N_nnul_all      += hmms[h]->task->N_null;
        loglik_all      += metrics_hmm[h][0];
        loglik_nnul_all += metrics_hmm[h][1];
        sse_all         += metrics_hmm[h][2];
        sse_nnul_all    += metrics_hmm[h][3];
        ncorr_all       += (NDAT)metrics_hmm[h][4];
        ncorr_nnul_all  += (NDAT)metrics_hmm[h][5];
    }
    
    // print predictions
    FILE *fid = NULL; // file for storing prediction should that be necessary
    HMMProblemSt *hmm = NULL;
    if(f_predictions>0) {
        fid = fopen(filename,"w");
        if(fid == NULL)
        {
            fprintf(stderr,"Can't write output model file %s\n",filename);
            exit(1);
        }
        for(t=0; t<task->N; t++) {
            h = (hmm_idx!=NULL)?hmm_idx[t]:0;
            //o = dat_obs[t];
            hmm = hmms[h];
            getSkillsAtRow(hmm->task, t, &ar, &n);
            if(ar[0]<0) {// if no skills
                for(m=0; m<nO; m++) fprintf(fid,"%12.10f%s",hmm->null_obs_ratios[m],(m<(nO-1))?"\t":"\n");
            } else { // skills present
                for(m=0; m<nO; m++) {
                    fprintf(fid,"%12.10f%s",hmm->task->dat_predict[t][m],(m<(nO-1))?"\t": ((f_predictions==1)?"\n":"\t") );// if we print states of KCs, continut
                }
                if(f_predictions==2) { // if we print out states of KC's as welll
//                    fprintf(stderr,"WARNING! Support for printing pL is not re-implemented \n");
                    for(int l=0; l<n; l++) { // all KC here
                        NDAT tt = l + hmm->task->dat_skill_rix[t];
                        fprintf(fid,"%12.10f%s",hmm->task->dat_predict_k[tt], (l==(n-1) && l==(n-1))?"\n":"\t"); // nnon boost // if end of all states: end line//UNBOOST
                    }
                }
            } // skills presen
        }
        fclose(fid);
    }// print predictions out

    // if necessary guess the obsevaion using Pi and B
    if(f_update_known=='g') {
        fprintf(stderr,"WARNING! Guessing for local update is not re-implemented \n");
//        NUMBER max_local_pred=0;
//        NPAR ix_local_pred=0;
//        for(m=0; m<nO; m++) {
//            if( local_pred[m]>max_local_pred ) {
//                max_local_pred = local_pred[m];
//                ix_local_pred = m;
//            }
//        }
//        o = ix_local_pred;
    }
    if(f_predictions==3) { // if we print out states of KC's as welll
        fprintf(stderr,"WARNING! Writing prediction after the local update is not re-implemented \n");
//        for(int l=0; l<n; l++) { // all KC here
//            fprintf(fid,"%12.10f%s",group_skill_map[g][ ar[l] ][0], (l==(n-1) && l==(n-1))?"\n":"\t"); // nnon boost // if end of all states: end line//UNBOOST
//            //                    fprintf(fid,"%12.10f%s",gsm(g, ar[l] )[0], (l==(n-1) && l==(n-1))?"\n":"\t"); // if end of all states: end line //BOOST
//        }
    }
    
    // tally metrics
	if(metrics != NULL) {
		metrics[0] = loglik_all;
		metrics[1] = loglik_nnul_all;
		metrics[2] = sqrt(sse_all/N_all);
		metrics[3] = sqrt(sse_nnul_all/(N_all-N_nnul_all));
		metrics[4] = (NUMBER)ncorr_all/N_all;
		metrics[5] = (NUMBER)ncorr_nnul_all/(N_all-N_nnul_all);
	}
	
    // recycle
//    for(int h=0;h<nhmms;h++) {
//        free2D(predict_hmm[h],hmms[h]->task->N);
//    }
//    free(predict_hmm);
    free2D(metrics_hmm, nhmms);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r8d, %ebx
movq %rcx, %r13
movq %rsi, %r14
movq %rdi, 0x80(%rsp)
movsbq 0x901(%rdx), %rax
movq %rax, 0x50(%rsp)
movzbl %al, %r12d
movb 0x74(%rdx), %r15b
movq %rdx, 0x58(%rsp)
movl 0x70(%rdx), %ebp
movzbl %bl, %eax
movq %rax, 0x30(%rsp)
cmpb $0x2, %bl
jl 0xdd7b
movq 0x58(%rsp), %r8
movb 0x900(%r8), %al
movl 0x908(%r8), %ecx
movl 0x904(%r8), %edx
movb 0x920(%r8), %sil
movb 0x75(%r8), %dil
movb 0x6c(%r8), %r8b
movl $0x1, %r11d
movq (%r13,%r11,8), %r10
movq 0x68(%r10), %r10
cmpb 0x900(%r10), %al
jne 0xe2b2
cmpb 0x901(%r10), %r12b
jne 0xe2b2
cmpl 0x908(%r10), %ecx
jne 0xe2b2
cmpl 0x904(%r10), %edx
jne 0xe2b2
cmpb 0x920(%r10), %sil
jne 0xe2b2
cmpb 0x74(%r10), %r15b
jne 0xe2b2
cmpb 0x75(%r10), %dil
jne 0xe2b2
cmpl 0x70(%r10), %ebp
jne 0xe2b2
cmpb 0x6c(%r10), %r8b
jne 0xe2b2
incq %r11
cmpb %r11b, 0x30(%rsp)
jne 0xdcff
movq %r9, 0x98(%rsp)
movsbl %bl, %edi
movl %edi, 0x6c(%rsp)
movl $0x6, %esi
callq 0x10493
movq %rax, 0x88(%rsp)
testb %bl, %bl
movq %r12, 0x48(%rsp)
movl %ebp, 0x4(%rsp)
movq %r13, 0x60(%rsp)
movb %r15b, 0x2(%rsp)
jle 0xdeb0
movq %r14, 0x40(%rsp)
xorpd %xmm1, %xmm1
xorl %ebx, %ebx
xorl %ecx, %ecx
xorl %r15d, %r15d
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
xorpd %xmm2, %xmm2
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x60(%rsp), %r13
movq 0x88(%rsp), %rbp
movl %ecx, 0x20(%rsp)
movsd %xmm3, 0x8(%rsp)
movsd %xmm2, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
movsd %xmm1, 0x28(%rsp)
movq (%r13,%rbx,8), %rdi
movq (%rbp,%rbx,8), %rsi
callq 0xb7c2
movsd 0x8(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movq (%r13,%rbx,8), %rax
movq 0x68(%rax), %rax
addl 0x8f8(%rax), %r14d
addl 0x914(%rax), %r12d
movq (%rbp,%rbx,8), %rax
addsd (%rax), %xmm2
addsd 0x8(%rax), %xmm3
addsd 0x10(%rax), %xmm0
addsd 0x18(%rax), %xmm1
cvttsd2si 0x20(%rax), %ecx
cvttsd2si 0x28(%rax), %eax
addl %ecx, %r15d
movl 0x20(%rsp), %ecx
addl %eax, %ecx
incq %rbx
cmpq %rbx, 0x30(%rsp)
jne 0xdde6
movl %r12d, 0x38(%rsp)
movl %r14d, 0x3c(%rsp)
cvtsi2sd %r15d, %xmm4
movsd %xmm4, 0x70(%rsp)
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
movsd %xmm4, 0x78(%rsp)
movl 0x4(%rsp), %ebp
movq 0x60(%rsp), %r13
movb 0x2(%rsp), %r15b
movq 0x40(%rsp), %r14
jmp 0xdee0
xorpd %xmm2, %xmm2
movl $0x0, 0x3c(%rsp)
movl $0x0, 0x38(%rsp)
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm4, %xmm4
movsd %xmm4, 0x70(%rsp)
movsd %xmm4, 0x78(%rsp)
testl %ebp, %ebp
movq 0x80(%rsp), %r12
movsd %xmm1, 0x28(%rsp)
jle 0xe15e
movsd %xmm3, 0x8(%rsp)
movsd %xmm2, 0x10(%rsp)
movsd %xmm0, 0x18(%rsp)
leaq 0x432f(%rip), %rsi      # 0x1223e
movq %r14, %rdi
callq 0x2230
testq %rax, %rax
je 0xe2b7
movq %rax, %rdi
movq 0x58(%rsp), %rax
cmpl $0x0, 0x8f8(%rax)
jle 0xe130
movq 0x50(%rsp), %rax
decq %rax
movq %rax, 0x20(%rsp)
cmpl $0x1, 0x4(%rsp)
leaq 0x332c(%rip), %r8       # 0x1127a
leaq 0x4340(%rip), %rax      # 0x12295
cmoveq %r8, %rax
movq %rax, 0x90(%rsp)
xorl %r15d, %r15d
leaq 0x431f(%rip), %rsi      # 0x1228a
movq 0x98(%rsp), %rax
testq %rax, %rax
movq %rdi, %rbp
movq %r8, %rbx
movq %rsi, %r14
je 0xdf88
movsbq (%rax,%r15), %rax
jmp 0xdf8a
xorl %eax, %eax
movq (%r13,%rax,8), %rax
movq %rax, %r13
movq 0x68(%rax), %rdi
movl %r15d, %esi
leaq 0xa0(%rsp), %rdx
leaq 0x3(%rsp), %rcx
callq 0x76ed
movq 0xa0(%rsp), %rax
cmpl $0x0, (%rax)
movq %r15, 0x40(%rsp)
js 0xe0bd
cmpb $0x0, 0x50(%rsp)
leaq 0x42c8(%rip), %rcx      # 0x12295
movq 0x20(%rsp), %rdi
movq %rbp, %rbx
movq %r14, %rbp
movq 0x90(%rsp), %r14
movq %r13, 0x30(%rsp)
jle 0xe036
movq %r13, %rax
xorl %r13d, %r13d
movq 0x68(%rax), %rax
movq 0x8c8(%rax), %rax
movq (%rax,%r15,8), %rax
movsd (%rax,%r13,8), %xmm0
cmpq %rdi, %r13
movq %r14, %rdx
cmovlq %rcx, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
movb $0x1, %al
movq %rcx, %r12
callq 0x22b0
movq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rdx
movq %r12, %rcx
movq 0x30(%rsp), %rax
incq %r13
cmpq %r13, %rdx
jne 0xdfed
cmpl $0x2, 0x4(%rsp)
leaq 0x3238(%rip), %r8       # 0x1127a
movq %rbx, %rdi
movq %rbp, %rsi
jne 0xe10e
movsbl 0x3(%rsp), %eax
testl %eax, %eax
jle 0xe10e
movq %rcx, %rbp
xorl %r13d, %r13d
movq 0x30(%rsp), %rcx
movq 0x68(%rcx), %rcx
movq 0x8b0(%rcx), %rdx
movq 0x8d0(%rcx), %rcx
movq 0x40(%rsp), %r9
movslq (%rdx,%r9,4), %rdx
addq %r13, %rdx
movsd (%rcx,%rdx,8), %xmm0
decl %eax
cmpl %eax, %r13d
movq %rbp, %rdx
cmoveq %r8, %rdx
movq %rdi, %r12
movq %r8, %r14
movb $0x1, %al
movq %rsi, %rbx
callq 0x22b0
movq %rbx, %rsi
movq %r14, %r8
movq %r12, %rdi
movsbl 0x3(%rsp), %eax
incq %r13
cmpl %eax, %r13d
jl 0xe061
jmp 0xe10e
cmpb $0x0, 0x50(%rsp)
leaq 0x41cc(%rip), %r15      # 0x12295
movq %rbp, %rdi
movq %rbx, %r8
movq %r14, %rsi
jle 0xe10e
movq %r13, %r12
xorl %r13d, %r13d
movq 0x10(%r12), %rax
movsd (%rax,%r13,8), %xmm0
cmpq 0x20(%rsp), %r13
movq %r8, %rdx
cmovlq %r15, %rdx
movq %rbp, %rdi
movb $0x1, %al
callq 0x22b0
movq %r14, %rsi
movq %rbx, %r8
movq %rbp, %rdi
incq %r13
cmpq %r13, 0x48(%rsp)
jne 0xe0da
movq 0x40(%rsp), %r15
incq %r15
movq 0x58(%rsp), %rax
movslq 0x8f8(%rax), %rax
cmpq %rax, %r15
movq 0x60(%rsp), %r13
jl 0xdf6b
callq 0x21b0
movq 0x80(%rsp), %r12
movl 0x4(%rsp), %ebp
movb 0x2(%rsp), %r15b
movsd 0x28(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
movsd 0x10(%rsp), %xmm2
movsd 0x8(%rsp), %xmm3
cmpb $0x67, %r15b
je 0xe24a
cmpl $0x3, %ebp
movq 0x88(%rsp), %rbx
movl 0x38(%rsp), %r14d
je 0xe27e
testq %r12, %r12
movl 0x3c(%rsp), %ebp
je 0xe22c
movsd %xmm2, (%r12)
movsd %xmm3, 0x8(%r12)
xorps %xmm2, %xmm2
cvtsi2sd %ebp, %xmm2
movsd %xmm2, 0x20(%rsp)
divsd %xmm2, %xmm0
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jb 0xe1b9
sqrtsd %xmm0, %xmm0
jmp 0xe1c8
callq 0x2160
xorpd %xmm3, %xmm3
movsd 0x28(%rsp), %xmm1
subl %r14d, %ebp
xorps %xmm2, %xmm2
cvtsi2sd %ebp, %xmm2
movsd %xmm0, 0x10(%r12)
divsd %xmm2, %xmm1
ucomisd %xmm3, %xmm1
jb 0xe1ec
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0xe201
movapd %xmm1, %xmm0
movsd %xmm2, 0x48(%rsp)
callq 0x2160
movsd 0x48(%rsp), %xmm2
movsd %xmm0, 0x18(%r12)
movsd 0x70(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x20(%r12)
movsd 0x78(%rsp), %xmm0
divsd %xmm2, %xmm0
movsd %xmm0, 0x28(%r12)
movq %rbx, %rdi
movl 0x6c(%rsp), %esi
callq 0x7747
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd %xmm0, 0x18(%rsp)
movsd %xmm2, 0x10(%rsp)
movsd %xmm3, 0x8(%rsp)
callq 0x2b25
movsd 0x8(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
jmp 0xe168
movsd %xmm0, 0x18(%rsp)
movsd %xmm2, 0x10(%rsp)
movsd %xmm3, 0x8(%rsp)
callq 0x2b47
movsd 0x8(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
movsd 0x18(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
jmp 0xe17e
callq 0x2afe
movq %r14, %rdi
callq 0x2b69
nop