// File: 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
subq $0xf0, %rsp
movb 0x28(%rbp), %al
movl 0x20(%rbp), %r10d
movq 0x18(%rbp), %r10
movl 0x10(%rbp), %r10d
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
andb $0x1, %al
movb %al, -0x29(%rbp)
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
imull 0x10(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x30(%rbp)
movslq -0x30(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x456f
movslq -0x28(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rdx
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x4528
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x30(%rbp), %eax
jg 0x45f4
movl -0x44(%rbp), %eax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
imull 0x10(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x40(%rbp)
movl $0x1, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0x45e7
movq -0x20(%rbp), %rax
movl -0x40(%rbp), %ecx
movl -0x48(%rbp), %edx
subl $0x1, %edx
imull 0x10(%rbp), %edx
subl %edx, %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movl -0x44(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl -0x48(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x4597
jmp 0x45e9
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x4576
movl -0x28(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x50(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xd8(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl -0x28(%rbp), %eax
incl %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xd0(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movl -0x30(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
movq %rax, -0x68(%rbp)
movl -0x28(%rbp), %eax
incl %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x70(%rbp)
movl -0x28(%rbp), %eax
subl $0x1, %eax
imull 0x10(%rbp), %eax
movl %eax, -0x74(%rbp)
movl $0x1, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x30(%rbp), %eax
jg 0x47c2
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x46ef
movq -0xd8(%rbp), %rax
movq -0x38(%rbp), %rcx
movl -0x78(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x7c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x7c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x46b2
movq -0xd8(%rbp), %rdx
movq -0xc0(%rbp), %rsi
movq -0xd0(%rbp), %rdi
movq -0x38(%rbp), %rcx
movl -0x30(%rbp), %r8d
movl -0x28(%rbp), %r9d
movl 0x10(%rbp), %r10d
movl -0x28(%rbp), %eax
subq $0x10, %rsp
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x3ee0
movq -0xc8(%rbp), %rax
addq $0x10, %rsp
movl -0x78(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x47b2
movq -0xc8(%rbp), %rax
movq -0xc0(%rbp), %rsi
movq -0xd0(%rbp), %rcx
movslq -0x80(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq -0x10(%rbp), %rcx
movl -0x74(%rbp), %edx
movslq -0x80(%rbp), %rdi
addl (%rsi,%rdi,4), %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm2
movl -0x78(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x474b
jmp 0x47b4
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x469f
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x4812
movq -0x38(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, %rax
je 0x47ff
movq -0xe0(%rbp), %rdi
callq 0x3200
jmp 0x4801
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x47cc
movq -0x38(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
je 0x482f
movq -0xe8(%rbp), %rdi
callq 0x3200
movl -0x30(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf0(%rbp)
movq %rcx, %rsp
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl -0x14(%rbp), %ecx
subl -0x74(%rbp), %ecx
cmpl %ecx, %eax
jge 0x48a8
movq -0xf0(%rbp), %rax
movq -0x10(%rbp), %rcx
movl -0x74(%rbp), %edx
addl -0x94(%rbp), %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x94(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x485f
movl 0x20(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0x48ce
movq 0x14711(%rip), %rax     # 0x18fc8
movq (%rax), %rdi
movl 0x20(%rbp), %edx
movl -0x30(%rbp), %ecx
leaq 0xbb0d(%rip), %rsi      # 0x103d4
movb $0x0, %al
callq 0x32a0
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x4917
movq -0xc8(%rbp), %rax
movslq -0x98(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x18(%rbp), %rax
movslq -0x98(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x48d8
xorps %xmm0, %xmm0
movsd %xmm0, -0xb0(%rbp)
testb $0x1, -0x29(%rbp)
jne 0x494b
movq -0xc8(%rbp), %rsi
movq -0xf0(%rbp), %rdi
movl -0x30(%rbp), %edx
callq 0x3770
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x49e4
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x49cf
movq -0xc8(%rbp), %rax
movq -0xf0(%rbp), %rcx
movslq -0xb4(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0xb4(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
mulsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
addsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x4955
movsd -0xb0(%rbp), %xmm0
callq 0x3190
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x50(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq