// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, %rbx
movq %rdi, 0x28(%rsp)
movq 0x8(%rsi), %rsi
leaq 0x895a(%rip), %rdi      # 0xc090
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x3050
movq (%rbx), %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, 0x8(%rsp)
movq 0x8(%rbx), %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
movq %rbx, %r13
callq 0x3030
movq %rax, %rbx
movq %rbp, %rdi
movq %r15, %rbp
callq 0x3030
movq %rax, 0x18(%rsp)
movq 0x8(%rsp), %rax
movq %r14, (%rax)
movq %rbx, 0x10(%rsp)
movq %r14, (%rbx)
movl $0x0, %r15d
testq %rbp, %rbp
jle 0x3860
movq 0x10(%r13), %rax
xorl %esi, %esi
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ecx, %ecx
xorl %edx, %edx
movq (%rax,%rsi,8), %r8
movq 0x8(%rax,%rsi,8), %r10
leaq 0x1(%rsi), %rdi
cmpq %r10, %r8
jge 0x3846
movq 0x18(%r13), %r9
movq (%r9,%r8,8), %r11
subq (%rax,%rsi,8), %r10
movq 0x8(%rax,%r11,8), %rbx
subq (%rax,%r11,8), %rbx
cmpq %rbx, %r10
jl 0x381f
cmpq %r11, %rsi
jge 0x382b
cmpq %rbx, %r10
jne 0x382b
movq %r11, (%r12,%rcx,8)
incq %rcx
incq %r15
jmp 0x383a
movq 0x18(%rsp), %r10
movq %r11, (%r10,%rdx,8)
incq %rdx
incq %r14
incq %r8
movq (%rax,%rdi,8), %r10
cmpq %r10, %r8
jl 0x37ff
movq 0x8(%rsp), %rsi
movq %rcx, (%rsi,%rdi,8)
movq 0x10(%rsp), %rsi
movq %rdx, (%rsi,%rdi,8)
movq %rdi, %rsi
cmpq %rbp, %rdi
jne 0x37e9
movq %rbp, 0x20(%rsp)
cmpq $0x0, (%r13)
jle 0x38de
xorl %ebp, %ebp
movq %r14, 0x30(%rsp)
movq 0x8(%rsp), %rax
movq (%rax,%rbp,8), %rcx
movq 0x8(%rax,%rbp,8), %rax
cmpq %rax, %rcx
je 0x38d5
leaq (%r12,%rax,8), %rsi
shlq $0x3, %rax
leaq (,%rcx,8), %rdx
subq %rdx, %rax
movq %r13, %rbx
leaq (%r12,%rcx,8), %r13
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %rsi, %r14
movq %rbx, %rcx
callq 0x744c
movq %r13, %rdi
movq %rbx, %r13
movq %r14, %rsi
movq 0x30(%rsp), %r14
movq %rbx, %rdx
callq 0x7513
incq %rbp
cmpq (%r13), %rbp
jl 0x3873
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
movq %r15, 0x8(%rax)
movq 0x8(%rsp), %rdx
movq %rdx, 0x10(%rax)
movq %r12, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq %r14, 0x28(%rax)
movq 0x10(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq 0x18(%rsp), %rcx
movq %rcx, 0x38(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq