// File: /workspace/github_cpp_makefile_star2_O1/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, -0x48(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %rcx
movq 0x8(%rsi), %rax
movq %rax, -0x50(%rbp)
movq %rcx, -0x58(%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 -0x40(%rbp), %rdi
leaq -0x2780(%rbp), %rcx
movq %r13, %rsi
callq 0x7c2e
leaq -0x13f8(%rbp), %rax
movq %r14, (%rax)
leaq -0x13f8(%rbp), %rdx
leaq -0x2780(%rbp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7c2e
leaq -0x40(%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 0x44d9
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0x42d5
movq (%rbx), %rax
cmpq $0x1, %r13
movq %r13, %rcx
adcq $0x0, %rcx
xorl %edx, %edx
cmpq $-0x1, (%rax,%rdx,8)
je 0x4458
incq %rdx
cmpq %rdx, %rcx
jne 0x42c2
leaq -0x13f8(%rbp), %rdi
callq 0x76d4
leaq -0x13f8(%rbp), %rdi
callq 0x21b0
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 0x42fb
movq $0x270, -0x1400(%rbp) # imm = 0x270
cmpq $0x2, -0x48(%rbp)
jb 0x4422
cmpq $0x1, %r13
movq %r13, %r12
adcq $0x0, %r12
movl $0x1, %r14d
xorpd %xmm0, %xmm0
movapd %xmm0, -0x70(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0x4394
movq -0x40(%rbp), %rax
xorl %edx, %edx
xorl %ecx, %ecx
addq (%rax,%rdx,8), %rcx
incq %rdx
cmpq %rdx, %r12
jne 0x4362
movq %rcx, %xmm0
punpckldq 0x6ce5(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x6ced(%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 0x9d7d
cmpq $0x2, %r13
jb 0x43f1
addsd 0x6c6a(%rip), %xmm0     # 0xb018
mulsd -0x70(%rbp), %xmm0
movl $0x1, %esi
movq -0x40(%rbp), %rax
xorl %ecx, %ecx
addq (%rax,%rsi,8), %rcx
movq %rcx, %xmm1
punpckldq 0x6c91(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x6c99(%rip), %xmm1     # 0xb070
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x43f3
incq %rsi
cmpq %rsi, %r13
jne 0x43be
xorl %esi, %esi
movq -0x40(%rbp), %rax
movq $0x0, (%rax,%rsi,8)
movq (%rbx), %rax
movq %rsi, (%rax,%rsi,8)
movq %r15, %rdi
leaq -0x40(%rbp), %rdx
movq %rbx, %rcx
callq 0x44d9
incq %r14
cmpq -0x48(%rbp), %r14
jne 0x4347
leaq -0x13f8(%rbp), %rdi
callq 0x2140
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x4443
movq -0x30(%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 0x6c58(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xab4d(%rip), %rsi      # 0xefc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2230
jmp 0x449d
jmp 0x448c
jmp 0x44a5
jmp 0x44a5
movq %rax, %r14
jmp 0x44bc
jmp 0x448c
movq %rax, %r14
leaq -0x13f8(%rbp), %rdi
callq 0x2140
jmp 0x44a8
movq %rax, %rdi
callq 0x84c7
movq %rax, %r14
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x44bc
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2180
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x44d1
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq %r14, %rdi
callq 0x2240