// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movq 0x8(%rsi), %rsi
leaq 0x896c(%rip), %rdi      # 0xc0a0
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x3050
movq (%r15), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x1(%rcx), %rax
shrq $0x3d, %rax
leaq 0x8(,%rcx,8), %rcx
negq %rax
movl $0x0, %r13d
sbbq %r13, %r13
orq %rcx, %r13
movq %r13, %rdi
callq 0x3030
movq %rax, 0x8(%rsp)
movq %r15, 0x20(%rsp)
movq 0x8(%r15), %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, %r15
movq %rbp, %rdi
movq 0x18(%rsp), %r13
callq 0x3030
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rax
movq %r14, (%rax)
movq %r15, 0x28(%rsp)
movq %r14, (%r15)
movl $0x0, %r15d
testq %r13, %r13
jle 0x3908
movq %rbx, 0x10(%rsp)
movq 0x20(%rsp), %rbp
movq 0x10(%rbp), %rax
movq (%rax), %rdi
xorl %ecx, %ecx
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %edx, %edx
xorl %esi, %esi
movq 0x8(%rax,%rcx,8), %r8
cmpq %r8, %rdi
jge 0x3851
movq 0x18(%rbp), %r9
movq %r8, %r10
subq (%rax,%rcx,8), %r10
movq (%r9,%rdi,8), %r11
movq 0x8(%rax,%r11,8), %rbx
subq (%rax,%r11,8), %rbx
cmpq %rbx, %r10
jl 0x382e
cmpq %r11, %rcx
jge 0x383a
cmpq %rbx, %r10
jne 0x383a
movq %r11, (%r12,%rdx,8)
incq %rdx
incq %r14
jmp 0x3849
movq 0x30(%rsp), %rbx
movq %r11, (%rbx,%rsi,8)
incq %rsi
incq %r15
incq %rdi
cmpq %r8, %rdi
jl 0x3812
incq %rcx
movq 0x8(%rsp), %rdi
movq %rdx, (%rdi,%rcx,8)
movq 0x28(%rsp), %rdi
movq %rsi, (%rdi,%rcx,8)
movq %r8, %rdi
cmpq %r13, %rcx
jne 0x37fd
testq %r13, %r13
jle 0x3903
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %r13, %rcx
xorl %ebp, %ebp
movq 0x20(%rsp), %r13
movq 0x8(%rsp), %rdx
movq 0x8(%rdx,%rbp,8), %rbx
cmpq %rbx, %rax
je 0x38ec
leaq (,%rbx,8), %rcx
leaq (%r12,%rbx,8), %rsi
movq %rsi, 0x40(%rsp)
leaq (,%rax,8), %rdx
subq %rdx, %rcx
leaq (%r12,%rax,8), %rdi
movq %rdi, 0x38(%rsp)
sarq $0x3, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rcx
callq 0x73b5
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq %r13, %rdx
callq 0x747c
movq (%r13), %rcx
incq %rbp
movq %rbx, %rax
cmpq %rcx, %rbp
jl 0x3889
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r13
jmp 0x3908
movq 0x10(%rsp), %rbx
movq %r13, (%rbx)
movq %r14, 0x8(%rbx)
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rbx)
movq %r12, 0x18(%rbx)
movq %r13, 0x20(%rbx)
movq %r15, 0x28(%rbx)
movq 0x28(%rsp), %rax
movq %rax, 0x30(%rbx)
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rbx)
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq