// File: sharmaeklavya2[P]ddos-on-anycast/src/lib/sample.hpp
// Function: weighed_sample(std::vector<int, std::allocator<int>> const&, std::vector<int, std::allocator<int>> const&, std::linear_congruential_engine<unsigned long, 48271ul, 0ul, 2147483647ul>&)
inline int weighed_sample(const ivec& v, const ivec& weights, rng_t& rng) {
    if(weights.size() != v.size()) {
        fprintf(stderr, "weighed_sample: container and weights have different sizes\n");
    }
    if(weights.empty()) {
        fprintf(stderr, "weighed_sample: nothing_to_sample\n");
    }
    ivec cum_weights(1, 0);
    cum_weights.reserve(v.size()+1);
    for(auto weight: weights) {
        cum_weights.push_back(weight + cum_weights.back());
    }

    int i = iunidist(0, cum_weights.back() - 1, rng);
    int j = std::upper_bound(cum_weights.begin(), cum_weights.end(), i) - cum_weights.begin() - 1;
    return v[j];
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x4f00
movq %rax, 0x40(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x4f00
movq %rax, %rcx
movq 0x40(%rsp), %rax
cmpq %rcx, %rax
je 0xa5f3
movq 0x229e6(%rip), %rax     # 0x2cfc8
movq (%rax), %rdi
leaq 0x16f55(%rip), %rsi     # 0x21541
movb $0x0, %al
callq 0x2180
movq 0xc8(%rsp), %rdi
callq 0x86a0
testb $0x1, %al
jne 0xa606
jmp 0xa61e
movq 0x229bb(%rip), %rax     # 0x2cfc8
movq (%rax), %rdi
leaq 0x16f66(%rip), %rsi     # 0x2157d
movb $0x0, %al
callq 0x2180
movl $0x0, 0xa4(%rsp)
leaq 0xa3(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x4f40
movq 0x38(%rsp), %rcx
leaq 0xa8(%rsp), %rdi
movl $0x1, %esi
leaq 0xa4(%rsp), %rdx
callq 0x4f50
jmp 0xa65c
leaq 0xa3(%rsp), %rdi
callq 0x4fd0
movq 0xd0(%rsp), %rdi
callq 0x4f00
movq %rax, %rsi
incq %rsi
leaq 0xa8(%rsp), %rdi
callq 0xa200
jmp 0xa68b
movq 0xc8(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
callq 0x4880
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rdi
callq 0x48b0
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x48e0
testb $0x1, %al
jne 0xa6dd
jmp 0xa787
leaq 0x80(%rsp), %rdi
callq 0x4920
movl (%rax), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x34(%rsp)
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xb450
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl 0x34(%rsp), %eax
movl (%rcx), %ecx
addl %ecx, %eax
movl %eax, 0x70(%rsp)
leaq 0x70(%rsp), %rsi
callq 0x46c0
jmp 0xa72a
jmp 0xa72c
leaq 0x80(%rsp), %rdi
callq 0x4930
jmp 0xa6c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa3(%rsp), %rdi
callq 0x4fd0
jmp 0xa85a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x5020
jmp 0xa85a
leaq 0xa8(%rsp), %rdi
callq 0xb450
movl (%rax), %esi
decl %esi
movq 0xc0(%rsp), %rdx
xorl %edi, %edi
callq 0x92e0
movl %eax, 0x24(%rsp)
jmp 0xa7ad
movl 0x24(%rsp), %eax
movl %eax, 0x6c(%rsp)
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x5bf0
movq 0x10(%rsp), %rdi
movq %rax, 0x58(%rsp)
callq 0x5a70
movq %rax, 0x50(%rsp)
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
leaq 0x6c(%rsp), %rdx
callq 0xb490
movq %rax, 0x18(%rsp)
jmp 0xa7f6
movq 0x18(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x5bf0
movq %rax, 0x48(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x5bb0
subq $0x1, %rax
movl %eax, 0x68(%rsp)
movq 0xd0(%rsp), %rdi
movslq 0x68(%rsp), %rsi
callq 0x4970
movl (%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x5020
movl 0xc(%rsp), %eax
addq $0xd8, %rsp
retq
movq 0x98(%rsp), %rdi
callq 0x21f0
nopw (%rax,%rax)