// File: 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 %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0xf0(%rbp)
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
callq 0x10e50
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq %rax, -0xe8(%rbp)
movq $0x0, -0x50(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x124c0
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rcx
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x16150
jmp 0x84208
leaq -0x51(%rbp), %rdi
callq 0x16570
movl -0x18(%rbp), %eax
movq %rax, -0x100(%rbp)
movq $0x0, -0x88(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x124c0
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rcx
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rdx
callq 0x16150
jmp 0x84259
leaq -0x89(%rbp), %rdi
callq 0x16570
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x843a5
movq -0xf0(%rbp), %rdi
movl -0x90(%rbp), %eax
incl %eax
movl %eax, -0x94(%rbp)
cvtsi2sdl -0x94(%rbp), %xmm0
movl -0x18(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
cvtsi2sdl -0x90(%rbp), %xmm0
movl -0x18(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x118(%rbp)
movslq -0x90(%rbp), %rsi
callq 0x107e0
movsd -0x118(%rbp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
movslq -0x90(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x107e0
movsd -0x110(%rbp), %xmm0
movq -0xf0(%rbp), %rdi
movsd %xmm0, (%rax)
movslq -0x90(%rbp), %rsi
callq 0x107e0
movsd (%rax), %xmm0
subsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x108(%rbp)
movslq -0x90(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x107e0
movsd -0x108(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x8426f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x51(%rbp), %rdi
callq 0x16570
jmp 0x8450f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x89(%rbp), %rdi
callq 0x16570
jmp 0x84506
leaq -0x48(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x16260
movq -0x128(%rbp), %rdi
movq %rax, -0xb8(%rbp)
callq 0x16290
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x84a00
movq %rax, -0x120(%rbp)
jmp 0x843eb
movq -0x120(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x18a90
movsd (%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
leaq -0x80(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x16260
movq -0x138(%rbp), %rdi
movq %rax, -0xd0(%rbp)
callq 0x16290
movq %rax, -0xd8(%rbp)
movq -0xd0(%rbp), %rdi
movq -0xd8(%rbp), %rsi
callq 0x84a00
movq %rax, -0x130(%rbp)
jmp 0x84454
movq -0x130(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x18a90
movsd (%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x28(%rbp), %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0x844a6
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0x844b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x10930
jmp 0x84506
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movl -0x18(%rbp), %edi
movsd -0x20(%rbp), %xmm0
callq 0x853a0
movsd %xmm0, -0x140(%rbp)
jmp 0x844c7
movsd -0x140(%rbp), %xmm1
movsd 0x3fd1(%rip), %xmm0     # 0x884a8
subsd %xmm1, %xmm0
movsd %xmm0, -0x148(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x10930
leaq -0x48(%rbp), %rdi
callq 0x10930
movsd -0x148(%rbp), %xmm0
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x10930
movq -0x60(%rbp), %rdi
callq 0x8a50
nopl (%rax,%rax)