// File: /workspace/github_cpp_makefile_star2_O1/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 0x28d3
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 0x7b74
movq $0x0, -0x30(%rbp)
testq %r15, %r15
je 0x2de0
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 0x7c2e
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 0x8eec
movq -0x30(%rbp), %rax
movq -0x58(%rbp), %rcx
movq $0x0, (%rcx,%rax,8)
movq -0x80(%rbp), %rax
movq -0x70(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2bd1
movq -0x30(%rbp), %rcx
movq %rcx, (%rax)
addq $0x8, %rax
movq %rax, -0x80(%rbp)
jmp 0x2bdd
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x9074
movq -0x30(%rbp), %rax
notq %rax
movq -0x38(%rbp), %rcx
addq %r14, %rcx
addq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq -0xa0(%rbp), %rax
cmpq %rax, -0x80(%rbp)
je 0x2d90
movq 0x8(%rbx), %rcx
subq (%rbx), %rcx
sarq $0x5, %rcx
cmpq -0x38(%rbp), %rcx
ja 0x2d90
movq (%rax), %r13
movq -0x90(%rbp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x2c31
addq $0x8, %rax
jmp 0x2c6d
movq -0x98(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x2180
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), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movq (%rax,%rcx,8), %r15
movq 0x8(%rax,%rcx,8), %r14
cmpq %r14, %r15
je 0x2bf2
movq (%r15), %rax
movq %rax, -0x40(%rbp)
movq -0x58(%rbp), %rcx
cmpq $-0x1, (%rcx,%rax,8)
jne 0x2d87
movq -0x80(%rbp), %rcx
movq -0x70(%rbp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x2ccc
movq %rax, (%rcx)
addq $0x8, %rcx
movq %rcx, -0x80(%rbp)
jmp 0x2cd8
movq %r12, %rdi
leaq -0x40(%rbp), %rsi
callq 0x9074
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 0x2d87
movq %rdx, %xmm0
punpckldq 0x8357(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x835f(%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 0x82c9(%rip), %xmm0     # 0xb008
divsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x2d78
movupd -0xe0(%rbp), %xmm0
movupd -0xd0(%rbp), %xmm1
movupd %xmm1, 0x10(%rsi)
movupd %xmm0, (%rsi)
addq $0x20, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x2d87
movq %rbx, %rdi
leaq -0xe0(%rbp), %rdx
callq 0x9286
addq $0x8, %r15
jmp 0x2c8f
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x5, %rax
cmpq -0x38(%rbp), %rax
jne 0x2e01
movq %r12, %rdi
callq 0x84d6
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
movq -0xc0(%rbp), %r14
movq -0xb8(%rbp), %r15
je 0x2dcc
movq -0x48(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq -0x30(%rbp), %rax
incq %rax
movq %rax, -0x30(%rbp)
cmpq %r15, %rax
jb 0x2b4e
leaq -0xf8(%rbp), %rdi
callq 0x7ac8
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 0x82af(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xc1a4(%rip), %rsi      # 0xefc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2230
jmp 0x2e2c
jmp 0x2e38
jmp 0x2e38
movq %rax, %r14
jmp 0x2e5c
movq %rax, %r14
jmp 0x2e47
jmp 0x2e38
movq %rax, %r14
leaq -0xb0(%rbp), %rdi
callq 0x84d6
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x2e5c
movq -0x48(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2e70
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2180
leaq -0xf8(%rbp), %rdi
callq 0x7ac8
movq %r14, %rdi
callq 0x2240