// File: /workspace/github_cpp_makefile_star2_O3/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 $0x2758, %rsp           # imm = 0x2758
movq %rcx, %r12
movq %rdx, -0x30(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %rcx
movq 0x8(%rsi), %rax
movq %rax, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
subq %rcx, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r13
movq $-0x1, %r14
leaq -0x13f8(%rbp), %rdx
movq %r14, (%rdx)
leaq -0x58(%rbp), %rdi
leaq -0x2780(%rbp), %rcx
movq %r13, %rsi
callq 0x7508
leaq -0x13f8(%rbp), %rax
movq %r14, (%rax)
leaq -0x13f8(%rbp), %rdx
leaq -0x2780(%rbp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7508
leaq -0x58(%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 0x431c
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x411c
movq (%rbx), %rax
cmpq $0x1, %r13
movq %r13, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
cmpq $-0x1, (%rax,%rdx,8)
je 0x429b
incq %rdx
cmpq %rdx, %rcx
jne 0x4109
leaq -0x13f8(%rbp), %rdi
callq 0x6fc0
leaq -0x13f8(%rbp), %rdi
callq 0x21a0
movl %eax, %eax
movq %rax, -0x2780(%rbp)
movl $0x1, %ecx
movq %rax, %rdx
shrq $0x1e, %rdx
xorl %eax, %edx
imull $0x6c078965, %edx, %eax # imm = 0x6C078965
addl %ecx, %eax
movq %rax, -0x2780(%rbp,%rcx,8)
incq %rcx
cmpq $0x270, %rcx            # imm = 0x270
jne 0x4142
movq $0x270, -0x1400(%rbp) # imm = 0x270
cmpq $0x2, -0x30(%rbp)
jb 0x4265
cmpq $0x1, %r13
movq %r13, %r12
adcq $0x0, %r12
movl $0x1, %r14d
xorpd %xmm0, %xmm0
movapd %xmm0, -0x70(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x41db
movq -0x58(%rbp), %rax
xorl %edx, %edx
xorl %ecx, %ecx
addq (%rax,%rdx,8), %rcx
incq %rdx
cmpq %rdx, %r12
jne 0x41a9
movq %rcx, %xmm0
punpckldq 0x6e9e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x6ea6(%rip), %xmm0     # 0xb070
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, -0x70(%rbp)
leaq -0x2780(%rbp), %rdi
callq 0x96b7
movq -0x58(%rbp), %rax
cmpq $0x2, %r13
jb 0x4238
addsd 0x6e1f(%rip), %xmm0     # 0xb018
mulsd -0x70(%rbp), %xmm0
movl $0x1, %esi
xorl %ecx, %ecx
addq (%rax,%rsi,8), %rcx
movq %rcx, %xmm1
punpckldq 0x6e4a(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x6e52(%rip), %xmm1     # 0xb070
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x423a
incq %rsi
cmpq %rsi, %r13
jne 0x4205
xorl %esi, %esi
movq $0x0, (%rax,%rsi,8)
movq (%rbx), %rax
movq %rsi, (%rax,%rsi,8)
movq %r15, %rdi
leaq -0x58(%rbp), %rdx
movq %rbx, %rcx
callq 0x431c
incq %r14
cmpq -0x30(%rbp), %r14
jne 0x418e
leaq -0x13f8(%rbp), %rdi
callq 0x2130
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x4286
movq -0x48(%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 0x6e15(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xad0a(%rip), %rsi      # 0xefc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2220
jmp 0x42e0
jmp 0x42cf
jmp 0x42e8
jmp 0x42e8
movq %rax, %r14
jmp 0x42ff
jmp 0x42cf
movq %rax, %r14
leaq -0x13f8(%rbp), %rdi
callq 0x2130
jmp 0x42eb
movq %rax, %rdi
callq 0x7da0
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x42ff
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x4314
movq -0x48(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq %r14, %rdi
callq 0x2230