// File: ekg[P]sgd2/src/sgd2.hpp
// Function: sgd2::maxmin_random_sp_weighted(std::vector<std::vector<sgd2::edge, std::allocator<sgd2::edge>>, std::allocator<std::vector<sgd2::edge, std::allocator<sgd2::edge>>>> const&, unsigned long, unsigned long)
std::vector<uint64_t> maxmin_random_sp_weighted(const std::vector<std::vector<edge>>& graph, uint64_t n_pivots, uint64_t p0)
{
    uint64_t n = graph.size();

    std::vector<double> mins(n, std::numeric_limits<double>::max());
    std::vector<uint64_t> argmins(n, -1);

    // first pivot
    mins[p0] = 0;
    argmins[p0] = p0;
    maxmin_bfs_weighted(graph, p0, mins, argmins);
    for (uint64_t i = 0; i < n; i++)
    {
        if (argmins[i] == -1)
            throw "graph has more than one connected component";
    }

    // remaining pivots
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_real_distribution<> rng(0, 1);
    for (uint64_t i = 1; i < n_pivots; i++)
    {
        // choose pivots with probability min
        double min_total = 0;
        for (uint64_t i = 0; i < n; i++)
        {
            min_total += mins[i];
        }
        double rn = rng(mt) * min_total;
        double cumul = 0;
        uint64_t argmax = n-1;
        for (uint64_t i = 1; i < n; i++)
        {
            cumul += mins[i];
            if (cumul >= rn)
            {
                argmax = i;
                break;
            }
        }

        mins[argmax] = 0;
        argmins[argmax] = argmax;
        maxmin_bfs_weighted(graph, argmax, mins, argmins);
    }
    return argmins;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2830, %rsp           # imm = 0x2830
movq %rdi, -0x27f0(%rbp)
movq %rdi, %rax
movq %rax, -0x27e8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0xbaa0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x27e0(%rbp)
callq 0xa6a0
movsd %xmm0, -0x48(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x27d8(%rbp)
callq 0xa6b0
movq -0x27e0(%rbp), %rsi
movq -0x27d8(%rbp), %rcx
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0xa6d0
jmp 0x67ab
leaq -0x49(%rbp), %rdi
callq 0xa750
movb $0x0, -0x5d(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x2800(%rbp)
movq $-0x1, -0x68(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0x27f8(%rbp)
callq 0x9df0
movq -0x27f0(%rbp), %rdi
movq -0x2800(%rbp), %rsi
movq -0x27f8(%rbp), %rcx
leaq -0x68(%rbp), %rdx
callq 0x9e10
jmp 0x67fb
leaq -0x69(%rbp), %rdi
callq 0x9e90
movq -0x20(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x2808(%rbp)
callq 0xa7a0
movq -0x27f0(%rbp), %rdi
movq $0x0, (%rax)
movq -0x20(%rbp), %rsi
movq %rsi, -0x2810(%rbp)
callq 0x9ee0
movq -0x2810(%rbp), %rsi
movq -0x2808(%rbp), %rdx
movq -0x27f0(%rbp), %rcx
movq %rsi, (%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x6b90
jmp 0x685d
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x690d
movq -0x27f0(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x9ee0
cmpq $-0x1, (%rax)
jne 0x68fa
movl $0x8, %edi
callq 0x2080
movq %rax, %rdi
leaq 0x17867(%rip), %rax     # 0x1e104
movq %rax, (%rdi)
movq 0x27719(%rip), %rsi     # 0x2dfc0
xorl %eax, %eax
movl %eax, %edx
callq 0x2290
jmp 0x6b8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0xa750
jmp 0x6b82
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x9e90
jmp 0x6b79
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x6b6d
jmp 0x68fc
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x6865
leaq -0x1400(%rbp), %rdi
callq 0x93e0
jmp 0x691b
leaq -0x1400(%rbp), %rdi
callq 0x9480
movl %eax, -0x2814(%rbp)
jmp 0x692f
movl -0x2814(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x2788(%rbp), %rdi
callq 0x94a0
jmp 0x6947
leaq -0x2798(%rbp), %rdi
xorps %xmm0, %xmm0
movsd 0x176af(%rip), %xmm1    # 0x1e008
callq 0xb090
jmp 0x6960
movq $0x1, -0x27a0(%rbp)
movq -0x27a0(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x6b32
xorps %xmm0, %xmm0
movsd %xmm0, -0x27a8(%rbp)
movq $0x0, -0x27b0(%rbp)
movq -0x27b0(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x69f4
movq -0x27b0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xa7a0
movsd (%rax), %xmm0
addsd -0x27a8(%rbp), %xmm0
movsd %xmm0, -0x27a8(%rbp)
movq -0x27b0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x27b0(%rbp)
jmp 0x6992
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x1400(%rbp), %rdi
callq 0x9900
jmp 0x6b6d
leaq -0x2798(%rbp), %rdi
leaq -0x2788(%rbp), %rsi
callq 0xb0c0
movsd %xmm0, -0x2820(%rbp)
jmp 0x6a11
movsd -0x2820(%rbp), %xmm0
mulsd -0x27a8(%rbp), %xmm0
movsd %xmm0, -0x27b8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x27c0(%rbp)
movq -0x28(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x27c8(%rbp)
movq $0x1, -0x27d0(%rbp)
movq -0x27d0(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x6ab7
movq -0x27d0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0xa7a0
movsd (%rax), %xmm0
addsd -0x27c0(%rbp), %xmm0
movsd %xmm0, -0x27c0(%rbp)
movsd -0x27c0(%rbp), %xmm0
ucomisd -0x27b8(%rbp), %xmm0
jb 0x6aa1
movq -0x27d0(%rbp), %rax
movq %rax, -0x27c8(%rbp)
jmp 0x6ab7
jmp 0x6aa3
movq -0x27d0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x27d0(%rbp)
jmp 0x6a4e
movq -0x27c8(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0x2828(%rbp)
callq 0xa7a0
movq -0x27f0(%rbp), %rdi
movq $0x0, (%rax)
movq -0x27c8(%rbp), %rsi
movq %rsi, -0x2830(%rbp)
callq 0x9ee0
movq -0x2830(%rbp), %rsi
movq -0x2828(%rbp), %rdx
movq -0x27f0(%rbp), %rcx
movq %rsi, (%rax)
movq -0x10(%rbp), %rdi
movq -0x27c8(%rbp), %rsi
callq 0x6b90
jmp 0x6b19
jmp 0x6b1b
movq -0x27a0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x27a0(%rbp)
jmp 0x696b
movb $0x1, -0x5d(%rbp)
leaq -0x1400(%rbp), %rdi
callq 0x9900
testb $0x1, -0x5d(%rbp)
jne 0x6b54
movq -0x27f0(%rbp), %rdi
callq 0xa130
leaq -0x40(%rbp), %rdi
callq 0xaa80
movq -0x27e8(%rbp), %rax
addq $0x2830, %rsp           # imm = 0x2830
popq %rbp
retq
movq -0x27f0(%rbp), %rdi
callq 0xa130
leaq -0x40(%rbp), %rdi
callq 0xaa80
movq -0x58(%rbp), %rdi
callq 0x22a0
nopl (%rax,%rax)