// File: 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
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x3b90
leaq -0x38(%rbp), %rdi
callq 0x3b90
movl -0x4(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
movsd 0x8ae3(%rip), %xmm0     # 0xb028
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x25e2
cvtsi2sdl -0x44(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x8ab9(%rip), %xmm1     # 0xb030
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x3bb0
jmp 0x258f
movsd -0x50(%rbp), %xmm2
xorps %xmm0, %xmm0
movsd 0x8a69(%rip), %xmm1     # 0xb008
callq 0x2430
movsd 0x8a8c(%rip), %xmm1     # 0xb038
addsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x3c20
jmp 0x25c4
jmp 0x25c6
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2555
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x298b
leaq -0x100(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x3c50
jmp 0x25f8
leaq -0x100(%rbp), %rdi
callq 0x3d80
jmp 0x2606
movslq -0x8(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq -0x119(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x43e0
movq -0x1c0(%rbp), %rsi
movq -0x1b8(%rbp), %rdx
leaq -0x118(%rbp), %rdi
callq 0x4400
jmp 0x2640
leaq -0x119(%rbp), %rdi
callq 0x4470
movslq -0x4(%rbp), %rax
movq %rax, -0x1d0(%rbp)
leaq -0x139(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x43e0
movq -0x1d0(%rbp), %rsi
movq -0x1c8(%rbp), %rdx
leaq -0x138(%rbp), %rdi
callq 0x4400
jmp 0x2686
leaq -0x139(%rbp), %rdi
callq 0x4470
leaq -0x100(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x4490
jmp 0x26a7
leaq -0x150(%rbp), %rdi
xorps %xmm0, %xmm0
movsd 0x894f(%rip), %xmm1     # 0xb008
callq 0x3ac0
jmp 0x26c0
movl $0x0, -0x154(%rbp)
movl -0x154(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x283c
leaq 0xedf0(%rip), %rsi      # 0x114d0
leaq -0x150(%rbp), %rdi
callq 0x4560
movsd %xmm0, -0x1d8(%rbp)
jmp 0x26f6
movsd -0x1d8(%rbp), %xmm0
movsd %xmm0, -0x160(%rbp)
leaq -0x138(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x4620
movq -0x1e8(%rbp), %rdi
movq %rax, -0x178(%rbp)
callq 0x4650
movq %rax, -0x180(%rbp)
movq -0x178(%rbp), %rdi
movq -0x180(%rbp), %rsi
leaq -0x160(%rbp), %rdx
callq 0x45d0
movq %rax, -0x1e0(%rbp)
jmp 0x2756
movq -0x1e0(%rbp), %rax
movq %rax, -0x170(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x4620
movq %rax, -0x188(%rbp)
leaq -0x170(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x4590
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x3b70
movsd (%rax), %xmm0
movsd %xmm0, -0x1f0(%rbp)
movslq -0x154(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0x3b70
movsd -0x1f0(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x154(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x154(%rbp)
jmp 0x26ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x297f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x119(%rbp), %rdi
callq 0x4470
jmp 0x297f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x139(%rbp), %rdi
callq 0x4470
jmp 0x2973
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x2967
movslq -0x4(%rbp), %rax
movq %rax, -0x200(%rbp)
leaq -0x1a1(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x43e0
movq -0x200(%rbp), %rsi
movq -0x1f8(%rbp), %rdx
leaq -0x1a0(%rbp), %rdi
callq 0x4400
jmp 0x2876
leaq -0x1a1(%rbp), %rdi
callq 0x4470
leaq -0x100(%rbp), %rdi
leaq -0x118(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
callq 0x4680
jmp 0x289e
leaq -0x100(%rbp), %rdi
leaq -0x138(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
callq 0x49c0
movsd %xmm0, -0x208(%rbp)
jmp 0x28c2
movsd -0x208(%rbp), %xmm0
movsd %xmm0, -0x1b0(%rbp)
movsd -0x1b0(%rbp), %xmm0
movsd %xmm0, -0x210(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x4ad0
leaq -0x138(%rbp), %rdi
callq 0x4ad0
leaq -0x118(%rbp), %rdi
callq 0x4ad0
leaq -0x100(%rbp), %rdi
callq 0x4b30
leaq -0x38(%rbp), %rdi
callq 0x4ad0
leaq -0x20(%rbp), %rdi
callq 0x4ad0
movsd -0x210(%rbp), %xmm0
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x1a1(%rbp), %rdi
callq 0x4470
jmp 0x2967
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x4ad0
leaq -0x138(%rbp), %rdi
callq 0x4ad0
leaq -0x118(%rbp), %rdi
callq 0x4ad0
leaq -0x100(%rbp), %rdi
callq 0x4b30
leaq -0x38(%rbp), %rdi
callq 0x4ad0
leaq -0x20(%rbp), %rdi
callq 0x4ad0
movq -0x58(%rbp), %rdi
callq 0x2200
nopw %cs:(%rax,%rax)