// File: /workspace/github_cpp_makefile_star2_O1/jmmccracken[P]PAI/ccmsrc.cpp
// Function: FindWeightsFromShadow(double*, int*, double*, double**, int, int, int, int)
void FindWeightsFromShadow(double dWeights[],  int iTstepOfNearestNeighborsTempRow[],  double dDelayVectorOfInterest[],double** dXShadow,int iCalShadManDim, int iEmbeddingDimension,int iLagTime,int iDelayVectorOfInterest_length){

    //Storage for sorting, calculating, and organizing
    double dDelayVector2Compare[iDelayVectorOfInterest_length],  //iterated delay vector for comparison
           dXShadowNorm,  //eucleadian distance between delay vectors being compared
           dUnsortedNorms[iCalShadManDim],  //unsorted eucleadian distances between delay vectors
           dSortedNorms[iCalShadManDim],  //sorted eucleadian distances between delay vectors
           dWeightDenominator,  //denominator in weights calculation (used for zero check)
           dWeightNormalization;  //normalization factor for weights
    int iTempValue;  //temporary value for time step of nearest neightbor delay vector

    //Loop through all of the delay vector to compare to the temp delay vector
    for(int iTstep = 1; iTstep <= iCalShadManDim; iTstep++ ){

        //Populate the temp delay vector for comparison
        for(int iCopyStep = 0; iCopyStep < iDelayVectorOfInterest_length; iCopyStep++ ){
            dDelayVector2Compare[iCopyStep] = dXShadow[iTstep-1][iCopyStep];
        }

        //Find the distance between the two temp vectors and save it
        dXShadowNorm = eDist(dDelayVectorOfInterest,dDelayVector2Compare,iDelayVectorOfInterest_length);

        //Save the norms to be sorted later
        dUnsortedNorms[iTstep-1] = dXShadowNorm;
        dSortedNorms[iTstep-1] = dXShadowNorm;
    }

    //Sort the norms
    std::sort(dSortedNorms,dSortedNorms+iCalShadManDim);

    //Save the time step locations of the E+1 closest neighbors
    iTempValue = 0;
    for(int iEDimStep = 0; iEDimStep < (iEmbeddingDimension+1); iEDimStep++ ){
        iTstepOfNearestNeighborsTempRow[iEDimStep] = -1;
    }
    for(int iEDimStep = 0; iEDimStep < (iEmbeddingDimension+1); iEDimStep++ ){
        iTempValue = FindAndCheckIndex(dUnsortedNorms,iCalShadManDim,dSortedNorms[iEDimStep+1],iTstepOfNearestNeighborsTempRow,(iEmbeddingDimension+1));
        iTstepOfNearestNeighborsTempRow[iEDimStep] = iTempValue;
    }

    //Find denominator for weight calculation
    if( dSortedNorms[1] == 0 ){
        dWeightDenominator = nan("");
        fprintf(stderr, "Warning in CCMcorr(): division by zero\n");
    }else{
        dWeightDenominator = dSortedNorms[1];
    }

    //Find weights
    dWeightNormalization = 0;
    for( int iWeightStep = 0;iWeightStep < (iEmbeddingDimension+1);iWeightStep++ ){
        dWeights[iWeightStep] = exp((-1*dSortedNorms[iWeightStep+1])/dWeightDenominator);
        dWeightNormalization += dWeights[iWeightStep];
    }

    //Check normalization factor just to be safe
    if( dWeightNormalization == 0 ){
        dWeightNormalization = nan("");
        fprintf(stderr, "Warning in CCMcorr(): division by zero\n");
    }

    //Find normalized weights
    for( int iWeightStep = 0;iWeightStep < (iEmbeddingDimension+1);iWeightStep++ ){
        dWeights[iWeightStep] = dWeights[iWeightStep]/dWeightNormalization;
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %rsi, -0x58(%rbp)
movq %rdi, -0x60(%rbp)
movl 0x18(%rbp), %ebx
movq %rsp, %r13
leaq 0xf(,%rbx,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movl %r8d, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %r15
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
movq %r8, -0x40(%rbp)
testl %r8d, %r8d
jle 0x4365
movq %rdx, %r12
movl %ebx, %eax
shlq $0x3, %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
incl %eax
movq %rax, -0x30(%rbp)
movl $0x1, %r14d
testl %ebx, %ebx
jle 0x430e
movq -0x50(%rbp), %rax
movq -0x8(%rax,%r14,8), %rsi
movq %r13, %rdi
movq -0x68(%rbp), %rdx
callq 0x3150
xorpd %xmm0, %xmm0
testl %ebx, %ebx
jle 0x4335
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm1
subsd (%r13,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %rbx
jne 0x4318
ucomisd 0x3cfb(%rip), %xmm0     # 0x8038
jb 0x4345
sqrtsd %xmm0, %xmm0
jmp 0x434a
callq 0x3140
movsd %xmm0, -0x8(%r15,%r14,8)
movq -0x38(%rbp), %rax
movsd %xmm0, -0x8(%rax,%r14,8)
incq %r14
cmpq -0x30(%rbp), %r14
jne 0x42f5
movq -0x40(%rbp), %rax
testl %eax, %eax
je 0x439a
cltq
movq -0x38(%rbp), %rbx
leaq (%rbx,%rax,8), %r12
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4d82
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4e6c
movq -0x48(%rbp), %rbx
testl %ebx, %ebx
movq -0x58(%rbp), %r13
js 0x43bd
movl %ebx, %eax
leaq 0x4(,%rax,4), %rdx
movq %r13, %rdi
movl $0xff, %esi
callq 0x30f0
leal 0x1(%rbx), %ecx
testl %ebx, %ebx
movq -0x40(%rbp), %r12
movq -0x38(%rbp), %rax
js 0x440d
movq %r13, %rdx
movq %r15, %r14
xorl %ebx, %ebx
leaq 0x1(%rbx), %rsi
movq %rsi, -0x30(%rbp)
movsd 0x8(%rax,%rbx,8), %xmm0
movq %r14, %rdi
movl %r12d, %esi
movq %rdx, %r15
movq %rcx, %r13
callq 0x459a
movq %r15, %rdx
movq %r13, %rcx
movl %eax, (%r15,%rbx,4)
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rsi
movq %rsi, %rbx
cmpq %rsi, %r13
jne 0x43d4
movq %rcx, %r12
movsd 0x8(%rax), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm2
movq %rax, %r15
jne 0x4428
jnp 0x44b4
cmpl $0x0, -0x48(%rbp)
movq -0x60(%rbp), %r13
js 0x4477
xorl %ebx, %ebx
movsd %xmm2, -0x50(%rbp)
movsd %xmm1, -0x30(%rbp)
leaq 0x1(%rbx), %r14
movsd 0x8(%r15,%rbx,8), %xmm0
xorpd 0x3bcf(%rip), %xmm0     # 0x8020
divsd %xmm2, %xmm0
callq 0x3310
movsd -0x50(%rbp), %xmm2
movsd -0x30(%rbp), %xmm1
movsd %xmm0, (%r13,%rbx,8)
addsd %xmm0, %xmm1
movq %r14, %rbx
cmpq %r14, %r12
jne 0x4439
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x4483
jnp 0x44ce
cmpl $0x0, -0x48(%rbp)
js 0x44a5
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r13,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x448b
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x3894
xorpd %xmm1, %xmm1
movsd (%r14), %xmm2
jmp 0x4428
leaq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x38d0
movsd (%r14), %xmm1
jmp 0x4483