// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x78(%rbp)
movq %rdi, %rax
movq %rax, -0x70(%rbp)
movq %rsi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
callq 0xd820
movq -0x68(%rbp), %rax
movq (%rax), %rax
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x10(%rbp)
movq (%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, -0x18(%rbp)
movq (%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3030
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x68(%rbp), %rcx
movq -0x28(%rbp), %rax
cmpq (%rcx), %rax
jge 0xdfb1
movq -0x68(%rbp), %rax
movq -0x28(%rbp), %rdx
movq -0x10(%rbp), %rcx
movq -0x28(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rcx
movq %rdx, (%rcx)
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rdx
movq 0x8(%rcx,%rdx,8), %rcx
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rdx
subq (%rax,%rdx,8), %rcx
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xdf50
movq -0x68(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq (%rax), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq -0x9fe(%rip), %rdx      # 0xd5d0
callq 0xe960
movq $0x0, -0x30(%rbp)
movq -0x68(%rbp), %rcx
movq -0x30(%rbp), %rax
cmpq (%rcx), %rax
jge 0xe032
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rcx
movq (%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xdfdb
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rax
movq $0x0, (%rax)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x68(%rbp), %rcx
movq -0x40(%rbp), %rax
cmpq (%rcx), %rax
jge 0xe115
movq -0x68(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x48(%rbp)
movq 0x10(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x68(%rbp), %rcx
movq -0x50(%rbp), %rax
movq 0x10(%rcx), %rcx
movq -0x48(%rbp), %rdx
cmpq 0x8(%rcx,%rdx,8), %rax
jge 0xe0ef
movq -0x78(%rbp), %rax
movq -0x68(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x50(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x58(%rbp)
movq -0x18(%rbp), %rcx
movq -0x58(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, -0x60(%rbp)
movq -0x60(%rbp), %rdx
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0xe086
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rdx
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xe051
movq -0x70(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)