// File: 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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movb %r8b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movb %al, -0x21(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rax
movb 0x900(%rax), %al
movb %al, -0x39(%rbp)
movq -0x18(%rbp), %rax
movb 0x901(%rax), %al
movb %al, -0x3a(%rbp)
movq -0x18(%rbp), %rax
movl 0x908(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movl 0x904(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movb 0x920(%rax), %al
movb %al, -0x51(%rbp)
movq -0x18(%rbp), %rax
movb 0x74(%rax), %al
movb %al, -0x52(%rbp)
movq -0x18(%rbp), %rax
movb 0x75(%rax), %al
movb %al, -0x53(%rbp)
movq -0x18(%rbp), %rax
movl 0x70(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movsbl 0x6c(%rax), %eax
movl %eax, -0x5c(%rbp)
movb $0x1, -0x35(%rbp)
movsbl -0x35(%rbp), %eax
movsbl -0x21(%rbp), %ecx
cmpl %ecx, %eax
jge 0x155eb
movsbl -0x39(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
movsbl 0x900(%rcx), %ecx
cmpl %ecx, %eax
jne 0x155ba
movsbl -0x3a(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
movsbl 0x901(%rcx), %ecx
cmpl %ecx, %eax
jne 0x155ba
movl -0x4c(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
cmpl 0x908(%rcx), %eax
jne 0x155ba
movl -0x50(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
cmpl 0x904(%rcx), %eax
jne 0x155ba
movsbl -0x51(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
movsbl 0x920(%rcx), %ecx
cmpl %ecx, %eax
jne 0x155ba
movsbl -0x52(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
movsbl 0x74(%rcx), %ecx
cmpl %ecx, %eax
jne 0x155ba
movsbl -0x53(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
movsbl 0x75(%rcx), %ecx
cmpl %ecx, %eax
jne 0x155ba
movl -0x58(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
cmpl 0x70(%rcx), %eax
jne 0x155ba
movl -0x5c(%rbp), %eax
movq -0x20(%rbp), %rcx
movsbq -0x35(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq 0x68(%rcx), %rcx
movsbl 0x6c(%rcx), %ecx
cmpl %ecx, %eax
je 0x155dc
movq 0xca17(%rip), %rax      # 0x21fd8
movq (%rax), %rdi
leaq 0x6dc3(%rip), %rsi      # 0x1c38e
movb $0x0, %al
callq 0x33c0
movl $0x1, %edi
callq 0x3330
jmp 0x155de
movb -0x35(%rbp), %al
addb $0x1, %al
movb %al, -0x35(%rbp)
jmp 0x15493
movsbl -0x21(%rbp), %edi
movl $0x6, %esi
callq 0x19050
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movb $0x0, -0x99(%rbp)
movsbl -0x99(%rbp), %eax
movsbl -0x21(%rbp), %ecx
cmpl %ecx, %eax
jge 0x157a0
movq -0x20(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x68(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x12530
movq -0x20(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x68(%rax), %rax
movl 0x8f8(%rax), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x20(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x68(%rax), %rax
movl 0x914(%rax), %eax
addl -0x70(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x68(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax), %xmm0
addsd -0x78(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x68(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd 0x8(%rax), %xmm0
addsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x68(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd 0x10(%rax), %xmm0
addsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x68(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsd 0x18(%rax), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movq -0x68(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cvttsd2si 0x20(%rax), %eax
addl -0x94(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x68(%rbp), %rax
movsbq -0x99(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cvttsd2si 0x28(%rax), %eax
addl -0x98(%rbp), %eax
movl %eax, -0x98(%rbp)
movb -0x99(%rbp), %al
addb $0x1, %al
movb %al, -0x99(%rbp)
jmp 0x1564c
movq $0x0, -0xa8(%rbp)
movq $0x0, -0xb0(%rbp)
cmpl $0x0, -0x58(%rbp)
jle 0x15b1a
movq -0x10(%rbp), %rdi
leaq 0x6b67(%rip), %rsi      # 0x1c332
callq 0x3310
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jne 0x15807
movq 0xc7f0(%rip), %rax      # 0x21fd8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x6b3e(%rip), %rsi      # 0x1c334
movb $0x0, %al
callq 0x33c0
movl $0x1, %edi
callq 0x3330
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x8f8(%rcx), %eax
jge 0x15b0e
cmpq $0x0, -0x30(%rbp)
je 0x1583c
movq -0x30(%rbp), %rax
movslq -0x34(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movl %eax, -0xbc(%rbp)
jmp 0x15846
xorl %eax, %eax
movl %eax, -0xbc(%rbp)
jmp 0x15846
movl -0xbc(%rbp), %eax
movb %al, -0x36(%rbp)
movq -0x20(%rbp), %rax
movsbq -0x36(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq 0x68(%rax), %rdi
movl -0x34(%rbp), %esi
leaq -0x48(%rbp), %rdx
leaq -0x37(%rbp), %rcx
callq 0x9ce0
movq -0x48(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x1592f
movb $0x0, -0x38(%rbp)
movsbl -0x38(%rbp), %eax
movsbl -0x3a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1592a
movq -0xa8(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xb0(%rbp), %rax
movq 0x10(%rax), %rax
movsbq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsbl -0x38(%rbp), %eax
movsbl -0x3a(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x158e9
leaq 0x6aa9(%rip), %rax      # 0x1c389
movq %rax, -0xd8(%rbp)
jmp 0x158f9
leaq 0x60aa(%rip), %rax      # 0x1b99a
movq %rax, -0xd8(%rbp)
jmp 0x158f9
movsd -0xc8(%rbp), %xmm0
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rdx
leaq 0x6a68(%rip), %rsi      # 0x1c37e
movb $0x1, %al
callq 0x33c0
movb -0x38(%rbp), %al
addb $0x1, %al
movb %al, -0x38(%rbp)
jmp 0x1588f
jmp 0x15afe
movb $0x0, -0x38(%rbp)
movsbl -0x38(%rbp), %eax
movsbl -0x3a(%rbp), %ecx
cmpl %ecx, %eax
jge 0x15a01
movq -0xa8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xb0(%rbp), %rax
movq 0x68(%rax), %rax
movq 0x8c8(%rax), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsbl -0x38(%rbp), %eax
movsbl -0x3a(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x1599c
leaq 0x69f6(%rip), %rax      # 0x1c389
movq %rax, -0xf0(%rbp)
jmp 0x159d0
cmpl $0x1, -0x58(%rbp)
jne 0x159b2
leaq 0x5ff1(%rip), %rax      # 0x1b99a
movq %rax, -0xf8(%rbp)
jmp 0x159c2
leaq 0x69d0(%rip), %rax      # 0x1c389
movq %rax, -0xf8(%rbp)
jmp 0x159c2
movq -0xf8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movsd -0xe0(%rbp), %xmm0
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rdx
leaq 0x6991(%rip), %rsi      # 0x1c37e
movb $0x1, %al
callq 0x33c0
movb -0x38(%rbp), %al
addb $0x1, %al
movb %al, -0x38(%rbp)
jmp 0x15933
cmpl $0x2, -0x58(%rbp)
jne 0x15afc
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movsbl -0x37(%rbp), %ecx
cmpl %ecx, %eax
jge 0x15afa
movl -0xb4(%rbp), %eax
movq -0xb0(%rbp), %rcx
movq 0x68(%rcx), %rcx
movq 0x8b0(%rcx), %rcx
movslq -0x34(%rbp), %rdx
addl (%rcx,%rdx,4), %eax
movl %eax, -0xb8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0xb0(%rbp), %rax
movq 0x68(%rax), %rax
movq 0x8d0(%rax), %rax
movslq -0xb8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x100(%rbp)
movl -0xb4(%rbp), %eax
movsbl -0x37(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x15ab2
movl -0xb4(%rbp), %eax
movsbl -0x37(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x15ab2
leaq 0x5ef1(%rip), %rax      # 0x1b99a
movq %rax, -0x110(%rbp)
jmp 0x15ac2
leaq 0x68d0(%rip), %rax      # 0x1c389
movq %rax, -0x110(%rbp)
jmp 0x15ac2
movsd -0x100(%rbp), %xmm0
movq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rdx
leaq 0x689f(%rip), %rsi      # 0x1c37e
movb $0x1, %al
callq 0x33c0
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x15a15
jmp 0x15afc
jmp 0x15afe
jmp 0x15b00
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1580e
movq -0xa8(%rbp), %rdi
callq 0x3260
movsbl -0x52(%rbp), %eax
cmpl $0x67, %eax
jne 0x15b3b
movq 0xc4ae(%rip), %rax      # 0x21fd8
movq (%rax), %rdi
leaq 0x691e(%rip), %rsi      # 0x1c452
movb $0x0, %al
callq 0x33c0
cmpl $0x3, -0x58(%rbp)
jne 0x15b59
movq 0xc490(%rip), %rax      # 0x21fd8
movq (%rax), %rdi
leaq 0x693b(%rip), %rsi      # 0x1c48d
movb $0x0, %al
callq 0x33c0
cmpq $0x0, -0x8(%rbp)
je 0x15bfb
movsd -0x78(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
movsd -0x80(%rbp), %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movsd -0x88(%rbp), %xmm0
cvtsi2sdl -0x6c(%rbp), %xmm1
divsd %xmm1, %xmm0
callq 0x31f0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movsd -0x90(%rbp), %xmm0
movl -0x6c(%rbp), %eax
subl -0x70(%rbp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
callq 0x31f0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x18(%rax)
cvtsi2sdl -0x94(%rbp), %xmm0
cvtsi2sdl -0x6c(%rbp), %xmm1
divsd %xmm1, %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x20(%rax)
cvtsi2sdl -0x98(%rbp), %xmm0
movl -0x6c(%rbp), %eax
subl -0x70(%rbp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movq -0x8(%rbp), %rax
movsd %xmm0, 0x28(%rax)
movq -0x68(%rbp), %rdi
movsbl -0x21(%rbp), %esi
callq 0xa1d0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)