// File: /workspace/github_cpp_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movl %esi, 0x1c(%rsp)
movl %edi, %r14d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
xorl %ebx, %ebx
movq %rbx, 0x50(%rsp)
movq %rbx, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
testl %edi, %edi
jle 0x2640
leal -0x1(%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x3aa6(%rip), %xmm1     # 0x6020
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
leaq 0x20(%rsp), %r15
leaq 0xa0(%rsp), %r12
leaq 0x40(%rsp), %r13
leaq 0x70(%rsp), %rbp
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd 0x10(%rsp), %xmm0
addsd 0x3a78(%rip), %xmm0     # 0x6028
movsd %xmm0, 0xa0(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x25d4
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x25df
movq %r15, %rdi
movq %r12, %rdx
callq 0x5238
movsd 0xa0(%rsp), %xmm0
mulsd %xmm0, %xmm0
mulsd 0x3a24(%rip), %xmm0     # 0x6018
callq 0x2240
mulsd 0x3a2f(%rip), %xmm0     # 0x6030
addsd 0x3a2f(%rip), %xmm0     # 0x6038
movsd %xmm0, 0x70(%rsp)
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x262a
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x2635
movq %r13, %rdi
movq %rbp, %rdx
callq 0x5310
incl %ebx
cmpl %ebx, %r14d
jne 0x259b
leaq 0xa0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x3770
leaq 0xa0(%rsp), %rdi
callq 0x3824
movl 0x1c(%rsp), %ebx
movslq %ebx, %rsi
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x3d22
movslq %r14d, %r14
leaq 0x58(%rsp), %rdi
leaq 0x88(%rsp), %rdx
movq %r14, %rsi
callq 0x3d22
movq 0x118(%rsp), %rax
testq %rax, %rax
je 0x26d5
movq 0x58(%rsp), %rcx
movq 0xa0(%rsp), %rdx
xorl %esi, %esi
testq %rsi, %rsi
je 0x26c5
movsd -0x8(%rcx,%rsi,8), %xmm0
addsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rcx,%rsi,8)
jmp 0x26cd
movsd (%rdx), %xmm0
movsd %xmm0, (%rcx)
incq %rsi
cmpq %rsi, %rax
jne 0x26ae
testl %ebx, %ebx
jle 0x2751
movl %ebx, %r15d
xorl %r12d, %r12d
leaq 0x7e0a(%rip), %rbx      # 0xa4f0
movq %rbx, %rdi
callq 0x4eae
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq %rax, %rcx
testq %rdx, %rdx
jle 0x2731
movq %rax, %rcx
movq %rdx, %rsi
shrq %rsi
movsd (%rcx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x2729
leaq (%rcx,%rsi,8), %rcx
addq $0x8, %rcx
notq %rsi
addq %rdx, %rsi
movq %rsi, %rdx
testq %rsi, %rsi
jg 0x270d
subq %rax, %rcx
movq 0x20(%rsp), %rax
movsd (%rax,%rcx), %xmm0
movq 0x70(%rsp), %rax
movsd %xmm0, (%rax,%r12,8)
incq %r12
cmpq %r15, %r12
jne 0x26e6
leaq 0x88(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq %r14, %rsi
callq 0x3d22
leaq 0xa0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x88(%rsp), %rdx
callq 0x3db0
movq 0x118(%rsp), %rax
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x27be
movq 0x88(%rsp), %rcx
movq 0x58(%rsp), %rdx
xorl %esi, %esi
movsd (%rcx,%rsi,8), %xmm2
subsd (%rdx,%rsi,8), %xmm2
mulsd %xmm2, %xmm2
addsd %xmm2, %xmm0
incq %rsi
cmpq %rsi, %rax
jne 0x27a4
movq %rax, %xmm2
punpckldq 0x38b5(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x38bd(%rip), %xmm2     # 0x6090
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
divsd %xmm3, %xmm0
ucomisd %xmm1, %xmm0
jb 0x27ef
sqrtsd %xmm0, %xmm0
jmp 0x27f4
callq 0x20f0
movsd %xmm0, 0x10(%rsp)
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x280c
callq 0x2140
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x281b
callq 0x2140
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x282a
callq 0x2140
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x283c
callq 0x2140
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x284e
callq 0x2140
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x2860
callq 0x2140
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x2872
callq 0x2140
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x2884
callq 0x2140
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2893
callq 0x2140
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x28a2
callq 0x2140
movsd 0x10(%rsp), %xmm0
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x28eb
callq 0x2140
jmp 0x28eb
jmp 0x28e8
movq %rax, %rbx
jmp 0x28fa
jmp 0x28da
movq %rax, %rbx
jmp 0x2909
jmp 0x28e3
jmp 0x28e3
movq %rax, %rbx
jmp 0x2916
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x28fa
callq 0x2140
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x2909
callq 0x2140
leaq 0xa0(%rsp), %rdi
callq 0x3f58
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2925
callq 0x2140
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2934
callq 0x2140
movq %rbx, %rdi
callq 0x2230