// File: /workspace/github_cpp_makefile_star2_O3/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, -0x38(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %rcx
movq 0x8(%rsi), %rax
movq %rax, -0x40(%rbp)
movq %rcx, -0x48(%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 -0x60(%rbp), %rdi
leaq -0x13f0(%rbp), %rcx
movq %r14, %rsi
movq %r13, %rdx
callq 0x77cc
movq $-0x1, (%r13)
leaq -0x2778(%rbp), %rdx
leaq -0x13f0(%rbp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7508
leaq -0x60(%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 0x54e5
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x5316
movq (%rbx), %rax
cmpq $0x1, %r14
movq %r14, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
cmpq $-0x1, (%rax,%rdx,8)
je 0x5464
incq %rdx
cmpq %rdx, %rcx
jne 0x5303
leaq -0x2778(%rbp), %rdi
callq 0x6fc0
leaq -0x2778(%rbp), %rdi
callq 0x21a0
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 0x5340
movq $0x270, -0x70(%rbp)     # imm = 0x270
cmpq $0x2, -0x38(%rbp)
jb 0x542e
leaq -0x1(%r14), %r13
cmpq $0x1, %r14
movq %r14, %r12
adcq $0x0, %r12
movl $0x1, %r15d
xorpd %xmm0, %xmm0
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x53ae
movq -0x60(%rbp), %rax
xorl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm0
incq %rcx
cmpq %rcx, %r12
jne 0x53a1
movsd %xmm0, -0x30(%rbp)
leaq -0x13f0(%rbp), %rdi
callq 0x96b7
movq -0x60(%rbp), %rax
movq %r13, %rsi
cmpq $0x2, %r14
jb 0x5402
addsd 0x5c44(%rip), %xmm0     # 0xb018
movsd -0x30(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, -0x30(%rbp)
xorpd %xmm0, %xmm0
movl $0x1, %esi
addsd (%rax,%rsi,8), %xmm0
ucomisd -0x30(%rbp), %xmm0
jae 0x5402
incq %rsi
cmpq %rsi, %r14
jne 0x53eb
movq %r13, %rsi
movq $0x0, (%rax,%rsi,8)
movq (%rbx), %rax
movq %rsi, (%rax,%rsi,8)
movq -0x68(%rbp), %rdi
leaq -0x60(%rbp), %rdx
movq %rbx, %rcx
callq 0x54e5
incq %r15
cmpq -0x38(%rbp), %r15
jne 0x538d
leaq -0x2778(%rbp), %rdi
callq 0x2130
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x544f
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
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 0x5cbf(%rip), %rcx      # 0xb134
movq %rcx, (%rax)
movq 0x9b41(%rip), %rsi      # 0xefc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2220
jmp 0x54a9
jmp 0x5498
jmp 0x54b1
jmp 0x54b1
movq %rax, %r14
jmp 0x54c8
jmp 0x5498
movq %rax, %r14
leaq -0x2778(%rbp), %rdi
callq 0x2130
jmp 0x54b4
movq %rax, %rdi
callq 0x7da0
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x54c8
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x54dd
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %r14, %rdi
callq 0x2230