// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movl %ecx, %r12d
movsd %xmm0, -0x78(%rbp)
movl %edx, %ecx
movq %rdi, %rbx
pushq $0x2
popq %rdi
movl %esi, %eax
cltd
idivl %edi
movl %eax, %r15d
leal -0x1(%r15), %eax
imull %r12d, %eax
movq %rsi, -0x60(%rbp)
movl %esi, %r14d
subl %eax, %r14d
cmpl %ecx, %r14d
movl %r8d, -0x2c(%rbp)
jge 0x33c6
movq 0x9c1d(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x4e7f(%rip), %rsi      # 0x8234
movl %r14d, %edx
xorl %eax, %eax
callq 0x3260
movl -0x2c(%rbp), %r8d
movl %r14d, %ecx
movl %ecx, %r14d
movq %rsp, %rdx
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
testl %ecx, %ecx
jle 0x33f0
xorl %eax, %eax
movl %r15d, (%rdx,%rax,4)
incq %rax
cmpq %rax, %r14
jne 0x33e4
movq %rdx, -0x68(%rbp)
testb %r8b, %r8b
je 0x3419
movq 0x9bc8(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4e9f(%rip), %rdi      # 0x82a9
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x3330
movl -0x2c(%rbp), %r8d
movq %r14, -0x58(%rbp)
testl %r14d, %r14d
movq -0x68(%rbp), %r14
jle 0x36bb
leal 0x1(%r15), %esi
movq -0x60(%rbp), %rax
incl %eax
movl %eax, -0x84(%rbp)
movq -0x58(%rbp), %rax
incl %eax
movq %rax, -0x90(%rbp)
pushq $0x1
popq %rdx
movq %r15, -0x98(%rbp)
movl %esi, -0x44(%rbp)
cmpl $0x6, -0x60(%rbp)
jl 0x36ab
pushq $0x2
popq %rax
movq %rdx, -0x70(%rbp)
movq %rax, -0x50(%rbp)
leal -0x1(%rax), %r14d
imull %r12d, %r14d
movq -0x60(%rbp), %rax
movl %eax, %r15d
subl %r14d, %r15d
movslq %r15d, %rax
movq %rax, -0x38(%rbp)
leaq (,%rax,8), %rdi
testl %eax, %eax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x3030
movq -0x50(%rbp), %r9
movq %rax, %r13
movl %r9d, %edx
movl %r15d, -0x88(%rbp)
movl %r15d, %r15d
cmpl $0x0, -0x38(%rbp)
movq %r15, -0xa0(%rbp)
jle 0x3542
movq %rdx, -0x40(%rbp)
leaq (,%rdx,8), %rcx
movq %rcx, -0x80(%rbp)
movq %r13, -0x38(%rbp)
xorl %r13d, %r13d
movq -0x80(%rbp), %rdi
callq 0x3030
movq -0x38(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
cmpq %r13, %r15
jne 0x34d4
movq -0x50(%rbp), %r9
leal 0x1(%r9), %r10d
movl -0x84(%rbp), %eax
subl %r14d, %eax
leaq -0x1(%r10), %rcx
pushq $0x1
popq %rdx
movq -0x38(%rbp), %r13
testl %r9d, %r9d
jle 0x3531
movq -0x8(%r13,%rdx,8), %rsi
xorl %edi, %edi
movl %r14d, %r8d
movslq %r8d, %r8
movsd (%rbx,%r8,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
subl %r12d, %r8d
incq %rdi
cmpq %rdi, %rcx
jne 0x3518
incq %rdx
incl %r14d
cmpq %rax, %rdx
jne 0x3509
movq -0x40(%rbp), %rdx
jmp 0x3546
leal 0x1(%r9), %r10d
movq %rsp, -0x80(%rbp)
movq %rsp, %r15
leaq 0xf(,%rdx,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %r10, -0x38(%rbp)
movl %r10d, %eax
movq %rsp, %r14
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r14
movq %r14, %rsp
movq %rsp, %rsi
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
testl %r9d, %r9d
jle 0x35b6
movq -0x70(%rbp), %rax
movq %rsi, -0x40(%rbp)
movq -0x8(%r13,%rax,8), %rsi
shlq $0x3, %rdx
movq %r15, %rdi
callq 0x3150
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %r9
subq $0x10, %rsp
movl %r9d, 0x8(%rsp)
movq %r14, -0x40(%rbp)
movq %r14, %rdi
movq %r15, %rdx
movq %r13, %rcx
movl -0x88(%rbp), %r14d
movl %r14d, %r8d
callq 0x41ec
popq %rax
popq %rcx
testl %r14d, %r14d
movq -0xa0(%rbp), %r15
jle 0x35fe
xorl %r14d, %r14d
movq (%r13,%r14,8), %rdi
callq 0x31b0
incq %r14
cmpq %r14, %r15
jne 0x35ec
movq %r13, %rdi
callq 0x31b0
movq -0x50(%rbp), %rcx
testl %ecx, %ecx
movq -0x68(%rbp), %r14
movl -0x2c(%rbp), %r8d
movsd -0x78(%rbp), %xmm1
movl -0x44(%rbp), %esi
movq -0x70(%rbp), %rdx
movq -0x38(%rbp), %rdi
js 0x3695
xorl %r13d, %r13d
movq -0x40(%rbp), %r15
movsd (%r15,%r13,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x363f
movl %ecx, (%r14,%rdx,4)
movl %esi, %ecx
testb %r8b, %r8b
jne 0x3655
leaq 0x1(%r13), %rax
movslq %ecx, %rdi
cmpq %rdi, %r13
movq %rax, %r13
jl 0x362d
jmp 0x3691
movq 0x996c(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x4c78(%rip), %rsi      # 0x82de
movl %r13d, %r8d
movsd -0x78(%rbp), %xmm1
movb $0x2, %al
movq %rcx, %r14
callq 0x3260
movq %r14, %rcx
movq -0x68(%rbp), %r14
movq -0x70(%rbp), %rdx
movl -0x44(%rbp), %esi
movsd -0x78(%rbp), %xmm1
movl -0x2c(%rbp), %r8d
jmp 0x3644
incl %ecx
movl %ecx, %edi
movq -0x80(%rbp), %rsp
movl %edi, %eax
movq -0x98(%rbp), %r15
cmpl %r15d, %edi
jl 0x3465
incq %rdx
cmpq -0x90(%rbp), %rdx
jne 0x3454
testb %r8b, %r8b
je 0x3724
movq %r14, %r13
movq 0x98fe(%rip), %r14      # 0xcfc8
movq (%r14), %rcx
leaq 0x4c3a(%rip), %rdi      # 0x830e
pushq $0x30
popq %rsi
pushq $0x1
popq %rdx
callq 0x3330
movq -0x58(%rbp), %r12
testl %r12d, %r12d
jle 0x370c
leaq 0x4ef7(%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 0x36f2
movq (%r14), %rcx
leaq 0x4c29(%rip), %rdi      # 0x833f
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x3330
movq %r13, %r14
movq 0x989d(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4c3d(%rip), %rdi      # 0x8372
pushq $0x49
popq %rsi
pushq $0x1
popq %rdx
callq 0x3330
movq %r14, %rdi
movq -0x58(%rbp), %rsi
callq 0x4454
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq