// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rcx, %r14
movq %rdx, -0x90(%rbp)
movq %rsi, %r13
movq %rdi, -0x58(%rbp)
movl 0x10(%rbp), %r12d
movq %r9, -0x38(%rbp)
leal -0x1(%r9), %ebx
imull %r12d, %ebx
movl %r8d, -0x74(%rbp)
movl %r8d, %r15d
subl %ebx, %r15d
leaq (,%r15,8), %rdi
testl %r15d, %r15d
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x3030
movq %r15, %rdx
movq %rax, %r15
movq %rbx, -0x80(%rbp)
movslq %ebx, %rbx
testl %edx, %edx
jle 0x4807
movq -0x38(%rbp), %rcx
movl %ecx, %eax
movq %rax, -0x60(%rbp)
leaq (,%rax,8), %rax
testl %ecx, %ecx
movq $-0x1, %rcx
cmovsq %rcx, %rax
movq %rax, -0x48(%rbp)
movq %r15, -0x30(%rbp)
xorl %r15d, %r15d
movq %rdx, -0x40(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3030
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
cmpq %r15, %rdx
jne 0x4603
movq -0x38(%rbp), %r9
leal 0x1(%r9), %eax
movl %edx, %ecx
incl %ecx
movq %rcx, -0x88(%rbp)
leaq -0x1(%rax), %rcx
movl $0x1, %edx
movq -0x80(%rbp), %rsi
movq -0x30(%rbp), %r10
testl %r9d, %r9d
jle 0x466e
movq -0x8(%r10,%rdx,8), %rdi
xorl %r8d, %r8d
movl %esi, %r11d
movslq %r11d, %r11
movsd (%r14,%r11,8), %xmm0
movsd %xmm0, (%rdi,%r8,8)
subl %r12d, %r11d
incq %r8
cmpq %r8, %rcx
jne 0x4654
incq %rdx
incl %esi
cmpq -0x88(%rbp), %rdx
jne 0x4644
movq %rsp, %rdx
movq -0x60(%rbp), %rsi
leaq 0xf(,%rsi,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x70(%rbp)
movq %rdx, %rsp
movl %eax, %r12d
movq %rsp, %r14
leaq 0xf(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %rax
movq -0x40(%rbp), %rcx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
movq %rax, %rdx
subq %rcx, %rdx
movq %rdx, -0x50(%rbp)
movq %rdx, %rsp
movq %rsp, %r15
leaq 0xf(,%r12,4), %rdx
andq $-0x10, %rdx
subq %rdx, %r15
movq %r15, %rsp
negq %rcx
addq %rcx, %rax
addq $-0x8, %rax
movq %rax, -0x68(%rbp)
shlq $0x3, %rsi
movq %rsi, -0x60(%rbp)
movl $0x1, %esi
testl %r9d, %r9d
movq %rsi, -0x48(%rbp)
jle 0x4761
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x8(%rcx,%rax,8), %rsi
movq -0x70(%rbp), %rdi
movq -0x60(%rbp), %rdx
callq 0x3150
subq $0x10, %rsp
movq -0x38(%rbp), %r9
movl %r9d, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq -0x70(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x41ec
movq -0x48(%rbp), %rsi
addq $0x10, %rsp
movq -0x68(%rbp), %rax
leaq (%rax,%rsi,8), %rax
movq $0x0, (%rax)
jmp 0x47a6
subq $0x10, %rsp
movl %r9d, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq -0x70(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %r8
callq 0x41ec
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %r9
addq $0x10, %rsp
movq -0x68(%rbp), %rax
movq $0x0, (%rax,%rsi,8)
testl %r9d, %r9d
js 0x47d4
movq -0x68(%rbp), %rax
leaq (%rax,%rsi,8), %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movsd (%r14,%rcx,8), %xmm1
movslq (%r15,%rcx,4), %rdx
addq %rbx, %rdx
mulsd (%r13,%rdx,8), %xmm1
addsd %xmm1, %xmm0
incq %rcx
cmpq %rcx, %r12
jne 0x47ac
movsd %xmm0, (%rax)
movq -0x38(%rbp), %r9
incq %rsi
cmpq -0x88(%rbp), %rsi
jne 0x4701
movq -0x40(%rbp), %r12
testl %r12d, %r12d
movq -0x30(%rbp), %r15
jle 0x4823
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
callq 0x31b0
incq %r14
cmpq %r14, %r12
jne 0x47f4
jmp 0x4823
movq %rsp, %rcx
leaq 0xf(,%rdx,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %rdx, %r12
movq %r15, %rdi
callq 0x31b0
movq %rsp, %r15
leaq 0xf(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq -0x80(%rbp), %r14
movq -0x90(%rbp), %rax
cmpl %eax, %r14d
notl %r14d
jge 0x4871
leaq (,%rbx,8), %rsi
addq %r13, %rsi
addl %r14d, %eax
leaq 0x8(,%rax,8), %rdx
movq %r15, %rdi
callq 0x3150
movl 0x20(%rbp), %edx
cmpl %edx, %r12d
jne 0x4911
movb 0x28(%rbp), %bl
testl %r12d, %r12d
jle 0x48c9
movq 0x18(%rbp), %rdi
addl -0x74(%rbp), %r14d
leaq 0x8(,%r14,8), %rdx
movq -0x50(%rbp), %r14
movq %r14, %rsi
callq 0x3150
testb %bl, %bl
je 0x48e5
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm1
subsd (%r14,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %r12
jne 0x48ab
jmp 0x48d5
xorpd %xmm0, %xmm0
testb %bl, %bl
movq -0x50(%rbp), %r14
je 0x48e5
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x48f5
sqrtsd %xmm0, %xmm0
jmp 0x48fa
movq %r15, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x40f0
jmp 0x48fa
callq 0x3140
movq -0x58(%rbp), %rax
movsd %xmm0, (%rax)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x86b0(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x3ac2(%rip), %rsi      # 0x83e4
movl %r12d, %ecx
xorl %eax, %eax
callq 0x3260
jmp 0x487d