// File: /workspace/github_cpp_makefile_star2_O1/jmmccracken[P]PAI/ccmsrc.cpp
// Function: FindEmbeddingDimension(double*, int, int, double, int, bool)
int FindEmbeddingDimension(double dX[],int iXlength,int iNumberOfTimeStep2Check,double dWeightToleranceLevel,int iLagTime,bool bVerboseFlag){

    //Set the max embedding dimension to check (to save computation time)
    int iMaxEDim = (iXlength/2);

    //Find minimum size of possible shadow manifolds and report a warning
    //if the requested number of time step checks is too large
    int iCalShadManDimMin = iXlength-((iMaxEDim-1)*iLagTime);
    if( iNumberOfTimeStep2Check > iCalShadManDimMin ){
        fprintf(stderr,"Warning in FindEmbeddingDimension(): the requested number of time step checks is too large; setting the value to %i\n",iCalShadManDimMin);
        iNumberOfTimeStep2Check = iCalShadManDimMin;
    }

    int iCutoffEDimForEachTimeStep[iNumberOfTimeStep2Check];
    for(int iter=0;iter < iNumberOfTimeStep2Check;iter++ ){
        iCutoffEDimForEachTimeStep[iter] = iMaxEDim;
    }

    if( bVerboseFlag ){
        fprintf(stderr,"-- FindEmbeddingDimension() verbose output begin --\n");
    }

    //Loop through the requested number of delay vectors to find the weights
    for(int iDelayVectorN = 1; iDelayVectorN <= iNumberOfTimeStep2Check;iDelayVectorN++ ){

        for(int iEstep = 2;iEstep < iMaxEDim;iEstep++ ){

            //Find calculated shadow manifold dimension
            int iCalShadManDim = iXlength-((iEstep-1)*iLagTime);

            //Assign some memory for the shadow manifold
            double** dXShadow;
            dXShadow = new double*[iCalShadManDim];
            for(int iter = 0;iter < iCalShadManDim;iter++ ){
                dXShadow[iter] = new double[iEstep];
            }
            int iTstep4delayvector;

            //Create the shadow manifold by creating the delay vectors sequentially
            for(int iShadowStep = 1; iShadowStep <= iCalShadManDim;iShadowStep++ ){
                iTstep4delayvector = iShadowStep+((iEstep-1)*iLagTime);
                for(int iDimStep = 1; iDimStep <= iEstep; iDimStep++ ){
                    dXShadow[iShadowStep-1][iDimStep-1] = dX[(iTstep4delayvector-((iDimStep-1)*iLagTime))-1];
                }
            }

            //Storage for sorting, calculating, and organizing
            double dDelayVectorOfInterest[iEstep],  //delay vector currently being compared
                   dWeights[(iEstep+1)],  //weights to contruct Y estimate
                   dYEstimateGivenX[iCalShadManDim];  //estimated Y used the shadow manifold of X

            int iTstepOfNearestNeighborsTempRow[iEstep+1];  //time steps of nearest neighbor delay vectors

            //Populate the temp delay vector
            for(int iCopyStep = 0; iCopyStep < iEstep; iCopyStep++ ){
                dDelayVectorOfInterest[iCopyStep] = dXShadow[iDelayVectorN-1][iCopyStep];
            }

            FindWeightsFromShadow(dWeights,iTstepOfNearestNeighborsTempRow,dDelayVectorOfInterest,dXShadow,iCalShadManDim,iEstep,iLagTime,iEstep);

            //Free the dXshadow memory
            for(int iter = 0;iter < iCalShadManDim;iter++ ){
                delete dXShadow[iter];
            }
            delete dXShadow;

            //Check weights
            for(int iWstep = 0;iWstep < (iEstep+1);iWstep++ ){
                if( dWeights[iWstep] < dWeightToleranceLevel ){
                    iCutoffEDimForEachTimeStep[iDelayVectorN] = iEstep;
                    iEstep = (iXlength/2)+1; //break out of the loop over embedding dimensions
                }
                if( bVerboseFlag ){
                    fprintf(stderr,"t = %i;E = %i; weight %i = %.20f (tol = %.20f)\n",iDelayVectorN,iEstep,iWstep,dWeights[iWstep],dWeightToleranceLevel);
                }
            }
        }
    }

   if( bVerboseFlag ){
        fprintf(stderr,"Cutoff embedding dimensions for each time step:\n");
        for(int iter=0;iter < iNumberOfTimeStep2Check;iter++ ){
            fprintf(stderr,"%i\n",iCutoffEDimForEachTimeStep[iter]);
        }
   }
   if( bVerboseFlag ){
       fprintf(stderr,"-- FindEmbeddingDimension() verbose output end --\n");
   }

   bool bCheck = true;
   for(int iter=0;iter < iNumberOfTimeStep2Check;iter++ ){
        if( iCutoffEDimForEachTimeStep[iter] == iMaxEDim){
                bCheck &= true;
        }
   }
    if( bCheck ){
        fprintf(stderr,"Warning in FindEmbeddingDimension(): cutoff dimension may have maxed out\n");
    }

    //Find and return mode
    int iEDimMode = iMode(iCutoffEDimForEachTimeStep,iNumberOfTimeStep2Check);
    return(iEDimMode);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %r8d, -0x2c(%rbp)
movl %ecx, %r12d
movsd %xmm0, -0x70(%rbp)
movl %edx, %r15d
movq %rdi, %rbx
pushq $0x2
popq %rcx
movl %esi, %eax
cltd
idivl %ecx
movl %eax, %r13d
leal -0x1(%r13), %eax
imull %r12d, %eax
movq %rsi, -0x68(%rbp)
movl %esi, %r14d
subl %eax, %r14d
cmpl %r15d, %r14d
jge 0x33c6
movq 0x9c19(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x4e7b(%rip), %rsi      # 0x8234
movl %r14d, %edx
xorl %eax, %eax
callq 0x3260
movl %r14d, %r15d
movl %r15d, %edx
movq %rsp, %rcx
leaq 0xf(,%rdx,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %rsp
testl %r15d, %r15d
jle 0x33f1
xorl %eax, %eax
movl %r13d, (%rcx,%rax,4)
incq %rax
cmpq %rax, %rdx
jne 0x33e5
movq %rdx, -0x88(%rbp)
cmpb $0x0, -0x2c(%rbp)
movq %rcx, -0x48(%rbp)
je 0x341e
movq 0x9bbf(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4e96(%rip), %rdi      # 0x82a9
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x3330
movq %r15, -0x60(%rbp)
testl %r15d, %r15d
movl -0x2c(%rbp), %r15d
jle 0x36c6
leal 0x1(%r13), %esi
movq -0x68(%rbp), %rax
incl %eax
movl %eax, -0x74(%rbp)
movq -0x60(%rbp), %rax
incl %eax
movq %rax, -0x90(%rbp)
pushq $0x1
popq %rdx
movq %r13, -0x98(%rbp)
movl %esi, -0x3c(%rbp)
movl %r12d, -0x78(%rbp)
cmpl $0x6, -0x68(%rbp)
jl 0x36b6
pushq $0x2
popq %rax
movl %eax, %ecx
movq %rdx, -0x50(%rbp)
leal -0x1(%rcx), %r15d
imull %r12d, %r15d
movq -0x68(%rbp), %rax
movl %eax, %r13d
subl %r15d, %r13d
movslq %r13d, %r14
leaq (,%r14,8), %rdi
testl %r14d, %r14d
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
movq %rcx, %r12
callq 0x3030
movq %rax, %rcx
movq %r12, -0x38(%rbp)
movl %r12d, %eax
movq %rax, -0x80(%rbp)
movl %r13d, -0x40(%rbp)
movl %r13d, %r12d
testl %r14d, %r14d
jle 0x34e4
movq -0x80(%rbp), %rax
leaq (,%rax,8), %rax
movq %rax, -0x58(%rbp)
xorl %r13d, %r13d
movq -0x58(%rbp), %rdi
movq %rcx, %r14
callq 0x3030
movq %r14, %rcx
movq %rax, (%r14,%r13,8)
incq %r13
cmpq %r13, %r12
jne 0x34c9
movq %r12, -0x58(%rbp)
cmpl $0x0, -0x40(%rbp)
movq -0x38(%rbp), %r9
movq %rcx, %r13
movl -0x78(%rbp), %r12d
jle 0x353c
leal 0x1(%r9), %eax
movl -0x74(%rbp), %ecx
subl %r15d, %ecx
decq %rax
pushq $0x1
popq %rdx
testl %r9d, %r9d
jle 0x3531
movq -0x8(%r13,%rdx,8), %rsi
xorl %edi, %edi
movl %r15d, %r8d
movslq %r8d, %r8
movsd (%rbx,%r8,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
subl %r12d, %r8d
incq %rdi
cmpq %rdi, %rax
jne 0x3518
incq %rdx
incl %r15d
cmpq %rcx, %rdx
jne 0x3509
movq %rsp, -0xa8(%rbp)
movq %rsp, %r15
movq -0x80(%rbp), %rdx
leaq 0xf(,%rdx,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
leaq 0x17(,%rdx,8), %rax
movabsq $0xffffffff0, %rcx      # imm = 0xFFFFFFFF0
andq %rcx, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
leaq 0x13(,%rdx,4), %rax
movabsq $0x7fffffff0, %rcx      # imm = 0x7FFFFFFF0
andq %rcx, %rax
movq %rsp, %rsi
subq %rax, %rsi
movq %rsi, %rsp
testl %r9d, %r9d
jle 0x35c4
movq -0x50(%rbp), %rax
movq %rsi, -0xa0(%rbp)
movq -0x8(%r13,%rax,8), %rsi
shlq $0x3, %rdx
movq %r15, %rdi
callq 0x3150
movq -0xa0(%rbp), %rsi
movq -0x38(%rbp), %r9
subq $0x10, %rsp
movl %r9d, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rdx
movq %r13, %rcx
movl -0x40(%rbp), %r15d
movl %r15d, %r8d
callq 0x426a
addq $0x10, %rsp
testl %r15d, %r15d
movq -0x58(%rbp), %rax
jle 0x360d
xorl %r15d, %r15d
movq (%r13,%r15,8), %rdi
testq %rdi, %rdi
je 0x3605
callq 0x31b0
movq -0x58(%rbp), %rax
incq %r15
cmpq %r15, %rax
jne 0x35f2
movq %r13, %rdi
callq 0x31b0
movq -0x38(%rbp), %rcx
testl %ecx, %ecx
js 0x3695
xorl %r13d, %r13d
movl -0x2c(%rbp), %r15d
movq -0x48(%rbp), %rdi
movsd -0x70(%rbp), %xmm1
movl -0x3c(%rbp), %esi
movq -0x50(%rbp), %rdx
movsd (%r14,%r13,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x3645
movl %ecx, (%rdi,%rdx,4)
movl %esi, %ecx
testb %r15b, %r15b
jne 0x365b
leaq 0x1(%r13), %rax
movslq %ecx, %r8
cmpq %r8, %r13
movq %rax, %r13
jl 0x3634
jmp 0x369d
movq 0x9966(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x4c72(%rip), %rsi      # 0x82de
movl %r13d, %r8d
movsd -0x70(%rbp), %xmm1
movb $0x2, %al
movq %rcx, -0x38(%rbp)
callq 0x3260
movq -0x38(%rbp), %rcx
movq -0x50(%rbp), %rdx
movl -0x3c(%rbp), %esi
movsd -0x70(%rbp), %xmm1
movq -0x48(%rbp), %rdi
jmp 0x364a
movl -0x2c(%rbp), %r15d
movq -0x50(%rbp), %rdx
movq -0xa8(%rbp), %rsp
incl %ecx
movq -0x98(%rbp), %r13
cmpl %r13d, %ecx
jl 0x346d
incq %rdx
cmpq -0x90(%rbp), %rdx
jne 0x345a
testb %r15b, %r15b
movq -0x60(%rbp), %rbx
je 0x373d
movq 0x98f2(%rip), %r14      # 0xcfc8
movq (%r14), %rcx
leaq 0x4c2e(%rip), %rdi      # 0x830e
pushq $0x30
popq %rsi
pushq $0x1
popq %rdx
callq 0x3330
testl %ebx, %ebx
movq -0x48(%rbp), %r13
movq -0x88(%rbp), %r12
jle 0x371e
leaq 0x4ee5(%rip), %rbx      # 0x85e6
xorl %r15d, %r15d
movq (%r14), %rdi
movl (%r13,%r15,4), %edx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3260
incq %r15
cmpq %r15, %r12
jne 0x3704
cmpb $0x0, -0x2c(%rbp)
movq -0x60(%rbp), %rbx
je 0x373d
movq (%r14), %rcx
leaq 0x4c0d(%rip), %rdi      # 0x833f
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x3330
movq 0x9884(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4c24(%rip), %rdi      # 0x8372
pushq $0x49
popq %rsi
pushq $0x1
popq %rdx
callq 0x3330
movq -0x48(%rbp), %rdi
movl %ebx, %esi
callq 0x44e1
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq