// File: /workspace/github_cpp_makefile_star2_O1/Quinny[P]IteratedPrisoners/./ipd/genetic.h
// Function: std::vector<ipd::prisoner_t, std::allocator<ipd::prisoner_t>> ipd::genetic::weighted_random_sample<ipd::prisoner_t>(std::vector<std::pair<ipd::prisoner_t, int>, std::allocator<std::pair<ipd::prisoner_t, int>>> const&, int)
std::vector<T>
weighted_random_sample(const std::vector<std::pair<T, int>>& v, int n) {
    qp::profiler selection_prof("weighted sample");
    std::vector<int> cummulative;
    int total = 0;
    for (auto& i: v) {
        total += i.second;
        cummulative.push_back(total);
    }

    std::vector<T> sample(n);
    for (int i = 0; i < n; ++i) {
        int prob = random_range(0, total - 1);
        auto it = std::upper_bound(cummulative.begin(), cummulative.end(), prob);
        assert(it != cummulative.end());
        auto index = std::distance(cummulative.begin(), it);
        sample[i] = v[index].first;
    }

    return sample;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
leaq -0x70(%rbp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x54f4(%rip), %rsi      # 0xe1e6
leaq 0x54fc(%rip), %rdx      # 0xe1f5
leaq -0x80(%rbp), %r12
movq %r12, %rdi
callq 0xaa78
callq 0x4060
leaq -0xa0(%rbp), %rdi
movq %rax, -0x18(%rdi)
movq $0x0, -0x10(%rdi)
movb $0x0, -0x8(%rdi)
leaq -0x90(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
callq 0x9bf0
movl %r15d, -0x3c(%rbp)
movq -0x80(%rbp), %rdi
cmpq %rbx, %rdi
je 0x8d4f
callq 0x4350
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x0, -0x2c(%rbp)
movq (%r14), %rbx
movq 0x8(%r14), %r15
cmpq %r15, %rbx
je 0x8dac
leaq -0x60(%rbp), %r12
leaq -0x2c(%rbp), %r13
movl -0x2c(%rbp), %eax
addl 0x40(%rbx), %eax
movl %eax, -0x2c(%rbp)
movq -0x58(%rbp), %rsi
cmpq -0x50(%rbp), %rsi
je 0x8d98
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x58(%rbp)
jmp 0x8da3
movq %r12, %rdi
movq %r13, %rdx
callq 0xd484
addq $0x48, %rbx
cmpq %r15, %rbx
jne 0x8d79
movl -0x3c(%rbp), %ebx
movslq %ebx, %rsi
leaq -0x2d(%rbp), %rdx
movq -0x38(%rbp), %rdi
callq 0xd3ec
testl %ebx, %ebx
jle 0x8e6e
movl %ebx, %r13d
xorl %ebx, %ebx
movl -0x2c(%rbp), %esi
decl %esi
xorl %edi, %edi
callq 0xd3a9
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq %rdx, %rdi
subq %rcx, %rdi
sarq $0x2, %rdi
movq %rcx, %rsi
testq %rdi, %rdi
jle 0x8e1f
movq %rcx, %rsi
movq %rdi, %r8
shrq %r8
movq %r8, %r9
notq %r9
addq %rdi, %r9
leaq (%rsi,%r8,4), %rdi
addq $0x4, %rdi
cmpl -0x4(%rdi), %eax
cmovgeq %rdi, %rsi
cmovlq %r8, %r9
movq %r9, %rdi
testq %r9, %r9
jg 0x8df5
cmpq %rdx, %rsi
je 0x8e9e
subq %rcx, %rsi
shrq $0x2, %rsi
leaq (%rsi,%rsi,8), %r15
shlq $0x3, %r15
addq (%r14), %r15
movq %rbx, %r12
shlq $0x6, %r12
movq -0x38(%rbp), %rax
addq (%rax), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x41a0
addq $0x20, %r12
addq $0x20, %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x9aea
incq %rbx
cmpq %r13, %rbx
jne 0x8dcc
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x8e7c
callq 0x4350
leaq -0xb8(%rbp), %rdi
callq 0x8bea
movq -0x38(%rbp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5351(%rip), %rdi      # 0xe1f6
leaq 0x5362(%rip), %rsi      # 0xe20e
leaq 0x536b(%rip), %rcx      # 0xe21e
movl $0xa5, %edx
callq 0x4210
jmp 0x8ed2
movq %rax, %r14
movq -0x80(%rbp), %rdi
cmpq %rbx, %rdi
je 0x8eff
callq 0x4350
jmp 0x8eff
movq %rax, %r14
jmp 0x8ee5
jmp 0x8ed9
movq %rax, %r14
movq -0x38(%rbp), %rdi
callq 0x84bc
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x8ef3
callq 0x4350
leaq -0xb8(%rbp), %rdi
callq 0x8bea
movq %r14, %rdi
callq 0x4520
nop