// File: /workspace/github_cpp_makefile_star2_O2/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
movl %r8d, %r13d
movq %rcx, %r14
movq %rdx, -0x88(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdi, %rbx
movl 0x28(%rbp), %eax
testl %eax, %eax
jle 0x45a7
movl 0x20(%rbp), %r15d
movl 0x18(%rbp), %edx
cmpl $0x0, 0x30(%rbp)
jle 0x45d7
movq %rbx, -0x50(%rbp)
leal -0x1(%rdx), %ecx
imull %r15d, %ecx
movq %r13, %rax
movl %ecx, -0x3c(%rbp)
subl %ecx, %eax
movl 0x28(%rbp), %ecx
addl %edx, %ecx
movq %rcx, -0x58(%rbp)
leaq (,%rax,8), %rdi
xorl %r13d, %r13d
testl %eax, %eax
pushq $-0x1
popq %rcx
cmovsq %rcx, %rdi
movq %rax, -0x38(%rbp)
cmovlel %r13d, %eax
movq %rax, -0x60(%rbp)
callq 0x3030
movq %rax, %rbx
movq -0x58(%rbp), %rcx
leaq (,%rcx,8), %rax
testl %ecx, %ecx
pushq $-0x1
popq %rcx
cmovsq %rcx, %rax
movq %rax, -0x48(%rbp)
cmpq %r13, -0x60(%rbp)
je 0x42fa
movq -0x48(%rbp), %rdi
callq 0x3030
movq %rax, (%rbx,%r13,8)
incq %r13
jmp 0x42e2
movl 0x28(%rbp), %eax
movq %rax, %rsi
decl %eax
movl 0x30(%rbp), %r13d
imull %r13d, %eax
xorl %edx, %edx
movl 0x18(%rbp), %r9d
testl %r9d, %r9d
movl $0x0, %ecx
cmovgl %r9d, %ecx
movslq %r9d, %r11
testl %esi, %esi
cmovlel %edx, %esi
movq %rsi, -0x30(%rbp)
movl -0x3c(%rbp), %esi
movslq %esi, %rdx
movq %rdx, -0x68(%rbp)
movq -0x60(%rbp), %rdx
incl %edx
movq %rdx, -0x48(%rbp)
pushq $0x1
popq %rdx
movq %r11, -0x78(%rbp)
cmpq -0x48(%rbp), %rdx
je 0x43a5
movl %esi, %edi
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0x436f
movslq %edi, %rdi
movsd (%r14,%rdi,8), %xmm0
movq -0x8(%rbx,%rdx,8), %r10
movsd %xmm0, (%r10,%r8,8)
incq %r8
subl %r15d, %edi
jmp 0x434e
movq -0x30(%rbp), %rdi
movl %eax, %r8d
subq $0x1, %rdi
jb 0x4398
movslq %r8d, %r8
movsd (%r12,%r8,8), %xmm0
movq -0x8(%rbx,%rdx,8), %r10
movsd %xmm0, (%r10,%r11,8)
incq %r11
subl %r13d, %r8d
jmp 0x4376
incq %rdx
incl %esi
incl %eax
movq -0x78(%rbp), %r11
jmp 0x4343
movq %rsp, %rdx
movq -0x58(%rbp), %rdi
leaq 0xf(,%rdi,8), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
leal 0x1(%r9), %eax
movq %rsp, %r15
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %r15
movq %r15, %rsp
movq %rsp, %rsi
movq -0x38(%rbp), %rcx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
movq %rcx, -0x80(%rbp)
subq %rcx, %rsi
movq %rsi, -0x30(%rbp)
movq %rsi, %rsp
movq %rsp, %r12
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
xorl %r13d, %r13d
testl %edi, %edi
cmovgl %edi, %r13d
pushq $0x1
popq %r14
movq %rdx, -0x90(%rbp)
cmpq -0x48(%rbp), %r14
je 0x44be
xorl %eax, %eax
cmpq %rax, %r13
je 0x4446
movq -0x8(%rbx,%r14,8), %rcx
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rdx,%rax,8)
incq %rax
jmp 0x442d
subq $0x10, %rsp
movq -0x58(%rbp), %rax
movl %eax, 0x8(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rcx
movq -0x38(%rbp), %r8
callq 0x3bbb
movq -0x78(%rbp), %r8
movq -0x30(%rbp), %rdi
movq -0x70(%rbp), %rdx
addq $0x10, %rsp
andq $0x0, -0x8(%rdi,%r14,8)
xorpd %xmm0, %xmm0
xorl %eax, %eax
movq -0x68(%rbp), %rsi
cmpq %r8, %rax
jg 0x44ab
movsd (%r15,%rax,8), %xmm1
movslq (%r12,%rax,4), %rcx
addq %rsi, %rcx
mulsd (%rdx,%rcx,8), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rdi,%r14,8)
incq %rax
jmp 0x4484
incq %r14
movl 0x18(%rbp), %r9d
movq -0x90(%rbp), %rdx
jmp 0x4421
xorl %r14d, %r14d
movq -0x60(%rbp), %r15
cmpq %r14, %r15
je 0x44d8
movq (%rbx,%r14,8), %rdi
callq 0x31e0
incq %r14
jmp 0x44c5
movq %rbx, %rdi
callq 0x31e0
movq %rsp, %rbx
subq -0x80(%rbp), %rbx
movq %rbx, %rsp
movq -0x88(%rbp), %rdx
subl -0x3c(%rbp), %edx
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
movq -0x70(%rbp), %rcx
movq -0x68(%rbp), %rsi
leaq (%rcx,%rsi,8), %rcx
movq -0x30(%rbp), %rsi
cmpq %rax, %rdx
je 0x451f
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
jmp 0x450b
movl 0x40(%rbp), %edx
cmpl %edx, -0x38(%rbp)
jne 0x460a
xorl %eax, %eax
movq 0x38(%rbp), %rcx
cmpq %rax, %r15
je 0x4545
movsd (%rsi,%rax,8), %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rax
jmp 0x4531
cmpb $0x0, 0x48(%rbp)
je 0x456d
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq %rax, %r15
je 0x457b
movsd (%rbx,%rax,8), %xmm1
subsd (%rsi,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x4551
movq %rbx, %rdi
movq -0x38(%rbp), %rdx
callq 0x3718
jmp 0x4590
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x458b
sqrtsd %xmm0, %xmm0
jmp 0x4590
callq 0x3180
movq -0x50(%rbp), %rax
movsd %xmm0, (%rax)
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8a1a(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x3e85(%rip), %rsi      # 0x843d
movl 0x28(%rbp), %edx
xorl %eax, %eax
callq 0x3290
leaq 0x420f(%rip), %rdi      # 0x87d8
callq 0x3060
movsd %xmm0, (%rbx)
jmp 0x4274
movq 0x89ea(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x3e9b(%rip), %rsi      # 0x8483
movl 0x30(%rbp), %edx
xorl %eax, %eax
callq 0x3290
leaq 0x41df(%rip), %rdi      # 0x87d8
callq 0x3060
movl 0x18(%rbp), %edx
movsd %xmm0, (%rbx)
jmp 0x4285
movq 0x89b7(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x3dc1(%rip), %rsi      # 0x83dc
movq -0x38(%rbp), %rcx
xorl %eax, %eax
callq 0x3290
movq -0x30(%rbp), %rsi
jmp 0x452b