// File: 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
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
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 0x8643(%rip), %xmm0     # 0xb028
divsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x2a82
cvtsi2sdl -0x44(%rbp), %xmm0
movsd -0x40(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd 0x8619(%rip), %xmm1     # 0xb030
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x3bb0
jmp 0x2a2f
movsd -0x50(%rbp), %xmm2
xorps %xmm0, %xmm0
movsd 0x85c9(%rip), %xmm1     # 0xb008
callq 0x2430
movsd 0x85ec(%rip), %xmm1     # 0xb038
addsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x3c20
jmp 0x2a64
jmp 0x2a66
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x29f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x2d03
leaq -0x100(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x3c50
jmp 0x2a98
movslq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x119(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x43e0
movq -0x178(%rbp), %rsi
movq -0x170(%rbp), %rdx
leaq -0x118(%rbp), %rdi
callq 0x4400
jmp 0x2ad2
leaq -0x119(%rbp), %rdi
callq 0x4470
leaq -0x100(%rbp), %rdi
callq 0x3d80
jmp 0x2aec
movslq -0x4(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x139(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x43e0
movq -0x188(%rbp), %rsi
movq -0x180(%rbp), %rdx
leaq -0x138(%rbp), %rdi
callq 0x4400
jmp 0x2b26
leaq -0x139(%rbp), %rdi
callq 0x4470
leaq -0x100(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x4490
jmp 0x2b47
movl -0x8(%rbp), %esi
leaq -0x100(%rbp), %rdi
leaq -0x118(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4b90
jmp 0x2b61
movslq -0x4(%rbp), %rax
movq %rax, -0x198(%rbp)
leaq -0x159(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x43e0
movq -0x198(%rbp), %rsi
movq -0x190(%rbp), %rdx
leaq -0x158(%rbp), %rdi
callq 0x4400
jmp 0x2b9b
leaq -0x159(%rbp), %rdi
callq 0x4470
leaq -0x100(%rbp), %rdi
leaq -0x118(%rbp), %rsi
leaq -0x158(%rbp), %rdx
callq 0x4680
jmp 0x2bc3
leaq -0x100(%rbp), %rdi
leaq -0x138(%rbp), %rsi
leaq -0x158(%rbp), %rdx
callq 0x49c0
movsd %xmm0, -0x1a0(%rbp)
jmp 0x2be7
movsd -0x1a0(%rbp), %xmm0
movsd %xmm0, -0x168(%rbp)
movsd -0x168(%rbp), %xmm0
movsd %xmm0, -0x1a8(%rbp)
leaq -0x158(%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 -0x1a8(%rbp), %xmm0
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x119(%rbp), %rdi
callq 0x4470
jmp 0x2cf7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x2ceb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x139(%rbp), %rdi
callq 0x4470
jmp 0x2ceb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x2cdf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x159(%rbp), %rdi
callq 0x4470
jmp 0x2cdf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x158(%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
nop