// File: /workspace/github_cpp_makefile_star2_O2/bolongz[P]Systematic-Alias-Sampling/test.cpp
// Function: cramer_von_mises_sas(int, int)
double cramer_von_mises_sas(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);
    
    std::vector<double> samples(samplecount); // store the results
    _sample.aliastable(); //generate table
    
    std::vector<double> cdf(bincount);
    _sample.cummulative_distribution(cdf);
    

    //_sample.systematicaliassampling(samplecount, samples);
    _sample.simple_sas(samplecount, samples);
    //_sample.goldratioaliassampling(samplecount, samples);

    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 $0x148, %rsp            # imm = 0x148
movl %esi, 0x1c(%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 0x38f2(%rip), %xmm1     # 0x6020
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
xorl %ebx, %ebx
testl %edi, %edi
movl $0x0, %r14d
movq %rdi, 0x38(%rsp)
cmovgl %edi, %r14d
leaq 0xb0(%rsp), %r13
leaq 0x20(%rsp), %rbp
cmpl %ebx, %r14d
je 0x27ba
cvtsi2sd %ebx, %xmm0
mulsd 0x10(%rsp), %xmm0
addsd 0x38b9(%rip), %xmm0     # 0x6028
movsd %xmm0, 0xb0(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3128
movsd 0xb0(%rsp), %xmm2
xorpd %xmm0, %xmm0
movsd 0x3878(%rip), %xmm1     # 0x6010
callq 0x239c
addsd 0x388b(%rip), %xmm0     # 0x6030
movsd %xmm0, 0x20(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4c52
incl %ebx
jmp 0x2758
leaq 0xb0(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x314a
movl 0x1c(%rsp), %ebx
movslq %ebx, %rsi
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x3568
leaq 0xb0(%rsp), %rdi
callq 0x3204
movslq 0x38(%rsp), %r14
leaq 0x58(%rsp), %rdi
leaq 0x40(%rsp), %rdx
movq %r14, %rsi
callq 0x3568
leaq 0xb0(%rsp), %r15
leaq 0x58(%rsp), %rsi
movq %r15, %rdi
callq 0x35ba
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x37dc
leaq 0x40(%rsp), %rdi
leaq 0xf(%rsp), %rdx
movq %r14, %rsi
callq 0x3568
leaq 0xb0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x35f8
leaq 0xb0(%rsp), %rbx
leaq 0x58(%rsp), %r14
leaq 0x40(%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 0x20(%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 $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4798
jmp 0x28e8
jmp 0x28e5
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x4798
jmp 0x28f9
jmp 0x28f6
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4798
leaq 0xb0(%rsp), %rdi
callq 0x37aa
jmp 0x291e
movq %rax, %rbx
jmp 0x2903
jmp 0x291b
jmp 0x291b
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x4798
leaq 0x90(%rsp), %rdi
callq 0x4798
movq %rbx, %rdi
callq 0x21e0