// File: /workspace/github_cpp_makefile_star2_O3/millanek[P]Dsuite/kstest.cpp
// Function: ks_test_of_uniformity(std::vector<double, std::allocator<double>>, std::ostream&, bool)
double ks_test_of_uniformity(std::vector<double> sampleVect0to1, std::ostream& outfile, bool printDebug) {

    unsigned int N = sampleVect0to1.size();
    
    double d, Dplusmax, Dminusmax;
    std::vector<double> DplusVals(N,0.0);
    std::vector<double> DminusVals(N,0.0);
    
    for (int i = 0; i < N; i++) {
        int j = i+1;
        double ratio = (double)j/N;
        double ratiominus = (double)i/N;
        DplusVals[i] = ratio - sampleVect0to1[i];
        DminusVals[i] = sampleVect0to1[i] - ratiominus;
    }
    
    Dplusmax = *max_element(DplusVals.begin(), DplusVals.end());
    Dminusmax = *max_element(DminusVals.begin(), DminusVals.end());
    
    
  //  std::cerr << "Dplusmax: " << Dplusmax << std::endl;
  //  std::cerr << "Dminusmax: " << Dminusmax << std::endl;
    
  //  print_vector(DplusVals, std::cerr, ',');
  //  print_vector(DminusVals, std::cerr, ',');
    
    
    if (Dplusmax > Dminusmax) {
        d = Dplusmax;
    } else {
        d = Dminusmax;
    }
    
   // std::cerr << "d: " << d << std::endl;
    
    // Return p-value
    return 1 - KScdf(N,d);

}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x50, %rsp
movq %rdi, %r14
movq 0x8(%rdi), %rbx
subq (%rdi), %rbx
sarq $0x3, %rbx
movl %ebx, %r15d
xorl %r13d, %r13d
leaq 0x10(%rsp), %rdx
movq %r13, (%rdx)
leaq 0x28(%rsp), %rdi
leaq 0x48(%rsp), %r12
movq %r15, %rsi
movq %r12, %rcx
callq 0xed0a
movq %r13, (%r12)
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
leaq 0xf(%rsp), %rcx
movq %r15, %rsi
callq 0xed0a
testl %ebx, %ebx
je 0x3e4a9
cvtsi2sd %r15, %xmm0
movq (%r14), %rax
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rdx
movl %ebx, %esi
xorl %edi, %edi
leaq 0x1(%rdi), %r8
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
divsd %xmm0, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %edi, %xmm2
divsd %xmm0, %xmm2
subsd (%rax,%rdi,8), %xmm1
movsd %xmm1, (%rcx,%rdi,8)
movsd (%rax,%rdi,8), %xmm1
subsd %xmm2, %xmm1
movsd %xmm1, (%rdx,%rdi,8)
movq %r8, %rdi
cmpq %r8, %rsi
jne 0x3e46e
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
cmpq %rcx, %rax
sete %sil
movq %rax, %rdx
addq $0x8, %rdx
cmpq %rcx, %rdx
sete %dil
orb %sil, %dil
jne 0x3e4ee
movsd (%rax), %xmm0
movsd (%rdx), %xmm1
ucomisd %xmm0, %xmm1
cmovaq %rdx, %rax
maxsd %xmm0, %xmm1
addq $0x8, %rdx
movapd %xmm1, %xmm0
cmpq %rcx, %rdx
jne 0x3e4d1
movsd (%rax), %xmm0
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
sete %sil
movq %rax, %rdx
addq $0x8, %rdx
cmpq %rcx, %rdx
sete %dil
orb %sil, %dil
jne 0x3e537
movsd (%rax), %xmm1
movsd (%rdx), %xmm2
ucomisd %xmm1, %xmm2
cmovaq %rdx, %rax
maxsd %xmm1, %xmm2
addq $0x8, %rdx
movapd %xmm2, %xmm1
cmpq %rcx, %rdx
jne 0x3e51a
maxsd (%rax), %xmm0
movl %ebx, %edi
callq 0x3e8f0
movsd %xmm0, 0x40(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3e55f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x3e576
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movsd 0x2ef2(%rip), %xmm0     # 0x41470
subsd 0x40(%rsp), %xmm0
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3e5b1
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
jmp 0x3e5b1
movq %rax, %rbx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x3e5c8
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x7420
movq %rbx, %rdi
callq 0x7740