// File: /workspace/github_cpp_makefile_star2_O2/myudelson[P]hmm-scalable/trainhmm.cpp
// Function: main
int main (int argc, char ** argv) {
//    int array[] = {1, 2, 3, 4, 5, 6, 7};
//    NDAT lim = 0;
//    int v = 5;
//    NDAT ix = binsearch(&v, array, 7, sizeof(int)); // SparseArray2D<int>::binsearch(&v, array, 7, &lim);
//    fprintf(stderr,"%d found=%d, lim=%d\n",v, ix, lim);
//    
//    v = 8;
//    ix = SparseArray2D<int>::binsearch(&v, array, 7, &lim);
//    fprintf(stderr,"%d found=%d, lim=%d\n",v, ix, lim);
//    
//    v = 0;
//    ix = SparseArray2D<int>::binsearch(&v, array, 7, &lim);
//    fprintf(stderr,"%d found=%d, lim=%d\n",v, ix, lim);
//    
//    int c = (unsigned long)ceil((double)34600/20000);
    
	clock_t tm_all = clock();//overall time //SEQ
//    double _tm_all = omp_get_wtime(); //PAR
    
	char input_file[1024]; // data
	char output_file[1024]; // model
    char colsole_file[1024]; // console copy
	char predict_file[1024]; // predictions
    
	set_param_defaults(&param);
    
    
    // parse parameters, step 1
	parse_arguments_step1(argc, argv, input_file, output_file, predict_file, colsole_file);

    FILE *fid_console = NULL;
    if(param.duplicate_console==1)
        fid_console = fopen(colsole_file,"w");
    
    if(!param.quiet) {
        printf("trainhmm starting...\n");
        if(param.duplicate_console==1) fprintf(fid_console, "trainhmm starting...\n");
    }

    clock_t tm_read = clock();//overall time //SEQ
//    double _tm_read = omp_get_wtime(); //PAR
    int read_ok = read_and_structure_data(input_file, fid_console);
    tm_read = (clock_t)(clock()-tm_read);//SEQ
    
//    _tm_read = omp_get_wtime()-_tm_read;//PAR
    
//    // experimental
//    for(NCAT k=0; k<param.nK; k++) {
//        char fn [256];
//        sprintf (fn, "b89_kts_skill%d_A.txt", k);
//        InputUtil::writeInputMatrix(fn, &param, param.k_numg[k], param.k_g_data[k]);
//    }

    if( ! read_ok )
        return 0;
    
    // once we know nO (number of observations) parse parameters, step 2
    parse_arguments_step2(argc, argv, fid_console);
    
//    write_pLo_irt();
    
    
//    //
//    // read item mean % correct
//    //
//    FILE *fid = fopen("a89_kts_train01_voc_i.txt","r");
//	max_line_length = 1024;
//	char *col;
//    string item;
//    map<string,NCAT>::iterator it;
//    param.item_complexity = Calloc(NUMBER, param.map_step_bwd->size());
//    line = (char *)malloc(max_line_length);// Malloc(char,max_line_length);
//    while( readline(fid)!=NULL) {
//		// Group
//		col = strtok(line,"\t\n\r");
//		item = string( col );
//		it = param.map_step_fwd->find(item);
//		if( it==param.map_step_fwd->end() ) { // not found
//            fprintf(stderr,"DID NOT FIND THE STEP!!\n");
//            return false;
//		}
//		else {
////            if( it->second > param.map_step_bwd->size()) {
////                int z = 0 ;
////            }
//            NUMBER v = atof( strtok(NULL,"\t\n\r") );
//            param.item_complexity[ it->second ] = v;
//		}
//    }
//    free(line);
//    fclose(fid);

    if(!param.quiet) {
        printf("input read, nO=%d, nG=%d, nK=%d, nI=%d, nZ=%d\n",param.nO, param.nG, param.nK, param.nI, param.nZ);
        if(param.duplicate_console==1) fprintf(fid_console, "input read, nO=%d, nG=%d, nK=%d, nI=%d, nZ=%d\n",param.nO, param.nG, param.nK, param.nI, param.nZ);
    }
    
    //    // write time
    //    if(param.time==1) {
    //        const char * fn = "a89_kts_times.txt";
    ////        const char * fn = "a89_uskts_times.txt";
    //        write_time_interval_data(&param, fn);
    //    }
    
    // erase blocking labels
    zeroLabels(&param);

    clock_t tm_fit = 0; //SEQ
    clock_t tm_predict = 0; //SEQ
//    double _tm_fit;//PAR
//    double _tm_predict;//PAR
    
    if(param.cv_folds==0) { // not cross-validation
        // create problem
        HMMProblem *hmm = NULL;
        switch(param.structure)
        {
            case STRUCTURE_SKILL: // Conjugate Gradient Descent
//            case STRUCTURE_GROUP: // Conjugate Gradient Descent
                hmm = new HMMProblem(&param);
                break;
                //            case STRUCTURE_PIg: // Gradient Descent: PI by group, A,B by skill
                //                hmm = new HMMProblemPiG(&param);
                //                break;
            case STRUCTURE_SKABslc: // Conjugate Gradient Descent
                hmm = new HMMProblemSlicedAB(&param);
                break;
            case STRUCTURE_SKAslc: // Conjugate Gradient Descent
                hmm = new HMMProblemSlicedA(&param);
                break;
            case STRUCTURE_PIgk: // Gradient Descent, pLo=f(K,G), other by K
                hmm = new HMMProblemPiGK(&param);
                break;
            case STRUCTURE_PIgkww: // Gradient Descent, pLo=f(K,G), other by K
                hmm = new HMMProblemPiGKww(&param);
                break;
            case STRUCTURE_PIAgk: // Gradient Descent, pLo=f(K,G), pT=f(K,G), other by K
                hmm = new HMMProblemPiAGK(&param);
                break;
            case STRUCTURE_Agk: // Gradient Descent, pT=f(K,G), other by K
                hmm = new HMMProblemAGK(&param);
                break;
//                case STRUCTURE_Agki: // Gradient Descent, pT=f(K,G), other by K
//                    hmm = new HMMProblemAGKi(&param);
//                    break;
            case STRUCTURE_PIABgk: // Gradient Descent, pT=f(K,G), other by K
                hmm = new HMMProblemPiABGK(&param);
                break;
                //            case BKT_GD_T: // Gradient Descent with Transfer
                //                hmm = new HMMProblemKT(&param);
                //                break;
            case STRUCTURE_COMP: // Gradient Descent, pT=f(K,G), other by K
                hmm = new HMMProblemComp(&param);
                break;
//             case STRUCTURE_ELOK: // Gradient Descent, pT=f(K,G), other by K
//                 hmm = new HMMProblemEloK(&param);
//                 break;

        }
        tm_fit = clock(); //SEQ
//        _tm_fit = omp_get_wtime(); //PAR
        hmm->fit();
        tm_fit = clock()-tm_fit;//SEQ
//        _tm_fit = omp_get_wtime()-_tm_fit;//PAR
        
        // write model
        hmm->toFile(output_file);
        
        if(param.metrics>0 || param.predictions>0) {
            NUMBER* metrics = Calloc(NUMBER, (size_t)7); // LL, AIC, BIC, RMSE, RMSEnonull, Acc, Acc_nonull;
            // takes care of predictions and metrics, writes predictions if param.predictions==1
            
            // temporary
            if(param.per_kc_rmse_acc) {
                param.kc_counts = Calloc(NDAT, (size_t)param.nK);
                param.kc_rmse = Calloc(NUMBER, (size_t)param.nK);
                param.kc_acc  = Calloc(NUMBER, (size_t)param.nK);
            }

            // NUMBER l1 = hmm->getSumLogPOPara(param.nSeq, param.k_data);
//            printf("hmm-style ll_no_null %15.7f\n",l1);
            
            tm_predict = clock(); //SEQ
//            _tm_predict = omp_get_wtime(); //PAR
//            if(param.structure==STRUCTURE_SKAslc) {
//                ((HMMProblemSlicedA *)hmm)->predict(metrics, predict_file, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix);
//            } else if(param.structure==STRUCTURE_SKABslc) {
//                ((HMMProblemSlicedAB *)hmm)->predict(metrics, predict_file, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix);
//            } else {
                HMMProblem::predict(metrics, predict_file, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix, &hmm, 1, NULL);
//            }
            tm_predict = clock()-tm_predict;//SEQ
//            _tm_predict = omp_get_wtime()-_tm_predict;//PAR
            
            if( param.metrics>0 /*&& !param.quiet*/) {
                printf("trained model LL=%15.7f (%15.7f), AIC=%8.6f, BIC=%8.6f, RMSE=%8.6f (%8.6f), Acc=%8.6f (%8.6f)\n",
                       metrics[0], metrics[1], // ll's
                       2*hmm->getNparams() + 2*metrics[0], // AIC
                       hmm->getNparams()*safelog(param.N) + 2*metrics[0], //BIC
                       metrics[2], metrics[3], // rmse's
                       metrics[4], metrics[5]); // acc's
                if(param.duplicate_console==1) fprintf(fid_console, "trained model LL=%15.7f (%15.7f), AIC=%8.6f, BIC=%8.6f, RMSE=%8.6f (%8.6f), Acc=%8.6f (%8.6f)\n",
                       metrics[0], metrics[1], // ll's
                       2*hmm->getNparams() + 2*metrics[0], // AIC
                       hmm->getNparams()*safelog(param.N) + 2*metrics[0],
                       metrics[2], metrics[3], // rmse's
                       metrics[4], metrics[5]); // acc's

            }
            free(metrics);

            // temporary
            if(param.per_kc_rmse_acc) {
                for(NCAT i=0; i<param.nK; i++) {
                    printf("KC %4u RMSE=%8.6f Acc=%8.6f\n",i,param.kc_rmse[i],param.kc_acc[i]);
                    if(param.duplicate_console==1) fprintf(fid_console, "KC %4u RMSE=%8.6f Acc=%8.6f\n",i,param.kc_rmse[i],param.kc_acc[i]);
                }
                free(param.kc_counts);
                free(param.kc_rmse);
                free(param.kc_acc);
            }
        
        } // if predict or metrics
        
        delete hmm;
    } else { // cross-validation
        NUMBER* metrics = Calloc(NUMBER, (size_t)7); // AIC, BIC, RMSE, RMSE no null
		NUMBER n_par = 0;
        switch (param.cv_strat) {
            case CV_GROUP:
                n_par = cross_validate(metrics, predict_file, output_file, &tm_fit, &tm_predict, fid_console);//SEQ
//                n_par = cross_validate(metrics, predict_file, output_file, &_tm_fit, &_tm_predict, fid_console);//PAR
                break;
            case CV_ITEM:
                n_par = cross_validate_item(metrics, predict_file, output_file, &tm_fit, &tm_predict, fid_console);//SEQ
//                n_par = cross_validate_item(metrics, predict_file, output_file, &_tm_fit, &_tm_predict, fid_console);//PAR
                break;
            case CV_NSTR:
                n_par = cross_validate_nstrat(metrics, predict_file, output_file, &tm_fit, &tm_predict, fid_console);//SEQ
//                n_par = cross_validate_nstrat(metrics, predict_file, output_file, &_tm_fit, &_tm_predict, fid_console);//PAR
                break;
            default:
                
                break;
        }

		printf("%d-fold cross-validation: LL=%15.7f (%15.7f), AIC=%8.6f, BIC=%8.6f, RMSE=%8.6f (%8.6f), Acc=%8.6f (%8.6f)\n",
			   param.cv_folds,
			   metrics[0], metrics[1], // ll's
			   2*n_par + 2*metrics[0], n_par*safelog(param.N) + 2*metrics[0],
			   metrics[2], metrics[3], // rmse's
			   metrics[4], metrics[5]); // acc's
		if(param.duplicate_console==1) fprintf(fid_console, "%d-fold cross-validation: LL=%15.7f (%15.7f), AIC=%8.6f, BIC=%8.6f, RMSE=%8.6f (%8.6f), Acc=%8.6f (%8.6f)\n",
											   param.cv_folds,
											   metrics[0], metrics[1], // ll's
											   2*n_par + 2*metrics[0], n_par*safelog(param.N) + 2*metrics[0],
											   metrics[2], metrics[3], // rmse's
											   metrics[4], metrics[5]); // acc's
		
		
        free(metrics);
    }
	// free data
	destroy_input_data(&param);
	
//	if(param.quiet == 0) {
        printf("timing: overall %f seconds, read %f, fit %f, predict %f\n",(NUMBER)((clock()-tm_all)/CLOCKS_PER_SEC), (NUMBER)tm_read/CLOCKS_PER_SEC,  (NUMBER)tm_fit/CLOCKS_PER_SEC,  (NUMBER)tm_predict/CLOCKS_PER_SEC);//SEQ
        if(param.duplicate_console==1) fprintf(fid_console, "timing: overall %f seconds, read %f, fit %f, predict %f\n",(NUMBER)((clock()-tm_all)/CLOCKS_PER_SEC), (NUMBER)tm_read/CLOCKS_PER_SEC,  (NUMBER)tm_fit/CLOCKS_PER_SEC,  (NUMBER)tm_predict/CLOCKS_PER_SEC);//SEQ
//        if(param.duplicate_console==1) fprintf(fid_console, "timing: overall %lf sec, read %lf sec, fit %lf sec, predict %lf sec\n",omp_get_wtime()-_tm_all, _tm_read, _tm_fit, _tm_predict);//PAR
//        printf("timing: overall %lf sec, read %lf sec, fit %lf sec, predict %lf sec\n",omp_get_wtime()-_tm_all, _tm_read, _tm_fit, _tm_predict);//PAR
//    }
    
    if(param.duplicate_console==1)
        fclose(fid_console);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1088, %rsp           # imm = 0x1088
movq %rsi, %r12
movl %edi, %ebp
callq 0x4140
movq %rax, 0x58(%rsp)
leaq 0x39aa5(%rip), %rdi     # 0x3e2c8
callq 0xa6ce
leaq 0xc80(%rsp), %rdx
leaq 0x480(%rsp), %rcx
leaq 0x80(%rsp), %r8
leaq 0x880(%rsp), %r9
movl %ebp, %edi
movq %r12, %rsi
callq 0x515a
cmpb $0x1, 0x39aa3(%rip)     # 0x3e2fc
jne 0x4874
leaq 0x2a96e(%rip), %rsi     # 0x2f1d0
leaq 0x880(%rsp), %rdi
callq 0x42b0
movq %rax, %r14
jmp 0x4877
xorl %r14d, %r14d
cmpb $0x0, 0x39a86(%rip)     # 0x3e304
jne 0x48aa
leaq 0x2b569(%rip), %rdi     # 0x2fdf0
callq 0x43d0
cmpb $0x1, 0x39a69(%rip)     # 0x3e2fc
jne 0x48aa
leaq 0x2a936(%rip), %rdi     # 0x2f1d2
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x44e0
callq 0x4140
movq %rax, %r13
leaq 0xc80(%rsp), %rdi
movq %r14, %rsi
callq 0x5a55
movl %eax, %ebx
callq 0x4140
testb %bl, %bl
je 0x50f4
movq %rax, %r15
movl %ebp, %edi
movq %r12, %rsi
movq %r14, %rdx
callq 0x647e
cmpb $0x0, 0x39a1c(%rip)     # 0x3e304
jne 0x4959
movsbl 0x3a290(%rip), %esi     # 0x3eb81
movl 0x3a28d(%rip), %edx     # 0x3eb84
movl 0x3a28b(%rip), %ecx     # 0x3eb88
movl 0x3a288(%rip), %r8d     # 0x3eb8c
movsbl 0x3a284(%rip), %r9d     # 0x3eb90
leaq 0x2a8d5(%rip), %rdi     # 0x2f1e8
xorl %eax, %eax
callq 0x4050
cmpb $0x1, 0x399db(%rip)     # 0x3e2fc
jne 0x4959
movsbl 0x3a257(%rip), %edx     # 0x3eb81
movl 0x3a254(%rip), %ecx     # 0x3eb84
movl 0x3a251(%rip), %r8d     # 0x3eb88
movl 0x3a24e(%rip), %r9d     # 0x3eb8c
movsbl 0x3a24b(%rip), %eax     # 0x3eb90
movl %eax, (%rsp)
leaq 0x2a899(%rip), %rsi     # 0x2f1e8
movq %r14, %rdi
xorl %eax, %eax
callq 0x4380
leaq 0x39968(%rip), %rdi     # 0x3e2c8
callq 0xacf4
andq $0x0, 0x70(%rsp)
andq $0x0, 0x68(%rsp)
cmpb $0x0, 0x39dbc(%rip)     # 0x3e734
movq %r13, 0x78(%rsp)
je 0x49dd
pushq $0x7
popq %rdi
pushq $0x8
popq %rsi
callq 0x4130
movq %rax, %r12
movzbl 0x39da1(%rip), %eax     # 0x3e735
cmpl $0x6e, %eax
je 0x4a54
cmpl $0x69, %eax
je 0x4a2d
xorpd %xmm0, %xmm0
cmpl $0x67, %eax
jne 0x4a79
leaq 0x80(%rsp), %rsi
leaq 0x480(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r8
movq %r12, %rdi
movq %r14, %r9
callq 0x6cb9
jmp 0x4a79
andq $0x0, 0x30(%rsp)
movzbl 0x3991c(%rip), %eax     # 0x3e306
xorl %ebp, %ebp
decl %eax
cmpl $0xc, %eax
ja 0x5108
leaq 0x2a622(%rip), %rcx     # 0x2f020
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq 0x58(%rsp), %r12
jmpq *%rax
movl $0x90, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x398a8(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x10be8
jmp 0x4cdc
leaq 0x80(%rsp), %rsi
leaq 0x480(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r8
movq %r12, %rdi
movq %r14, %r9
callq 0x73db
jmp 0x4a79
leaq 0x80(%rsp), %rsi
leaq 0x480(%rsp), %rdx
leaq 0x70(%rsp), %rcx
leaq 0x68(%rsp), %r8
movq %r12, %rdi
movq %r14, %r9
callq 0x7ab2
movsd %xmm0, 0x38(%rsp)
movsbl 0x39cae(%rip), %ebx     # 0x3e734
movsd (%r12), %xmm1
movsd %xmm1, 0x40(%rsp)
movsd 0x8(%r12), %xmm2
movsd %xmm2, 0x48(%rsp)
movapd %xmm1, %xmm2
addsd %xmm1, %xmm2
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
addsd %xmm1, %xmm2
movsd %xmm2, 0x60(%rsp)
cvtsi2sdl 0x3a0b1(%rip), %xmm0 # 0x3eb78
callq 0x999b
movsd (%r12), %xmm3
movsd 0x10(%r12), %xmm4
addsd %xmm3, %xmm3
mulsd 0x38(%rsp), %xmm0
addsd %xmm0, %xmm3
movsd 0x18(%r12), %xmm5
movsd 0x20(%r12), %xmm6
movsd 0x28(%r12), %xmm7
leaq 0x2a790(%rip), %rdi     # 0x2f293
movl %ebx, %esi
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
movb $0x8, %al
callq 0x4050
cmpb $0x1, 0x397d7(%rip)     # 0x3e2fc
jne 0x4bc9
movsbl 0x39c02(%rip), %ebx     # 0x3e734
movsd (%r12), %xmm1
movsd %xmm1, 0x40(%rsp)
movsd 0x8(%r12), %xmm0
movsd %xmm0, 0x48(%rsp)
movapd %xmm1, %xmm0
addsd %xmm1, %xmm0
movsd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
cvtsi2sdl 0x3a00d(%rip), %xmm0 # 0x3eb78
callq 0x999b
movsd (%r12), %xmm1
movsd 0x10(%r12), %xmm4
addsd %xmm1, %xmm1
movsd 0x38(%rsp), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm1, %xmm3
movsd 0x18(%r12), %xmm5
movsd 0x20(%r12), %xmm6
movsd 0x28(%r12), %xmm7
leaq 0x2a6e8(%rip), %rsi     # 0x2f293
movq %r14, %rdi
movl %ebx, %edx
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
movb $0x8, %al
callq 0x4380
movq %r12, %rdi
callq 0x42c0
movq 0x70(%rsp), %r13
movq 0x68(%rsp), %rbp
movq 0x58(%rsp), %r12
jmp 0x503a
movl $0xa0, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x396cf(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x1e104
jmp 0x4cdc
movl $0xa0, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x396ae(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x1a938
jmp 0x4cdc
movl $0x98, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x3968d(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x18ddc
jmp 0x4cdc
movl $0xd0, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x3966c(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x226c0
jmp 0x4cdc
movl $0xa8, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x3964e(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x27eb6
jmp 0x4cdc
movl $0x98, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x39630(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x1c43c
jmp 0x4cdc
movl $0xa8, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x39612(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x2018c
jmp 0x4cdc
movl $0xa0, %edi
callq 0x4250
movq %rax, %rbp
leaq 0x395f4(%rip), %rsi     # 0x3e2c8
movq %rax, %rdi
callq 0x2d064
movq %rbp, 0x30(%rsp)
callq 0x4140
movq %rax, %rbx
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x48(%rax)
callq 0x4140
movq %rax, %r13
movq (%rbp), %rax
leaq 0x480(%rsp), %rsi
movq %rbp, %rdi
callq *0x40(%rax)
cmpl $0x0, 0x3960c(%rip)     # 0x3e320
jg 0x4d26
cmpl $0x0, 0x3960b(%rip)     # 0x3e328
jg 0x4d26
xorl %ebp, %ebp
jmp 0x5027
pushq $0x7
popq %rdi
pushq $0x8
popq %rsi
callq 0x4130
movq %rax, %r12
cmpb $0x1, 0x39f11(%rip)     # 0x3ec4c
jne 0x4d80
movq %rbx, %rbp
movslq 0x39e41(%rip), %rbx     # 0x3eb88
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x4130
movq %rax, 0x39f07(%rip)     # 0x3ec60
pushq $0x8
popq %rsi
movq %rbx, %rdi
callq 0x4130
movq %rax, 0x39ee5(%rip)     # 0x3ec50
movq %rbx, %rdi
movq %rbp, %rbx
pushq $0x8
popq %rsi
callq 0x4130
movq %rax, 0x39ed8(%rip)     # 0x3ec58
callq 0x4140
movq %rax, 0x38(%rsp)
movq 0x39da7(%rip), %rdx     # 0x3eb38
movq 0x39da8(%rip), %rcx     # 0x3eb40
movq 0x39da9(%rip), %r8      # 0x3eb48
movq 0x39daa(%rip), %r9      # 0x3eb50
movups 0x39dab(%rip), %xmm0    # 0x3eb58
leaq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movups %xmm0, (%rsp)
andq $0x0, 0x20(%rsp)
movl $0x1, 0x18(%rsp)
leaq 0x80(%rsp), %rsi
movq %r12, %rdi
callq 0x12f48
callq 0x4140
movq %rax, %rbp
cmpl $0x0, 0x39538(%rip)     # 0x3e320
jle 0x4f62
movsd (%r12), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x8(%r12), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x30(%rsp), %rdi
callq 0x13d9c
addl %eax, %eax
cvtsi2sd %eax, %xmm0
movsd (%r12), %xmm1
addsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
movq 0x30(%rsp), %rdi
callq 0x13d9c
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x60(%rsp)
cvtsi2sdl 0x39d31(%rip), %xmm0 # 0x3eb78
callq 0x999b
movsd (%r12), %xmm1
movsd 0x10(%r12), %xmm4
addsd %xmm1, %xmm1
movsd 0x60(%rsp), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm1, %xmm3
movsd 0x18(%r12), %xmm5
movsd 0x20(%r12), %xmm6
movsd 0x28(%r12), %xmm7
leaq 0x2a390(%rip), %rdi     # 0x2f217
movsd 0x50(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movb $0x8, %al
callq 0x4050
cmpb $0x1, 0x39455(%rip)     # 0x3e2fc
jne 0x4f62
movsd (%r12), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x8(%r12), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x30(%rsp), %rdi
callq 0x13d9c
addl %eax, %eax
cvtsi2sd %eax, %xmm0
movsd (%r12), %xmm1
addsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp)
movq 0x30(%rsp), %rdi
callq 0x13d9c
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x60(%rsp)
cvtsi2sdl 0x39c72(%rip), %xmm0 # 0x3eb78
callq 0x999b
movsd (%r12), %xmm1
movsd 0x10(%r12), %xmm4
addsd %xmm1, %xmm1
movsd 0x60(%rsp), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm1, %xmm3
movsd 0x18(%r12), %xmm5
movsd 0x20(%r12), %xmm6
movsd 0x28(%r12), %xmm7
leaq 0x2a2d1(%rip), %rsi     # 0x2f217
movq %r14, %rdi
movsd 0x50(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movb $0x8, %al
callq 0x4380
subq 0x38(%rsp), %rbp
movq %r12, %rdi
callq 0x42c0
cmpb $0x1, 0x39cd6(%rip)     # 0x3ec4c
jne 0x4ff2
movq %rbx, 0x38(%rsp)
xorl %r12d, %r12d
leaq 0x2a2ef(%rip), %rbx     # 0x2f276
movslq 0x39bfa(%rip), %rax     # 0x3eb88
cmpq %rax, %r12
jge 0x4ff9
movq 0x39cb6(%rip), %rax     # 0x3ec50
movsd (%rax,%r12,8), %xmm0
movq 0x39cb1(%rip), %rax     # 0x3ec58
movsd (%rax,%r12,8), %xmm1
movq %rbx, %rdi
movl %r12d, %esi
movb $0x2, %al
callq 0x4050
cmpb $0x1, 0x3933b(%rip)     # 0x3e2fc
jne 0x4fed
movq 0x39c86(%rip), %rax     # 0x3ec50
movsd (%rax,%r12,8), %xmm0
movq 0x39c81(%rip), %rax     # 0x3ec58
movsd (%rax,%r12,8), %xmm1
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movb $0x2, %al
callq 0x4380
incq %r12
jmp 0x4f87
movq 0x58(%rsp), %r12
jmp 0x5027
movq 0x39c60(%rip), %rdi     # 0x3ec60
callq 0x42c0
movq 0x39c44(%rip), %rdi     # 0x3ec50
callq 0x42c0
movq 0x39c40(%rip), %rdi     # 0x3ec58
callq 0x42c0
movq 0x58(%rsp), %r12
movq 0x38(%rsp), %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5037
movq (%rdi), %rax
callq *0x8(%rax)
subq %rbx, %r13
subq 0x78(%rsp), %r15
leaq 0x39282(%rip), %rdi     # 0x3e2c8
callq 0xa8c2
callq 0x4140
subq %r12, %rax
movl $0xf4240, %ebx          # imm = 0xF4240
cqto
idivq %rbx
cvtsi2sd %rax, %xmm0
cvtsi2sd %r15, %xmm1
movsd 0x29f99(%rip), %xmm4    # 0x2f008
divsd %xmm4, %xmm1
cvtsi2sd %r13, %xmm2
divsd %xmm4, %xmm2
cvtsi2sd %rbp, %xmm3
divsd %xmm4, %xmm3
leaq 0x2a272(%rip), %rdi     # 0x2f2fe
movsd %xmm1, 0x58(%rsp)
movsd %xmm2, 0x78(%rsp)
movsd %xmm3, 0x38(%rsp)
movb $0x4, %al
callq 0x4050
cmpb $0x1, 0x39250(%rip)     # 0x3e2fc
jne 0x50f4
callq 0x4140
subq %r12, %rax
cqto
idivq %rbx
cvtsi2sd %rax, %xmm0
leaq 0x2a237(%rip), %rsi     # 0x2f2fe
movq %r14, %rdi
movsd 0x58(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
movsd 0x38(%rsp), %xmm3
movb $0x4, %al
callq 0x4380
cmpb $0x1, 0x39212(%rip)     # 0x3e2fc
jne 0x50f4
movq %r14, %rdi
callq 0x4240
xorl %eax, %eax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x58(%rsp), %r12
jmp 0x4ce1
jmp 0x5138
jmp 0x5118
jmp 0x512c
movq %rax, %rbx
movl $0xa8, %esi
jmp 0x514a
movq %rax, %rbx
movl $0xd0, %esi
jmp 0x514a
movq %rax, %rbx
movl $0x98, %esi
jmp 0x514a
jmp 0x5138
movq %rax, %rbx
movl $0xa0, %esi
jmp 0x514a
movq %rax, %rbx
movl $0x90, %esi
movq %rbp, %rdi
callq 0x4260
movq %rbx, %rdi
callq 0x4480