// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movl %r8d, %ebx
movq %rsi, 0x10(%rsp)
movq %rdi, %r12
movsbl 0x901(%rdx), %eax
movl %eax, 0x1c(%rsp)
movzbl %al, %eax
movq %rax, 0x60(%rsp)
movb 0x74(%rdx), %r13b
movq %rdx, 0x30(%rsp)
movl 0x70(%rdx), %eax
movl %eax, 0xc(%rsp)
movzbl %bl, %r14d
cmpb $0x2, %bl
jl 0xdf14
movq 0x30(%rsp), %r8
movb 0x900(%r8), %al
movl 0x908(%r8), %ebp
movl 0x904(%r8), %edx
movb 0x920(%r8), %sil
movb 0x75(%r8), %dil
movb 0x6c(%r8), %r8b
movl $0x1, %r11d
movq (%rcx,%r11,8), %r10
movq 0x68(%r10), %r10
cmpb 0x900(%r10), %al
jne 0xe3c1
movq 0x60(%rsp), %r15
cmpb 0x901(%r10), %r15b
jne 0xe3c1
cmpl 0x908(%r10), %ebp
jne 0xe3c1
cmpl 0x904(%r10), %edx
jne 0xe3c1
cmpb 0x920(%r10), %sil
jne 0xe3c1
cmpb 0x74(%r10), %r13b
jne 0xe3c1
cmpb 0x75(%r10), %dil
jne 0xe3c1
movl 0xc(%rsp), %r15d
cmpl 0x70(%r10), %r15d
jne 0xe3c1
cmpb 0x6c(%r10), %r8b
jne 0xe3c1
incq %r11
cmpq %r11, %r14
jne 0xde8d
movb %r13b, 0xb(%rsp)
movq %rcx, 0x78(%rsp)
movq %r9, 0xa0(%rsp)
movsbl %bl, %ebp
movl %ebp, %edi
movl $0x6, %esi
callq 0x10834
movq %rax, %r13
testb %bl, %bl
movq %r12, 0x70(%rsp)
movl %ebp, 0x18(%rsp)
jle 0xdff3
pxor %xmm0, %xmm0
xorpd %xmm3, %xmm3
xorl %ebx, %ebx
xorl %ecx, %ecx
xorl %r15d, %r15d
xorpd %xmm2, %xmm2
movq 0x78(%rsp), %rbp
movdqa %xmm0, 0x50(%rsp)
movapd %xmm3, 0x20(%rsp)
movapd %xmm2, 0x40(%rsp)
movq (%rbp,%rbx,8), %rdi
movq (%r13,%rbx,8), %rsi
movl %ecx, %r12d
callq 0xb8ee
movl %r12d, %ecx
movapd 0x20(%rsp), %xmm3
movapd 0x40(%rsp), %xmm2
movq (%rbp,%rbx,8), %rax
movq 0x68(%rax), %rax
addl 0x8f8(%rax), %r15d
addl 0x914(%rax), %ecx
movq (%r13,%rbx,8), %rax
movupd (%rax), %xmm0
addpd %xmm0, %xmm3
movupd 0x10(%rax), %xmm0
movupd 0x20(%rax), %xmm1
cvttpd2dq %xmm1, %xmm1
addpd %xmm0, %xmm2
movdqa 0x50(%rsp), %xmm0
paddd %xmm1, %xmm0
incq %rbx
cmpq %rbx, %r14
jne 0xdf61
cvtdq2pd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movq 0x70(%rsp), %r12
movl 0x18(%rsp), %ebp
jmp 0xe00d
xorpd %xmm3, %xmm3
xorl %r15d, %r15d
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
xorpd %xmm2, %xmm2
movl 0xc(%rsp), %r14d
testl %r14d, %r14d
movapd %xmm2, 0x40(%rsp)
jle 0xe2a9
movl %ecx, 0x38(%rsp)
movapd %xmm3, 0x20(%rsp)
movq %r13, 0x88(%rsp)
leaq 0x52f0(%rip), %rsi      # 0x1332a
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x2230
testq %rax, %rax
je 0xe3c6
movq %rax, %rdi
movq 0x30(%rsp), %rax
cmpl $0x0, 0x8f8(%rax)
jle 0xe27e
movslq 0x1c(%rsp), %rcx
movq %rcx, 0x90(%rsp)
leaq -0x1(%rcx), %rax
movq %rax, 0x10(%rsp)
cmpl $0x1, 0xc(%rsp)
leaq 0x41f3(%rip), %r8       # 0x1227a
leaq 0x52f3(%rip), %rax      # 0x13381
cmoveq %r8, %rax
movq %rax, 0x98(%rsp)
xorl %r12d, %r12d
leaq 0x52d2(%rip), %rsi      # 0x13376
movl %r15d, 0x3c(%rsp)
movq 0xa0(%rsp), %rax
testq %rax, %rax
movq %rdi, %rbp
movq %r8, %rbx
movq %rsi, %r14
je 0xe0c6
movsbq (%rax,%r12), %rax
jmp 0xe0c8
xorl %eax, %eax
movq 0x78(%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq %rax, 0x50(%rsp)
movq 0x68(%rax), %rdi
movl %r12d, %esi
leaq 0xa8(%rsp), %rdx
leaq 0xa(%rsp), %rcx
callq 0x7797
movq 0xa8(%rsp), %rax
cmpl $0x0, (%rax)
movq %r12, 0x80(%rsp)
js 0xe204
cmpb $0x0, 0x1c(%rsp)
leaq 0x526d(%rip), %rcx      # 0x13381
movq 0x10(%rsp), %rdi
movq %rbp, %rbx
movq %r14, %rbp
movq 0x98(%rsp), %r14
movq 0x50(%rsp), %rax
jle 0xe17a
xorl %r15d, %r15d
movq 0x68(%rax), %rax
movq 0x8c8(%rax), %rax
movq (%rax,%r12,8), %rax
movsd (%rax,%r15,8), %xmm0
cmpq %rdi, %r15
movq %r14, %rdx
cmovlq %rcx, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
movb $0x1, %al
movq %rcx, %r13
callq 0x22b0
movq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rdx
movq %r13, %rcx
movq 0x50(%rsp), %rax
incq %r15
cmpq %r15, %rdx
jne 0xe131
cmpl $0x2, 0xc(%rsp)
leaq 0x40f4(%rip), %r8       # 0x1227a
movq %rbx, %rdi
movq %rbp, %rsi
jne 0xe259
movsbl 0xa(%rsp), %eax
testl %eax, %eax
jle 0xe259
movq %rcx, %rbp
xorl %r15d, %r15d
movq 0x50(%rsp), %rcx
movq 0x68(%rcx), %rcx
movq 0x8b0(%rcx), %rdx
movq 0x8d0(%rcx), %rcx
movq 0x80(%rsp), %r9
movslq (%rdx,%r9,4), %rdx
addq %r15, %rdx
movsd (%rcx,%rdx,8), %xmm0
decl %eax
cmpl %eax, %r15d
movq %rbp, %rdx
cmoveq %r8, %rdx
movq %rdi, %r13
movq %r8, %r14
movb $0x1, %al
movq %rsi, %rbx
callq 0x22b0
movq %rbx, %rsi
movq %r14, %r8
movq %r13, %rdi
movsbl 0xa(%rsp), %eax
incq %r15
cmpl %eax, %r15d
jl 0xe1a5
jmp 0xe259
cmpb $0x0, 0x90(%rsp)
leaq 0x516e(%rip), %r12      # 0x13381
movq 0x50(%rsp), %r13
movq %rbp, %rdi
movq %rbx, %r8
movq %r14, %rsi
jle 0xe259
xorl %r15d, %r15d
movq 0x10(%r13), %rax
movsd (%rax,%r15,8), %xmm0
cmpq 0x10(%rsp), %r15
movq %r8, %rdx
cmovlq %r12, %rdx
movq %rbp, %rdi
movb $0x1, %al
callq 0x22b0
movq %r14, %rsi
movq %rbx, %r8
movq %rbp, %rdi
incq %r15
cmpq %r15, 0x60(%rsp)
jne 0xe226
movq 0x80(%rsp), %r12
incq %r12
movq 0x30(%rsp), %rax
movslq 0x8f8(%rax), %rax
cmpq %rax, %r12
movl 0x3c(%rsp), %r15d
jl 0xe0a9
callq 0x21b0
movq 0x70(%rsp), %r12
movl 0x18(%rsp), %ebp
movl 0xc(%rsp), %r14d
movq 0x88(%rsp), %r13
movapd 0x40(%rsp), %xmm2
movapd 0x20(%rsp), %xmm3
movl 0x38(%rsp), %ecx
cmpb $0x67, 0xb(%rsp)
je 0xe381
cmpl $0x3, %r14d
je 0xe3a1
testq %r12, %r12
je 0xe365
movupd %xmm3, (%r12)
movd %r15d, %xmm0
subl %ecx, %r15d
movd %r15d, %xmm1
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
cvtdq2pd %xmm0, %xmm1
movapd %xmm2, %xmm0
divsd %xmm1, %xmm0
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
movapd %xmm1, 0x60(%rsp)
jb 0xe300
sqrtsd %xmm0, %xmm0
jmp 0xe315
callq 0x2160
xorpd %xmm3, %xmm3
movapd 0x60(%rsp), %xmm1
movapd 0x40(%rsp), %xmm2
movsd %xmm0, 0x10(%r12)
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
divsd %xmm0, %xmm2
ucomisd %xmm3, %xmm2
jb 0xe33b
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0xe34a
movapd %xmm2, %xmm0
callq 0x2160
movapd 0x60(%rsp), %xmm1
movsd %xmm0, 0x18(%r12)
movapd 0xb0(%rsp), %xmm0
divpd %xmm1, %xmm0
movupd %xmm0, 0x20(%r12)
movq %r13, %rdi
movl %ebp, %esi
callq 0x77d5
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movapd %xmm3, 0x20(%rsp)
movl %ecx, %ebx
callq 0x2b73
movl %ebx, %ecx
movapd 0x20(%rsp), %xmm3
movapd 0x40(%rsp), %xmm2
jmp 0xe2b4
movapd %xmm3, 0x20(%rsp)
movl %ecx, %ebx
callq 0x2b95
movl %ebx, %ecx
movapd 0x20(%rsp), %xmm3
movapd 0x40(%rsp), %xmm2
jmp 0xe2be
callq 0x2b4c
movq %rbx, %rdi
callq 0x2bb7