// File: myudelson[P]hmm-scalable/trainhmm.cpp
// Function: cross_validate_item(double*, char const*, char const*, long*, long*, _IO_FILE*)
NUMBER cross_validate_item(NUMBER* metrics, const char *filename, const char *model_file_name, clock_t *tm_fit, clock_t *tm_predict, FILE *fid_console) {//SEQ
//NUMBER cross_validate_item(NUMBER* metrics, const char *filename, const char *model_file_name, double *tm_fit, double *tm_predict, FILE *fid_console) {//PAR
    NPAR f;
    NCAT I; // item
    NDAT t;
    clock_t tm0;//SEQ
//    double _tm0;//PAR
    char *ch;
    FILE *fid = NULL; // file for storing prediction should that be necessary
    FILE *fid_folds = NULL; // file for reading/writing folds
    if(param.predictions>0) {  // if we have to write the predictions file
        fid = fopen(filename,"w");
        if(fid == NULL)
        {
            fprintf(stderr, "Can't write output model file %s\n",filename);
            if(param.duplicate_console==1) fprintf(fid_console, "Can't write output model file %s\n",filename);
            exit(1);
        }
    }
    // produce folds
    NPAR *folds = Calloc(NPAR, (size_t)param.nI);
    NDAT *fold_counts = Calloc(NDAT, (size_t)param.cv_folds);
    srand ( (unsigned int)time(NULL) ); // randomize

    // folds file
    if(param.cv_folds_file[0] > 0) { // file is specified
        if(param.cv_inout_flag=='i') {
            fid_folds = fopen(param.cv_folds_file,"r");
            if(fid_folds == NULL)
            {
                fprintf(stderr, "Can't open folds file for reading %s\n",filename);
                if(param.duplicate_console==1) fprintf(fid_console, "Can't open folds file for reading %s\n",filename);
                exit(1);
            }
            max_line_length = 1024;
            line = (char *)malloc((size_t)max_line_length);// Malloc(char,max_line_length);
        } else if(param.cv_inout_flag=='o') {
            fid_folds = fopen(param.cv_folds_file,"w");
            if(fid_folds == NULL)
            {
                fprintf(stderr, "Can't open folds file for writing %s\n",filename);
                if(param.duplicate_console==1) fprintf(fid_console, "Can't open folds file for writing %s\n",filename);
                exit(1);
            }
        }
    }
    for(I=0; I<param.nI; I++) {
        if( param.cv_folds_file[0]==0 || (param.cv_folds_file[0] > 0 && param.cv_inout_flag=='o') ) { // output or default
            folds[I] = (NPAR)(rand() % param.cv_folds); // produce folds
            if(param.cv_folds_file[0] > 0 )
                fprintf(fid_folds,"%i\n",folds[I]); // write out
        } else if( (param.cv_folds_file[0] > 0 && param.cv_inout_flag=='i') ) {
            readline(fid_folds);//
            ch = strtok(line,"\t\n\r");
            if(ch == NULL) {
                fprintf(stderr, "Error reading input folds file (potentialy wrong number of rows)\n");
                if(param.duplicate_console==1) fprintf(fid_console, "Error reading input folds file (potentialy wrong number of rows)\n");
                exit(1);
            }
            folds[I] = (NPAR)(atoi(ch));
        }
    }
    if(param.cv_folds_file[0] > 0) { // file is specified
        fclose(fid_folds);
        if(param.cv_inout_flag=='i')
            free(line);
    }
    
    // count number of items in each fold
    for(t=0; t<param.N; t++) fold_counts[ folds[param.dat_item[t]/*->get(t)*/] ]++;
    // create and fit multiple problems
    HMMProblem* hmms[param.cv_folds];
    int q = param.quiet;
//    param.quiet = 1;
    for(f=0; f<param.cv_folds; f++) {
        switch(param.structure)
        {
            case STRUCTURE_SKILL: // Conjugate Gradient Descent
//            case STRUCTURE_GROUP: // Conjugate Gradient Descent
                hmms[f] = 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
                hmms[f] = new HMMProblemSlicedAB(&param);
                break;
            case STRUCTURE_SKAslc: // Conjugate Gradient Descent
                hmms[f] = new HMMProblemSlicedA(&param);
                break;
            case STRUCTURE_PIgk: // Gradient Descent, pLo=f(K,G), other by K
                hmms[f] = new HMMProblemPiGK(&param);
                break;
            case STRUCTURE_PIAgk: // Gradient Descent, pLo=f(K,G), pT=f(K,G), other by K
                hmms[f] = new HMMProblemPiAGK(&param);
                break;
            case STRUCTURE_Agk: // Gradient Descent, pT=f(K,G), other by K
                hmms[f] = new HMMProblemAGK(&param);
                break;
            case STRUCTURE_PIABgk: // Gradient Descent, pLo=f(K,G), pT=f(K,G), other by K
                hmms[f] = new HMMProblemPiABGK(&param);
                break;
//            case BKT_GD_T: // Gradient Descent with Transfer
//                hmm = new HMMProblemKT(&param);
//                break;
        }
        // block respective data - do not fit the data belonging to the fold
        NPAR *saved_obs = Calloc(NPAR, (size_t)fold_counts[f]);
        NDAT count_saved = 0;
        for(t=0; t<param.N; t++) {
            if( folds[ param.dat_item[t]/*->get(t)*/ ] == f ) {
                saved_obs[count_saved++] = param.dat_obs[t];
                param.dat_obs[t] = -1;//->set(t, -1);
            }
        }
        // now compute
        tm0 = clock(); //SEQ
//        _tm0 = omp_get_wtime(); //PAR
        
        hmms[f]->fit();
        *(tm_fit) += (clock_t)(clock()- tm0);//SEQ
//        *(tm_fit) += omp_get_wtime()-_tm0;//PAR
        
        // write model
        char fname[1024];
        sprintf(fname,"%s_%i",model_file_name,f);
        hmms[f]->toFile(fname);
        
        // UN-block respective data
        count_saved = 0;
        for(t=0; t<param.N; t++)
            if( folds[ param.dat_item[t]/*->get(t)*/ ] == f )
                param.dat_obs[t]=saved_obs[count_saved++];//->set(t, saved_obs[count_saved++]);
        free(saved_obs);
//        if(q == 0) {
            printf("fold %d is done\n",f+1);
            if(param.duplicate_console==1) fprintf(fid_console, "fold %d is done\n",f+1);
//        }
    }
    free(fold_counts);
    param.quiet = (NPAR)q;

    tm0 = clock();//SEQ
//    _tm0 = omp_get_wtime();//PAR
	
	// new prediction
	//		create a general fold-identifying array
	NPAR *dat_fold = Calloc(NPAR, param.N);
	for(NDAT t=0; t<param.N; t++) dat_fold[t] = folds[ param.dat_item[t] ];
	//		predict
	HMMProblem::predict(metrics, filename, param.dat_obs, param.dat_group, param.dat_skill, param.dat_skill_stacked, param.dat_skill_rcount, param.dat_skill_rix, hmms, param.cv_folds/*nhmms*/, dat_fold);

    printf("Cross-validation is done wrong, if A or B matrices are 'sliced'\n");
    
    free(dat_fold);
	
	*(tm_predict) += (clock_t)(clock()- tm0);//SEQ
//    *(tm_predict) += omp_get_wtime()-_tm0;//PAR
    
    // delete problems
    NCAT n_par = 0;
    for(f=0; f<param.cv_folds; f++) {
        n_par += hmms[f]->getNparams();
        delete hmms[f];
    }
    n_par /= f;
    free(folds);

	return n_par;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x510, %rsp            # imm = 0x510
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
cmpl $0x0, 0x573bb(%rip)     # 0x613b8
jle 0xa064
movq -0x20(%rbp), %rdi
leaq 0x4a1fe(%rip), %rsi     # 0x54208
callq 0x53a0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0xa062
movq 0x56fb7(%rip), %rax     # 0x60fd8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x4beb5(%rip), %rsi     # 0x55ee4
movb $0x0, %al
callq 0x5470
movsbl 0x5734f(%rip), %eax     # 0x6138c
cmpl $0x1, %eax
jne 0xa058
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x4be93(%rip), %rsi     # 0x55ee4
movb $0x0, %al
callq 0x5470
movl $0x1, %edi
callq 0x53c0
jmp 0xa064
movslq 0x57bb1(%rip), %rdi     # 0x61c1c
movl $0x1, %esi
callq 0x5190
movq %rax, -0x78(%rbp)
movsbq 0x57743(%rip), %rdi     # 0x617c4
movl $0x4, %esi
callq 0x5190
movq %rax, -0x80(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x52a0
movl %eax, %edi
callq 0x52c0
movsbl 0x57721(%rip), %eax     # 0x617c7
cmpl $0x0, %eax
jle 0xa1ca
movsbl 0x57b11(%rip), %eax     # 0x61bc7
cmpl $0x69, %eax
jne 0xa14b
leaq 0x57292(%rip), %rdi     # 0x61358
addq $0x46f, %rdi            # imm = 0x46F
leaq 0x4be32(%rip), %rsi     # 0x55f06
callq 0x53a0
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0xa12c
movq 0x56eed(%rip), %rax     # 0x60fd8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x4be0f(%rip), %rsi     # 0x55f08
movb $0x0, %al
callq 0x5470
movsbl 0x57285(%rip), %eax     # 0x6138c
cmpl $0x1, %eax
jne 0xa122
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x4bded(%rip), %rsi     # 0x55f08
movb $0x0, %al
callq 0x5470
movl $0x1, %edi
callq 0x53c0
movl $0x400, 0x57bca(%rip)   # imm = 0x400
movslq 0x57bc3(%rip), %rdi     # 0x61d00
callq 0x5410
movq %rax, 0x57bbf(%rip)     # 0x61d08
jmp 0xa1c8
movsbl 0x57a75(%rip), %eax     # 0x61bc7
cmpl $0x6f, %eax
jne 0xa1c6
leaq 0x571fa(%rip), %rdi     # 0x61358
addq $0x46f, %rdi            # imm = 0x46F
leaq 0x4a09c(%rip), %rsi     # 0x54208
callq 0x53a0
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
jne 0xa1c4
movq 0x56e55(%rip), %rax     # 0x60fd8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x4bd9d(%rip), %rsi     # 0x55f2e
movb $0x0, %al
callq 0x5470
movsbl 0x571ed(%rip), %eax     # 0x6138c
cmpl $0x1, %eax
jne 0xa1ba
movq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x4bd7b(%rip), %rsi     # 0x55f2e
movb $0x0, %al
callq 0x5470
movl $0x1, %edi
callq 0x53c0
jmp 0xa1c6
jmp 0xa1c8
jmp 0xa1ca
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl 0x57a42(%rip), %eax     # 0x61c1c
jge 0xa2f8
movsbl 0x575e0(%rip), %eax     # 0x617c7
cmpl $0x0, %eax
je 0xa204
movsbl 0x575d4(%rip), %eax     # 0x617c7
cmpl $0x0, %eax
jle 0xa24d
movsbl 0x579c8(%rip), %eax     # 0x61bc7
cmpl $0x6f, %eax
jne 0xa24d
callq 0x50d0
movsbl 0x575b4(%rip), %ecx     # 0x617c4
cltd
idivl %ecx
movq -0x78(%rbp), %rax
movslq -0x48(%rbp), %rcx
movb %dl, (%rax,%rcx)
movsbl 0x575a2(%rip), %eax     # 0x617c7
cmpl $0x0, %eax
jle 0xa248
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rax
movslq -0x48(%rbp), %rcx
movsbl (%rax,%rcx), %edx
leaq 0x4c075(%rip), %rsi     # 0x562b6
movb $0x0, %al
callq 0x5470
jmp 0xa2e8
movsbl 0x57573(%rip), %eax     # 0x617c7
cmpl $0x0, %eax
jle 0xa2e6
movsbl 0x57963(%rip), %eax     # 0x61bc7
cmpl $0x69, %eax
jne 0xa2e6
movq -0x70(%rbp), %rdi
callq 0xb520
movq 0x57a8f(%rip), %rdi     # 0x61d08
leaq 0x4b28d(%rip), %rsi     # 0x5550d
callq 0x54d0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0xa2d0
movq 0x56d41(%rip), %rax     # 0x60fd8
movq (%rax), %rdi
leaq 0x4bcb3(%rip), %rsi     # 0x55f54
movb $0x0, %al
callq 0x5470
movsbl 0x570dd(%rip), %eax     # 0x6138c
cmpl $0x1, %eax
jne 0xa2c6
movq -0x40(%rbp), %rdi
leaq 0x4bc95(%rip), %rsi     # 0x55f54
movb $0x0, %al
callq 0x5470
movl $0x1, %edi
callq 0x53c0
movq -0x60(%rbp), %rdi
callq 0x54c0
movb %al, %dl
movq -0x78(%rbp), %rax
movslq -0x48(%rbp), %rcx
movb %dl, (%rax,%rcx)
jmp 0xa2e8
jmp 0xa2ea
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xa1d1
movsbl 0x574c8(%rip), %eax     # 0x617c7
cmpl $0x0, %eax
jle 0xa327
movq -0x70(%rbp), %rdi
callq 0x52f0
movsbl 0x578b3(%rip), %eax     # 0x61bc7
cmpl $0x69, %eax
jne 0xa325
movq 0x579e8(%rip), %rdi     # 0x61d08
callq 0x53b0
jmp 0xa327
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x578d1(%rip), %eax     # 0x61c08
jge 0xa369
movq -0x80(%rbp), %rax
movq -0x78(%rbp), %rcx
movq 0x578b0(%rip), %rdx     # 0x61bf8
movslq -0x4c(%rbp), %rsi
movslq (%rdx,%rsi,4), %rdx
movsbq (%rcx,%rdx), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xa32e
movzbl 0x57454(%rip), %edx     # 0x617c4
movl %edx, %eax
movq %rsp, %rcx
movq %rcx, -0x88(%rbp)
movl %edx, %ecx
leal 0xf(,%rcx,8), %ecx
andl $-0x10, %ecx
movl %ecx, %edx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x4d8(%rbp)
movq %rcx, %rsp
movq %rax, -0x90(%rbp)
movsbl 0x56fec(%rip), %eax     # 0x61394
movl %eax, -0x94(%rbp)
movb $0x0, -0x41(%rbp)
movsbl -0x41(%rbp), %eax
movsbl 0x57407(%rip), %ecx     # 0x617c4
cmpl %ecx, %eax
jge 0xa8af
movsbl 0x56fca(%rip), %eax     # 0x61396
decl %eax
movl %eax, %ecx
movq %rcx, -0x4e0(%rbp)
subl $0xb, %eax
ja 0xa6d3
movq -0x4e0(%rbp), %rax
leaq 0x49dba(%rip), %rcx     # 0x541a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x90, %edi
callq 0x5300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x4e8(%rbp)
leaq 0x56f43(%rip), %rsi     # 0x61358
callq 0x1e660
jmp 0xa41c
movq -0x4d8(%rbp), %rax
movq -0x4e8(%rbp), %rdx
movsbq -0x41(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xa6d3
movq -0x4e8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
movl $0x90, %esi
callq 0x5310
jmp 0xaa85
movl $0xd0, %edi
callq 0x5300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x4f0(%rbp)
leaq 0x56eda(%rip), %rsi     # 0x61358
callq 0x3d840
jmp 0xa485
movq -0x4d8(%rbp), %rax
movq -0x4f0(%rbp), %rdx
movsbq -0x41(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xa6d3
movq -0x4f0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
movl $0xd0, %esi
callq 0x5310
jmp 0xaa85
movl $0xa8, %edi
callq 0x5300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x4f8(%rbp)
leaq 0x56e71(%rip), %rsi     # 0x61358
callq 0x47550
jmp 0xa4ee
movq -0x4d8(%rbp), %rax
movq -0x4f8(%rbp), %rdx
movsbq -0x41(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xa6d3
movq -0x4f8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
movl $0xa8, %esi
callq 0x5310
jmp 0xaa85
movl $0x98, %edi
callq 0x5300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x500(%rbp)
leaq 0x56e08(%rip), %rsi     # 0x61358
callq 0x2cfa0
jmp 0xa557
movq -0x4d8(%rbp), %rax
movq -0x500(%rbp), %rdx
movsbq -0x41(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xa6d3
movq -0x500(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
movl $0x98, %esi
callq 0x5310
jmp 0xaa85
movl $0xa0, %edi
callq 0x5300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x508(%rbp)
leaq 0x56d9f(%rip), %rsi     # 0x61358
callq 0x35eb0
jmp 0xa5c0
movq -0x4d8(%rbp), %rax
movq -0x508(%rbp), %rdx
movsbq -0x41(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xa6d3
movq -0x508(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
movl $0xa0, %esi
callq 0x5310
jmp 0xaa85
movl $0x98, %edi
callq 0x5300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x510(%rbp)
leaq 0x56d36(%rip), %rsi     # 0x61358
callq 0x32c80
jmp 0xa629
movq -0x4d8(%rbp), %rax
movq -0x510(%rbp), %rdx
movsbq -0x41(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xa6d3
movq -0x510(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
movl $0x98, %esi
callq 0x5310
jmp 0xaa85
movl $0xa8, %edi
callq 0x5300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x518(%rbp)
leaq 0x56ccd(%rip), %rsi     # 0x61358
callq 0x396f0
jmp 0xa692
movq -0x4d8(%rbp), %rax
movq -0x518(%rbp), %rdx
movsbq -0x41(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xa6d3
movq -0x518(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
movl $0xa8, %esi
callq 0x5310
jmp 0xaa85
movq -0x80(%rbp), %rax
movsbq -0x41(%rbp), %rcx
movslq (%rax,%rcx,4), %rdi
movl $0x1, %esi
callq 0x5190
movq %rax, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x574fd(%rip), %eax     # 0x61c08
jge 0xa774
movq -0x78(%rbp), %rax
movq 0x574e0(%rip), %rcx     # 0x61bf8
movslq -0x4c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %eax
movsbl -0x41(%rbp), %ecx
cmpl %ecx, %eax
jne 0xa767
movq 0x57495(%rip), %rax     # 0x61bc8
movslq -0x4c(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0xb0(%rbp), %rax
movl -0xb4(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0xb4(%rbp)
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq 0x57469(%rip), %rax     # 0x61bc8
movslq -0x4c(%rbp), %rcx
movb $-0x1, (%rax,%rcx)
jmp 0xa769
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xa702
callq 0x51a0
movq %rax, %rcx
movq -0x4d8(%rbp), %rax
movq %rcx, -0x58(%rbp)
movsbq -0x41(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq (%rdi), %rax
callq *0x48(%rax)
callq 0x51a0
movq %rax, %rcx
subq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
leaq -0x4c0(%rbp), %rdi
movq -0x28(%rbp), %rdx
movsbl -0x41(%rbp), %ecx
leaq 0x4b7d4(%rip), %rsi     # 0x55f96
movb $0x0, %al
callq 0x50c0
movq -0x4d8(%rbp), %rax
movsbq -0x41(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x4c0(%rbp), %rsi
movq (%rdi), %rax
callq *0x40(%rax)
movl $0x0, -0xb4(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x57408(%rip), %eax     # 0x61c08
jge 0xa85a
movq -0x78(%rbp), %rax
movq 0x573eb(%rip), %rcx     # 0x61bf8
movslq -0x4c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsbl (%rax,%rcx), %eax
movsbl -0x41(%rbp), %ecx
cmpl %ecx, %eax
jne 0xa84d
movq -0xb0(%rbp), %rax
movl -0xb4(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0xb4(%rbp)
movslq %ecx, %rcx
movb (%rax,%rcx), %dl
movq 0x57382(%rip), %rax     # 0x61bc8
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
jmp 0xa84f
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xa7f7
movq -0xb0(%rbp), %rdi
callq 0x53b0
movsbl -0x41(%rbp), %esi
addl $0x1, %esi
leaq 0x4b728(%rip), %rdi     # 0x55f9c
movb $0x0, %al
callq 0x5060
movsbl 0x56b0a(%rip), %eax     # 0x6138c
cmpl $0x1, %eax
jne 0xa8a0
movq -0x40(%rbp), %rdi
movsbl -0x41(%rbp), %edx
addl $0x1, %edx
leaq 0x4b703(%rip), %rsi     # 0x55f9c
movb $0x0, %al
callq 0x5470
jmp 0xa8a2
movb -0x41(%rbp), %al
addb $0x1, %al
movb %al, -0x41(%rbp)
jmp 0xa3b2
movq -0x80(%rbp), %rdi
callq 0x53b0
movl -0x94(%rbp), %eax
movb %al, 0x56ad0(%rip)      # 0x61394
callq 0x51a0
movq %rax, -0x58(%rbp)
movslq 0x57334(%rip), %rdi     # 0x61c08
movl $0x1, %esi
callq 0x5190
movq %rax, -0x4c8(%rbp)
movl $0x0, -0x4cc(%rbp)
movl -0x4cc(%rbp), %eax
cmpl 0x5730d(%rip), %eax     # 0x61c08
jge 0xa938
movq -0x78(%rbp), %rax
movq 0x572f0(%rip), %rcx     # 0x61bf8
movslq -0x4cc(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
movb (%rax,%rcx), %dl
movq -0x4c8(%rbp), %rax
movslq -0x4cc(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x4cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4cc(%rbp)
jmp 0xa8ef
movq -0x4d8(%rbp), %r11
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq 0x5727a(%rip), %rdx     # 0x61bc8
movq 0x5727b(%rip), %rcx     # 0x61bd0
movq 0x5727c(%rip), %r8      # 0x61bd8
movq 0x5727d(%rip), %r9      # 0x61be0
movq 0x5727e(%rip), %r14     # 0x61be8
movq 0x5727f(%rip), %rbx     # 0x61bf0
movb 0x56e4c(%rip), %r10b    # 0x617c4
movq -0x4c8(%rbp), %rax
subq $0x30, %rsp
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movsbl %r10b, %r10d
movl %r10d, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x224d0
addq $0x30, %rsp
leaq 0x4b5fe(%rip), %rdi     # 0x55fad
movb $0x0, %al
callq 0x5060
movq -0x4c8(%rbp), %rdi
callq 0x53b0
callq 0x51a0
movq %rax, %rcx
subq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl $0x0, -0x4d0(%rbp)
movb $0x0, -0x41(%rbp)
movsbl -0x41(%rbp), %eax
movsbl 0x56dd3(%rip), %ecx     # 0x617c4
cmpl %ecx, %eax
jge 0xaa4e
movq -0x4d8(%rbp), %rax
movsbq -0x41(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x23820
movl %eax, %ecx
movq -0x4d8(%rbp), %rax
addl -0x4d0(%rbp), %ecx
movl %ecx, -0x4d0(%rbp)
movsbq -0x41(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x520(%rbp)
cmpq $0x0, %rax
je 0xaa42
movq -0x520(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xaa44
movb -0x41(%rbp), %al
addb $0x1, %al
movb %al, -0x41(%rbp)
jmp 0xa9e6
movsbl -0x41(%rbp), %ecx
movl -0x4d0(%rbp), %eax
cltd
idivl %ecx
movl %eax, -0x4d0(%rbp)
movq -0x78(%rbp), %rdi
callq 0x53b0
cvtsi2sdl -0x4d0(%rbp), %xmm0
movq -0x88(%rbp), %rax
movq %rax, %rsp
leaq -0x10(%rbp), %rsp
popq %rbx
popq %r14
popq %rbp
retq
movq -0xa0(%rbp), %rdi
callq 0x5570
nopw %cs:(%rax,%rax)