// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %r9, %r12
movq %rcx, %r15
movq %rdx, -0x80(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdi, %r13
movl 0x30(%rbp), %r14d
movl 0x28(%rbp), %eax
testl %eax, %eax
jle 0x4d35
movl 0x18(%rbp), %ecx
testl %r14d, %r14d
movl %r8d, -0x44(%rbp)
jle 0x4d4b
movq %r13, -0x88(%rbp)
leal -0x1(%rcx), %eax
imull 0x20(%rbp), %eax
movl %r8d, %r13d
movq %rax, -0x60(%rbp)
subl %eax, %r13d
movl 0x28(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x2c(%rbp)
movslq %r13d, %r14
leaq (,%r14,8), %rdi
testl %r14d, %r14d
movq $-0x1, %rbx
cmovsq %rbx, %rdi
callq 0x3030
movq %rax, %rcx
movq %r13, -0x38(%rbp)
movl %r13d, %edx
testl %r14d, %r14d
jle 0x4a26
movslq -0x2c(%rbp), %rax
leaq (,%rax,8), %rsi
testl %eax, %eax
cmovsq %rbx, %rsi
movq %rsi, -0x58(%rbp)
xorl %r13d, %r13d
movq -0x58(%rbp), %rdi
movq %rcx, %r14
movq %rdx, %rbx
callq 0x3030
movq %rbx, %rdx
movq %r14, %rcx
movq %rax, (%r14,%r13,8)
incq %r13
cmpq %r13, %rbx
jne 0x4a05
movq %rcx, -0x40(%rbp)
movq %rdx, -0x68(%rbp)
cmpl $0x0, -0x38(%rbp)
movl 0x30(%rbp), %r14d
movl 0x20(%rbp), %ebx
jle 0x4ae6
movl 0x28(%rbp), %eax
movq %rax, %rsi
decl %eax
imull %r14d, %eax
movl 0x18(%rbp), %ecx
movq %rcx, %rdx
incl %ecx
movslq %edx, %rdx
incl %esi
movq -0x38(%rbp), %rdi
incl %edi
decq %rcx
shlq $0x3, %rdx
decq %rsi
movl $0x1, %r8d
movq -0x60(%rbp), %r9
movl 0x18(%rbp), %r10d
testl %r10d, %r10d
jle 0x4aa4
movq -0x40(%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 %ebx, %r13d
incq %r11
cmpq %r11, %rcx
jne 0x4a8a
movl 0x28(%rbp), %r10d
testl %r10d, %r10d
jle 0x4ad9
movq -0x40(%rbp), %r10
movq -0x8(%r10,%r8,8), %r10
addq %rdx, %r10
movl %eax, %r11d
xorl %r13d, %r13d
movslq %r11d, %r11
movsd (%r12,%r11,8), %xmm0
movsd %xmm0, (%r10,%r13,8)
incq %r13
subl %r14d, %r11d
cmpq %r13, %rsi
jne 0x4abf
incq %r8
incl %r9d
incl %eax
cmpq %rdi, %r8
jne 0x4a72
movl -0x2c(%rbp), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
movl 0x18(%rbp), %eax
leal 0x1(%rax), %r14d
movq %rsp, %r15
leaq 0xf(,%r14,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %rcx
movq -0x68(%rbp), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rax, -0x78(%rbp)
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %rsp, %r12
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movslq -0x60(%rbp), %rbx
cmpl $0x0, -0x38(%rbp)
jle 0x4c32
movl 0x18(%rbp), %eax
movl 0x28(%rbp), %ecx
addl %ecx, %eax
decl %eax
leaq 0x8(,%rax,8), %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
incl %eax
movq %rax, -0x98(%rbp)
movl $0x1, %r13d
movq %rdx, -0x58(%rbp)
cmpl $0x0, -0x2c(%rbp)
movq -0x40(%rbp), %rcx
jle 0x4bb4
movq -0x8(%rcx,%r13,8), %rsi
movq %rdx, %rdi
movq -0x90(%rbp), %rdx
callq 0x3150
movq -0x40(%rbp), %rcx
movq -0x58(%rbp), %rdx
subq $0x10, %rsp
movl -0x2c(%rbp), %eax
movl %eax, 0x8(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq -0x38(%rbp), %r8
movl 0x18(%rbp), %eax
movq %rax, %r9
callq 0x426a
addq $0x10, %rsp
movq -0x50(%rbp), %rsi
movq $0x0, -0x8(%rsi,%r13,8)
movl 0x18(%rbp), %eax
testl %eax, %eax
movq -0x70(%rbp), %rdx
js 0x4c1e
movsd -0x8(%rsi,%r13,8), %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 0x4bf9
movsd %xmm0, -0x8(%rsi,%r13,8)
incq %r13
cmpq -0x98(%rbp), %r13
movq -0x58(%rbp), %rdx
jne 0x4b8e
cmpl $0x0, -0x38(%rbp)
movq -0x68(%rbp), %r13
movq -0x40(%rbp), %r15
jle 0x4c59
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
testq %rdi, %rdi
je 0x4c51
callq 0x31b0
incq %r14
cmpq %r14, %r13
jne 0x4c43
movq %r15, %rdi
callq 0x31b0
movq %rsp, %r15
subq -0x78(%rbp), %r15
movq %r15, %rsp
movq -0x60(%rbp), %r12
movq -0x80(%rbp), %rax
cmpl %eax, %r12d
notl %r12d
jge 0x4c96
movq -0x70(%rbp), %rcx
leaq (%rcx,%rbx,8), %rsi
addl %r12d, %eax
leaq 0x8(,%rax,8), %rdx
movq %r15, %rdi
callq 0x3150
movq -0x38(%rbp), %r14
movl 0x40(%rbp), %edx
cmpl %edx, %r14d
jne 0x4d62
movb 0x48(%rbp), %bl
testl %r14d, %r14d
jle 0x4cc7
movq 0x38(%rbp), %rdi
addl -0x44(%rbp), %r12d
leaq 0x8(,%r12,8), %rdx
movq -0x50(%rbp), %rsi
callq 0x3150
testb %bl, %bl
je 0x4d05
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testl %r14d, %r14d
movq -0x50(%rbp), %rcx
jle 0x4cf9
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm2
subsd (%rcx,%rax,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
incq %rax
cmpq %rax, %r13
jne 0x4cde
ucomisd %xmm1, %xmm0
jb 0x4d16
sqrtsd %xmm0, %xmm0
jmp 0x4d1b
movq %r15, %rdi
movq -0x50(%rbp), %rsi
movl %r14d, %edx
callq 0x4186
jmp 0x4d1b
callq 0x3140
movq -0x88(%rbp), %rax
movsd %xmm0, (%rax)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rbp), %edi
movq %r13, %rsi
movl %r8d, %ebx
callq 0x393b
movl %ebx, %r8d
jmp 0x498f
movl %r14d, %edi
movq %r13, %rsi
callq 0x3975
movl 0x18(%rbp), %ecx
movl -0x44(%rbp), %r8d
jmp 0x499f
movq 0x825f(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x3671(%rip), %rsi      # 0x83e4
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3260
jmp 0x4ca6