// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %ecx, %r12d
movsd %xmm0, -0x58(%rbp)
movl %edx, %ecx
movl %esi, %r14d
movq %rdi, %rbx
pushq $0x2
popq %rdi
movl %esi, %eax
cltd
idivl %edi
movq %rax, -0x48(%rbp)
decl %eax
imull %r12d, %eax
movl %esi, -0x78(%rbp)
subl %eax, %r14d
cmpl %ecx, %r14d
movl %r8d, -0x2c(%rbp)
jge 0x3879
movq 0x976a(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x49c4(%rip), %rsi      # 0x822c
movl %r14d, %edx
xorl %eax, %eax
callq 0x3290
movl -0x2c(%rbp), %r8d
movl %r14d, %ecx
movl %ecx, %eax
movq %rsp, %r14
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %edx
movl %ecx, -0x74(%rbp)
cmovgl %ecx, %edx
movq %rdx, -0x40(%rbp)
movq -0x48(%rbp), %rcx
cmpq %rax, -0x40(%rbp)
je 0x38b6
movl %ecx, (%r14,%rax,4)
incq %rax
jmp 0x38a7
testb %r8b, %r8b
je 0x38db
movq 0x9706(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x49d5(%rip), %rdi      # 0x82a1
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x3360
movl -0x2c(%rbp), %r8d
movq -0x48(%rbp), %rax
leal 0x1(%rax), %esi
movq -0x40(%rbp), %rax
incl %eax
movq %rax, -0x88(%rbp)
pushq $0x1
popq %rdx
movq %r14, -0x38(%rbp)
movl %esi, -0x30(%rbp)
cmpq -0x88(%rbp), %rdx
je 0x3b1a
pushq $0x2
popq %rax
movl %eax, %ecx
movq %rdx, -0x50(%rbp)
cmpl -0x48(%rbp), %ecx
jge 0x3b12
leal -0x1(%rcx), %r14d
imull %r12d, %r14d
movl -0x78(%rbp), %edx
subl %r14d, %edx
movslq %edx, %rax
leaq (,%rax,8), %rdi
testl %eax, %eax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
movl $0x0, %eax
movl %edx, -0x7c(%rbp)
cmovgl %edx, %eax
movq %rax, -0x70(%rbp)
movq %rcx, -0x68(%rbp)
callq 0x3030
movq -0x68(%rbp), %r9
movq %rax, %r15
movl %r9d, %eax
movq %rax, -0x90(%rbp)
leaq (,%rax,8), %rax
movq %rax, -0x60(%rbp)
xorl %r13d, %r13d
cmpq %r13, -0x70(%rbp)
je 0x398e
movq -0x60(%rbp), %rdi
callq 0x3030
movq -0x68(%rbp), %r9
movq %rax, (%r15,%r13,8)
incq %r13
jmp 0x3972
testl %r9d, %r9d
movl $0x0, %eax
cmovgl %r9d, %eax
movq -0x70(%rbp), %rcx
incl %ecx
pushq $0x1
popq %rdx
movq -0x50(%rbp), %r10
cmpq %rcx, %rdx
je 0x39d9
movl %r14d, %esi
xorl %edi, %edi
cmpq %rdi, %rax
je 0x39d1
movslq %esi, %rsi
movsd (%rbx,%rsi,8), %xmm0
movq -0x8(%r15,%rdx,8), %r8
movsd %xmm0, (%r8,%rdi,8)
incq %rdi
subl %r12d, %esi
jmp 0x39b1
incq %rdx
incl %r14d
jmp 0x39a7
movq %rsp, -0x60(%rbp)
movq %rsp, %rdx
movq -0x90(%rbp), %rdi
leaq 0xf(,%rdi,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, %rsp
leaq 0x17(,%rdi,8), %rcx
movabsq $0xffffffff0, %rsi      # imm = 0xFFFFFFFF0
andq %rsi, %rcx
movq %rsp, %r13
subq %rcx, %r13
movq %r13, %rsp
leaq 0x13(,%rdi,4), %rcx
movabsq $0x7fffffff0, %rsi      # imm = 0x7FFFFFFF0
andq %rsi, %rcx
movq %rsp, %rsi
subq %rcx, %rsi
movq %rsi, %rsp
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x3a50
movq -0x8(%r15,%r10,8), %rdi
movsd (%rdi,%rcx,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0x3a37
subq $0x10, %rsp
movl %r9d, 0x8(%rsp)
movq %r13, %rdi
movq %r15, %rcx
movl -0x7c(%rbp), %r8d
callq 0x3bbb
addq $0x10, %rsp
xorl %r14d, %r14d
cmpq %r14, -0x70(%rbp)
je 0x3a83
movq (%r15,%r14,8), %rdi
callq 0x31e0
incq %r14
jmp 0x3a6f
movq %r15, %rdi
callq 0x31e0
xorl %r15d, %r15d
movq -0x38(%rbp), %r14
movl -0x2c(%rbp), %r8d
movsd -0x58(%rbp), %xmm1
movl -0x30(%rbp), %esi
movq -0x50(%rbp), %rdx
movq -0x68(%rbp), %rcx
movslq %ecx, %rax
cmpq %rax, %r15
jg 0x3b07
movsd (%r13,%r15,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x3ac1
movl %ecx, (%r14,%rdx,4)
movl %esi, %ecx
testb %r8b, %r8b
jne 0x3acb
incq %r15
jmp 0x3aa6
movq 0x94f6(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x47fa(%rip), %rsi      # 0x82d6
movl %r15d, %r8d
movsd -0x58(%rbp), %xmm1
movb $0x2, %al
movq %rcx, %r14
callq 0x3290
movq %r14, %rcx
movq -0x38(%rbp), %r14
movq -0x50(%rbp), %rdx
movl -0x30(%rbp), %esi
movsd -0x58(%rbp), %xmm1
movl -0x2c(%rbp), %r8d
jmp 0x3ac6
incl %ecx
movq -0x60(%rbp), %rsp
jmp 0x390f
incq %rdx
jmp 0x38f9
testb %r8b, %r8b
je 0x3b85
movq 0x94a2(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x47d6(%rip), %rdi      # 0x8306
pushq $0x30
popq %rsi
pushq $0x1
popq %rdx
callq 0x3360
leaq 0x4a7e(%rip), %rbx      # 0x85c0
xorl %r14d, %r14d
movq 0x947c(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
cmpq %r14, -0x40(%rbp)
je 0x3b6f
movq -0x38(%rbp), %rax
movl (%rax,%r14,4), %edx
movq %rcx, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3290
incq %r14
jmp 0x3b45
leaq 0x47c1(%rip), %rdi      # 0x8337
pushq $0x32
popq %rsi
pushq $0x1
popq %rdx
callq 0x3360
movq -0x38(%rbp), %r14
movq 0x943c(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x47d4(%rip), %rdi      # 0x836a
pushq $0x49
popq %rsi
pushq $0x1
popq %rdx
callq 0x3360
movq %r14, %rdi
movl -0x74(%rbp), %esi
callq 0x3dfc
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq