// File: /workspace/github_cpp_makefile_star2_O2/sjain12[P]CliqueCounting/tests/../Escape/Digraph.h
// Function: degreeOrdered(Escape::CGraph*)
CDAG degreeOrdered(CGraph *g)
{
    CDAG ret;     // CDAG to be returned
    printf("In degreeOrdered. number of edges= %ld", g->nEdges);
    CGraph outdag = {g->nVertices, 0, new EdgeIdx[g->nVertices+1], new VertexIdx[g->nEdges+1]};  // Initialize DAG of out-edges
    CGraph indag = {g->nVertices, 0, new EdgeIdx[g->nVertices+1], new VertexIdx[g->nEdges+1]};   // Initialize DAG of in-edges
    EdgeIdx outcur = 0;
    EdgeIdx incur = 0;
    VertexIdx dest;
    VertexIdx degi;
    VertexIdx degdest;

    outdag.offsets[0] = 0;
    indag.offsets[0] = 0;
    for (VertexIdx i=0; i < g->nVertices; ++i)   // Looping over all vertices in g
    {
        for (EdgeIdx j = g->offsets[i]; j < g->offsets[i+1]; ++j)   // Looping over neighbors of i in g
        {
            dest = g->nbors[j];     // We are now looking at edge (i,dest)
            degi = g->offsets[i+1] - g->offsets[i];   // Degree of i
            degdest = g->offsets[dest+1]- g->offsets[dest];   // Degree of dest
            //printf("i=%ld dest=%ld degi=%ld degdest=%ld\n",i,dest,degi,degdest);

            //We now orient the edge depending of degi vs degdest.
            // We break ties according to vertex id.
            // In the output, the g-edge (i,dest) is either pointing to dest (in if condition) or pointing to i (in else condition).

            if (degi < degdest || (degi == degdest && i < dest))   
            {
                outdag.nbors[outcur] = dest;   // We want point edge from i to dest. So this directed edge is added to outdag.
                ++outcur;                      // Increment pointer in outdag.nbors and the number of edges in outdag.
                ++outdag.nEdges;
            }
            else
            {
                indag.nbors[incur] = dest;     // We point edge from dest to i. So this edge goes into indag.
                ++incur;                       // Pointer and number of edges incremented
                ++indag.nEdges;
            }
        }
        outdag.offsets[i+1] = outcur;         // We have finished all edges incident to i, so we can update offsets in DAGs.
        indag.offsets[i+1] = incur;
    }

    for (VertexIdx i=0; i < g->nVertices;++i)  // Loops over vertices
        std::sort(outdag.nbors+outdag.offsets[i], outdag.nbors+outdag.offsets[i+1], DegreeComp(g)); // In outdag, sort all neighbors of i according to their degree. Note that DegreeComp gives the desired comparator.

    ret.outlist = outdag;
    ret.inlist = indag;

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
movq 0x8(%rsi), %rsi
leaq 0x79ca(%rip), %rdi      # 0xb080
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x3040
movq (%r14), %r15
leaq 0x1(%r15), %rax
shrq $0x3d, %rax
leaq 0x8(,%r15,8), %rcx
negq %rax
movl $0x0, %r13d
sbbq %r13, %r13
orq %rcx, %r13
movq %r13, %rdi
callq 0x3030
movq %rax, (%rsp)
movq 0x8(%r14), %rax
leaq 0x1(%rax), %rcx
shrq $0x3d, %rcx
leaq 0x8(,%rax,8), %rax
negq %rcx
movl $0x0, %ebp
sbbq %rbp, %rbp
orq %rax, %rbp
movq %rbp, %rdi
callq 0x3030
movq %rax, %r12
movq %r13, %rdi
callq 0x3030
movq %rax, %r13
movq %rbp, %rdi
callq 0x3030
movq (%rsp), %rcx
andq $0x0, (%rcx)
movq %rax, %rbp
movq %r13, 0x10(%rsp)
andq $0x0, (%r13)
testq %r15, %r15
movl $0x0, %eax
movq %r15, 0x8(%rsp)
cmovgq %r15, %rax
movq %rax, 0x20(%rsp)
xorl %ecx, %ecx
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %edx, %edx
cmpq 0x20(%rsp), %rdx
je 0x37d7
movq 0x10(%r14), %rdi
movq (%rdi,%rdx,8), %r8
movq 0x8(%rdi,%rdx,8), %r9
leaq 0x1(%rdx), %rsi
movq %r9, %r10
subq %r8, %r10
cmpq %r9, %r8
jge 0x37c1
movq 0x18(%r14), %rax
movq (%rax,%r8,8), %r11
movq 0x8(%rdi,%r11,8), %rax
subq (%rdi,%r11,8), %rax
cmpq %rax, %r10
jl 0x37a5
cmpq %r11, %rdx
jge 0x37b1
cmpq %rax, %r10
jne 0x37b1
movq %r11, (%r12,%rcx,8)
incq %rcx
incq %r15
jmp 0x37bc
movq %r11, (%rbp,%rbx,8)
incq %rbx
incq %r13
incq %r8
jmp 0x3780
movq (%rsp), %rax
movq %rcx, (%rax,%rsi,8)
movq 0x10(%rsp), %rax
movq %rbx, (%rax,%rsi,8)
movq %rsi, %rdx
jmp 0x3762
xorl %ebx, %ebx
movq 0x8(%rsp), %rax
cmpq %rax, %rbx
jge 0x3808
movq (%rsp), %rcx
movq (%rcx,%rbx,8), %rax
movq 0x8(%rcx,%rbx,8), %rcx
leaq (%r12,%rax,8), %rdi
incq %rbx
leaq (%r12,%rcx,8), %rsi
movq %r14, %rdx
callq 0x5b83
movq (%r14), %rax
jmp 0x37de
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
movq %r15, 0x8(%rax)
movq (%rsp), %rdx
movq %rdx, 0x10(%rax)
movq %r12, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq %r13, 0x28(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq %rbp, 0x38(%rax)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq