// File: /workspace/github_cpp_makefile_star2_O2/sjain12[P]CliqueCounting/Graph.cpp
// Function: Escape::CGraph::renameByDegreeOrder() const
CGraph CGraph::renameByDegreeOrder() const
{
    CGraph ret = newCGraph(nVertices, nEdges);
    Pair *deg_info = new Pair[nVertices];

    VertexIdx *mapping = new VertexIdx[nVertices];
    VertexIdx *inverse = new VertexIdx[nVertices];

    
    // Construct array of pairs, storing old vertex label and degree
    for (VertexIdx i=0; i < nVertices; i++)
    {
        deg_info[i].first = i;
        deg_info[i].second = offsets[i+1]-offsets[i];
    }

    // sort the pairs by degree (if degree is same, sort by old vertex label)
    std::sort(deg_info,deg_info+nVertices,Escape::pairCompareSecond);

    // Construct the mapping of old vertex label to new vertex label
    // So mapping[i] is what i is mapped to
    // And inverse[i] is what maps to i
    for (VertexIdx i=0; i < nVertices; i++)
    {
        mapping[deg_info[i].first] = i;
        inverse[i] = deg_info[i].first;
    }

    // Initialize offsets of output CGraph
    ret.offsets[0] = 0;
    EdgeIdx current = 0;


    // Loop over new vertices
    for (VertexIdx new_label=0; new_label < nVertices; new_label++)
    {
        VertexIdx old_label = inverse[new_label]; // corresponding old label for new vertices
        for (EdgeIdx pos = offsets[old_label]; pos < offsets[old_label+1]; pos++) // loop over neighbors of old label
        {
            VertexIdx old_nbr = nbors[pos];
            VertexIdx new_nbr = mapping[old_nbr]; //corresponding new neighbor
            ret.nbors[current] = new_nbr; // insert new neighbor in nbors of output
            current++;
        }
        ret.offsets[new_label+1] = current; // all neighbors of new_label have been added, so we set offset for new_label+1
    }

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq (%rsi), %rsi
movq 0x8(%r14), %rdx
movq %rdi, 0x10(%rsp)
callq 0x6f96
movq (%r14), %rbp
movq %rbp, %rax
shrq $0x3c, %rax
movq %rbp, %rcx
shlq $0x4, %rcx
xorl %ebx, %ebx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
movq %rcx, 0x8(%rsp)
orq %rcx, %rdi
callq 0x3030
movq %rax, %r13
movq %rbp, %rax
shrq $0x3d, %rax
leaq (,%rbp,8), %rcx
negq %rax
movl $0x0, %r12d
sbbq %r12, %r12
orq %rcx, %r12
movq %r12, %rdi
callq 0x3030
movq %rax, %r15
movq %r12, %rdi
callq 0x3030
movq %rax, %r12
movq 0x10(%r14), %rax
testq %rbp, %rbp
cmovleq %rbx, %rbp
leaq 0x8(%r13), %rcx
cmpq %rbx, %rbp
je 0x7339
movq %rbx, -0x8(%rcx)
movq 0x8(%rax,%rbx,8), %rdx
subq (%rax,%rbx,8), %rdx
incq %rbx
movq %rdx, (%rcx)
addq $0x10, %rcx
jmp 0x731b
movq %r13, %rsi
addq 0x8(%rsp), %rsi
leaq -0x4f4(%rip), %rdx      # 0x6e54
movq %r13, %rdi
callq 0x7f03
movq (%r14), %rsi
xorl %ecx, %ecx
testq %rsi, %rsi
cmovleq %rcx, %rsi
movq 0x10(%rsp), %rax
cmpq %rcx, %rsi
je 0x737b
movq (%r13), %rdx
movq %rcx, (%r15,%rdx,8)
movq %rdx, (%r12,%rcx,8)
incq %rcx
addq $0x10, %r13
jmp 0x7361
movq 0x10(%rax), %rbx
andq $0x0, (%rbx)
movq 0x10(%r14), %rcx
movq 0x18(%rax), %rdx
xorl %esi, %esi
xorl %edi, %edi
cmpq (%r14), %rsi
jge 0x73c5
movq (%r12,%rsi,8), %r8
movq (%rcx,%r8,8), %r9
movq 0x18(%r14), %r10
cmpq 0x8(%rcx,%r8,8), %r9
jge 0x73bb
movq (%r10,%r9,8), %r11
movq (%r15,%r11,8), %r11
movq %r11, (%rdx,%rdi,8)
incq %rdi
incq %r9
jmp 0x73a0
movq %rdi, 0x8(%rbx,%rsi,8)
incq %rsi
jmp 0x738f
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq