// File: /workspace/github_cpp_makefile_star2_O1/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 0x2650
leal -0x1(%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x3a96(%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 0x3a68(%rip), %xmm0     # 0x6028
movsd %xmm0, 0xa0(%rsp)
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x25e4
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x25ef
movq %r15, %rdi
movq %r12, %rdx
callq 0x520a
movsd 0xa0(%rsp), %xmm0
mulsd %xmm0, %xmm0
mulsd 0x3a14(%rip), %xmm0     # 0x6018
callq 0x2240
mulsd 0x3a1f(%rip), %xmm0     # 0x6030
addsd 0x3a1f(%rip), %xmm0     # 0x6038
movsd %xmm0, 0x70(%rsp)
movq 0x48(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
je 0x263a
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x48(%rsp)
jmp 0x2645
movq %r13, %rdi
movq %rbp, %rdx
callq 0x52e2
incl %ebx
cmpl %ebx, %r14d
jne 0x25ab
leaq 0xa0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x376c
leaq 0xa0(%rsp), %rdi
callq 0x3820
movl 0x1c(%rsp), %ebx
movslq %ebx, %rsi
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x3d24
movslq %r14d, %r14
leaq 0x58(%rsp), %rdi
leaq 0x88(%rsp), %rdx
movq %r14, %rsi
callq 0x3d24
movq 0x118(%rsp), %rax
testq %rax, %rax
je 0x26e5
movq 0x58(%rsp), %rcx
movq 0xa0(%rsp), %rdx
xorl %esi, %esi
testq %rsi, %rsi
je 0x26d5
movsd -0x8(%rcx,%rsi,8), %xmm0
addsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rcx,%rsi,8)
jmp 0x26dd
movsd (%rdx), %xmm0
movsd %xmm0, (%rcx)
incq %rsi
cmpq %rsi, %rax
jne 0x26be
testl %ebx, %ebx
jle 0x2761
movl %ebx, %r15d
xorl %r12d, %r12d
leaq 0x7dea(%rip), %rbx      # 0xa4e0
movq %rbx, %rdi
callq 0x4ea6
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq %rax, %rcx
testq %rdx, %rdx
jle 0x2741
movq %rax, %rcx
movq %rdx, %rsi
shrq %rsi
movsd (%rcx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x2739
leaq (%rcx,%rsi,8), %rcx
addq $0x8, %rcx
notq %rsi
addq %rdx, %rsi
movq %rsi, %rdx
testq %rsi, %rsi
jg 0x271d
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 0x26f6
leaq 0x88(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq %r14, %rsi
callq 0x3d24
leaq 0xa0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x88(%rsp), %rdx
callq 0x3dae
movq 0x118(%rsp), %rax
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0x27ce
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 0x27b4
movq %rax, %xmm2
punpckldq 0x38a5(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x38ad(%rip), %xmm2     # 0x6090
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
divsd %xmm3, %xmm0
ucomisd %xmm1, %xmm0
jb 0x27ff
sqrtsd %xmm0, %xmm0
jmp 0x2804
callq 0x20f0
movsd %xmm0, 0x10(%rsp)
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x281c
callq 0x2140
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x282b
callq 0x2140
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x283a
callq 0x2140
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x284c
callq 0x2140
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x285e
callq 0x2140
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x2870
callq 0x2140
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x2882
callq 0x2140
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x2894
callq 0x2140
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x28a3
callq 0x2140
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x28b2
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 0x28fb
callq 0x2140
jmp 0x28fb
jmp 0x28f8
movq %rax, %rbx
jmp 0x290a
jmp 0x28ea
movq %rax, %rbx
jmp 0x2919
jmp 0x28f3
jmp 0x28f3
movq %rax, %rbx
jmp 0x2926
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x290a
callq 0x2140
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x2919
callq 0x2140
leaq 0xa0(%rsp), %rdi
callq 0x3f50
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x2935
callq 0x2140
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x2944
callq 0x2140
movq %rbx, %rdi
callq 0x2230