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

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

    // first pivot
    mins[p0] = 0;
    argmins[p0] = p0;
    maxmin_bfs_unweighted(graph, p0, mins, argmins);
    for (uint64_t i = 0; i < n; i++)
    {
        if (argmins[i] == -1)
            throw "graph is not strongly connected, or is not indexed from zero";
    }

    // 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++)
    {
        // uint64_t max = mins[0], argmax = 0;
        // for (uint64_t i = 1; i < n; i++)
        // {
        //     if (mins[i] > max)
        //     {
        //         max = mins[i];
        //         argmax = i;
        //     }
        // }
        // maxmin non-random above

        // choose pivots with probability min
        uint64_t min_total = 0;
        for (uint64_t i = 0; i < n; i++)
        {
            min_total += mins[i];
        }
        double rn = rng(mt) * min_total;
        uint64_t cumul = 0;
        uint64_t argmax = 0;
        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_unweighted(graph, argmax, mins, argmins);
    }
    // TODO: look for error in bfs here
    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, -0x48(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rsi), %rax
subq (%rsi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, %r12
leaq -0x13e0(%rbp), %r14
orq $-0x1, (%r14)
leaq -0x40(%rbp), %rdi
leaq -0x2768(%rbp), %rcx
movq %rax, %rsi
movq %r14, %rdx
callq 0x5f48
orq $-0x1, (%r14)
leaq -0x13e0(%rbp), %rdx
leaq -0x2768(%rbp), %rcx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5f48
leaq -0x40(%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 0x39e9
movq (%rbx), %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x388c
cmpq $-0x1, (%rax,%rcx,8)
leaq 0x1(%rcx), %rcx
jne 0x3859
pushq $0x8
popq %rdi
callq 0x2090
leaq 0x7849(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xd73e(%rip), %rsi      # 0x10fc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2200
leaq -0x13e0(%rbp), %rdi
callq 0x5b0e
leaq -0x13e0(%rbp), %rdi
callq 0x2180
movl %eax, %esi
leaq -0x2768(%rbp), %rdi
callq 0x6ccc
leaq -0x58(%rbp), %rcx
andq $0x0, (%rcx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x8(%rcx)
pushq $0x1
popq %r13
cmpq -0x48(%rbp), %r13
jae 0x3985
movq -0x40(%rbp), %rax
xorl %r14d, %r14d
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x38ed
addq (%rax,%rcx,8), %r14
incq %rcx
jmp 0x38df
leaq -0x58(%rbp), %rdi
leaq -0x2768(%rbp), %rsi
callq 0x651c
movq %r14, %xmm1
punpckldq 0x7756(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x775e(%rip), %xmm1     # 0xb070
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm2, %xmm0
movq -0x40(%rbp), %rax
xorl %ecx, %ecx
pushq $0x1
popq %rsi
cmpq %r12, %rsi
jae 0x3960
addq (%rax,%rsi,8), %rcx
movq %rcx, %xmm1
punpckldq 0x771f(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x7727(%rip), %xmm1     # 0xb070
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x3962
incq %rsi
jmp 0x392b
xorl %esi, %esi
andq $0x0, (%rax,%rsi,8)
movq (%rbx), %rax
movq %rsi, (%rax,%rsi,8)
movq %r15, %rdi
leaq -0x40(%rbp), %rdx
movq %rbx, %rcx
callq 0x39e9
incq %r13
jmp 0x38cc
leaq -0x13e0(%rbp), %rdi
callq 0x5d56
leaq -0x40(%rbp), %rdi
callq 0x7894
movq %rbx, %rax
addq $0x2748, %rsp           # imm = 0x2748
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x39cd
jmp 0x39cd
movq %rax, %r14
jmp 0x39d8
jmp 0x39bc
jmp 0x39bc
movq %rax, %r14
leaq -0x13e0(%rbp), %rdi
callq 0x5d56
jmp 0x39d0
movq %rax, %r14
movq %rbx, %rdi
callq 0x7894
leaq -0x40(%rbp), %rdi
callq 0x7894
movq %r14, %rdi
callq 0x2210