// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0xa0(%rbp)
movq %rdi, -0x98(%rbp)
movq %rsi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x15894(%rip), %rdi     # 0x190f0
movb $0x0, %al
callq 0x3050
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq $0x1, %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x1, %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
addq $0x1, %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x1, %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x30(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x38(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x3acd
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x78(%rbp), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jge 0x3a9a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x80(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x78(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x78(%rbp), %rdx
subq (%rcx,%rdx,8), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rax
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x60(%rbp), %rdx
subq (%rcx,%rdx,8), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jl 0x3a35
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jne 0x3a5f
movq -0x78(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x3a5f
movq -0x60(%rbp), %rdx
movq -0x10(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x3a87
movq -0x60(%rbp), %rdx
movq -0x30(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x3a89
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x399e
movq -0x50(%rbp), %rdx
movq -0x18(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x58(%rbp), %rdx
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x3979
movq $0x0, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x3b70
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x88(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq -0x88(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x7170
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0x90(%rbp), %rdx
callq 0x7120
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x3ad8
movq -0x98(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq -0x28(%rbp), %rdx
movq %rdx, (%rcx)
movq -0x20(%rbp), %rdx
movq %rdx, 0x8(%rcx)
movq -0x18(%rbp), %rdx
movq %rdx, 0x10(%rcx)
movq -0x10(%rbp), %rdx
movq %rdx, 0x18(%rcx)
movq -0x48(%rbp), %rdx
movq %rdx, 0x20(%rcx)
movq -0x40(%rbp), %rdx
movq %rdx, 0x28(%rcx)
movq -0x38(%rbp), %rdx
movq %rdx, 0x30(%rcx)
movq -0x30(%rbp), %rdx
movq %rdx, 0x38(%rcx)
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)