// File: /workspace/github_cpp_makefile_star2_O3/jmmccracken[P]PAI/ccmsrc.cpp
// Function: FindLagTimeStep(double*, int, int, double, bool)
int FindLagTimeStep(double dX[],int iXlength,int iMaxAutoCorrToCheck,double dCompareTolerance,bool bVerboseFlag){

    if( iMaxAutoCorrToCheck > (iXlength/2) ){
        fprintf(stderr, "Warning in FindLagTimeStep(): the maximum autocorreltaion lag is half the library length; ignoring requested value and checking to a lag of half the library length\n");
        iMaxAutoCorrToCheck = (iXlength/2);
    }

    double dAutoCorrs[iMaxAutoCorrToCheck],
           dCurrentAutoCorr = 0;
    int iTempVecLength = 0;

    //Calculate autocorrelations
    if( bVerboseFlag ){
            fprintf(stderr,"-- FindLagTimeStep() verbose output begin --\n");
    }
    for(int iAutoCorrLagStep = 1;iAutoCorrLagStep <= iMaxAutoCorrToCheck;iAutoCorrLagStep++ ){

        iTempVecLength = iXlength-iAutoCorrLagStep;
        double dXtemp1[iTempVecLength],dXtemp2[iTempVecLength];

        for(int iTempVecBuildStep=0;iTempVecBuildStep < iTempVecLength;iTempVecBuildStep++ ){
            dXtemp1[iTempVecBuildStep] = dX[iTempVecBuildStep];
            dXtemp2[iTempVecBuildStep] = dX[iTempVecBuildStep+iAutoCorrLagStep];
        }

        dCurrentAutoCorr = Pcorr(dXtemp1,dXtemp2,iTempVecLength);
        dAutoCorrs[iAutoCorrLagStep-1] = dCurrentAutoCorr*dCurrentAutoCorr;
        if( bVerboseFlag ){
            fprintf(stderr,"%.20f\n",dAutoCorrs[iAutoCorrLagStep-1]);
        }
    }
    if( bVerboseFlag ){
            fprintf(stderr,"-- FindLagTimeStep() verbose output end --\n");
    }

    //Find max
    int iCurrentMaxAutoCorrLag = 1;
    dCurrentAutoCorr = dAutoCorrs[0];
    for(int iter = 1;iter < iMaxAutoCorrToCheck;iter++ ){

        if( fabs(dAutoCorrs[iter]-dCurrentAutoCorr) < dCompareTolerance ){
            fprintf(stderr, "Warning in FindLagTimeStep(): autocorrelations may be cyclic; run with verbose flag to see all the autocorrelations\n");
        }

    if( fabs(dCurrentAutoCorr-1) < dCompareTolerance ){
            fprintf(stderr, "Warning in FindLagTimeStep(): autocorrelations may be near unity; run with verbose flag to see all the autocorrelations\n");
        }

        if( dAutoCorrs[iter] > dCurrentAutoCorr ){
            dCurrentAutoCorr = dAutoCorrs[iter];
            iCurrentMaxAutoCorrLag = iter+1;
        }
    }

    return(iCurrentMaxAutoCorrLag);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %r13d
movsd %xmm0, -0x40(%rbp)
movl %esi, %r15d
movq %rdi, -0x58(%rbp)
movl %esi, %ebx
shrl $0x1f, %ebx
addl %esi, %ebx
sarl %ebx
cmpl %edx, %ebx
jl 0x40d0
movl %edx, %r12d
movq %rsp, %rcx
leaq 0xf(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
testb %r13b, %r13b
jne 0x40dc
testl %r12d, %r12d
jle 0x3fdc
movslq %r15d, %r15
decq %r15
xorl %r14d, %r14d
movq %r12, -0x60(%rbp)
movl %r13d, -0x4c(%rbp)
movq %rsp, -0x38(%rbp)
movl %r15d, %r13d
movq %rsp, %r12
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
testq %r15, %r15
jle 0x3f84
shlq $0x3, %r13
movq -0x58(%rbp), %rsi
leaq (%rsi,%r14,8), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq %r12, %rdi
movq %r13, %rdx
callq 0x3150
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
callq 0x3150
movq %r12, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x40f0
mulsd %xmm0, %xmm0
movq -0x48(%rbp), %rax
movsd %xmm0, (%rax,%r14,8)
movl -0x4c(%rbp), %r13d
testb %r13b, %r13b
jne 0x3fc2
movq -0x38(%rbp), %rsp
incq %r14
decq %r15
movq -0x60(%rbp), %r12
cmpq %r14, %r12
jne 0x3f2c
jmp 0x3fdc
movq 0x8fff(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x4140(%rip), %rsi      # 0x8113
movb $0x1, %al
callq 0x3260
jmp 0x3fa9
testb %r13b, %r13b
jne 0x40e6
movl $0x1, %ebx
cmpl $0x2, %r12d
movsd -0x40(%rbp), %xmm1
jl 0x40bf
movq -0x48(%rbp), %rax
movsd (%rax), %xmm4
movl $0x1, %r14d
movapd 0x4001(%rip), %xmm2     # 0x8010
movsd 0x4019(%rip), %xmm3     # 0x8030
movq -0x48(%rbp), %rax
movsd (%rax,%r14,8), %xmm5
movapd %xmm5, %xmm0
subsd %xmm4, %xmm0
andpd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
ja 0x405f
movapd %xmm4, %xmm0
addsd %xmm3, %xmm0
andpd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
ja 0x408f
incq %r14
ucomisd %xmm4, %xmm5
cmoval %r14d, %ebx
maxsd %xmm4, %xmm5
movapd %xmm5, %xmm4
cmpq %r14, %r12
jne 0x4017
jmp 0x40bf
movsd %xmm4, -0x38(%rbp)
movsd %xmm5, -0x30(%rbp)
callq 0x37e3
movsd -0x30(%rbp), %xmm5
movsd -0x38(%rbp), %xmm4
movsd 0x3fb0(%rip), %xmm3     # 0x8030
movapd 0x3f88(%rip), %xmm2     # 0x8010
movsd -0x40(%rbp), %xmm1
jmp 0x4033
movsd %xmm4, -0x38(%rbp)
movsd %xmm5, -0x30(%rbp)
callq 0x3812
movsd -0x30(%rbp), %xmm5
movsd -0x38(%rbp), %xmm4
movsd 0x3f80(%rip), %xmm3     # 0x8030
movapd 0x3f58(%rip), %xmm2     # 0x8010
movsd -0x40(%rbp), %xmm1
jmp 0x4045
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x375b
movl %ebx, %edx
jmp 0x3eed
callq 0x3785
jmp 0x3f12
callq 0x37b4
jmp 0x3fe5