// File: bobverity[P]MavericK/misc.cpp
// Function: calculateAutoCorr(std::vector<double, std::allocator<double>>&)
double calculateAutoCorr(vector<double> &v) {
    
    // get basic properties of vector
    int v_size = int(v.size());
    double mu = mean(v);
    double sigma2 = var(v);
    
    // calculate x range over which autocorrelation will be calculated
    vector<int> x;
    int x_largest = 0;
    int dx = 1;
    bool break_on = false;
    for (int l=0; l<100; l++) {
        for (int j=0; j<100; j++) {
            x_largest += dx;
            if (x_largest>v_size) {
                break_on = true;
                break;
            }
            x.push_back(x_largest);
        }
        if (break_on) {
            break;
        }
        dx *= 2;
    }
    
    // calculate raw autocorrelation
    int x_size = int(x.size());
    vector<double> yy(x_size);
    vector<double> n(x_size);
    for (int i=0; i<v_size; i++) {
        for (int j=0; j<x_size; j++) {
            if ((i+x[j])>(v_size-1)) {
                continue;
            }
            yy[j] += (v[i]-mu)*(v[i+x[j]]-mu)/sigma2;
            n[j]++;
        }
    }
    for (int j=0; j<x_size; j++) {
        yy[j] /= double(n[j]);
    }
    
    // calculate total autocorrelation
    double autoCorr = yy[0];
    for (int i=1; i<x_size; i++) {
        dx = x[i]-x[i-1];
        if (yy[i]>0) {
            autoCorr += dx*yy[i-1] - 0.5*(dx+1)*(yy[i-1]-yy[i]);
        } else {
            dx = int(floor((dx*yy[i-1])/(yy[i-1]-yy[i])));
            autoCorr += dx*yy[i-1] - 0.5*(dx+1)*(yy[i-1]-yy[i]);
            break;
        }
    }
    autoCorr = 1 + 2*autoCorr;
    
    // correct for possible issues
    if (sigma2==0 || autoCorr<1)
        autoCorr = 1;
    
    return(autoCorr);
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x170(%rsp)
movq 0x170(%rsp), %rdi
callq 0x9b80
movl %eax, 0x16c(%rsp)
movq 0x170(%rsp), %rdi
callq 0x33380
movsd %xmm0, 0x160(%rsp)
movq 0x170(%rsp), %rdi
xorps %xmm0, %xmm0
movl $0x1, %esi
callq 0x333e0
movsd %xmm0, 0x158(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x16030
movl $0x0, 0x13c(%rsp)
movl $0x1, 0x138(%rsp)
movb $0x0, 0x137(%rsp)
movl $0x0, 0x130(%rsp)
cmpl $0x64, 0x130(%rsp)
jge 0x360d6
movl $0x0, 0x12c(%rsp)
cmpl $0x64, 0x12c(%rsp)
jge 0x360a4
movl 0x138(%rsp), %eax
addl 0x13c(%rsp), %eax
movl %eax, 0x13c(%rsp)
movl 0x13c(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jle 0x3605f
movb $0x1, 0x137(%rsp)
jmp 0x360a4
leaq 0x140(%rsp), %rdi
leaq 0x13c(%rsp), %rsi
callq 0xc740
jmp 0x36076
jmp 0x36078
movl 0x12c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x12c(%rsp)
jmp 0x36026
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
jmp 0x367a9
testb $0x1, 0x137(%rsp)
je 0x360b0
jmp 0x360d6
movl 0x138(%rsp), %eax
shll %eax
movl %eax, 0x138(%rsp)
movl 0x130(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x130(%rsp)
jmp 0x3600d
leaq 0x140(%rsp), %rdi
callq 0xb030
movl %eax, 0x118(%rsp)
movslq 0x118(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0xff(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
callq 0x8780
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
leaq 0x100(%rsp), %rdi
callq 0x8790
jmp 0x3612e
leaq 0xff(%rsp), %rdi
callq 0x8910
movslq 0x118(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0xdf(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0x8780
movq 0xa0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
leaq 0xe0(%rsp), %rdi
callq 0x8790
jmp 0x3617f
leaq 0xdf(%rsp), %rdi
callq 0x8910
movl $0x0, 0xd8(%rsp)
movl 0xd8(%rsp), %eax
cmpl 0x16c(%rsp), %eax
jge 0x3637e
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
cmpl 0x118(%rsp), %eax
jge 0x36366
movl 0xd8(%rsp), %eax
movl %eax, 0x9c(%rsp)
movslq 0xd4(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x8ba0
movq %rax, %rcx
movl 0x9c(%rsp), %eax
addl (%rcx), %eax
movl 0x16c(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x36265
jmp 0x36350
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
leaq 0xff(%rsp), %rdi
callq 0x8910
jmp 0x367a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
leaq 0xdf(%rsp), %rdi
callq 0x8910
leaq 0x100(%rsp), %rdi
callq 0x88b0
jmp 0x367a9
movq 0x170(%rsp), %rdi
movslq 0xd8(%rsp), %rsi
callq 0x8c20
movsd (%rax), %xmm0
subsd 0x160(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movq 0x170(%rsp), %rax
movq %rax, 0x78(%rsp)
movl 0xd8(%rsp), %eax
movl %eax, 0x84(%rsp)
movslq 0xd4(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x8ba0
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movl 0x84(%rsp), %eax
addl (%rcx), %eax
movslq %eax, %rsi
callq 0x8c20
movsd 0x88(%rsp), %xmm0
movsd (%rax), %xmm1
subsd 0x160(%rsp), %xmm1
mulsd %xmm1, %xmm0
divsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movslq 0xd4(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd 0x90(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movslq 0xd4(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x8c20
movsd 0x25eb8(%rip), %xmm0    # 0x5c200
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x361b6
jmp 0x36368
movl 0xd8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd8(%rsp)
jmp 0x36197
movl $0x0, 0xd0(%rsp)
movl 0xd0(%rsp), %eax
cmpl 0x118(%rsp), %eax
jge 0x363f2
movslq 0xd0(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movslq 0xd0(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd 0x70(%rsp), %xmm1
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl 0xd0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd0(%rsp)
jmp 0x36389
leaq 0x100(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0xc8(%rsp)
movl $0x1, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
cmpl 0x118(%rsp), %eax
jge 0x36709
movslq 0xc4(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x8ba0
movl (%rax), %eax
movl %eax, 0x6c(%rsp)
movl 0xc4(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x140(%rsp), %rdi
callq 0x8ba0
movq %rax, %rcx
movl 0x6c(%rsp), %eax
subl (%rcx), %eax
movl %eax, 0x138(%rsp)
movslq 0xc4(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3657c
cvtsi2sdl 0x138(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movl 0xc4(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movl 0x138(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x25d55(%rip), %xmm0    # 0x5c238
mulsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0xc4(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movslq 0xc4(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd 0x48(%rsp), %xmm3
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm2
subsd (%rax), %xmm3
mulsd %xmm3, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
jmp 0x366f1
cvtsi2sdl 0x138(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0xc4(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd 0x10(%rsp), %xmm0
mulsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0xc4(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movslq 0xc4(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
subsd (%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x46b0
cvttsd2si %xmm0, %eax
movl %eax, 0x138(%rsp)
cvtsi2sdl 0x138(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0xc4(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x138(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x25bdd(%rip), %xmm0    # 0x5c238
mulsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0xc4(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movslq 0xc4(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x8c20
movsd 0x28(%rsp), %xmm3
movsd 0x30(%rsp), %xmm1
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm2
subsd (%rax), %xmm3
mulsd %xmm3, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
jmp 0x36709
jmp 0x366f3
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x3641b
movsd 0xc8(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd 0x25ae2(%rip), %xmm1    # 0x5c200
addsd %xmm1, %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0x158(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x36741
jp 0x36741
jmp 0x36754
movsd 0x25ab7(%rip), %xmm0    # 0x5c200
ucomisd 0xc8(%rsp), %xmm0
jbe 0x36765
movsd 0x25aa4(%rip), %xmm0    # 0x5c200
movsd %xmm0, 0xc8(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x88b0
leaq 0x100(%rsp), %rdi
callq 0x88b0
leaq 0x140(%rsp), %rdi
callq 0xc6e0
movsd 0x8(%rsp), %xmm0
addq $0x178, %rsp            # imm = 0x178
retq
leaq 0x140(%rsp), %rdi
callq 0xc6e0
movq 0x120(%rsp), %rdi
callq 0x4730
nopw %cs:(%rax,%rax)