// File: /workspace/github_cpp_makefile_star2_O2/sharmaeklavya2[P]ddos-on-anycast/src/lib/graph_gen.cpp
// Function: make_random_connected_graph(int, int, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>&, int)
void make_random_connected_graph(int n, int approx_m, ipvec& l, int seed) {
    // Make a connected graph with (n vertices and approximately approx_m edges).
    // Returns the number of nodes in this graph. Returns the anti-symmetric edge list in l.
    // int n = std::max(1, int(std::normal_distribution<double>(n, n * 0.25)(rng));

    rng_t rng(seed);

    int max_edges = n * (n-1) / 2;
    if(approx_m >= max_edges) {
        for(int i=0; i<n; ++i) {
            for(int j=i+1; j<n; ++j) {
                l.emplace_back(i, j);
            }
        }
        return;
    }

    // get distance between all points
    vector<double> xcoords(n, 0.0), ycoords(n, 0.0);
    for(int i=0; i<n; ++i) {
        xcoords[i] = runidist(0, 1, rng);
        ycoords[i] = runidist(0, 1, rng);
    }

    typedef std::tuple<double, int, int> diitrip;
    vector<diitrip> all_edges;
    all_edges.reserve(max_edges);
    for(int i=0; i<n; ++i) {
        for(int j=i+1; j<n; ++j) {
            diitrip t(norm(xcoords[i]-xcoords[j], ycoords[i]-ycoords[j]), i, j);
            all_edges.push_back(t);
        }
    }
    std::sort(all_edges.begin(), all_edges.end());

    // create MST
    DisjointSets ds(n);
    int n_components = n;
    ipvec edges2; vector<double> weights2;
    edges2.reserve(all_edges.size());
    weights2.reserve(all_edges.size());
    double weight_sum = 0;
    // all_edges2.reserve(all_edges.size());
    for(const diitrip& t: all_edges) {
        int i, j; double norm;
        std::tie(norm, i, j) = t;
        int i2 = ds.find(i), j2 = ds.find(j);
        if(i2 == j2) {
            double weight = std::exp(-norm/2);
            weight_sum += weight;
            edges2.emplace_back(i, j);
            weights2.push_back(weight);
        }
        else {
            n_components--;
            ds.unite(i, j);
            l.emplace_back(i, j);
        }
    }
    approx_m -= n-1;

    ivec indices;
    multi_sample(weights2, approx_m, indices, rng);
    for(int index: indices) {
        l.push_back(edges2[index]);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdx, %rbx
movl %edi, %r14d
movslq %ecx, %rax
xorl %ecx, %ecx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
xorl %edx, %edx
divq %rdi
cmpq $0x1, %rdx
adcq $0x0, %rdx
movq %rdx, 0x50(%rsp)
leal -0x1(%r14), %eax
movl %eax, 0x4(%rsp)
imull %r14d, %eax
pushq $0x2
popq %rdi
cltd
idivl %edi
cmpl %esi, %eax
jle 0x533f
movl %eax, %ebp
movl %esi, 0x2c(%rsp)
movslq %r14d, %r15
leaq 0xb8(%rsp), %r12
andq $0x0, (%r12)
leaq 0x58(%rsp), %rdi
leaq 0xa0(%rsp), %rcx
movq %r15, %rsi
movq %r12, %rdx
callq 0x5754
andq $0x0, (%r12)
leaq 0xa0(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rsi
callq 0x5754
xorl %r12d, %r12d
testl %r14d, %r14d
movl $0x0, %r13d
cmovgl %r14d, %r13d
leaq 0x50(%rsp), %r15
cmpq %r12, %r13
je 0x5385
movq %r15, %rdi
callq 0x56c1
movq 0x58(%rsp), %rax
movsd %xmm0, (%rax,%r12,8)
movq %r15, %rdi
callq 0x56c1
movq 0xa0(%rsp), %rax
movsd %xmm0, (%rax,%r12,8)
incq %r12
jmp 0x530c
leaq 0xb8(%rsp), %r15
leaq 0x58(%rsp), %r12
movl %ecx, 0xb8(%rsp)
cmpl %r14d, %ecx
jl 0x536f
jmp 0x5635
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x572e
movl 0x58(%rsp), %ecx
incl %ecx
movl %ecx, 0x58(%rsp)
cmpl %r14d, %ecx
jl 0x535d
movl 0xb8(%rsp), %ecx
incl %ecx
jmp 0x534c
movq %r13, 0x88(%rsp)
movq %rbx, 0x48(%rsp)
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movslq %ebp, %rsi
callq 0x57aa
movl 0x4(%rsp), %ebp
negq %rbp
pushq $0x1
popq %rbx
xorl %r13d, %r13d
leaq 0x10(%rsp), %r15
leaq 0xb8(%rsp), %r12
cmpq 0x88(%rsp), %r13
je 0x544c
movq %rbx, 0x90(%rsp)
leaq (%rbx,%rbp), %rax
cmpq $0x1, %rax
je 0x5439
movq 0x58(%rsp), %rax
movsd (%rax,%r13,8), %xmm0
subsd (%rax,%rbx,8), %xmm0
movq 0xa0(%rsp), %rax
movsd (%rax,%r13,8), %xmm1
subsd (%rax,%rbx,8), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
addsd %xmm1, %xmm0
movl %ebx, 0xb8(%rsp)
movl %r13d, 0xbc(%rsp)
movsd %xmm0, 0xc0(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x5836
incq %rbx
jmp 0x53d8
incq %r13
movq 0x90(%rsp), %rbx
incq %rbx
jmp 0x53c6
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x609b
leaq 0xb8(%rsp), %rdi
movl %r14d, %esi
callq 0x5854
xorpd %xmm0, %xmm0
leaq 0xf0(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movapd %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x4, %rsi
callq 0x589a
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x4, %rsi
leaq 0x70(%rsp), %rdi
callq 0x592e
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
leaq 0x8(%rsp), %rbx
leaq 0xc(%rsp), %r12
leaq 0xb8(%rsp), %rbp
cmpq %r15, %r14
je 0x558c
movq %rbx, 0x30(%rsp)
movq %r12, 0x38(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x59be
movl 0xc(%rsp), %esi
movq %rbp, %rdi
callq 0x59d4
movl %eax, %r13d
movl 0x8(%rsp), %esi
movq %rbp, %rdi
callq 0x59d4
cmpl %eax, %r13d
jne 0x5563
movsd 0x98(%rsp), %xmm0
mulsd 0x60bc(%rip), %xmm0     # 0xb5f0
callq 0x2200
movsd %xmm0, 0x30(%rsp)
leaq 0xf0(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x572e
leaq 0x70(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x5a02
jmp 0x5583
movl 0xc(%rsp), %esi
movl 0x8(%rsp), %edx
movq %rbp, %rdi
callq 0x5a24
movq 0x48(%rsp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x572e
addq $0x10, %r14
jmp 0x54d6
movl 0x2c(%rsp), %esi
subl 0x4(%rsp), %esi
xorpd %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movapd %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rcx
callq 0xa6a8
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %r14
movq 0x48(%rsp), %r15
cmpq %r14, %rbx
je 0x55e6
movslq (%rbx), %rsi
shlq $0x3, %rsi
addq 0xf0(%rsp), %rsi
movq %r15, %rdi
callq 0x5a6c
addq $0x4, %rbx
jmp 0x55c4
leaq 0x30(%rsp), %rdi
callq 0x3660
leaq 0x70(%rsp), %rdi
callq 0x5ea6
leaq 0xf0(%rsp), %rdi
callq 0x366e
leaq 0xb8(%rsp), %rdi
callq 0x5a8a
leaq 0x10(%rsp), %rdi
callq 0x5f40
leaq 0xa0(%rsp), %rdi
callq 0x5ea6
leaq 0x58(%rsp), %rdi
callq 0x5ea6
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5654
jmp 0x5695
movq %rax, %rbx
jmp 0x56af
jmp 0x5667
jmp 0x5695
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3660
jmp 0x566a
jmp 0x5667
jmp 0x5667
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x5ea6
leaq 0xf0(%rsp), %rdi
callq 0x366e
leaq 0xb8(%rsp), %rdi
callq 0x5a8a
jmp 0x5698
movq %rax, %rbx
jmp 0x56a2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5f40
leaq 0xa0(%rsp), %rdi
callq 0x5ea6
leaq 0x58(%rsp), %rdi
callq 0x5ea6
movq %rbx, %rdi
callq 0x21f0