// File: /workspace/github_cpp_makefile_star2_O3/jmmccracken[P]PAI/ccmsrc.cpp
// Function: CCMcorr2(double&, double*, int, double*, int, double*, int, int, int, int, int, double*, int, bool)
void CCMcorr2(double &dPcorrYYX, double dY[],int iY_length,double dX_UsedForShadow[],int iX_UsedForShadow_length,double dZ_UsedForShadow[],int iZ_UsedForShadow_length,int iEmbeddingDimension,int iLagTime,int iYEmbeddingDimension,int iYLagTime,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);
    //}

    if( iYEmbeddingDimension < 1){
        fprintf(stderr, "Error in CCMCorr2():  Y embedding dimension is %i which is not valid\n", iYEmbeddingDimension);
        dPcorrYYX = nan("");
    }
    if( iYLagTime < 1){
        fprintf(stderr, "Error in CCMCorr2():  Y lag time is %i which is not valid\n", iYLagTime);
        dPcorrYYX  = nan("");
    }

    //Find calculated shadow manifold dimension
    int iCalShadManDim = iX_UsedForShadow_length-((iEmbeddingDimension-1)*iLagTime);

    //Assign some memory for the shadow manifold
    int iEsum = (iEmbeddingDimension+iYEmbeddingDimension);
    double** dXZShadow;
    dXZShadow = new double*[iCalShadManDim];
    for(int iter = 0;iter < iCalShadManDim;iter++ ){
        dXZShadow[iter] = new double[iEsum];
    }
    int iTstep4delayvector,iYTstep4delayvector,bar;
    
    //Create the shadow manifold by creating the delay vectors sequentially
    for(int iShadowStep = 1; iShadowStep <= iCalShadManDim; iShadowStep++ ){
        bar = 0;
        iTstep4delayvector = iShadowStep+((iEmbeddingDimension-1)*iLagTime);
        for(int iDimStep = 1; iDimStep <= iEmbeddingDimension; iDimStep++ ){
            dXZShadow[iShadowStep-1][iDimStep-1] = dX_UsedForShadow[(iTstep4delayvector-((iDimStep-1)*iLagTime))-1];
            bar++;
        }
	    iYTstep4delayvector = iShadowStep+((iYEmbeddingDimension-1)*iYLagTime);
        //printf(">> %d, >>, %d, %d, %d, %d, %d, %d\n",iEsum, iTstep4delayvector,iEmbeddingDimension,iLagTime,iYTstep4delayvector,iYEmbeddingDimension,iYLagTime);
        for(int iDimStep = 1; iDimStep <= iYEmbeddingDimension; iDimStep++ ){
            dXZShadow[iShadowStep-1][iEmbeddingDimension+iDimStep-1] = dZ_UsedForShadow[(iYTstep4delayvector-((iDimStep-1)*iYLagTime))-1];            
            bar++;
        }
        /*for(int foo = 0; foo<bar; foo++){
            printf("%.2f,",dXZShadow[iShadowStep-1][foo]);
        }
        printf("\n");*/
    }

    //Storage for sorting, calculating, and organizing
    double dDelayVectorOfInterest[iEsum],  //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 < iEsum; iCopyStep++ ){
            //printf("%.2f,", dXZShadow[iDelayVectorN-1][iCopyStep]);
            dDelayVectorOfInterest[iCopyStep] = dXZShadow[iDelayVectorN-1][iCopyStep];
        }
        //printf("\n");

        FindWeightsFromShadow(dWeights,iTstepOfNearestNeighborsTempRow,dDelayVectorOfInterest,dXZShadow,iCalShadManDim,iEmbeddingDimension,iLagTime,iEsum);

        //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 dXZShadow[iter];
    }
    delete dXZShadow;

    //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 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 %r9, %r12
