// File: /workspace/github_c_makefile_star2_O2/ComplexNetworks-DCC-FCUP[P]gtrieScanner/GraphUtils.cpp
// Function: GraphUtils::orderGraph(Graph*, Graph*)
void GraphUtils::orderGraph(Graph *old, Graph *g) {
  int i, j, aux;
  int size= old->numNodes();
  int degree[size];
  int *ds[size];
  int neighbours[size];
  vector<int> *v;

  for (i=0; i<size; i++) {
    degree[i] = old->nodeOutEdges(i) + old->nodeOutEdges(i);
    neighbours[i] = old->numNeighbours(i);
    ds[i] = new int[neighbours[i]];        
    v = old->neighbours(i);
    for (j=0; j<neighbours[i]; j++)
      ds[i][j] = old->nodeOutEdges((*v)[j]) + old->nodeOutEdges((*v)[j]);
    qsort(ds[i], old->numNeighbours(i), sizeof(int), _compare_int);
  }
  
  _degree =  degree;
  _ds     = ds;
  _neighbours = neighbours;
  int n[size], r[size];
  for (i=0; i<size; i++) n[i]=i;
  qsort(n, size, sizeof(int), _compare_degree);
  for (i=0; i<size; i++) r[n[i]]=i;

  for (i=0; i<size; i++)
    delete[] ds[i];

  g->createGraph(size, old->type());
  for (i=0; i<size; i++) {
    v = old->outEdges(i);
    aux = v->size();
    for (j=0; j<aux; j++)
      g->addEdge(r[i], r[(*v)[j]]);
    v = old->inEdges(i);
    aux = v->size();
    for (j=0; j<aux; j++)
      g->addEdge(r[(*v)[j]], r[i]);
  }
  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, -0x40(%rbp)
movq (%rdi), %rax
movq %rdi, -0x30(%rbp)
callq *0x40(%rax)
movl %eax, %r14d
movl %eax, %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
movq %rsp, %rax
movq %rdx, -0x70(%rbp)
subq %rdx, %rax
movq %rax, -0x50(%rbp)
movq %rax, %rsp
xorl %r15d, %r15d
testl %r14d, %r14d
cmovlel %r15d, %r14d
movq %r14, -0x60(%rbp)
movq -0x30(%rbp), %r12
cmpq %r14, %r15
je 0x570a
movq -0x30(%rbp), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
movl %r15d, %esi
callq *0x60(%rax)
movq %r15, %r14
movl %eax, %r15d
movq (%rbx), %rax
movq %rbx, %rdi
movl %r14d, %esi
callq *0x60(%rax)
addl %r15d, %eax
movq -0x58(%rbp), %rcx
movl %eax, (%rcx,%r14,4)
movq (%rbx), %rax
movq %rbx, %rdi
movl %r14d, %esi
callq *0x70(%rax)
movl %eax, %r13d
movq -0x50(%rbp), %rax
movl %r13d, (%rax,%r14,4)
movl %r13d, %edi
shlq $0x2, %rdi
testl %r13d, %r13d
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, -0x68(%rbp)
movq %rcx, (%rax,%r14,8)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, -0x78(%rbp)
movl %r14d, %esi
callq *0x98(%rax)
movq %rax, %r15
movslq %r13d, %rbx
xorl %r14d, %r14d
cmpq %rbx, %r14
jge 0x56d7
movq (%r15), %rax
movl (%rax,%r14,4), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x60(%rax)
movl %eax, %r13d
movq (%r15), %rax
movl (%rax,%r14,4), %esi
movq (%r12), %rax
movq %r12, %rdi
callq *0x60(%rax)
addl %r13d, %eax
movq -0x68(%rbp), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
jmp 0x569d
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
movq -0x78(%rbp), %r15
movl %r15d, %esi
callq *0x70(%rax)
movslq %eax, %rsi
movq -0x68(%rbp), %rdi
pushq $0x4
popq %rdx
leaq -0x193(%rip), %rcx      # 0x5566
callq 0x2400
incq %r15
movq -0x60(%rbp), %r14
jmp 0x560f
movq -0x58(%rbp), %rax
movq %rax, 0x1408b(%rip)     # 0x197a0
movq -0x38(%rbp), %rax
movq %rax, 0x14088(%rip)     # 0x197a8
movq -0x50(%rbp), %rax
movq %rax, 0x14085(%rip)     # 0x197b0
movq %rsp, %r12
movq -0x70(%rbp), %rax
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
xorl %eax, %eax
cmpq %rax, %r14
je 0x5751
movl %eax, (%r12,%rax,4)
incq %rax
jmp 0x5743
movslq -0x48(%rbp), %rsi
leaq -0x1ea(%rip), %rcx      # 0x5572
pushq $0x4
popq %rdx
movq %r12, %rdi
callq 0x2400
xorl %eax, %eax
cmpq %rax, %r14
je 0x577c
movslq (%r12,%rax,4), %rcx
movl %eax, (%r13,%rcx,4)
incq %rax
jmp 0x5769
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x579a
movq -0x38(%rbp), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x5795
callq 0x22b0
incq %rbx
jmp 0x577e
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
movq -0x40(%rbp), %rdi
movq (%rdi), %rcx
movq -0x48(%rbp), %rsi
movl %eax, %edx
callq *0x18(%rcx)
xorl %r12d, %r12d
cmpq %r14, %r12
je 0x586c
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
movl %r12d, %esi
callq *0xb8(%rax)
movq %rax, %r15
movq 0x8(%rax), %rbx
subq (%rax), %rbx
shrq $0x2, %rbx
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x5810
movl (%r13,%r12,4), %esi
movq (%r15), %rax
movslq (%rax,%r14,4), %rax
movl (%r13,%rax,4), %edx
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
incq %r14
jmp 0x57eb
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
movl %r12d, %esi
callq *0xc0(%rax)
movq %rax, %r15
movq 0x8(%rax), %rbx
subq (%rax), %rbx
shrq $0x2, %rbx
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0x5860
movq (%r15), %rax
movslq (%rax,%r14,4), %rax
movl (%r13,%rax,4), %esi
movl (%r13,%r12,4), %edx
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
callq *0x30(%rax)
incq %r14
jmp 0x583b
incq %r12
movq -0x60(%rbp), %r14
jmp 0x57b7
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop