// File: 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
subq $0xa0, %rsp
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl 0x18(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x30(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x98(%rbp)
movq %rcx, %rsp
movq %rax, -0x38(%rbp)
movl -0x24(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl -0x24(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x88(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
movl $0x1, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x24(%rbp), %eax
jg 0x402a
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x3fd0
movq -0x98(%rbp), %rax
movq -0x20(%rbp), %rcx
movl -0x68(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movq (%rcx,%rdx,8), %rcx
movslq -0x6c(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x6c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3f93
movq -0x98(%rbp), %rsi
movq -0x18(%rbp), %rdi
movl 0x18(%rbp), %edx
callq 0x4330
movq -0x90(%rbp), %rcx
movq -0x88(%rbp), %rax
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
movl -0x68(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movsd %xmm0, (%rcx,%rdx,8)
movsd -0x40(%rbp), %xmm0
movl -0x68(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x3f80
movq -0x88(%rbp), %rdi
movslq -0x24(%rbp), %rax
shlq $0x3, %rax
movq %rdi, %rsi
addq %rax, %rsi
callq 0x5090
movl $0x0, -0x64(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x4079
movq -0x10(%rbp), %rax
movslq -0x70(%rbp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x4052
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x40d7
movq -0x90(%rbp), %rdi
movq -0x88(%rbp), %rax
movl -0x24(%rbp), %esi
movl -0x74(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x10(%rbp), %rdx
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
callq 0x43c0
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %edx
movq -0x10(%rbp), %rax
movslq -0x74(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x4080
movq -0x88(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4119
jp 0x4119
leaq 0xd4a5(%rip), %rdi      # 0x1159a
callq 0x3060
movsd %xmm0, -0x58(%rbp)
movq 0x14ec2(%rip), %rax     # 0x18fc8
movq (%rax), %rdi
leaq 0xc29c(%rip), %rsi      # 0x103ac
movb $0x0, %al
callq 0x32a0
jmp 0x412a
movq -0x88(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x419c
movq -0x88(%rbp), %rax
movl -0x78(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd 0xbeca(%rip), %xmm0     # 0x10028
mulsd (%rax,%rcx,8), %xmm0
divsd -0x58(%rbp), %xmm0
callq 0x3340
movq -0x8(%rbp), %rax
movslq -0x78(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x8(%rbp), %rax
movslq -0x78(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x60(%rbp)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x4139
movsd -0x60(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x41d5
jp 0x41d5
leaq 0xd3e7(%rip), %rdi      # 0x1159a
callq 0x3060
movsd %xmm0, -0x60(%rbp)
movq 0x14e04(%rip), %rax     # 0x18fc8
movq (%rax), %rdi
leaq 0xc1de(%rip), %rsi      # 0x103ac
movb $0x0, %al
callq 0x32a0
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0x4213
movq -0x8(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd -0x60(%rbp), %xmm0
movq -0x8(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x41dc
movq -0x30(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nop