// File: myudelson[P]hmm-scalable/HMMProblemPiGK.cpp
// Function: HMMProblemPiGK::GradientDescent()
NUMBER HMMProblemPiGK::GradientDescent() {
	NCAT k, g, /*ki, gi, nX, */x;
    NCAT nK = this->p->nK, nG = this->p->nG;

	//
	// fit all as 1 skill first, set group gradients to 0, and do not fit them
	//
	if(this->p->single_skill>0) {
        FitBit *fb = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->tol, this->p->tol_mode);
        fb->link( HMMProblem::getPI(0), HMMProblem::getA(0), HMMProblem::getB(0), this->p->nSeq, this->p->k_data);// link skill 0 (we'll copy fit parameters to others
        if(this->p->block_fitting[0]!=0) fb->pi = NULL;
        if(this->p->block_fitting[1]!=0) fb->A  = NULL;
        if(this->p->block_fitting[2]!=0) fb->B  = NULL;
        
        fb->init(FBS_PARm1);
        fb->init(FBS_PARm2);
        fb->init(FBS_GRAD);
        if(this->p->solver==METHOD_CGD) {
            fb->init(FBS_GRADm1);
            fb->init(FBS_DIRm1);
        }
        
        NCAT* original_ks = Calloc(NCAT, (size_t)this->p->nSeq);
        for(x=0; x<this->p->nSeq; x++) { original_ks[x] = this->p->all_data[x].k; this->p->all_data[x].k = 0; } // save progonal k's
        FitResult fr = GradientDescentBit(fb);
        for(x=0; x<this->p->nSeq; x++) { this->p->all_data[x].k = original_ks[x]; } // restore original k's
        free(original_ks);
        if( !this->p->quiet )
            printf("single skill iter#%3d p(O|param)= %15.7f >> %15.7f, conv=%d\n", fr.iter,fr.pO0,fr.pO,fr.conv);
        delete fb;
    }
	//
	// Main fit
	//
    if( this->p->single_skill!=2 ) { // if not "force single skill"
        NPAR first_iteration_qualify = this->p->first_iteration_qualify; // at what iteration, qualification for skill/group convergence should start
        NPAR iterations_to_qualify = this->p->iterations_to_qualify; // how many consecutive iterations necessary for skill/group to qualify as converged
        NCAT iterations_limit = this->p->iterations_limit; // how many consecutive iterations to do, negat3s prior two parameters
        NCAT* iter_qual_skill = Calloc(NCAT, (size_t)nK);
        NCAT* iter_qual_group = Calloc(NCAT, (size_t)nG);
        NCAT* iter_fail_skill = Calloc(NCAT, (size_t)nK); // counting concecutive number of failures to converge for skills
        NCAT* iter_fail_group = Calloc(NCAT, (size_t)nG); // counting concecutive number of failures to converge for groups
        int skip_k = 0, skip_g = 0;

        int i = 0; // count runs
//        int parallel_now = this->p->parallel==1; //PAR
//        #pragma omp parallel if(parallel_now) shared(iter_qual_group,iter_qual_skill,iter_fail_skill,iter_fail_group)//PAR
//        {//PAR
        while( (skip_k<nK || skip_g<nG) && i<iterations_limit) {
            //
            // Skills first
            //
            
            if(skip_k<nK) {
//                #pragma omp for schedule(dynamic) //PAR
                for(k=0; k<nK; k++) { // for all A,B-by-skill
                    if(iter_qual_skill[k]==iterations_to_qualify || iter_fail_skill[k]==iterations_to_qualify)
                        continue;
                    FitBit *fb = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->tol, this->p->tol_mode);
                    // link
                    fb->link( HMMProblem::getPI(k), HMMProblem::getA(k), HMMProblem::getB(k), this->p->k_numg[k], this->p->k_g_data[k]);// link skill 0 (we'll copy fit parameters to others
                    if(this->p->block_fitting[0]!=0) fb->pi = NULL;
                    if(this->p->block_fitting[1]!=0) fb->A  = NULL;
                    if(this->p->block_fitting[2]!=0) fb->B  = NULL;

                    fb->Cslice = 0;
                    fb->init(FBS_PARm1);
                    fb->init(FBS_PARm2);
                    fb->init(FBS_GRAD);
                    if(this->p->solver==METHOD_CGD) {
                        fb->init(FBS_GRADm1);
                        fb->init(FBS_DIRm1);
                    }
                    FitResult fr = GradientDescentBit(fb);

					// count number of concecutive failures
					if(fr.iter==this->p->maxiter) {
						iter_fail_skill[k]++;
					} else {
						iter_fail_skill[k]=0;
					}
					
					// decide on convergence
                    if(i>=first_iteration_qualify || fb->xndat==0) {
                        if(fr.iter==1 /*e<=this->p->tol*/ || skip_g==nG || fb->xndat==0) { // converged quick, or don't care (others all converged
                            iter_qual_skill[k]++;
                            if(fb->xndat==0) {
                                iter_qual_skill[k]=iterations_to_qualify;
                                fr.conv = 1;
                            }
                            if(iter_qual_skill[k]==iterations_to_qualify || skip_g==nG) {// criterion met, or don't care (others all converged)
                                if(skip_g==nG) iter_qual_skill[k]=iterations_to_qualify; // G not changing anymore
//                                #pragma omp critical(update_skip_k)//PAR
//                                {//PAR
                                    skip_k++;
//                                }//PAR
                                if( !this->p->quiet && ( /*(!conv && iter<this->p->maxiter) ||*/ (fr.conv || fr.iter==this->p->maxiter) )) {
                                    printf("run %2d skipK %4d skill %4d iter#%3d p(O|param)= %15.7f >> %15.7f, conv=%d\n",i,skip_k,k,fr.iter,fr.pO0,fr.pO,fr.conv);
                                }
                            }
                        }
                        else
                            iter_qual_skill[k]=0;
                    } // decide on convergence
                    delete fb;
                } // for all skills
            }
            //
            // PIg second
            //
            if(skip_g<nG && i<iterations_limit){
//                #pragma omp for schedule(dynamic)//PAR
                for(g=0; g<nG; g++) { // for all PI-by-user
                    if(iter_qual_group[g]==iterations_to_qualify || iter_fail_group[g]==iterations_to_qualify)
                        continue;
                    FitBit *fb = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->tol, this->p->tol_mode);
                    // link
                    fb->link(this->getPIg(g), NULL, NULL, this->p->g_numk[g], this->p->g_k_data[g]);
                    fb->observe_non01_boundaries=0;
                    if(this->p->block_fitting[0]!=0) fb->pi = NULL;
                    if(this->p->block_fitting[1]!=0) fb->A  = NULL;
                    if(this->p->block_fitting[2]!=0) fb->B  = NULL;

                    fb->Cslice = 1;
                    fb->init(FBS_PARm1);
                    fb->init(FBS_PARm2);
                    fb->init(FBS_GRAD);
                    if(this->p->solver==METHOD_CGD) {
                        fb->init(FBS_GRADm1);
                        fb->init(FBS_DIRm1);
                    }
                    FitResult fr = GradientDescentBit(fb);
					
					// count number of concecutive failures
					if(fr.iter==this->p->maxiter) {
						iter_fail_group[g]++;
					} else {
						iter_fail_group[g]=0;
					}
					
                    // decide on convergence
                    if(i>=first_iteration_qualify || fb->xndat==0) { //can qualify or  student had no skill labelled rows
                        if(fr.iter==1 /*e<=this->p->tol*/ || skip_k==nK || fb->xndat==0) { // converged quick, or don't care (others all converged), or
                            iter_qual_group[g]++;
                            if(fb->xndat==0) {
                                iter_qual_group[g]=iterations_to_qualify;
                                fr.conv = 1;
                            }
                            if(iter_qual_group[g]==iterations_to_qualify || skip_k==nK) {// criterion met, or don't care (others all converged)
                                if(skip_k==nK) iter_qual_group[g]=iterations_to_qualify; // K not changing anymore
//                                #pragma omp critical(update_skip_g)//PAR
//                                {//PAR
                                    skip_g++;
//                                }//PAR
                                if( !this->p->quiet && ( /*(!conv && iter<this->p->maxiter) ||*/ (fr.conv || fr.iter==this->p->maxiter) )) {
                                    printf("run %2d skipG %4d group %4d iter#%3d p(O|param)= %15.7f >> %15.7f, conv=%d\n",i,skip_g,g,fr.iter,fr.pO0,fr.pO,fr.conv);
                                }
                            }
                        }
                        else
                            iter_qual_group[g]=0;
                    } // decide on convergence
                    delete fb;
                } // for all groups
            }
