// File: 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
subq $0x110, %rsp            # imm = 0x110
movb 0x48(%rbp), %al
movl 0x40(%rbp), %r10d
movq 0x38(%rbp), %r10
movl 0x30(%rbp), %r10d
movl 0x28(%rbp), %r10d
movl 0x20(%rbp), %r10d
movl 0x18(%rbp), %r10d
movl 0x10(%rbp), %r10d
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
andb $0x1, %al
movb %al, -0x31(%rbp)
cmpl $0x1, 0x28(%rbp)
jge 0x4a6b
movq 0x14585(%rip), %rax     # 0x18fc8
movq (%rax), %rdi
movl 0x28(%rbp), %edx
leaq 0xb9e5(%rip), %rsi      # 0x10435
movb $0x0, %al
callq 0x32a0
leaq 0xcb3c(%rip), %rdi      # 0x1159a
callq 0x3060
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
cmpl $0x1, 0x30(%rbp)
jge 0x4aa0
movq 0x14550(%rip), %rax     # 0x18fc8
movq (%rax), %rdi
movl 0x30(%rbp), %edx
leaq 0xb9f6(%rip), %rsi      # 0x1047b
movb $0x0, %al
callq 0x32a0
leaq 0xcb07(%rip), %rdi      # 0x1159a
callq 0x3060
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
movl -0x24(%rbp), %eax
movl 0x18(%rbp), %ecx
subl $0x1, %ecx
imull 0x20(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x38(%rbp)
movl 0x18(%rbp), %eax
addl 0x28(%rbp), %eax
movl %eax, -0x3c(%rbp)
movslq -0x38(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x4b32
movslq -0x3c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rdx
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x4aeb
movl $0x1, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jg 0x4c43
movl $0x0, -0x58(%rbp)
movl -0x5c(%rbp), %eax
movl 0x18(%rbp), %ecx
subl $0x1, %ecx
imull 0x20(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x50(%rbp)
movl $0x1, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl 0x18(%rbp), %eax
jg 0x4bbe
movq -0x20(%rbp), %rax
movl -0x50(%rbp), %ecx
movl -0x60(%rbp), %edx
subl $0x1, %edx
imull 0x20(%rbp), %edx
subl %edx, %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl -0x60(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x4b65
movl -0x5c(%rbp), %eax
movl 0x28(%rbp), %ecx
subl $0x1, %ecx
imull 0x30(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x54(%rbp)
movl $0x1, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl 0x28(%rbp), %eax
jg 0x4c33
movq -0x30(%rbp), %rax
movl -0x54(%rbp), %ecx
movl -0x64(%rbp), %edx
subl $0x1, %edx
imull 0x30(%rbp), %edx
subl %edx, %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x48(%rbp), %rax
movl -0x5c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl 0x18(%rbp), %ecx
addl -0x64(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x4bd7
jmp 0x4c35
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x4b39
movl -0x3c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x70(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf8(%rbp)
movq %rcx, %rsp
movq %rax, -0x78(%rbp)
movl 0x18(%rbp), %eax
incl %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf0(%rbp)
movq %rcx, %rsp
movq %rax, -0x80(%rbp)
movl -0x38(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xe8(%rbp)
movq %rcx, %rsp
movq %rax, -0x88(%rbp)
movl 0x18(%rbp), %eax
incl %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xe0(%rbp)
movq %rcx, %rsp
movq %rax, -0x90(%rbp)
movl 0x18(%rbp), %eax
subl $0x1, %eax
imull 0x20(%rbp), %eax
movl %eax, -0x94(%rbp)
movl $0x1, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x38(%rbp), %eax
jg 0x4e56
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x4d62
movq -0xf8(%rbp), %rax
movq -0x48(%rbp), %rcx
movl -0x98(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x9c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x9c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x4d13
movq -0xf8(%rbp), %rdx
movq -0xe0(%rbp), %rsi
movq -0xf0(%rbp), %rdi
movq -0x48(%rbp), %rcx
movl -0x38(%rbp), %r8d
movl 0x18(%rbp), %r9d
movl 0x20(%rbp), %r10d
movl -0x3c(%rbp), %eax
subq $0x10, %rsp
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x3ee0
movq -0xe8(%rbp), %rax
addq $0x10, %rsp
movl -0x98(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl $0x0, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movl 0x18(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x4e40
movq -0xe8(%rbp), %rax
movq -0xe0(%rbp), %rsi
movq -0xf0(%rbp), %rcx
movslq -0xa0(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq -0x10(%rbp), %rcx
movl -0x94(%rbp), %edx
movslq -0xa0(%rbp), %rdi
addl (%rsi,%rdi,4), %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm2
movl -0x98(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x4dc4
jmp 0x4e42
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x4cfa
movl $0x0, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x4ea6
movq -0x48(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x100(%rbp)
cmpq $0x0, %rax
je 0x4e93
movq -0x100(%rbp), %rdi
callq 0x3200
jmp 0x4e95
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x4e60
movq -0x48(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, %rax
je 0x4ec3
movq -0x108(%rbp), %rdi
callq 0x3200
movl -0x38(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x110(%rbp)
movq %rcx, %rsp
movq %rax, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl -0x14(%rbp), %ecx
subl -0x94(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4f42
movq -0x110(%rbp), %rax
movq -0x10(%rbp), %rcx
movl -0x94(%rbp), %edx
addl -0xb4(%rbp), %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0xb4(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x4ef3
movl 0x40(%rbp), %eax
cmpl -0x38(%rbp), %eax
je 0x4f68
movq 0x14077(%rip), %rax     # 0x18fc8
movq (%rax), %rdi
movl 0x40(%rbp), %edx
movl -0x38(%rbp), %ecx
leaq 0xb473(%rip), %rsi      # 0x103d4
movb $0x0, %al
callq 0x32a0
movl $0x0, -0xb8(%rbp)
movl -0xb8(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x4fb1
movq -0xe8(%rbp), %rax
movslq -0xb8(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x38(%rbp), %rax
movslq -0xb8(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x4f72
xorps %xmm0, %xmm0
movsd %xmm0, -0xd0(%rbp)
testb $0x1, -0x31(%rbp)
jne 0x4fe5
movq -0xe8(%rbp), %rsi
movq -0x110(%rbp), %rdi
movl -0x38(%rbp), %edx
callq 0x3770
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
jmp 0x507e
movl $0x0, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x5069
movq -0xe8(%rbp), %rax
movq -0x110(%rbp), %rcx
movslq -0xd4(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0xd4(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xc0(%rbp), %xmm0
mulsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd -0xc8(%rbp), %xmm0
addsd -0xd0(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x4fef
movsd -0xd0(%rbp), %xmm0
callq 0x3190
movq -0x8(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x70(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)