// File: /workspace/github_cpp_makefile_star2_O2/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 -0xa8(%rbp), %rdi
callq 0x2528
leaq -0x1(%r14), %rsi
movq %rsi, -0x58(%rbp)
imulq %r14, %rsi
shrq %rsi
cmpq $0x1, %r14
cmoveq %r14, %rsi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
callq 0x5e8e
xorl %eax, %eax
leaq -0x70(%rbp), %rbx
leaq -0xf8(%rbp), %r12
leaq -0x48(%rbp), %r15
movq $0x0, -0x38(%rbp)
movq %r14, -0x50(%rbp)
movq %rax, -0x30(%rbp)
cmpq -0x58(%rbp), %rax
jae 0x285e
orq $-0x1, -0xf8(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
leaq -0x90(%rbp), %rcx
callq 0x5f48
movq %r12, %rdi
callq 0x5fa8
movq -0x30(%rbp), %rax
movq -0x70(%rbp), %rcx
andq $0x0, (%rcx,%rax,8)
movq %r12, %rdi
leaq -0x30(%rbp), %rsi
callq 0x7ad8
movq -0x30(%rbp), %rax
notq %rax
movq -0x38(%rbp), %rcx
addq %r14, %rcx
addq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
movq -0xe8(%rbp), %rcx
sarq $0x5, %rax
cmpq %rcx, -0xc8(%rbp)
je 0x2834
cmpq -0x38(%rbp), %rax
ja 0x2834
movq (%rcx), %r14
movq %r12, %rdi
callq 0x7d34
movq -0xa8(%rbp), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %r13
movq 0x8(%rax,%rcx), %rbx
cmpq %rbx, %r13
je 0x273f
movq (%r13), %rax
movq %rax, -0x48(%rbp)
movq -0x70(%rbp), %rcx
cmpq $-0x1, (%rcx,%rax,8)
jne 0x282b
movq %r12, %rdi
movq %r15, %rsi
callq 0x7ad8
movq -0x70(%rbp), %rax
movq (%rax,%r14,8), %rdx
incq %rdx
movq -0x48(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
cmpq %rcx, %rax
jae 0x282b
movq %rdx, %xmm0
punpckldq 0x8881(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x8889(%rip), %xmm0     # 0xb070
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq %rax, -0x90(%rbp)
movq %rcx, -0x88(%rbp)
movsd %xmm1, -0x80(%rbp)
mulsd %xmm1, %xmm1
movsd 0x87f6(%rip), %xmm0     # 0xb008
divsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x7d9c
addq $0x8, %r13
jmp 0x278b
cmpq -0x38(%rbp), %rax
jne 0x2880
movq %r12, %rdi
callq 0x66ee
leaq -0x70(%rbp), %rbx
movq %rbx, %rdi
callq 0x7894
movq -0x30(%rbp), %rax
incq %rax
movq -0x50(%rbp), %r14
jmp 0x26de
leaq -0xa8(%rbp), %rdi
callq 0x5e36
movq -0x40(%rbp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x2090
leaq 0x8832(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xe727(%rip), %rsi      # 0x10fc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2200
jmp 0x28a7
jmp 0x28b5
movq %rax, %r14
jmp 0x28cd
movq %rax, %r14
jmp 0x28c4
jmp 0x28b5
jmp 0x28b5
movq %rax, %r14
leaq -0xf8(%rbp), %rdi
callq 0x66ee
leaq -0x70(%rbp), %rdi
callq 0x7894
movq -0x40(%rbp), %rdi
callq 0x77d2
leaq -0xa8(%rbp), %rdi
callq 0x5e36
movq %r14, %rdi
callq 0x2210