//            fr.conv = checkConvergenceBig(fbs, nK+nG, this->p->tol, &crit);
//            
//            computeAlphaAndPOParam(this->p->nSeq, this->p->k_data);
//            ll = HMMProblem::getSumLogPOPara(this->p->nSeq, this->p->k_data);
//            printf("*%i ll=%15.7f, crit=%12.10f\n",i,ll,crit);
//            #pragma omp single//PAR
//            {//PAR
            i++;
//            }//PAR
        }
//        }//PAR
        // recycle qualifications
        if( iter_qual_skill != NULL ) free(iter_qual_skill);
        if( iter_qual_group != NULL) free(iter_qual_group);
        if( iter_fail_skill != NULL ) free(iter_fail_skill);
        if( iter_fail_group != NULL) free(iter_fail_group);

    } // if not "force single skill
        
//    for(x=0;x<nX;x++) delete fbs[x];
//    if(fbs!=NULL) free(fbs);
    
    /*
    if(false){
        NCAT q, x;
        NCAT nQ = this->p->nK + this->p->nG;
        FitBit **fbs = Calloc(FitBit*, (size_t)nQ);
        for(q=0;q<nQ;q++) {
            NCAT xndat;
            struct data** x_data;
            if(q<this->p->nK) { // skills
                x = q;
                xndat = this->p->k_numg[x];
                x_data = this->p->k_g_data[x];
                fbs[q] = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->tol, this->p->tol_mode);
                fbs[q]->link( HMMProblem::getPI(x), HMMProblem::getA(x), HMMProblem::getB(x), xndat, x_data);
            } else { // groups
                x = q - this->p->nK;
                xndat = this->p->g_numk[x];
                x_data = this->p->g_k_data[x];
                fbs[q] = new FitBit(this->p->nS, this->p->nO, this->p->nK, this->p->nG, this->p->tol, this->p->tol_mode);
                fbs[q]->link( this->getPIg(x), NULL, NULL, xndat, x_data);
            }
            fbs[q]->init(FBS_PARm1);
            fbs[q]->init(FBS_GRAD);
            if(this->p->solver==METHOD_CGD) {
                fbs[q]->init(FBS_GRADm1);
                fbs[q]->init(FBS_DIRm1);
            }
        }
        // FitResult fr=
        GradientDescentBitBig(fbs, nQ);
        
        for(q=0;q<nQ;q++) delete fbs[q];
        if(fbs!=NULL) free(fbs);
    }
    */

    // compute loglik
    return getSumLogPOPara(this->p->nSeq, this->p->k_data);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq 0x88(%rax), %rcx
