// File: /workspace/github_cpp_makefile_star2_O1/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 %edx, %r14d
movl %esi, %r12d
movq %rdi, -0x60(%rbp)
movl %esi, %ebx
shrl $0x1f, %ebx
addl %esi, %ebx
sarl %ebx
cmpl %edx, %ebx
jl 0x4165
movl %r14d, %eax
movq %rsp, %rcx
movq %rax, -0x58(%rbp)
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
testb %r13b, %r13b
jne 0x4172
movl %r14d, -0x4c(%rbp)
testl %r14d, %r14d
jle 0x4070
movslq %r12d, %r12
decq %r12
xorl %r15d, %r15d
movl %r13d, -0x50(%rbp)
movq %rsp, -0x38(%rbp)
movl %r12d, %r13d
movq %rsp, %rbx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
testq %r12, %r12
jle 0x401b
shlq $0x3, %r13
movq -0x60(%rbp), %rsi
leaq (%rsi,%r15,8), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq %rbx, %rdi
movq %r13, %rdx
callq 0x3150
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
callq 0x3150
movq %rbx, %rdi
movq %r14, %rsi
movl %r12d, %edx
callq 0x4186
mulsd %xmm0, %xmm0
movq -0x48(%rbp), %rax
movsd %xmm0, (%rax,%r15,8)
movl -0x50(%rbp), %r13d
testb %r13b, %r13b
jne 0x4056
movq -0x38(%rbp), %rsp
incq %r15
decq %r12
cmpq %r15, -0x58(%rbp)
jne 0x3fc3
jmp 0x4070
movq 0x8f6b(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x40ac(%rip), %rsi      # 0x8113
movb $0x1, %al
callq 0x3260
jmp 0x4040
testb %r13b, %r13b
jne 0x417c
movl $0x1, %r14d
cmpl $0x2, -0x4c(%rbp)
movsd -0x40(%rbp), %xmm1
jl 0x4153
movq -0x48(%rbp), %rax
movsd (%rax), %xmm4
movl $0x1, %ebx
movapd 0x3f6d(%rip), %xmm2     # 0x8010
movsd 0x3f85(%rip), %xmm3     # 0x8030
movq -0x48(%rbp), %rax
movsd (%rax,%rbx,8), %xmm5
movapd %xmm5, %xmm0
subsd %xmm4, %xmm0
andpd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
ja 0x40f3
movapd %xmm4, %xmm0
addsd %xmm3, %xmm0
andpd %xmm2, %xmm0
ucomisd %xmm0, %xmm1
ja 0x4123
incq %rbx
ucomisd %xmm4, %xmm5
cmoval %ebx, %r14d
maxsd %xmm4, %xmm5
movapd %xmm5, %xmm4
cmpq %rbx, -0x58(%rbp)
jne 0x40ab
jmp 0x4153
movsd %xmm4, -0x38(%rbp)
movsd %xmm5, -0x30(%rbp)
callq 0x37fb
movsd -0x30(%rbp), %xmm5
movsd -0x38(%rbp), %xmm4
movsd 0x3f1c(%rip), %xmm3     # 0x8030
movapd 0x3ef4(%rip), %xmm2     # 0x8010
movsd -0x40(%rbp), %xmm1
jmp 0x40c6
movsd %xmm4, -0x38(%rbp)
movsd %xmm5, -0x30(%rbp)
callq 0x382a
movsd -0x30(%rbp), %xmm5
movsd -0x38(%rbp), %xmm4
movsd 0x3eec(%rip), %xmm3     # 0x8030
movapd 0x3ec4(%rip), %xmm2     # 0x8010
movsd -0x40(%rbp), %xmm1
jmp 0x40d8
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3773
movl %ebx, %r14d
jmp 0x3f80
callq 0x379d
jmp 0x3fa9
callq 0x37cc
jmp 0x4079