// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2748, %rsp           # imm = 0x2748
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rsi), %rax
subq (%rsi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, %r12
movabsq $0x7fefffffffffffff, %rax # imm = 0x7FEFFFFFFFFFFFFF
leaq -0x13e8(%rbp), %rdx
movq %rax, (%rdx)
leaq -0x48(%rbp), %rdi
leaq -0x2770(%rbp), %rcx
movq %r12, %rsi
callq 0x6124
leaq -0x13e8(%rbp), %rax
orq $-0x1, (%rax)
leaq -0x13e8(%rbp), %rdx
leaq -0x2770(%rbp), %rcx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5f48
leaq -0x48(%rbp), %rdx
movq (%rdx), %rax
andq $0x0, (%rax,%r13,8)
movq (%rbx), %rax
movq %r13, (%rax,%r13,8)
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rcx
callq 0x46c4
movq (%rbx), %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x4580
cmpq $-0x1, (%rax,%rcx,8)
leaq 0x1(%rcx), %rcx
jne 0x454d
pushq $0x8
popq %rdi
callq 0x2090
leaq 0x6bc8(%rip), %rcx      # 0xb134
movq %rcx, (%rax)
movq 0xca4a(%rip), %rsi      # 0x10fc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2200
leaq -0x13e8(%rbp), %rdi
callq 0x5b0e
leaq -0x13e8(%rbp), %rdi
callq 0x2180
movl %eax, %esi
leaq -0x2770(%rbp), %rdi
callq 0x6ccc
leaq -0x60(%rbp), %rcx
andq $0x0, (%rcx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x8(%rcx)
leaq -0x1(%r12), %rax
movq %rax, -0x50(%rbp)
pushq $0x1
popq %r13
cmpq %r14, %r13
jae 0x4660
xorpd %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x48(%rbp), %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x45fa
movsd -0x30(%rbp), %xmm0
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x30(%rbp)
incq %rcx
jmp 0x45e1
leaq -0x60(%rbp), %rdi
leaq -0x2770(%rbp), %rsi
callq 0x651c
movsd -0x30(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
xorpd %xmm0, %xmm0
movq -0x48(%rbp), %rax
pushq $0x1
popq %rsi
cmpq %r12, %rsi
jae 0x4639
addsd (%rax,%rsi,8), %xmm0
ucomisd -0x30(%rbp), %xmm0
jae 0x463d
incq %rsi
jmp 0x4623
movq -0x50(%rbp), %rsi
andq $0x0, (%rax,%rsi,8)
movq (%rbx), %rax
movq %rsi, (%rax,%rsi,8)
movq %r15, %rdi
leaq -0x48(%rbp), %rdx
movq %rbx, %rcx
callq 0x46c4
incq %r13
jmp 0x45c9
leaq -0x13e8(%rbp), %rdi
callq 0x5d56
leaq -0x48(%rbp), %rdi
callq 0x89de
movq %rbx, %rax
addq $0x2748, %rsp           # imm = 0x2748
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x46a8
jmp 0x46a8
movq %rax, %r14
jmp 0x46b3
jmp 0x4697
jmp 0x4697
movq %rax, %r14
leaq -0x13e8(%rbp), %rdi
callq 0x5d56
jmp 0x46ab
movq %rax, %r14
movq %rbx, %rdi
callq 0x7894
leaq -0x48(%rbp), %rdi
callq 0x89de
movq %r14, %rdi
callq 0x2210