// File: /workspace/github_cpp_makefile_star2_O2/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
movq %r9, 0x90(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdi, 0x58(%rsp)
movb 0x900(%rdx), %al
movsbl 0x901(%rdx), %ecx
movslq %ecx, %rcx
movq %rcx, 0x10(%rsp)
movl 0x908(%rdx), %ecx
movl 0x904(%rdx), %ebp
movb 0x920(%rdx), %r12b
movb 0x74(%rdx), %r14b
movb 0x75(%rdx), %dil
movl 0x70(%rdx), %r15d
movq %rdx, 0x98(%rsp)
movb 0x6c(%rdx), %r9b
movslq %r8d, %r10
pushq $0x1
popq %r11
movq 0x10(%rsp), %rdx
cmpq %r10, %r11
jge 0xc843
movq 0x40(%rsp), %rbx
movq (%rbx,%r11,8), %rbx
movq 0x68(%rbx), %rbx
cmpb 0x900(%rbx), %al
jne 0xc81d
cmpb 0x901(%rbx), %dl
jne 0xc81d
cmpl 0x908(%rbx), %ecx
jne 0xc81d
cmpl 0x904(%rbx), %ebp
jne 0xc81d
cmpb 0x920(%rbx), %r12b
jne 0xc81d
cmpb 0x74(%rbx), %r14b
jne 0xc81d
cmpb 0x75(%rbx), %dil
jne 0xc81d
cmpl 0x70(%rbx), %r15d
jne 0xc81d
incq %r11
cmpb 0x6c(%rbx), %r9b
je 0xc7c7
movq 0x97b4(%rip), %rax      # 0x15fd8
movq (%rax), %rcx
leaq 0x4b0c(%rip), %rdi      # 0x1133a
pushq $0x1
popq %rdx
movl $0xc3, %esi
callq 0x3400
pushq $0x1
popq %rdi
callq 0x3240
movq %rsi, 0x88(%rsp)
movl %r15d, 0x8(%rsp)
movb %r14b, 0x6(%rsp)
movsbl %r8b, %ebp
pushq $0x6
popq %rsi
movl %ebp, %edi
callq 0xebcc
movq %rax, %r13
movzbl %bpl, %eax
xorl %ebx, %ebx
movl %ebp, 0xc(%rsp)
testb %bpl, %bpl
cmovlel %ebx, %eax
movzbl %al, %r14d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
xorpd %xmm4, %xmm4
xorl %r15d, %r15d
xorl %r12d, %r12d
xorpd %xmm3, %xmm3
cmpq %rbx, %r14
movapd %xmm3, 0x30(%rsp)
movapd %xmm4, 0x60(%rsp)
je 0xc912
movl %r15d, %ebp
movq 0x40(%rsp), %r15
movq (%r15,%rbx,8), %rdi
movq (%r13,%rbx,8), %rsi
callq 0xa6be
movapd 0x60(%rsp), %xmm4
movapd 0x30(%rsp), %xmm3
movq (%r15,%rbx,8), %rax
movl %ebp, %r15d
movq 0x68(%rax), %rax
addl 0x8f8(%rax), %r15d
addl 0x914(%rax), %r12d
movq (%r13,%rbx,8), %rax
movupd (%rax), %xmm0
movupd 0x10(%rax), %xmm1
movupd 0x20(%rax), %xmm2
addpd %xmm0, %xmm4
addpd %xmm1, %xmm3
cvttpd2dq %xmm2, %xmm0
movdqa 0x70(%rsp), %xmm1
paddd %xmm0, %xmm1
movdqa %xmm1, 0x70(%rsp)
incq %rbx
jmp 0xc892
cmpl $0x0, 0x8(%rsp)
movq 0x58(%rsp), %rbx
movl 0xc(%rsp), %r14d
jle 0xcb15
movl %r12d, 0x28(%rsp)
movl %r15d, 0x2c(%rsp)
movq %r13, 0x80(%rsp)
leaq 0x499e(%rip), %rsi      # 0x112de
movq 0x88(%rsp), %r14
movq %r14, %rdi
callq 0x3220
testq %rax, %rax
je 0xcc46
movq %rax, %rbx
movq 0x10(%rsp), %r13
movl %r13d, %eax
decq %r13
cmpl $0x1, 0x8(%rsp)
leaq 0x3907(%rip), %rcx      # 0x1027a
leaq 0x49bb(%rip), %rdx      # 0x11335
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rsp)
xorl %ebp, %ebp
testb %al, %al
movl $0x0, %ecx
cmovgl %eax, %ecx
movzbl %cl, %r15d
movq 0x98(%rsp), %rax
movslq 0x8f8(%rax), %rax
cmpq %rax, %rbp
jge 0xcae5
movq 0x90(%rsp), %rax
testq %rax, %rax
je 0xc9bf
movsbq (%rax,%rbp), %rax
jmp 0xc9c1
xorl %eax, %eax
movq 0x40(%rsp), %rcx
movq (%rcx,%rax,8), %r14
movq 0x68(%r14), %rdi
movl %ebp, %esi
leaq 0xa0(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x6a19
movq 0xa0(%rsp), %rax
cmpl $0x0, (%rax)
js 0xcaa0
xorl %r12d, %r12d
cmpq %r12, %r15
je 0xca39
movq 0x68(%r14), %rax
movq 0x8c8(%rax), %rax
movq (%rax,%rbp,8), %rax
movsd (%rax,%r12,8), %xmm0
cmpq %r13, %r12
movq 0x10(%rsp), %rdx
leaq 0x4916(%rip), %rax      # 0x11335
cmovlq %rax, %rdx
movq %rbx, %rdi
leaq 0x48fd(%rip), %rsi      # 0x1132a
movb $0x1, %al
callq 0x32d0
incq %r12
jmp 0xc9f6
cmpl $0x2, 0x8(%rsp)
jne 0xcadd
xorl %r12d, %r12d
movsbl 0x7(%rsp), %eax
cmpl %eax, %r12d
jge 0xcadd
movq 0x68(%r14), %rcx
movq 0x8b0(%rcx), %rdx
movq 0x8d0(%rcx), %rcx
movslq (%rdx,%rbp,4), %rdx
addq %r12, %rdx
movsd (%rcx,%rdx,8), %xmm0
decl %eax
cmpl %eax, %r12d
leaq 0x48b6(%rip), %rdx      # 0x11335
leaq 0x37f4(%rip), %rax      # 0x1027a
cmoveq %rax, %rdx
movq %rbx, %rdi
leaq 0x4896(%rip), %rsi      # 0x1132a
movb $0x1, %al
callq 0x32d0
incq %r12
jmp 0xca47
xorl %r12d, %r12d
cmpq %r12, %r15
je 0xcadd
movq 0x10(%r14), %rax
movsd (%rax,%r12,8), %xmm0
cmpq %r13, %r12
leaq 0x37be(%rip), %rdx      # 0x1027a
leaq 0x4872(%rip), %rax      # 0x11335
cmovlq %rax, %rdx
movq %rbx, %rdi
leaq 0x4859(%rip), %rsi      # 0x1132a
movb $0x1, %al
callq 0x32d0
incq %r12
jmp 0xcaa3
incq %rbp
jmp 0xc993
movq %rbx, %rdi
callq 0x31b0
movq 0x58(%rsp), %rbx
movl 0xc(%rsp), %r14d
movq 0x80(%rsp), %r13
movapd 0x30(%rsp), %xmm3
movl 0x2c(%rsp), %r15d
movapd 0x60(%rsp), %xmm4
movl 0x28(%rsp), %r12d
cmpb $0x67, 0x6(%rsp)
je 0xcbec
cmpl $0x3, 0x8(%rsp)
je 0xcc19
testq %rbx, %rbx
je 0xcbcf
movupd %xmm4, (%rbx)
movd %r15d, %xmm0
subl %r12d, %r15d
movd %r15d, %xmm1
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
cvtdq2pd %xmm0, %xmm1
movapd %xmm3, %xmm0
divsd %xmm1, %xmm0
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm0
movapd %xmm1, 0x10(%rsp)
jb 0xcb6b
sqrtsd %xmm0, %xmm0
jmp 0xcb80
callq 0x3160
xorpd %xmm4, %xmm4
movapd 0x10(%rsp), %xmm1
movapd 0x30(%rsp), %xmm3
cvtdq2pd 0x70(%rsp), %xmm2
movsd %xmm0, 0x10(%rbx)
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
unpckhpd %xmm3, %xmm3            # xmm3 = xmm3[1,1]
divsd %xmm0, %xmm3
ucomisd %xmm4, %xmm3
jb 0xcba7
sqrtsd %xmm3, %xmm0
jmp 0xcbc1
movapd %xmm3, %xmm0
movaps %xmm2, 0x40(%rsp)
callq 0x3160
movapd 0x40(%rsp), %xmm2
movapd 0x10(%rsp), %xmm1
movsd %xmm0, 0x18(%rbx)
divpd %xmm1, %xmm2
movupd %xmm2, 0x20(%rbx)
movq %r13, %rdi
movl %r14d, %esi
callq 0x6bc3
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x93e5(%rip), %rax      # 0x15fd8
movq (%rax), %rcx
leaq 0x4801(%rip), %rdi      # 0x113fe
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x3400
movapd 0x60(%rsp), %xmm4
movapd 0x30(%rsp), %xmm3
jmp 0xcb20
movq 0x93b8(%rip), %rax      # 0x15fd8
movq (%rax), %rcx
leaq 0x480f(%rip), %rdi      # 0x11439
pushq $0x4a
popq %rsi
pushq $0x1
popq %rdx
callq 0x3400
movapd 0x60(%rsp), %xmm4
movapd 0x30(%rsp), %xmm3
jmp 0xcb2b
movq 0x938b(%rip), %rax      # 0x15fd8
movq (%rax), %rdi
leaq 0x4689(%rip), %rsi      # 0x112e0
movq %r14, %rdx
xorl %eax, %eax
callq 0x32d0
jmp 0xc83b