// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movl %r9d, -0x38(%rbp)
movq %rcx, %r12
movq %rdx, -0x58(%rbp)
movq %rsi, %r13
movq %rdi, -0x50(%rbp)
movl 0x18(%rbp), %edx
movq %rsp, %rbx
leaq 0xf(,%rdx,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl %r8d, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
xorl %eax, %eax
testl %edx, %edx
movl $0x0, %r15d
cmovgl %edx, %r15d
testl %r8d, %r8d
movl %r8d, -0x44(%rbp)
cmovgl %r8d, %eax
incl %eax
movq %rax, -0x60(%rbp)
pushq $0x1
popq %r14
cmpq -0x60(%rbp), %r14
je 0x3c8d
xorl %eax, %eax
cmpq %rax, %r15
je 0x3c63
movq -0x8(%r12,%r14,8), %rcx
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
jmp 0x3c4a
movq -0x58(%rbp), %rdi
movq %rbx, %rsi
movl 0x18(%rbp), %edx
callq 0x3e83
movq -0x30(%rbp), %rax
movsd %xmm0, -0x8(%rax,%r14,8)
movq -0x40(%rbp), %rax
movsd %xmm0, -0x8(%rax,%r14,8)
incq %r14
jmp 0x3c42
movslq -0x44(%rbp), %rax
movq -0x40(%rbp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x462f
movslq -0x38(%rbp), %rbx
xorl %eax, %eax
movq -0x50(%rbp), %r12
cmpq %rbx, %rax
jg 0x3cb8
orl $-0x1, (%r13,%rax,4)
incq %rax
jmp 0x3ca8
incl -0x38(%rbp)
xorl %r14d, %r14d
cmpq %rbx, %r14
jg 0x3cee
leaq 0x1(%r14), %r15
movq -0x40(%rbp), %rax
movsd 0x8(%rax,%r14,8), %xmm0
movq -0x30(%rbp), %rdi
movl -0x44(%rbp), %esi
movq %r13, %rdx
movl -0x38(%rbp), %ecx
callq 0x3ec4
movl %eax, (%r13,%r14,4)
movq %r15, %r14
jmp 0x3cbe
movq -0x40(%rbp), %rax
movsd 0x8(%rax), %xmm1
xorpd %xmm0, %xmm0
movsd %xmm1, -0x38(%rbp)
ucomisd %xmm0, %xmm1
jne 0x3d0c
jnp 0x3d93
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
xorl %r14d, %r14d
cmpq %rbx, %r14
jg 0x3d57
leaq 0x1(%r14), %r15
movq -0x40(%rbp), %rax
movsd 0x8(%rax,%r14,8), %xmm0
xorpd 0x42ec(%rip), %xmm0     # 0x8020
divsd -0x38(%rbp), %xmm0
callq 0x3340
movsd %xmm0, (%r12,%r14,8)
movsd -0x30(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
movq %r15, %r14
jmp 0x3d18
xorpd %xmm0, %xmm0
movsd -0x30(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x3d68
jnp 0x3dc5
xorl %eax, %eax
cmpq %rbx, %rax
jg 0x3d84
movsd (%r12,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rax
jmp 0x3d6a
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4a3e(%rip), %rdi      # 0x87d8
callq 0x3060
movsd %xmm0, -0x38(%rbp)
movq 0x921d(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x45ff(%rip), %rdi      # 0x83b4
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x3360
jmp 0x3d0c
leaq 0x4a0c(%rip), %rdi      # 0x87d8
callq 0x3060
movsd %xmm0, -0x30(%rbp)
movq 0x91eb(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x45cd(%rip), %rdi      # 0x83b4
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x3360
movsd -0x30(%rbp), %xmm1
jmp 0x3d68