// File: /workspace/github_cpp_makefile_star2_O2/jmmccracken[P]PAI/ccmsrc.cpp
// Function: CCMcorr(double&, double*, int, double*, int, int, int, double*, int, bool)
void CCMcorr(double &dPcorrYYX, double dY[],int iY_length, double dX_UsedForShadow[],int iX_UsedForShadow_length, int iEmbeddingDimension,int iLagTime, double dYestimate[], int iYestimate_length, bool bL2){

    //Check Min
    //if( iEmbeddingDimension < 3){
    //    fprintf(stderr, "Error in CCMCorr(): embedding dimension is %i which is less than 3\n", iEmbeddingDimension);
    //    return(nan(""));
    //}

    //Check Max
    //TODO: Figure out maximum for Embedding dimension (it depends on
    //      LagTime and the library length of X)
    //if( EmbeddingDimension  MAX_TBD){
    //    fprintf(stderr, "Error in CCMCorr(): embedding dimension is %i which is more than %i\n", EmbeddingDimension,MAX_TBD);
    //    return(-1);
    //}

    //Find calculated shadow manifold dimension
    int iCalShadManDim = iX_UsedForShadow_length-((iEmbeddingDimension-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[iEmbeddingDimension];
    }
    int iTstep4delayvector;

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

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

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

    //Find starting point in Y for the estimate
    int iYStart = (iEmbeddingDimension-1)*iLagTime;

    //Loop through all of the delay vectors to find it distance to every other one
    for(int iDelayVectorN = 1; iDelayVectorN <= iCalShadManDim;iDelayVectorN++ ){

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

	//printf("BE -- %.20f,%.20f",dWeights[0],dWeights[1]);
        FindWeightsFromShadow(dWeights,iTstepOfNearestNeighborsTempRow,dDelayVectorOfInterest,dXShadow,iCalShadManDim,iEmbeddingDimension,iLagTime,iEmbeddingDimension);
	//printf("AF -- %.20f,%.20f",dWeights[0],dWeights[1]);

        //Find Y point estimates from X shadow manifold
        dYEstimateGivenX[iDelayVectorN-1] = 0;
        for( int iWeightStep = 0;iWeightStep < (iEmbeddingDimension+1);iWeightStep++ ){
            dYEstimateGivenX[iDelayVectorN-1] += dWeights[iWeightStep]*dY[(iYStart+iTstepOfNearestNeighborsTempRow[iWeightStep])];
        }
    }

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

    //Clip Y to calculate correlation
    double dYclipped[iCalShadManDim];
    for(int iYstep = 0 ;iYstep < (iY_length-iYStart);iYstep++ ){
        dYclipped[iYstep] = dY[iYStart+iYstep];
    }

    //Put the estimate into the proper array
    if( iYestimate_length != iCalShadManDim ){
        fprintf(stderr, "Error in CCMCorr(): size of estimated Y container (%i) is smaller than size of estimated Y (%i)\n", iYestimate_length,iCalShadManDim);
    }
//    dYestimate = dYclipped;
    for(int iCopyStep = 0;iCopyStep < iCalShadManDim;iCopyStep++ ){
	//printf("Compare: %.20f,%.20f\n",dYclipped[iCopyStep],dYestimate[iCopyStep]);
	dYestimate[iCopyStep] = dYEstimateGivenX[iCopyStep];
    }

    //Find correlation or L2 norm of Y and its estimate
    double dDiff,dDiffSquared;
    double dRunningSum = 0;
    if( !bL2 ){
    	dPcorrYYX = Pcorr(dYclipped,dYEstimateGivenX,iCalShadManDim);
    }else{
	for(int iCopyStep = 0;iCopyStep < iCalShadManDim;iCopyStep++ ){
	    dDiff = dYclipped[iCopyStep] - dYEstimateGivenX[iCopyStep];
	    dDiffSquared = dDiff*dDiff;
            dRunningSum += dDiffSquared;
        }
    	dPcorrYYX = sqrt(dRunningSum);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, %r15d
movq %rcx, %r14
movq %rdx, -0x70(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, -0x78(%rbp)
movl 0x10(%rbp), %r13d
leal -0x1(%r15), %eax
imull %r13d, %eax
movl %eax, -0x34(%rbp)
subl %eax, %r8d
leaq (,%r8,8), %rdi
xorl %r12d, %r12d
testl %r8d, %r8d
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
movq %r8, -0x30(%rbp)
cmovlel %r12d, %r8d
movq %r8, -0x50(%rbp)
callq 0x3030
movq %rax, %rbx
movq %r15, %rcx
movl %ecx, %eax
movq %rax, -0x40(%rbp)
leaq (,%rax,8), %r15
movq %rcx, -0x48(%rbp)
testl %ecx, %ecx
pushq $-0x1
popq %rax
cmovsq %rax, %r15
cmpq %r12, -0x50(%rbp)
je 0x3fc3
movq %r15, %rdi
callq 0x3030
movq %rax, (%rbx,%r12,8)
incq %r12
jmp 0x3fac
xorl %r12d, %r12d
movq -0x48(%rbp), %rax
testl %eax, %eax
cmovgl %eax, %r12d
movl -0x34(%rbp), %ecx
movslq %ecx, %r8
movq -0x50(%rbp), %rax
leal 0x1(%rax), %r9d
pushq $0x1
popq %rax
cmpq %r9, %rax
je 0x4011
movl %ecx, %edx
xorl %esi, %esi
cmpq %rsi, %r12
je 0x400a
movslq %edx, %rdx
movsd (%r14,%rdx,8), %xmm0
movq -0x8(%rbx,%rax,8), %rdi
movsd %xmm0, (%rdi,%rsi,8)
incq %rsi
subl %r13d, %edx
jmp 0x3fea
incq %rax
incl %ecx
jmp 0x3fe1
movq %rsp, %rdx
movq -0x40(%rbp), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
movq -0x48(%rbp), %rdi
leal 0x1(%rdi), %eax
movq %rsp, %r10
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r10
movq %r10, %rsp
movq %rsp, %rsi
movq -0x30(%rbp), %rcx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
movq %rcx, -0x68(%rbp)
subq %rcx, %rsi
movq %rsi, -0x40(%rbp)
movq %rsi, %rsp
movq %rsp, %r13
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movslq %edi, %r14
pushq $0x1
popq %r15
movq %r8, -0x58(%rbp)
movq %r9, -0x90(%rbp)
movq %rdx, -0x88(%rbp)
movq %r10, -0x80(%rbp)
cmpq %r9, %r15
je 0x413a
xorl %eax, %eax
cmpq %rax, %r12
je 0x40bd
movq -0x8(%rbx,%r15,8), %rcx
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rdx,%rax,8)
incq %rax
jmp 0x40a4
subq $0x10, %rsp
movq -0x48(%rbp), %r9
movl %r9d, 0x8(%rsp)
movq %r10, %rdi
movq %r13, %rsi
movq %rbx, %rcx
movq -0x30(%rbp), %r8
callq 0x3bbb
movq -0x80(%rbp), %r10
movq -0x90(%rbp), %r9
movq -0x58(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq $0x10, %rsp
andq $0x0, -0x8(%rsi,%r15,8)
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq %r14, %rax
jg 0x412b
movsd (%r10,%rax,8), %xmm1
movslq (%r13,%rax,4), %rcx
addq %rdi, %rcx
mulsd (%rdx,%rcx,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rsi,%r15,8)
incq %rax
jmp 0x4103
incq %r15
movq -0x88(%rbp), %rdx
jmp 0x4099
xorl %r14d, %r14d
movq -0x50(%rbp), %r15
cmpq %r14, %r15
je 0x4154
movq (%rbx,%r14,8), %rdi
callq 0x31e0
incq %r14
jmp 0x4141
movq %rbx, %rdi
callq 0x31e0
movq %rsp, %rbx
subq -0x68(%rbp), %rbx
movq %rbx, %rsp
movq -0x70(%rbp), %rsi
subl -0x34(%rbp), %esi
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rdx
leaq (%rcx,%rdx,8), %rcx
movl 0x20(%rbp), %edx
cmpq %rax, %rsi
je 0x4197
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
jmp 0x4183
cmpl %edx, -0x30(%rbp)
jne 0x4220
xorl %eax, %eax
movq 0x18(%rbp), %rcx
movq -0x40(%rbp), %rsi
cmpq %rax, %r15
je 0x41be
movsd (%rsi,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rax
jmp 0x41aa
cmpb $0x0, 0x28(%rbp)
je 0x41e6
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq %rax, %r15
je 0x41f4
movsd (%rbx,%rax,8), %xmm1
subsd (%rsi,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x41ca
movq %rbx, %rdi
movq -0x30(%rbp), %rdx
callq 0x3718
jmp 0x4209
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x4204
sqrtsd %xmm0, %xmm0
jmp 0x4209
callq 0x3180
movq -0x78(%rbp), %rax
movsd %xmm0, (%rax)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8da1(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x41ab(%rip), %rsi      # 0x83dc
movq -0x30(%rbp), %rcx
xorl %eax, %eax
callq 0x3290
jmp 0x41a0