// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movl %ecx, %ebx
movl %edx, %r12d
movl %esi, %r15d
movq %rdi, %r14
pushq $0x2
popq %rcx
movl %esi, %eax
cltd
idivl %ecx
cmpl %r12d, %eax
movsd %xmm0, -0x38(%rbp)
jl 0x369e
movl %r12d, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movl %ebx, -0x30(%rbp)
testb %bl, %bl
jne 0x36cc
movslq %r12d, %r12
movslq %r15d, %rax
movq %rax, -0x40(%rbp)
leaq 0x8(%r14), %rbx
pushq $0x1
popq %r15
cmpq %r12, %r15
jg 0x3580
movq %rsp, %r13
movq -0x40(%rbp), %rdx
subq %r15, %rdx
movl %edx, %eax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, %rsp
movq %rsp, %rsi
subq %rax, %rsi
movq %rsi, %rsp
xorl %eax, %eax
cmpq %rdx, %rax
jge 0x353b
movsd (%r14,%rax,8), %xmm0
movsd %xmm0, (%rdi,%rax,8)
movsd (%rbx,%rax,8), %xmm0
movsd %xmm0, (%rsi,%rax,8)
incq %rax
jmp 0x351c
callq 0x3718
mulsd %xmm0, %xmm0
movq -0x48(%rbp), %rax
movsd %xmm0, -0x8(%rax,%r15,8)
cmpb $0x0, -0x30(%rbp)
jne 0x3566
movq %r13, %rsp
incq %r15
addq $0x8, %rbx
movsd -0x38(%rbp), %xmm0
jmp 0x34e7
movq 0x9a5b(%rip), %rax      # 0xcfc8
movq (%rax), %rdi
leaq 0x4b94(%rip), %rsi      # 0x810b
movb $0x1, %al
callq 0x3290
jmp 0x3555
cmpb $0x0, -0x30(%rbp)
jne 0x36f2
movq -0x48(%rbp), %rax
movsd (%rax), %xmm3
pushq $0x1
popq %rbx
pushq $0x1
popq %r14
movapd 0x4a6f(%rip), %xmm2     # 0x8010
movsd 0x4a87(%rip), %xmm4     # 0x8030
pushq $0x78
popq %r15
movq %r14, %r13
cmpq %r12, %r13
jge 0x368d
movq -0x48(%rbp), %rax
movsd (%rax,%r13,8), %xmm5
movapd %xmm5, %xmm1
subsd %xmm3, %xmm1
andpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
ja 0x35fc
movapd %xmm3, %xmm1
addsd %xmm4, %xmm1
andpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
ja 0x3643
incq %r13
ucomisd %xmm3, %xmm5
maxsd %xmm3, %xmm5
cmoval %r13d, %ebx
movapd %xmm5, %xmm3
jmp 0x35b0
movq 0x99c5(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4b31(%rip), %rdi      # 0x813e
pushq $0x74
popq %rsi
movq %r14, %rdx
movsd %xmm3, -0x30(%rbp)
movsd %xmm5, -0x40(%rbp)
callq 0x3360
movsd -0x40(%rbp), %xmm5
movsd 0x4a01(%rip), %xmm4     # 0x8030
movsd -0x30(%rbp), %xmm3
movapd 0x49d4(%rip), %xmm2     # 0x8010
movsd -0x38(%rbp), %xmm0
jmp 0x35d5
movq 0x997e(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4b5f(%rip), %rdi      # 0x81b3
movq %r15, %rsi
movq %r14, %rdx
movsd %xmm3, -0x30(%rbp)
movsd %xmm5, -0x40(%rbp)
callq 0x3360
movsd -0x40(%rbp), %xmm5
movsd 0x49ba(%rip), %xmm4     # 0x8030
movsd -0x30(%rbp), %xmm3
movapd 0x498d(%rip), %xmm2     # 0x8010
movsd -0x38(%rbp), %xmm0
jmp 0x35e7
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r13d
movq 0x9920(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4986(%rip), %rdi      # 0x8038
pushq $0x1
popq %rdx
movl $0xa4, %esi
callq 0x3360
movsd -0x38(%rbp), %xmm0
movl %r13d, %r12d
jmp 0x34ae
movq 0x98f5(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4a00(%rip), %rdi      # 0x80dd
pushq $0x2d
popq %rsi
pushq $0x1
popq %rdx
callq 0x3360
movsd -0x38(%rbp), %xmm0
jmp 0x34d5
movq 0x98cf(%rip), %rax      # 0xcfc8
movq (%rax), %rcx
leaq 0x4a0f(%rip), %rdi      # 0x8112
pushq $0x2b
popq %rsi
pushq $0x1
popq %rdx
callq 0x3360
movsd -0x38(%rbp), %xmm0
jmp 0x358a