// File: /workspace/github_cpp_makefile_star2_O3/ekg[P]sgd2/src/sgd2.hpp
// Function: sgd2::bfs(unsigned long, unsigned long, unsigned long*, unsigned long*)
std::vector<term> bfs(uint64_t n, uint64_t m, uint64_t* I, uint64_t* J)
{
    auto graph = build_graph_unweighted(n, m, I, J);
        
    uint64_t nC2;
    if (n == 1) {
        nC2 = 1;
    } else {
        nC2 = (n*(n-1))/2;
    }
    std::vector<term> terms;
    terms.reserve(nC2);

    uint64_t terms_size_goal = 0; // to keep track of when to stop searching i<j

    for (uint64_t source=0; source<n-1; source++) // no need to do final vertex because i<j
    {
        std::vector<uint64_t> d(n, -1); // distances from source
        std::queue<uint64_t> q;
        
        d[source] = 0;
        q.push(source);

        terms_size_goal += n-source-1; // this is how many terms exist for i<j

        while (!q.empty() && terms.size() <= terms_size_goal)
        {
            uint64_t current = q.front();
            q.pop();
            for (uint64_t next : graph[current])
            {
                if (d[next] == -1)
                {
                    q.push(next);
                    uint64_t d_ij = d[current] + 1;
                    d[next] = d_ij;

                    if (source < next) // only add terms for i<j
                    {
                        double w_ij = 1.0 / ((double)d_ij*d_ij);
                        terms.push_back(term(source, next, d_ij, w_ij));
                    }
                }
            }
        }
        if (terms.size() != terms_size_goal)
        {
            throw "graph is not strongly connected, or is not indexed from zero";
        }
    }
    return terms;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x2839
leaq -0x1(%r14), %r15
movq %r15, %rsi
imulq %r14, %rsi
shrq %rsi
cmpq $0x1, %r14
cmoveq %r14, %rsi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq %rbx, %rdi
callq 0x744e
movq $0x0, -0x30(%rbp)
testq %r15, %r15
je 0x2d0a
movq $0x0, -0x38(%rbp)
leaq -0xb0(%rbp), %r12
movq %r15, -0xb8(%rbp)
movq %r14, -0xc0(%rbp)
movq $-0x1, -0xb0(%rbp)
leaq -0x58(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
leaq -0xe0(%rbp), %rcx
callq 0x7508
xorpd %xmm0, %xmm0
movapd %xmm0, -0x70(%rbp)
movapd %xmm0, -0x80(%rbp)
movapd %xmm0, -0x90(%rbp)
movapd %xmm0, -0xa0(%rbp)
movapd %xmm0, -0xb0(%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x8802
movq -0x30(%rbp), %rax
movq -0x58(%rbp), %rcx
movq $0x0, (%rcx,%rax,8)
movq -0x80(%rbp), %rcx
movq -0x70(%rbp), %rax
addq $-0x8, %rax
cmpq %rax, %rcx
je 0x2ae3
movq -0x30(%rbp), %rax
movq %rax, (%rcx)
addq $0x8, %rcx
movq %rcx, -0x80(%rbp)
jmp 0x2af7
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x898a
movq -0x30(%rbp), %rax
movq -0x80(%rbp), %rcx
notq %rax
movq -0x38(%rbp), %rdx
addq %r14, %rdx
addq %rax, %rdx
movq %rdx, -0x38(%rbp)
movq -0xa0(%rbp), %rax
cmpq %rax, %rcx
je 0x2cba
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
sarq $0x5, %rcx
cmpq -0x38(%rbp), %rcx
ja 0x2cba
movq (%rax), %r13
movq -0x90(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2b46
addq $0x8, %rax
jmp 0x2b82
movq -0x98(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x2170
movq -0x88(%rbp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, -0x88(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
leaq 0x200(%rax), %rcx
movq %rcx, -0x90(%rbp)
movq %rax, -0xa0(%rbp)
movq -0xf8(%rbp), %rcx
leaq (,%r13,2), %rdx
addq %r13, %rdx
movq (%rcx,%rdx,8), %r15
movq 0x8(%rcx,%rdx,8), %r14
cmpq %r14, %r15
je 0x2cb0
movq (%r15), %rcx
movq %rcx, -0x40(%rbp)
movq -0x58(%rbp), %rax
cmpq $-0x1, (%rax,%rcx,8)
jne 0x2c9c
movq -0x80(%rbp), %rdx
movq -0x70(%rbp), %rsi
addq $-0x8, %rsi
cmpq %rsi, %rdx
je 0x2be1
movq %rcx, (%rdx)
addq $0x8, %rdx
movq %rdx, -0x80(%rbp)
jmp 0x2bf1
movq %r12, %rdi
leaq -0x40(%rbp), %rsi
callq 0x898a
movq -0x58(%rbp), %rax
movq (%rax,%r13,8), %rdx
incq %rdx
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq %rcx, %rax
jae 0x2c9c
movq %rdx, %xmm0
punpckldq 0x8442(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x844a(%rip), %xmm0     # 0xb070
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq %rax, -0xe0(%rbp)
movq %rcx, -0xd8(%rbp)
movsd %xmm1, -0xd0(%rbp)
mulsd %xmm1, %xmm1
movsd 0x83b4(%rip), %xmm0     # 0xb008
divsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x2c8d
movupd -0xe0(%rbp), %xmm0
movupd -0xd0(%rbp), %xmm1
movupd %xmm1, 0x10(%rsi)
movupd %xmm0, (%rsi)
addq $0x20, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x2c9c
movq %rbx, %rdi
leaq -0xe0(%rbp), %rdx
callq 0x8b9c
addq $0x8, %r15
cmpq %r14, %r15
jne 0x2bad
movq -0xa0(%rbp), %rax
cmpq %rax, -0x80(%rbp)
jne 0x2b18
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x5, %rax
cmpq -0x38(%rbp), %rax
jne 0x2d2b
movq %r12, %rdi
callq 0x7dae
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
movq -0xc0(%rbp), %r14
movq -0xb8(%rbp), %r15
je 0x2cf6
movq -0x48(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq -0x30(%rbp), %rax
incq %rax
movq %rax, -0x30(%rbp)
cmpq %r15, %rax
jb 0x2a60
leaq -0xf8(%rbp), %rdi
callq 0x7396
movq %rbx, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x8, %edi
callq 0x2080
leaq 0x8385(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xc27a(%rip), %rsi      # 0xefc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2220
jmp 0x2d56
jmp 0x2d62
jmp 0x2d62
movq %rax, %r14
jmp 0x2d86
movq %rax, %r14
jmp 0x2d71
jmp 0x2d62
movq %rax, %r14
leaq -0xb0(%rbp), %rdi
callq 0x7dae
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x2d86
movq -0x48(%rbp), %rsi
subq %rdi, %rsi
callq 0x2170
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2d9a
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2170
leaq -0xf8(%rbp), %rdi
callq 0x7396
movq %r14, %rdi
callq 0x2230