// File: /workspace/github_cpp_makefile_star2_O2/bolongz[P]Systematic-Alias-Sampling/test.cpp
// Function: cramer_von_mises_iid(int, int)
double cramer_von_mises_iid(int bincount, int samplecount){
    
    std::vector<double> pmf;
    std::vector<double> values;
    
    double h = (20.0) / (bincount - 1); //[take value in [-4, 4]
    for(int i = 0; i < bincount; i++){
        double t = -10 + i * h;
        values.push_back(t);
        pmf.push_back(_normal_distribution(0.0, 1.0, t) + 0.02);
    }
    
    SystematicAliasSampling _sample(pmf, values);
    _sample.aliastable();

    std::vector<double> samples(samplecount); // store the results
    std::vector<double> cdf(bincount);
    _sample.cummulative_distribution(cdf);
    
    std::uniform_real_distribution<> __random(0.0, 1.00);
    
    /* binary search */
    for(int i = 0; i < samplecount; i++){
        double x  = __random(gen);
        size_t index = std::upper_bound(cdf.begin(), cdf.end(), x) - cdf.begin();
        samples[i] = values[index];
    }
    
    
    std::vector<double> edf(bincount);
   
    _sample.empirical_distribution(samples, edf);
   
    double cvm = _sample.cramer_von_mises(cdf, edf);
    
    return cvm;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rsi, 0x68(%rsp)
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %r15
movaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
leaq 0x70(%rsp), %r12
andq $0x0, 0x10(%r12)
movaps %xmm0, (%r12)
leal -0x1(%rdi), %eax
cvtsi2sd %eax, %xmm0
movsd 0x3bab(%rip), %xmm1     # 0x6020
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
xorl %r14d, %r14d
testl %edi, %edi
movl $0x0, %ebx
movq %rdi, 0x60(%rsp)
cmovgl %edi, %ebx
leaq 0xc0(%rsp), %r13
leaq 0x48(%rsp), %rbp
cmpl %r14d, %ebx
je 0x2502
cvtsi2sd %r14d, %xmm0
mulsd 0x10(%rsp), %xmm0
addsd 0x3b72(%rip), %xmm0     # 0x6028
movsd %xmm0, 0xc0(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3128
movsd 0xc0(%rsp), %xmm2
xorpd %xmm0, %xmm0
movsd 0x3b31(%rip), %xmm1     # 0x6010
callq 0x239c
addsd 0x3b44(%rip), %xmm0     # 0x6030
movsd %xmm0, 0x48(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4c52
incl %r14d
jmp 0x249e
leaq 0xc0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x314a
leaq 0xc0(%rsp), %rdi
callq 0x3204
movq 0x68(%rsp), %rbp
movslq %ebp, %rsi
leaq 0x48(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x3568
movslq 0x60(%rsp), %r14
leaq 0x18(%rsp), %rdi
leaq 0x30(%rsp), %rdx
movq %r14, %rsi
callq 0x3568
leaq 0xc0(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x35ba
xorpd %xmm0, %xmm0
movhpd 0x3a9b(%rip), %xmm0     # xmm0 = xmm0[0],mem[0]
leaq 0xb0(%rsp), %r15
movapd %xmm0, (%r15)
xorl %ebx, %ebx
testl %ebp, %ebp
cmovlel %ebx, %ebp
leaq 0x7f30(%rip), %r12      # 0xa4c0
leaq 0x30(%rsp), %r13
cmpq %rbx, %rbp
je 0x25db
movq %r15, %rdi
movq %r12, %rsi
callq 0x35f0
movsd %xmm0, 0x30(%rsp)
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x4d39
subq 0x18(%rsp), %rax
movq 0x70(%rsp), %rcx
movsd (%rcx,%rax), %xmm0
movq 0x48(%rsp), %rax
movsd %xmm0, (%rax,%rbx,8)
incq %rbx
jmp 0x2595
leaq 0x30(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq %r14, %rsi
callq 0x3568
leaq 0xc0(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x35f8
leaq 0xc0(%rsp), %rbx
leaq 0x18(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3746
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
callq 0x4798
movq %r14, %rdi
callq 0x4798
leaq 0x48(%rsp), %rdi
callq 0x4798
movq %rbx, %rdi
callq 0x37aa
leaq 0x70(%rsp), %rdi
callq 0x4798
leaq 0x90(%rsp), %rdi
callq 0x4798
movsd 0x10(%rsp), %xmm0
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4798
jmp 0x269f
jmp 0x269c
movq %rax, %rbx
jmp 0x26a9
jmp 0x2693
movq %rax, %rbx
jmp 0x26b3
jmp 0x26c4
jmp 0x269c
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4798
leaq 0x48(%rsp), %rdi
callq 0x4798
leaq 0xc0(%rsp), %rdi
callq 0x37aa
jmp 0x26c7
jmp 0x26c4
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x4798
leaq 0x90(%rsp), %rdi
callq 0x4798
movq %rbx, %rdi
callq 0x21e0