// File: /workspace/github_cpp_makefile_star2_O1/ekg[P]sgd2/src/sgd2.hpp
// Function: sgd2::dijkstra(unsigned long, unsigned long, unsigned long*, unsigned long*, double*)
std::vector<term> dijkstra(uint64_t n, uint64_t m, uint64_t* I, uint64_t* J, double* V)
{
    auto graph = build_graph_weighted(n, m, I, J, V);

    uint64_t 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<bool> visited(n, false);
        std::vector<double> d(n, std::numeric_limits<double>::max()); // init 'tentative' distances to infinity

        // I am not using a fibonacci heap. I AM NOT USING A FIBONACCI HEAP
        // edges are used 'invisibly' here
        std::priority_queue<edge, std::vector<edge>, edge_comp> pq;

        d[source] = 0;
        pq.push(edge(source,0));

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

        while (!pq.empty() && terms.size() <= terms_size_goal)
        {
            uint64_t current = pq.top().target;
            double d_ij = pq.top().weight;
            pq.pop();
            
            if (!visited[current]) // ignore redundant elements in queue
            {
                visited[current] = true;

                if (source < current) // only add terms for i<j
                {
                    double w_ij = 1.0 / (d_ij*d_ij);
                    terms.push_back(term(source, current, d_ij, w_ij));
                }
                for (edge e : graph[current])
                {
                    // here the edge is not 'invisible'
                    uint64_t next = e.target;
                    double weight = e.weight;

                    if (d_ij + weight < d[next]) // update tentative value of d 
                    {
                        d[next] = d_ij + weight;
                        pq.push(edge(next, d[next]));
                    }
                }
            }
        }
        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 0x2e84
leaq -0x1(%r14), %r15
movq %r15, %rsi
movq %r14, -0x38(%rbp)
imulq %r14, %rsi
shrq %rsi
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq %rbx, %rdi
callq 0x7b74
movq %r15, -0xd8(%rbp)
testq %r15, %r15
movq -0x38(%rbp), %rsi
je 0x346f
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
leaq -0x90(%rbp), %r12
leaq -0xb8(%rbp), %r13
addq $0x3f, %rax
movq %rax, -0xe0(%rbp)
movq $0x0, -0x30(%rbp)
xorl %r14d, %r14d
movq %r14, -0x40(%rbp)
movb $0x0, -0x90(%rbp)
leaq -0x68(%rbp), %rdi
movq %r12, %rdx
movq %r13, %rcx
movq %rsi, %r14
callq 0x7e9e
movabsq $0x7fefffffffffffff, %rax # imm = 0x7FEFFFFFFFFFFFFF
movq %rax, -0x90(%rbp)
leaq -0xd0(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x7f04
xorl %ecx, %ecx
movq %rcx, -0x80(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x90(%rbp)
movq -0xd0(%rbp), %rax
movq -0x40(%rbp), %r14
movq %rcx, (%rax,%r14,8)
movq %r14, -0xb8(%rbp)
movq %rcx, -0xb0(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x7f90
movq %r14, %rax
notq %rax
movq -0x30(%rbp), %rcx
addq -0x38(%rbp), %rcx
addq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq -0x90(%rbp), %rdi
cmpq -0x88(%rbp), %rdi
je 0x33ed
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x5, %rax
cmpq -0x30(%rbp), %rax
ja 0x33ed
movq (%rdi), %r15
movsd 0x8(%rdi), %xmm0
movsd %xmm0, -0x98(%rbp)
movq %r12, %rdi
callq 0x8036
leaq 0x3f(%r15), %rax
testq %r15, %r15
cmovnsq %r15, %rax
sarq $0x6, %rax
shlq $0x3, %rax
addq -0x68(%rbp), %rax
movq %r15, %rcx
andq -0xe0(%rbp), %rcx
xorl %edx, %edx
movabsq $-0x8000000000000000, %rsi # imm = 0x8000000000000000
cmpq %rsi, %rcx
setbe %dl
movl $0x1, %esi
movl %r15d, %ecx
shlq %cl, %rsi
movq -0x8(%rax,%rdx,8), %rcx
btq %r15, %rcx
jb 0x326b
leaq -0x8(,%rdx,8), %rdx
orq %rsi, %rcx
movq %rcx, (%rax,%rdx)
movq -0x40(%rbp), %rax
cmpq %r15, %rax
jae 0x3383
movsd -0x98(%rbp), %xmm2
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd 0x7cdf(%rip), %xmm1     # 0xb008
divsd %xmm0, %xmm1
movq %rax, -0xb8(%rbp)
movq %r15, -0xb0(%rbp)
movsd %xmm2, -0xa8(%rbp)
movsd %xmm1, -0xa0(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x3378
movupd -0xb8(%rbp), %xmm0
movupd -0xa8(%rbp), %xmm1
movupd %xmm1, 0x10(%rsi)
movupd %xmm0, (%rsi)
addq $0x20, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x3383
movq %rbx, %rdi
movq %r13, %rdx
callq 0x9286
movq -0xf8(%rbp), %rax
leaq (%r15,%r15,2), %rcx
movq (%rax,%rcx,8), %r15
movq 0x8(%rax,%rcx,8), %r14
cmpq %r14, %r15
je 0x326b
movq (%r15), %rax
movsd 0x8(%r15), %xmm0
addsd -0x98(%rbp), %xmm0
movq -0xd0(%rbp), %rcx
movsd (%rcx,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x33e7
movsd %xmm0, (%rcx,%rax,8)
movsd (%rcx,%rax,8), %xmm0
movq %rax, -0xb8(%rbp)
movsd %xmm0, -0xb0(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x7f90
addq $0x10, %r15
jmp 0x3397
movq 0x8(%rbx), %rax
subq (%rbx), %rax
sarq $0x5, %rax
cmpq -0x30(%rbp), %rax
jne 0x3490
testq %rdi, %rdi
je 0x3413
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
movq -0x40(%rbp), %r14
je 0x3432
movq -0xc0(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x345b
movq -0x48(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
xorl %eax, %eax
movq %rax, -0x68(%rbp)
movl %eax, -0x60(%rbp)
movq %rax, -0x58(%rbp)
movl %eax, -0x50(%rbp)
movq %rax, -0x48(%rbp)
incq %r14
cmpq -0xd8(%rbp), %r14
movq -0x38(%rbp), %rsi
jne 0x31d9
leaq -0xf8(%rbp), %rdi
callq 0x7df2
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 0x7c20(%rip), %rcx      # 0xb0c1
movq %rcx, (%rax)
movq 0xbb15(%rip), %rsi      # 0xefc0
movq %rax, %rdi
xorl %edx, %edx
callq 0x2230
jmp 0x34bb
jmp 0x34c9
jmp 0x34c9
movq %rax, %r14
jmp 0x3528
movq %rax, %r14
jmp 0x34ff
jmp 0x34c9
jmp 0x34c9
movq %rax, %r14
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x34e4
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x34ff
movq -0xc0(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x3528
movq -0x48(%rbp), %rsi
subq %rdi, %rsi
callq 0x2180
xorl %eax, %eax
movq %rax, -0x68(%rbp)
movl %eax, -0x60(%rbp)
movq %rax, -0x58(%rbp)
movl %eax, -0x50(%rbp)
movq %rax, -0x48(%rbp)
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x353c
movq 0x10(%rbx), %rsi
subq %rdi, %rsi
callq 0x2180
leaq -0xf8(%rbp), %rdi
callq 0x7df2
movq %r14, %rdi
callq 0x2240