// File: myudelson[P]hmm-scalable/HMMProblemPiGKww.cpp
// Function: HMMProblemPiGKww::GradientDescent()
NUMBER HMMProblemPiGKww::GradientDescent() {
	NCAT k, g, x;
    NPAR nS = this->p->nS; NPAR nO = this->p->nO; NCAT nK = this->p->nK, nG = this->p->nG;
    NUMBER loglik = 0;
    FitResult fr;

//    // all together
//    NUMBER ** copyPI = init2D<NUMBER>((NDAT)this->sizes[0], (NDAT)nS);
//    NUMBER ** copyPIg = init2D<NUMBER>(nG, (NDAT)nS);
//    NUMBER *** copyA =  init3D<NUMBER>((NDAT)this->sizes[1], (NDAT)nS, (NDAT)nS);
//    NUMBER *** copyB =  init3D<NUMBER>((NDAT)this->sizes[2], (NDAT)nS, (NDAT)nO);

    //
	// 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(nS, nO, nK, 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
        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"
        NCAT first_iteration_qualify = this->p->first_iteration_qualify; // at what iteration, qualification for skill/group convergence should start
        NCAT iterations_to_qualify = this->p->iterations_to_qualify; // how many concecutive iterations necessary for skill/group to qualify as converged
        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
        while(skip_k<nK || skip_g<nG /*|| frww.conv==0*/ ) {
            //
            // Skills first
            //
            for(k=0; k<nK && skip_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(nS, nO, nK, nG, this->p->tol, this->p->tol_mode);
                // link and fit
                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);
                }
                
//                NCAT xndat = this->p->k_numg[k];
//                struct data** x_data = this->p->k_g_data[k];
//                cpy1D<NUMBER>(this->pi[k],copyPI[k],nS); /*prep hide*/
                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) {
                    if(fr.iter==1 /*e<=this->p->tol*/ || skip_g==nG) { // converged quick, or don't care (others all converged
                        iter_qual_skill[k]++;
                        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
                            skip_k++;
                            if( !this->p->quiet && ( /*(!conv && iter<this->p->maxiter) ||*/ (fr.conv || fr.iter==this->p->maxiter) )) {
                                computeAlphaAndPOParam(fb->xndat, fb->x_data);
                                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
//                swap1D<NUMBER>(this->pi[k],copyPI[k],nS); /*hide*/
                delete fb;
            } // for all skills
            //
            // PIg second
            //
            for(g=0; g<nG && skip_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(nS, nO, nK, nG, this->p->tol, this->p->tol_mode);
//                NCAT xndat = this->p->g_numk[g];
//                struct data** x_data = this->p->g_k_data[g];
//                cpy1D<NUMBER>(this->PIg[g],copyPIg[g],nS); /*prep hide*/
                // vvvvvvvvvvvvvvvvvvvvv ONLY PART THAT IS DIFFERENT FROM others
                fb->link(this->getPIg(g), NULL, NULL, this->p->g_numk[g], this->p->g_k_data[g]);
                fb->Cslice = 1;
                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);
                }
                // decide on convergence
                fr = GradientDescentBit(fb);
				
				// count number of concecutive failures
				if(fr.iter==this->p->maxiter) {
					iter_fail_group[g]++;
				} else {
					iter_fail_group[g]=0;
				}
				
                if(i>=first_iteration_qualify) {
                    if(fr.iter==1 /*e<=this->p->tol*/ || skip_k==nK) { // converged quick, or don't care (others all converged
                        iter_qual_group[g]++;
                        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
                            skip_g++;
                            if( !this->p->quiet && ( /*(!conv && iter<this->p->maxiter) ||*/ (fr.conv || fr.iter==this->p->maxiter) )) {
                                computeAlphaAndPOParam(fb->xndat, fb->x_data);
                                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
//                swap1D<NUMBER>(this->PIg[g],copyPIg[g],nS); /*hide*/
            } // for all groups
            
//            // vvv temporary explorative
//            NCAT xndat = this->p->nSeq;
//            struct data** x_data = this->p->k_data;
//            
//            this->ww[0]=1; this->ww[1]=1; this->ww[2]=0;
//            computeAlphaAndPOParam(xndat, x_data);
//            NUMBER l1 = loglik = getSumLogPOPara(this->p->nSeq, this->p->k_data);
//            
//            this->ww[0]=1.01; this->ww[1]=1; this->ww[2]=0;
//            computeAlphaAndPOParam(xndat, x_data);
//            NUMBER l2 = loglik = getSumLogPOPara(this->p->nSeq, this->p->k_data);
//
//            this->ww[0]=1.05; this->ww[1]=1; this->ww[2]=0;
//            computeAlphaAndPOParam(xndat, x_data);
//            NUMBER l3 = loglik = getSumLogPOPara(this->p->nSeq, this->p->k_data);
//            // ^^^ temporary explorative
//            
            
            
            //
            // ww third
            //
            // fit bit and for result for ww
            FitResult frww;
            FitBit *fbww = new FitBit(nS, nO, nK, nG, this->p->tol, 1/*do project to simplex*/);
            fbww->link(this->ww, NULL, NULL, this->p->nSeq, this->p->k_data);

            fbww->init(FBS_PARm1);
            fbww->init(FBS_GRAD);
            if(this->p->solver==METHOD_CGD) {
                fbww->init(FBS_GRADm1);
                fbww->init(FBS_DIRm1);
            }
//            NCAT xndat = this->p->nSeq;
//            struct data** x_data = this->p->k_data;
            computeAlphaAndPOParam(fbww->xndat, fbww->x_data);
//            loglik = getSumLogPOPara(this->p->nSeq, this->p->k_data);
            bool non01constraints = this->non01constraints;
            this->non01constraints = false;
            frww = GradientDescentBit(fbww);
            this->non01constraints = non01constraints;
//            loglik = getSumLogPOPara(this->p->nSeq, this->p->k_data);
            
//            // unhide
//            cpy2D<NUMBER>(copyPI,this->PI,nK,nS);
//            cpy2D<NUMBER>(copyPIg,this->PIg,nG,nS);
//            cpy3D<NUMBER>(copyA,this->A,nK,nS,nS);
//            cpy3D<NUMBER>(copyB,this->B,nK,nS,nS);
            delete fbww;
            i++;
        }
        // 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
    
    // compute loglik
    loglik = getSumLogPOPara(this->p->nSeq, this->p->k_data);
    return loglik;

//    // all together
//    if(copyPI!=NULL) free2D(copyPI,(NDAT)this->sizes[0]);
//    if(copyPIg!=NULL) free2D(copyPIg,(NDAT)nG);
//    if(copyA !=NULL) free3D(copyA, (NDAT)this->sizes[1], (NDAT)nS);
//    if(copyB !=NULL) free3D(copyB, (NDAT)this->sizes[2], (NDAT)nS);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x198(%rbp)
movq 0x88(%rax), %rcx
movb 0x8b8(%rcx), %cl
movb %cl, -0x15(%rbp)
movq 0x88(%rax), %rcx
movb 0x8b9(%rcx), %cl
movb %cl, -0x16(%rbp)
movq 0x88(%rax), %rcx
movl 0x8c0(%rcx), %ecx
movl %ecx, -0x1c(%rbp)
movq 0x88(%rax), %rcx
movl 0x8bc(%rcx), %ecx
movl %ecx, -0x20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x0, %eax
jle 0x2c2a8
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x198(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x1a0(%rbp)
movsbl -0x15(%rbp), %esi
movsbl -0x16(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movq 0x88(%rax), %rax
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r9d
callq 0xd080
jmp 0x2bf9b
movq -0x198(%rbp), %rdi
movq -0x1a0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x1b8(%rbp)
xorl %esi, %esi
callq 0x1a560
movq -0x198(%rbp), %rdi
movq %rax, -0x1b0(%rbp)
xorl %esi, %esi
callq 0x1a5d0
movq -0x198(%rbp), %rdi
movq %rax, -0x1a8(%rbp)
xorl %esi, %esi
callq 0x1a640
movq -0x1b8(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
movq -0x1a8(%rbp), %rdx
movq %rax, %rcx
movq -0x198(%rbp), %rax
movq 0x88(%rax), %r8
movl 0x8dc(%r8), %r8d
movq 0x88(%rax), %rax
movq 0x8f0(%rax), %r9
callq 0xd990
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x2c073
movq -0x58(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x2c073
movq -0x1a0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2ce35
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x2c099
movq -0x58(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x2c0bf
movq -0x58(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x58(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0x58(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0x58(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x2c11c
movq -0x58(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0x58(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movslq 0x8dc(%rax), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0x70(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x198(%rbp), %rcx
movl -0x14(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x2c1b8
movq -0x198(%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 -0x70(%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 0x2c146
movq -0x198(%rbp), %rsi
movq -0x58(%rbp), %rdx
leaq -0x98(%rbp), %rdi
callq 0x1f530
leaq -0x50(%rbp), %rdi
leaq -0x98(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movl $0x0, -0x14(%rbp)
movq -0x198(%rbp), %rcx
movl -0x14(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x8dc(%rcx), %eax
jge 0x2c23d
movq -0x198(%rbp), %rax
movq -0x70(%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 0x2c1eb
movq -0x70(%rbp), %rdi
callq 0x5380
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x2c278
movl -0x50(%rbp), %esi
movsd -0x48(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movl -0x30(%rbp), %edx
leaq 0x24706(%rip), %rdi     # 0x50977
movb $0x2, %al
callq 0x5060
movq -0x58(%rbp), %rax
movq %rax, -0x1c0(%rbp)
cmpq $0x0, %rax
je 0x2c2a6
movq -0x1c0(%rbp), %rdi
callq 0xd3a0
movq -0x1c0(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2c2a8
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3d(%rax), %eax
cmpl $0x2, %eax
je 0x2cdfb
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rcx
movsbl 0x978(%rcx), %ecx
movl %ecx, -0x9c(%rbp)
movq 0x88(%rax), %rax
movsbl 0x979(%rax), %eax
movl %eax, -0xa0(%rbp)
movslq -0x1c(%rbp), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0xa8(%rbp)
movslq -0x20(%rbp), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0xb0(%rbp)
movslq -0x1c(%rbp), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0xb8(%rbp)
movslq -0x20(%rbp), %rdi
movl $0x4, %esi
callq 0x5180
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl $0x0, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movl -0xc4(%rbp), %ecx
movb $0x1, %al
cmpl -0x1c(%rbp), %ecx
movb %al, -0x1c1(%rbp)
jl 0x2c389
movl -0xc8(%rbp), %eax
cmpl -0x20(%rbp), %eax
setl %al
movb %al, -0x1c1(%rbp)
movb -0x1c1(%rbp), %al
testb $0x1, %al
jne 0x2c398
jmp 0x2cda1
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %ecx
xorl %eax, %eax
cmpl -0x1c(%rbp), %ecx
movb %al, -0x1c2(%rbp)
jge 0x2c3c1
movl -0xc4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
setl %al
movb %al, -0x1c2(%rbp)
movb -0x1c2(%rbp), %al
testb $0x1, %al
jne 0x2c3d0
jmp 0x2c7e0
movq -0xa8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xa0(%rbp), %eax
je 0x2c3fc
movq -0xb8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xa0(%rbp), %eax
jne 0x2c401
jmp 0x2c7d2
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x198(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x1d0(%rbp)
movsbl -0x15(%rbp), %esi
movsbl -0x16(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movq 0x88(%rax), %rax
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r9d
callq 0xd080
jmp 0x2c446
movq -0x198(%rbp), %rdi
movq -0x1d0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a560
movq -0x198(%rbp), %rdi
movq %rax, -0x1e0(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a5d0
movq -0x198(%rbp), %rdi
movq %rax, -0x1d8(%rbp)
movl -0xc(%rbp), %esi
callq 0x1a640
movq -0x1e8(%rbp), %rdi
movq -0x1e0(%rbp), %rsi
movq -0x1d8(%rbp), %rdx
movq %rax, %rcx
movq -0x198(%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 -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x2c53a
movq -0xd8(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x2c53a
movq -0x1d0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2ce35
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x2c563
movq -0xd8(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x2c58c
movq -0xd8(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0xd8(%rbp), %rax
movb $0x0, 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 -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x2c606
movq -0xd8(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0xd8(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0x198(%rbp), %rsi
movq -0xd8(%rbp), %rdx
leaq -0x100(%rbp), %rdi
callq 0x1f530
leaq -0x50(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movq -0x198(%rbp), %rcx
movl -0x50(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x2c661
movq -0xb8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x2c673
movq -0xb8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0xcc(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jl 0x2c79f
cmpl $0x1, -0x50(%rbp)
je 0x2c69a
movl -0xc8(%rbp), %eax
cmpl -0x20(%rbp), %eax
jne 0x2c78b
movq -0xa8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0xa8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xa0(%rbp), %eax
je 0x2c6d3
movl -0xc8(%rbp), %eax
cmpl -0x20(%rbp), %eax
jne 0x2c789
movl -0xc8(%rbp), %eax
cmpl -0x20(%rbp), %eax
jne 0x2c6f2
movl -0xa0(%rbp), %edx
movq -0xa8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x198(%rbp), %rax
movl -0xc4(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0xc4(%rbp)
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x2c787
cmpl $0x0, -0x30(%rbp)
jne 0x2c731
movq -0x198(%rbp), %rcx
movl -0x50(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x2c787
movq -0x198(%rbp), %rdi
movq -0xd8(%rbp), %rax
movl 0xe8(%rax), %esi
movq -0xd8(%rbp), %rax
movq 0xf0(%rax), %rdx
callq 0x1b0c0
movl -0xcc(%rbp), %esi
movl -0xc4(%rbp), %edx
movl -0xc(%rbp), %ecx
movl -0x50(%rbp), %r8d
movsd -0x48(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movl -0x30(%rbp), %r9d
leaq 0x24234(%rip), %rdi     # 0x509b4
movb $0x2, %al
callq 0x5060
jmp 0x2c789
jmp 0x2c79d
movq -0xa8(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x2c79f
movq -0xd8(%rbp), %rax
movq %rax, -0x1f0(%rbp)
cmpq $0x0, %rax
je 0x2c7d0
movq -0x1f0(%rbp), %rdi
callq 0xd3a0
movq -0x1f0(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2c7d2
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x2c39f
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %ecx
xorl %eax, %eax
cmpl -0x20(%rbp), %ecx
movb %al, -0x1f1(%rbp)
jge 0x2c809
movl -0xc8(%rbp), %eax
cmpl -0x20(%rbp), %eax
setl %al
movb %al, -0x1f1(%rbp)
movb -0x1f1(%rbp), %al
testb $0x1, %al
jne 0x2c818
jmp 0x2cbc8
movq -0xb0(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xa0(%rbp), %eax
je 0x2c844
movq -0xc0(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xa0(%rbp), %eax
jne 0x2c849
jmp 0x2cbba
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x198(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x200(%rbp)
movsbl -0x15(%rbp), %esi
movsbl -0x16(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movq 0x88(%rax), %rax
movsd 0x28(%rax), %xmm0
movsbl 0x30(%rax), %r9d
callq 0xd080
jmp 0x2c88e
movq -0x198(%rbp), %rdi
movq -0x200(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x208(%rbp)
movl -0x10(%rbp), %esi
movq (%rdi), %rax
callq *0xc0(%rax)
movq -0x208(%rbp), %rdi
movq %rax, %rsi
movq -0x198(%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 -0x198(%rbp), %rax
movq -0x108(%rbp), %rcx
movb $0x1, 0xfa(%rcx)
movq 0x88(%rax), %rax
movsbl 0x981(%rax), %eax
cmpl $0x0, %eax
je 0x2c961
movq -0x108(%rbp), %rax
movq $0x0, 0x10(%rax)
jmp 0x2c961
movq -0x200(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2ce35
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x982(%rax), %eax
cmpl $0x0, %eax
je 0x2c98a
movq -0x108(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x983(%rax), %eax
cmpl $0x0, %eax
je 0x2c9b3
movq -0x108(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x108(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0x108(%rbp), %rdi
movl $0x7, %esi
callq 0xdcb0
movq -0x108(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x2ca1f
movq -0x108(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0x108(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
movq -0x198(%rbp), %rsi
movq -0x108(%rbp), %rdx
leaq -0x130(%rbp), %rdi
callq 0x1f530
leaq -0x50(%rbp), %rdi
leaq -0x130(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movq -0x198(%rbp), %rcx
movl -0x50(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x2ca7a
movq -0xc0(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x2ca8c
movq -0xc0(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0xcc(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jl 0x2cbb8
cmpl $0x1, -0x50(%rbp)
je 0x2cab3
movl -0xc4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x2cba4
movq -0xb0(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0xb0(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0xa0(%rbp), %eax
je 0x2caec
movl -0xc4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x2cba2
movl -0xc4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x2cb0b
movl -0xa0(%rbp), %edx
movq -0xb0(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x198(%rbp), %rax
movl -0xc8(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0xc8(%rbp)
movq 0x88(%rax), %rax
cmpb $0x0, 0x3c(%rax)
jne 0x2cba0
cmpl $0x0, -0x30(%rbp)
jne 0x2cb4a
movq -0x198(%rbp), %rcx
movl -0x50(%rbp), %eax
movq 0x88(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0x2cba0
movq -0x198(%rbp), %rdi
movq -0x108(%rbp), %rax
movl 0xe8(%rax), %esi
movq -0x108(%rbp), %rax
movq 0xf0(%rax), %rdx
callq 0x1b0c0
movl -0xcc(%rbp), %esi
movl -0xc8(%rbp), %edx
movl -0x10(%rbp), %ecx
movl -0x50(%rbp), %r8d
movsd -0x48(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movl -0x30(%rbp), %r9d
leaq 0x23e67(%rip), %rdi     # 0x50a00
movb $0x2, %al
callq 0x5060
jmp 0x2cba2
jmp 0x2cbb6
movq -0xb0(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x2cbb8
jmp 0x2cbba
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x2c7e7
movl $0x110, %edi            # imm = 0x110
callq 0x52d0
movq %rax, %rdi
movq -0x198(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x210(%rbp)
movsbl -0x15(%rbp), %esi
movsbl -0x16(%rbp), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
movq 0x88(%rax), %rax
movsd 0x28(%rax), %xmm0
movl $0x1, %r9d
callq 0xd080
jmp 0x2cc0e
movq -0x198(%rbp), %rax
movq -0x210(%rbp), %rcx
movq %rcx, -0x160(%rbp)
movq -0x160(%rbp), %rdi
movq 0x98(%rax), %rsi
movq 0x88(%rax), %rcx
movl 0x8dc(%rcx), %r8d
movq 0x88(%rax), %rax
movq 0x8f0(%rax), %r9
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0xd990
movq -0x160(%rbp), %rdi
movl $0x2, %esi
callq 0xdcb0
movq -0x160(%rbp), %rdi
movl $0x3, %esi
callq 0xdcb0
movq -0x198(%rbp), %rax
movq 0x88(%rax), %rax
movsbl 0x3f(%rax), %eax
cmpl $0x3, %eax
jne 0x2ccd8
movq -0x160(%rbp), %rdi
movl $0x4, %esi
callq 0xdcb0
movq -0x160(%rbp), %rdi
movl $0x6, %esi
callq 0xdcb0
jmp 0x2ccd8
movq -0x210(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
jmp 0x2ce35
movq -0x198(%rbp), %rdi
movq -0x160(%rbp), %rax
movl 0xe8(%rax), %esi
movq -0x160(%rbp), %rax
movq 0xf0(%rax), %rdx
callq 0x1b0c0
movq -0x198(%rbp), %rsi
movb 0x80(%rsi), %al
andb $0x1, %al
movb %al, -0x161(%rbp)
movb $0x0, 0x80(%rsi)
movq -0x160(%rbp), %rdx
leaq -0x190(%rbp), %rdi
callq 0x1f530
leaq -0x158(%rbp), %rdi
leaq -0x190(%rbp), %rsi
movl $0x28, %edx
callq 0x5250
movq -0x198(%rbp), %rax
movb -0x161(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x80(%rax)
movq -0x160(%rbp), %rax
movq %rax, -0x218(%rbp)
cmpq $0x0, %rax
je 0x2cd8d
movq -0x218(%rbp), %rdi
callq 0xd3a0
movq -0x218(%rbp), %rdi
movl $0x110, %esi            # imm = 0x110
callq 0x52e0
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x2c364
cmpq $0x0, -0xa8(%rbp)
je 0x2cdb7
movq -0xa8(%rbp), %rdi
callq 0x5380
cmpq $0x0, -0xb0(%rbp)
je 0x2cdcd
movq -0xb0(%rbp), %rdi
callq 0x5380
cmpq $0x0, -0xb8(%rbp)
je 0x2cde3
movq -0xb8(%rbp), %rdi
callq 0x5380
cmpq $0x0, -0xc0(%rbp)
je 0x2cdf9
movq -0xc0(%rbp), %rdi
callq 0x5380
jmp 0x2cdfb
movq -0x198(%rbp), %rdi
movq 0x88(%rdi), %rax
movl 0x8dc(%rax), %esi
movq 0x88(%rdi), %rax
movq 0x8f0(%rax), %rdx
callq 0x1a9f0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x5540
nop