movq %rcx, %r15
movq %rdx, -0x80(%rbp)
movq %rsi, -0x78(%rbp)
movq %rdi, %rbx
movl 0x28(%rbp), %eax
testl %eax, %eax
jle 0x4d0a
movl 0x20(%rbp), %r14d
movl 0x18(%rbp), %eax
cmpl $0x0, 0x30(%rbp)
jle 0x4d20
movq %rbx, -0x70(%rbp)
leal -0x1(%rax), %ebx
imull %r14d, %ebx
movl %r8d, -0x64(%rbp)
movl %r8d, %r13d
subl %ebx, %r13d
leaq (,%r13,8), %rdi
testl %r13d, %r13d
movq $-0x1, %rcx
cmovsq %rcx, %rdi
callq 0x3030
movq %r13, %rdi
movq %rax, -0x38(%rbp)
movq %rbx, -0x40(%rbp)
movslq %ebx, %rbx
testl %edi, %edi
jle 0x4c00
movq %rbx, -0x90(%rbp)
movl 0x28(%rbp), %eax
movl 0x18(%rbp), %ecx
addl %ecx, %eax
movl %eax, %ecx
movq %rcx, -0x48(%rbp)
leaq (,%rcx,8), %rcx
movq %rax, -0x60(%rbp)
testl %eax, %eax
movq $-0x1, %rax
cmovsq %rax, %rcx
movq %rcx, -0x50(%rbp)
xorl %r13d, %r13d
movq %rdi, %rbx
movq -0x50(%rbp), %rdi
callq 0x3030
movq %rbx, %rdi
movq -0x38(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
cmpq %r13, %rbx
jne 0x49ea
movl 0x28(%rbp), %eax
leal -0x1(%rax), %ecx
imull 0x30(%rbp), %ecx
movl 0x18(%rbp), %ebx
leal 0x1(%rbx), %r8d
movslq %ebx, %rdx
leal 0x1(%rax), %esi
movq %rdi, -0x58(%rbp)
incl %edi
movq %rdi, -0x50(%rbp)
movq %r8, -0x30(%rbp)
leaq -0x1(%r8), %rdi
shlq $0x3, %rdx
decq %rsi
movl $0x1, %r8d
movq -0x40(%rbp), %r9
testl %ebx, %ebx
jle 0x4a70
movq -0x38(%rbp), %r10
movq -0x8(%r10,%r8,8), %r10
xorl %r11d, %r11d
movl %r9d, %r13d
movslq %r13d, %r13
movsd (%r15,%r13,8), %xmm0
movsd %xmm0, (%r10,%r11,8)
subl %r14d, %r13d
incq %r11
cmpq %r11, %rdi
jne 0x4a56
movl 0x28(%rbp), %eax
testl %eax, %eax
movl 0x30(%rbp), %eax
jle 0x4aa6
movq -0x38(%rbp), %r10
movq -0x8(%r10,%r8,8), %r10
addq %rdx, %r10
movl %ecx, %r11d
xorl %r13d, %r13d
movslq %r11d, %r11
movsd (%r12,%r11,8), %xmm0
movsd %xmm0, (%r10,%r13,8)
incq %r13
subl %eax, %r11d
cmpq %r13, %rsi
jne 0x4a8c
incq %r8
incl %r9d
incl %ecx
cmpq -0x50(%rbp), %r8
jne 0x4a43
movq %rsp, %rdx
movq -0x48(%rbp), %rax
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, %rsp
movl -0x30(%rbp), %r14d
movq %rsp, %r15
leaq 0xf(,%r14,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %rsi
movq -0x58(%rbp), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
movq %rsp, %r12
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq -0x60(%rbp), %rax
decl %eax
leaq 0x8(,%rax,8), %rax
movq %rax, -0x88(%rbp)
movl $0x1, %r13d
movq %rdx, -0x48(%rbp)
movq %rsi, -0x30(%rbp)
cmpl $0x0, -0x60(%rbp)
movq -0x38(%rbp), %rbx
jle 0x4b59
movq -0x8(%rbx,%r13,8), %rsi
movq %rdx, %rdi
movq -0x88(%rbp), %rdx
callq 0x3150
movq -0x48(%rbp), %rdx
subq $0x10, %rsp
movq -0x60(%rbp), %rax
movl %eax, 0x8(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rcx
movq -0x58(%rbp), %r8
movl 0x18(%rbp), %ebx
movl %ebx, %r9d
callq 0x41ec
addq $0x10, %rsp
movq -0x30(%rbp), %rsi
movq $0x0, -0x8(%rsi,%r13,8)
testl %ebx, %ebx
movq -0x78(%rbp), %rdx
movq -0x90(%rbp), %rbx
js 0x4bc8
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm1
movslq (%r12,%rax,4), %rcx
addq %rbx, %rcx
mulsd (%rdx,%rcx,8), %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %r14
jne 0x4ba3
movsd %xmm0, -0x8(%rsi,%r13,8)
incq %r13
cmpq -0x50(%rbp), %r13
movq -0x48(%rbp), %rdx
jne 0x4b37
movq -0x58(%rbp), %r12
testl %r12d, %r12d
jle 0x4c1c
xorl %r14d, %r14d
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %r15
movq (%r15,%r14,8), %rdi
callq 0x31b0
incq %r14
cmpq %r14, %r12
jne 0x4bed
jmp 0x4c24
movq %rsp, %rcx
leaq 0xf(,%rdi,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
movq %rdi, %r12
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %r15
movl 0x40(%rbp), %r14d
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), %rax
cmpl %eax, %r13d
notl %r13d
jge 0x4c6c
movq -0x78(%rbp), %rcx
leaq (%rcx,%rbx,8), %rsi
addl %r13d, %eax
leaq 0x8(,%rax,8), %rdx
movq %r15, %rdi
callq 0x3150
cmpl %r14d, %r12d
movq -0x30(%rbp), %rsi
jne 0x4d39
movb 0x48(%rbp), %bl
testl %r12d, %r12d
jle 0x4cc5
movq 0x38(%rbp), %rdi
addl -0x64(%rbp), %r13d
leaq 0x8(,%r13,8), %rdx
movq -0x30(%rbp), %rsi
callq 0x3150
movq -0x30(%rbp), %rsi
testb %bl, %bl
je 0x4ce1
xorpd %xmm0, %xmm0
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm1
subsd (%rsi,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %r12
jne 0x4ca8
jmp 0x4ccd
xorpd %xmm0, %xmm0
testb %bl, %bl
je 0x4ce1
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq -0x70(%rbp), %rbx
jb 0x4cf2
sqrtsd %xmm0, %xmm0
jmp 0x4cf7
movq %r15, %rdi
movl %r12d, %edx
callq 0x40f0
movq -0x70(%rbp), %rbx
jmp 0x4cf7
callq 0x3140
movsd %xmm0, (%rbx)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rbp), %edi
movq %rbx, %rsi
movl %r8d, %r14d
callq 0x3923
movl %r14d, %r8d
jmp 0x495e
movl 0x30(%rbp), %edi
movq %rbx, %rsi
movl %r8d, %r13d
callq 0x395d
movl 0x18(%rbp), %eax
movl %r13d, %r8d
jmp 0x496f
movq 0x8288(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x369a(%rip), %rsi      # 0x83e4
movl %r14d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x3260
movq -0x30(%rbp), %rsi
jmp 0x4c79