// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2758, %rsp           # imm = 0x2758
movq %rcx, %r12
movq %rdx, -0x50(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %rcx
movq 0x8(%rsi), %rax
movq %rax, -0x58(%rbp)
movq %rcx, -0x60(%rbp)
subq %rcx, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r14
movabsq $0x7fefffffffffffff, %rax # imm = 0x7FEFFFFFFFFFFFFF
leaq -0x2778(%rbp), %r13
movq %rax, (%r13)
leaq -0x48(%rbp), %rdi
leaq -0x13f0(%rbp), %rcx
movq %r14, %rsi
movq %r13, %rdx
callq 0x7f04
movq $-0x1, (%r13)
leaq -0x2778(%rbp), %rdx
leaq -0x13f0(%rbp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7c2e
leaq -0x48(%rbp), %rdx
movq (%rdx), %rax
movq $0x0, (%rax,%r12,8)
movq (%rbx), %rax
movq %r12, (%rax,%r12,8)
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x58fa
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x5727
movq (%rbx), %rax
cmpq $0x1, %r14
movq %r14, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
cmpq $-0x1, (%rax,%rdx,8)
je 0x5879
incq %rdx
cmpq %rdx, %rcx
jne 0x5714
leaq -0x2778(%rbp), %rdi
callq 0x76d4
leaq -0x2778(%rbp), %rdi
callq 0x21b0
movq %r15, -0x68(%rbp)
movl %eax, %eax
movq %rax, -0x13f0(%rbp)
movl $0x1, %ecx
movq %rax, %rdx
shrq $0x1e, %rdx
xorl %eax, %edx
imull $0x6c078965, %edx, %eax # imm = 0x6C078965
addl %ecx, %eax
movq %rax, -0x13f0(%rbp,%rcx,8)
incq %rcx
cmpq $0x270, %rcx            # imm = 0x270
jne 0x5751
movq $0x270, -0x70(%rbp)     # imm = 0x270
cmpq $0x2, -0x50(%rbp)
jb 0x5843
leaq -0x1(%r14), %r13
cmpq $0x1, %r14
movq %r14, %r12
adcq $0x0, %r12
movl $0x1, %r15d
xorpd %xmm0, %xmm0
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x57bf
movq -0x48(%rbp), %rax
xorl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %r12
jne 0x57b2
movsd %xmm0, -0x30(%rbp)
leaq -0x13f0(%rbp), %rdi
callq 0x9d7d
movq %r13, %rsi
cmpq $0x2, %r14
jb 0x5813
addsd 0x5837(%rip), %xmm0     # 0xb018
movsd -0x30(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
xorpd %xmm0, %xmm0
movl $0x1, %esi
movq -0x48(%rbp), %rax
addsd (%rax,%rsi,8), %xmm0
ucomisd -0x30(%rbp), %xmm0
jae 0x5813
incq %rsi
cmpq %rsi, %r14
jne 0x57fc
movq %r13, %rsi
movq -0x48(%rbp), %rax
movq $0x0, (%rax,%rsi,8)
movq (%rbx), %rax
movq %rsi, (%rax,%rsi,8)
movq -0x68(%rbp), %rdi
leaq -0x48(%rbp), %rdx
movq %rbx, %rcx
callq 0x58fa
incq %r15
cmpq -0x50(%rbp), %r15
jne 0x579e
leaq -0x2778(%rbp), %rdi
callq 0x2140
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x5864
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq %rbx, %rax
addq $0x2758, %rsp           # imm = 0x2758
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x2080
leaq 0x58aa(%rip), %rcx      # 0xb134
movq %rcx, (%rax)
movq 0x972c(%rip), %rsi      # 0xefc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2230
jmp 0x58be
jmp 0x58ad
jmp 0x58c6
jmp 0x58c6
movq %rax, %r14
jmp 0x58dd
jmp 0x58ad
movq %rax, %r14
leaq -0x2778(%rbp), %rdi
callq 0x2140
jmp 0x58c9
movq %rax, %rdi
callq 0x84c7
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x58dd
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2180
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x58f2
movq -0x38(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq %r14, %rdi
callq 0x2240