// File: 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
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x128(%rbp)
movq %rdi, %rax
movq %rax, -0x120(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x45d0
movq -0x118(%rbp), %rdx
leaq 0x156ea(%rip), %rsi     # 0x2024f
leaq -0x70(%rbp), %rdi
callq 0x44d0
jmp 0xab70
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xa440
jmp 0xab7f
leaq -0x70(%rbp), %rdi
callq 0x4230
leaq -0x71(%rbp), %rdi
callq 0x4430
leaq -0xa0(%rbp), %rdi
callq 0x1cb20
movl $0x0, -0xa4(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x1cb40
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rdi
callq 0x1cb70
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1cba0
testb $0x1, %al
jne 0xabf4
jmp 0xac8f
leaq -0xb8(%rbp), %rdi
callq 0x1cbe0
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movl 0x40(%rax), %ecx
movl -0xa4(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xa4(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa4(%rbp), %rsi
callq 0x1cc00
jmp 0xac34
jmp 0xac36
leaq -0xb8(%rbp), %rdi
callq 0x1cc70
jmp 0xabd8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0xac6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x4230
leaq -0x71(%rbp), %rdi
callq 0x4430
jmp 0xaef5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0xaee0
movb $0x0, -0xc9(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, -0x138(%rbp)
leaq -0xca(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x97d0
movq -0x128(%rbp), %rdi
movq -0x138(%rbp), %rsi
movq -0x130(%rbp), %rdx
callq 0x1cc90
jmp 0xacd0
leaq -0xca(%rbp), %rdi
callq 0x9870
movl $0x0, -0xd0(%rbp)
movl -0xd0(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xae9f
movl -0xa4(%rbp), %esi
decl %esi
xorl %edi, %edi
callq 0x1c900
movl %eax, -0x13c(%rbp)
jmp 0xad0c
movl -0x13c(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x1cd50
movq -0x150(%rbp), %rdi
movq %rax, -0xe8(%rbp)
callq 0x1cd80
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
leaq -0xd4(%rbp), %rdx
callq 0x1cd00
movq %rax, -0x148(%rbp)
jmp 0xad68
movq -0x148(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x1cd80
movq %rax, -0xf8(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x1cdb0
testb $0x1, %al
jne 0xada2
jmp 0xada4
jmp 0xae03
leaq 0x154b4(%rip), %rdi     # 0x2025f
leaq 0x154c5(%rip), %rsi     # 0x20277
movl $0xa5, %edx
leaq 0x154c9(%rip), %rcx     # 0x20287
callq 0x4270
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xca(%rbp), %rdi
callq 0x9870
jmp 0xaee0
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
callq 0x9890
jmp 0xaee0
leaq -0xa0(%rbp), %rdi
callq 0x1cd50
movq %rax, -0x108(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x108(%rbp), %rdi
movq -0x110(%rbp), %rsi
callq 0x1cdf0
movq %rax, -0x158(%rbp)
jmp 0xae40
movq -0x158(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x10(%rbp), %rdi
movq -0x100(%rbp), %rsi
callq 0x1ce30
movq -0x128(%rbp), %rdi
movq %rax, -0x160(%rbp)
movslq -0xd0(%rbp), %rsi
callq 0xaf20
movq -0x160(%rbp), %rsi
movq %rax, %rdi
callq 0xb560
jmp 0xae89
jmp 0xae8b
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0xace6
movb $0x1, -0xc9(%rbp)
testb $0x1, -0xc9(%rbp)
jne 0xaebb
movq -0x128(%rbp), %rdi
callq 0x9890
leaq -0xa0(%rbp), %rdi
callq 0x1ce50
leaq -0x50(%rbp), %rdi
callq 0xa860
movq -0x120(%rbp), %rax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0xa0(%rbp), %rdi
callq 0x1ce50
leaq -0x50(%rbp), %rdi
callq 0xa860
movq -0x80(%rbp), %rdi
callq 0x45c0
nop