movl 0x8c0(%rcx), %ecx
movl %ecx, -0x18(%rbp)
movq 0x88(%rax), %rcx
movl 0x8bc(%rcx), %ecx
movl %ecx, -0x1c(%rbp)
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x0, %eax
jle 0x29714
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x108(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x110(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r9d
callq 0xd080
jmp 0x2941f
movq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x128(%rbp)
xorl %esi, %esi
callq 0x1a560
movq -0x108(%rbp), %rdi
movq %rax, -0x120(%rbp)
xorl %esi, %esi
callq 0x1a5d0
movq -0x108(%rbp), %rdi
movq %rax, -0x118(%rbp)
xorl %esi, %esi
callq 0x1a640
movq -0x128(%rbp), %rdi
movq -0x120(%rbp), %rsi
movq -0x118(%rbp), %rdx
movq %rax, %rcx
movq -0x108(%rbp), %rax
movq 0x88(%rax), %r8
movl 0x8dc(%r8), %r8d
movq 0x88(%rax), %rax
movq 0x8f0(%rax), %r9
callq 0xd990
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x294f7
movq -0x28(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x294f7
movq -0x110(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2a139
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x2951d
movq -0x28(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x29543
movq -0x28(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x28(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0x28(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0x28(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x295a0
movq -0x28(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0x28(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movslq 0x8dc(%rax), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x40(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x108(%rbp), %rcx
movl -0x14(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x2963c
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x8e8(%rcx), %rcx
movslq -0x14(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rcx
movl 0x58(%rcx), %esi
movq -0x40(%rbp), %rcx
movslq -0x14(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x88(%rax), %rax
movq 0x8e8(%rax), %rax
movslq -0x14(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x58(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x295ca
movq -0x108(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x68(%rbp), %rdi
callq 0x1f530
movl $0x0, -0x14(%rbp)
movq -0x108(%rbp), %rcx
movl -0x14(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x296a9
movq -0x108(%rbp), %rax
movq -0x40(%rbp), %rcx
movslq -0x14(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movq 0x88(%rax), %rax
movq 0x8e8(%rax), %rax
movslq -0x14(%rbp), %rdx
imulq $0x60, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x58(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x29657
movq -0x40(%rbp), %rdi
callq 0x5380
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x296e4
movl -0x68(%rbp), %esi
movsd -0x60(%rbp), %xmm0
movsd -0x50(%rbp), %xmm1
movl -0x48(%rbp), %edx
leaq 0x2729a(%rip), %rdi     # 0x50977
movb $0x2, %al
callq 0x5060
movq -0x28(%rbp), %rax
movq %rax, -0x130(%rbp)
cmpq $0x0, %rax
je 0x29712
movq -0x130(%rbp), %rdi
callq 0xd3a0
movq -0x130(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x29714
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x2, %eax
je 0x2a109
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rcx
movb 0x978(%rcx), %cl
movb %cl, -0x69(%rbp)
movq 0x88(%rax), %rcx
movb 0x979(%rcx), %cl
movb %cl, -0x6a(%rbp)
movq 0x88(%rax), %rax
movl 0x97c(%rax), %eax
movl %eax, -0x70(%rbp)
movslq -0x18(%rbp), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x78(%rbp)
movslq -0x1c(%rbp), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x80(%rbp)
movslq -0x18(%rbp), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x88(%rbp)
movslq -0x1c(%rbp), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x94(%rbp), %eax
cmpl -0x18(%rbp), %eax
jl 0x297f0
movl -0x98(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x131(%rbp)
jge 0x29802
movl -0x9c(%rbp), %eax
cmpl -0x70(%rbp), %eax
setl %al
movb %al, -0x131(%rbp)
movb -0x131(%rbp), %al
testb $0x1, %al
jne 0x29811
jmp 0x2a0bb
movl -0x94(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x29c65
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x29c63
movq -0x78(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movsbl -0x6a(%rbp), %ecx
cmpl %ecx, %eax
je 0x2985c
movq -0x88(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movsbl -0x6a(%rbp), %ecx
cmpl %ecx, %eax
jne 0x29861
jmp 0x29c55
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x108(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x140(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r9d
callq 0xd080
jmp 0x298b2
movq -0x108(%rbp), %rdi
movq -0x140(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x158(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a560
movq -0x108(%rbp), %rdi
movq %rax, -0x150(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a5d0
movq -0x108(%rbp), %rdi
movq %rax, -0x148(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a640
movq -0x158(%rbp), %rdi
movq -0x150(%rbp), %rsi
movq -0x148(%rbp), %rdx
movq %rax, %rcx
movq -0x108(%rbp), %rax
movq 0x88(%rax), %r8
movq 0x8e0(%r8), %r8
movslq -0xc(%rbp), %r9
movl (%r8,%r9,4), %r8d
movq 0x88(%rax), %rax
movq 0x8f8(%rax), %rax
movslq -0xc(%rbp), %r9
movq (%rax,%r9,8), %r9
callq 0xd990
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x299a6
movq -0xa8(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x299a6
movq -0x140(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2a139
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x299cf
movq -0xa8(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x299f8
movq -0xa8(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0xa8(%rbp), %rax
movb $0x0, 0xfa(%rax)
movq -0xa8(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0xa8(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0xa8(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x29a72
movq -0xa8(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0xa8(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0x108(%rbp), %rsi
movq -0xa8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
callq 0x1f530
movq -0x108(%rbp), %rcx
movl -0xd0(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x29abb
movq -0x88(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x29acd
movq -0x88(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x9c(%rbp), %eax
movsbl -0x69(%rbp), %ecx
cmpl %ecx, %eax
jge 0x29aef
movq -0xa8(%rbp), %rax
cmpl $0x0, 0xe8(%rax)
jne 0x29c22
cmpl $0x1, -0xd0(%rbp)
je 0x29b17
movl -0x98(%rbp), %eax
cmpl -0x1c(%rbp), %eax
je 0x29b17
movq -0xa8(%rbp), %rax
cmpl $0x0, 0xe8(%rax)
jne 0x29c11
movq -0x78(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0xa8(%rbp), %rax
cmpl $0x0, 0xe8(%rax)
jne 0x29b51
movsbl -0x6a(%rbp), %edx
movq -0x78(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl $0x1, -0xb0(%rbp)
movq -0x78(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movsbl -0x6a(%rbp), %ecx
cmpl %ecx, %eax
je 0x29b73
movl -0x98(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x29c0f
movl -0x98(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x29b8d
movsbl -0x6a(%rbp), %edx
movq -0x78(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x108(%rbp), %rax
movl -0x94(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x94(%rbp)
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x29c0d
cmpl $0x0, -0xb0(%rbp)
jne 0x29bd2
movq -0x108(%rbp), %rcx
movl -0xd0(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x29c0d
movl -0x9c(%rbp), %esi
movl -0x94(%rbp), %edx
movl -0xc(%rbp), %ecx
movl -0xd0(%rbp), %r8d
movsd -0xc8(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm1
movl -0xb0(%rbp), %r9d
leaq 0x26dae(%rip), %rdi     # 0x509b4
movb $0x2, %al
callq 0x5060
jmp 0x29c0f
jmp 0x29c20
movq -0x78(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x29c22
movq -0xa8(%rbp), %rax
movq %rax, -0x160(%rbp)
cmpq $0x0, %rax
je 0x29c53
movq -0x160(%rbp), %rdi
callq 0xd3a0
movq -0x160(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x29c55
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x29827
jmp 0x29c65
movl -0x98(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2a0a7
movl -0x9c(%rbp), %eax
cmpl -0x70(%rbp), %eax
jge 0x2a0a7
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x2a0a5
movq -0x80(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movsbl -0x6a(%rbp), %ecx
cmpl %ecx, %eax
je 0x29cbf
movq -0x90(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movsbl -0x6a(%rbp), %ecx
cmpl %ecx, %eax
jne 0x29cc4
jmp 0x2a097
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x108(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x168(%rbp)
movq 0x88(%rax), %rax
movsbl 0x8b8(%rax), %esi
movsbl 0x8b9(%rax), %edx
movl 0x8c0(%rax), %ecx
movl 0x8bc(%rax), %r8d
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r9d
callq 0xd080
jmp 0x29d15
movq -0x108(%rbp), %rdi
movq -0x168(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x170(%rbp)
movl -0x10(%rbp), %esi
movq (%rdi), %rax
callq *0xc0(%rax)
movq -0x170(%rbp), %rdi
movq %rax, %rsi
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rcx
movq 0x900(%rcx), %rcx
movslq -0x10(%rbp), %rdx
movl (%rcx,%rdx,4), %r8d
movq 0x88(%rax), %rax
movq 0x910(%rax), %rax
movslq -0x10(%rbp), %rcx
movq (%rax,%rcx,8), %r9
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0xd990
movq -0x108(%rbp), %rax
movq -0xd8(%rbp), %rcx
movb $0x0, 0xf9(%rcx)
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x29de8
movq -0xd8(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x29de8
movq -0x168(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2a139
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x29e11
movq -0xd8(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x29e3a
movq -0xd8(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0xd8(%rbp), %rax
movb $0x1, 0xfa(%rax)
movq -0xd8(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0xd8(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0xd8(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x29eb4
movq -0xd8(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0xd8(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0x108(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0x100(%rbp), %rdi
callq 0x1f530
movq -0x108(%rbp), %rcx
movl -0x100(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x29efd
movq -0x90(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x29f0f
movq -0x90(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x9c(%rbp), %eax
movsbl -0x69(%rbp), %ecx
cmpl %ecx, %eax
jge 0x29f31
movq -0xd8(%rbp), %rax
cmpl $0x0, 0xe8(%rax)
jne 0x2a064
cmpl $0x1, -0x100(%rbp)
je 0x29f59
movl -0x94(%rbp), %eax
cmpl -0x18(%rbp), %eax
je 0x29f59
movq -0xd8(%rbp), %rax
cmpl $0x0, 0xe8(%rax)
jne 0x2a053
movq -0x80(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0xd8(%rbp), %rax
cmpl $0x0, 0xe8(%rax)
jne 0x29f93
movsbl -0x6a(%rbp), %edx
movq -0x80(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl $0x1, -0xe0(%rbp)
movq -0x80(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movsbl -0x6a(%rbp), %ecx
cmpl %ecx, %eax
je 0x29fb5
movl -0x94(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x2a051
movl -0x94(%rbp), %eax
cmpl -0x18(%rbp), %eax
jne 0x29fcf
movsbl -0x6a(%rbp), %edx
movq -0x80(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x108(%rbp), %rax
movl -0x98(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x98(%rbp)
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x2a04f
cmpl $0x0, -0xe0(%rbp)
jne 0x2a014
movq -0x108(%rbp), %rcx
movl -0x100(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x2a04f
movl -0x9c(%rbp), %esi
movl -0x98(%rbp), %edx
movl -0x10(%rbp), %ecx
movl -0x100(%rbp), %r8d
movsd -0xf8(%rbp), %xmm0
movsd -0xe8(%rbp), %xmm1
movl -0xe0(%rbp), %r9d
leaq 0x269b8(%rip), %rdi     # 0x50a00
movb $0x2, %al
callq 0x5060
jmp 0x2a051
jmp 0x2a062
movq -0x80(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x2a064
movq -0xd8(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpq $0x0, %rax
je 0x2a095
movq -0x178(%rbp), %rdi
callq 0xd3a0
movq -0x178(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2a097
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x29c8a
jmp 0x2a0a7
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x297d2
cmpq $0x0, -0x78(%rbp)
je 0x2a0cb
movq -0x78(%rbp), %rdi
callq 0x5380
cmpq $0x0, -0x80(%rbp)
je 0x2a0db
movq -0x80(%rbp), %rdi
callq 0x5380
cmpq $0x0, -0x88(%rbp)
je 0x2a0f1
movq -0x88(%rbp), %rdi
callq 0x5380
cmpq $0x0, -0x90(%rbp)
je 0x2a107
movq -0x90(%rbp), %rdi
callq 0x5380
jmp 0x2a109
movq -0x108(%rbp), %rdi
movq 0x88(%rdi), %rax
movl 0x8dc(%rax), %esi
movq 0x88(%rdi), %rax
movq 0x8f0(%rax), %rdx
callq 0x1a9f0
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x5540
nopw %cs:(%rax,%rax)