// File: /workspace/github_cpp_makefile_star2_O3/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, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %rsi, -0x30(%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, -0x38(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %r8, %r15
testl %r8d, %r8d
jle 0x42eb
movq %rdx, %r12
leaq (,%rbx,8), %rax
movq %rax, -0x68(%rbp)
leal 0x1(%r15), %eax
movq %rax, -0x50(%rbp)
movl $0x1, %r14d
xorpd %xmm0, %xmm0
testl %ebx, %ebx
jle 0x42b7
movq -0x48(%rbp), %rax
movq -0x8(%rax,%r14,8), %rsi
movq %r13, %rdi
movq -0x68(%rbp), %rdx
callq 0x3150
xorpd %xmm0, %xmm0
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 0x429a
ucomisd 0x3d79(%rip), %xmm0     # 0x8038
jb 0x42c7
sqrtsd %xmm0, %xmm0
jmp 0x42cc
callq 0x3140
movq -0x38(%rbp), %rax
movsd %xmm0, -0x8(%rax,%r14,8)
movq -0x58(%rbp), %rax
movsd %xmm0, -0x8(%rax,%r14,8)
incq %r14
cmpq -0x50(%rbp), %r14
jne 0x4277
testl %r15d, %r15d
je 0x431e
movslq %r15d, %rax
movq -0x58(%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 0x4d60
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4e4a
movq -0x40(%rbp), %rax
leal 0x1(%rax), %ecx
movl %ecx, -0x50(%rbp)
movl %ecx, %r13d
testl %eax, %eax
movq %r15, -0x48(%rbp)
movq -0x58(%rbp), %r15
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %r12
js 0x4384
leaq (,%r13,4), %rdx
movl $0xff, %esi
callq 0x30f0
movq -0x30(%rbp), %rdx
xorl %ebx, %ebx
leaq 0x1(%rbx), %r14
movsd 0x8(%r15,%rbx,8), %xmm0
movq %r12, %rdi
movq -0x48(%rbp), %rsi
movl -0x50(%rbp), %ecx
callq 0x4514
movq -0x30(%rbp), %rcx
movl %eax, (%rcx,%rbx,4)
movq -0x30(%rbp), %rdx
movq %r14, %rbx
cmpq %r14, %r13
jne 0x4357
movsd 0x8(%r15), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x439a
jnp 0x443e
cmpl $0x0, -0x40(%rbp)
movq -0x60(%rbp), %r12
js 0x442b
xorpd %xmm1, %xmm1
xorl %ebx, %ebx
movsd %xmm2, -0x38(%rbp)
movsd %xmm1, -0x30(%rbp)
leaq 0x1(%rbx), %r14
movsd 0x8(%r15,%rbx,8), %xmm0
xorpd 0x3c55(%rip), %xmm0     # 0x8020
divsd %xmm2, %xmm0
callq 0x3310
movsd -0x38(%rbp), %xmm2
movsd -0x30(%rbp), %xmm1
movsd %xmm0, (%r12,%rbx,8)
addsd %xmm0, %xmm1
movq %r14, %rbx
cmpq %r14, %r13
jne 0x43b3
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x43fc
jnp 0x442b
cmpl $0x0, -0x40(%rbp)
js 0x441c
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rax
cmpq %rax, %r13
jne 0x4404
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 0x38b8
movsd (%r14), %xmm1
jmp 0x43fc
leaq -0x70(%rbp), %r14
movq %r14, %rdi
callq 0x387c
movsd (%r14), %xmm2
jmp 0x439a