// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rcx, %r14
movq %rdx, -0x88(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdi, -0x98(%rbp)
movl 0x10(%rbp), %ebx
movq %r9, -0x38(%rbp)
leal -0x1(%r9), %eax
imull %ebx, %eax
movl %r8d, -0x5c(%rbp)
movl %r8d, %r15d
movq %rax, -0x48(%rbp)
subl %eax, %r15d
movslq %r15d, %r12
leaq (,%r12,8), %rdi
testl %r12d, %r12d
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x3030
movq %rax, %r13
movq %r15, -0x30(%rbp)
movl %r15d, %r10d
testl %r12d, %r12d
jle 0x469e
movq -0x38(%rbp), %rax
movslq %eax, %rcx
shlq $0x3, %rcx
testl %eax, %eax
movq $-0x1, %rax
cmovsq %rax, %rcx
movq %rcx, -0x58(%rbp)
xorl %r15d, %r15d
movq -0x58(%rbp), %rdi
movq %r13, %r12
movq %r10, %r13
callq 0x3030
movq %r13, %r10
movq %r12, %r13
movq %rax, (%r12,%r15,8)
incq %r15
cmpq %r15, %r10
jne 0x467d
cmpl $0x0, -0x30(%rbp)
jle 0x46f1
movq -0x38(%rbp), %rax
incl %eax
movq -0x30(%rbp), %rcx
incl %ecx
decq %rax
movl $0x1, %edx
movq -0x48(%rbp), %rsi
cmpl $0x0, -0x38(%rbp)
jle 0x46e7
movq -0x8(%r13,%rdx,8), %rdi
xorl %r8d, %r8d
movl %esi, %r9d
movslq %r9d, %r9
movsd (%r14,%r9,8), %xmm0
movsd %xmm0, (%rdi,%r8,8)
subl %ebx, %r9d
incq %r8
cmpq %r8, %rax
jne 0x46cd
incq %rdx
incl %esi
cmpq %rcx, %rdx
jne 0x46bc
movq %r13, -0x50(%rbp)
movq -0x38(%rbp), %rcx
movl %ecx, %eax
movq %rsp, %rdx
movq %rax, -0x68(%rbp)
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
leal 0x1(%rcx), %r15d
movq %rsp, %r12
leaq 0xf(,%r15,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %rax
movq %r10, -0x90(%rbp)
leaq 0xf(,%r10,8), %rcx
andq $-0x10, %rcx
movq %rcx, -0x80(%rbp)
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movq %rax, %rsp
movq %rsp, %r14
leaq 0xf(,%r15,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movslq -0x48(%rbp), %rbx
cmpl $0x0, -0x30(%rbp)
jle 0x4836
shlq $0x3, -0x68(%rbp)
movq -0x30(%rbp), %rax
incl %eax
movq %rax, -0xa0(%rbp)
movl $0x1, %edi
movq %rdx, -0x78(%rbp)
cmpl $0x0, -0x38(%rbp)
movq -0x50(%rbp), %rcx
movq %rdi, %r13
jle 0x47b5
movq -0x8(%rcx,%r13,8), %rsi
movq %rdx, %rdi
movq -0x68(%rbp), %rdx
callq 0x3150
movq -0x50(%rbp), %rcx
movq -0x78(%rbp), %rdx
subq $0x10, %rsp
movq %r13, -0x58(%rbp)
movq -0x38(%rbp), %r13
movl %r13d, 0x8(%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq -0x30(%rbp), %r8
movl %r13d, %r9d
callq 0x426a
addq $0x10, %rsp
movq -0x40(%rbp), %rsi
movq -0x58(%rbp), %rdi
movq $0x0, -0x8(%rsi,%rdi,8)
testl %r13d, %r13d
movq -0x70(%rbp), %rdx
js 0x4822
movsd -0x8(%rsi,%rdi,8), %xmm0
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm1
movslq (%r14,%rax,4), %rcx
addq %rbx, %rcx
mulsd (%rdx,%rcx,8), %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %r15
jne 0x47fe
movsd %xmm0, -0x8(%rsi,%rdi,8)
incq %rdi
cmpq -0xa0(%rbp), %rdi
movq -0x78(%rbp), %rdx
jne 0x478f
movq -0x30(%rbp), %r13
testl %r13d, %r13d
movq -0x90(%rbp), %r12
movq -0x50(%rbp), %r15
jle 0x4863
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
testq %rdi, %rdi
je 0x485b
callq 0x31b0
incq %r14
cmpq %r14, %r12
jne 0x484d
movq %r15, %rdi
callq 0x31b0
movq %rsp, %r15
subq -0x80(%rbp), %r15
movq %r15, %rsp
movq -0x48(%rbp), %r14
movq -0x88(%rbp), %rax
cmpl %eax, %r14d
notl %r14d
jge 0x48a3
movq -0x70(%rbp), %rcx
leaq (%rcx,%rbx,8), %rsi
addl %r14d, %eax
leaq 0x8(,%rax,8), %rdx
movq %r15, %rdi
callq 0x3150
movl 0x20(%rbp), %edx
cmpl %edx, %r13d
jne 0x493e
movb 0x28(%rbp), %bl
testl %r13d, %r13d
jle 0x48d0
movq 0x18(%rbp), %rdi
addl -0x5c(%rbp), %r14d
leaq 0x8(,%r14,8), %rdx
movq -0x40(%rbp), %rsi
callq 0x3150
testb %bl, %bl
je 0x490e
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testl %r13d, %r13d
movq -0x40(%rbp), %rcx
jle 0x4902
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm2
subsd (%rcx,%rax,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
incq %rax
cmpq %rax, %r12
jne 0x48e7
ucomisd %xmm1, %xmm0
jb 0x491f
sqrtsd %xmm0, %xmm0
jmp 0x4924
movq %r15, %rdi
movq -0x40(%rbp), %rsi
movl %r13d, %edx
callq 0x4186
jmp 0x4924
callq 0x3140
movq -0x98(%rbp), %rax
movsd %xmm0, (%rax)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8683(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x3a95(%rip), %rsi      # 0x83e4
movl %r13d, %ecx
xorl %eax, %eax
callq 0x3260
jmp 0x48af