// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, -0x58(%rbp)
movq %rdi, %r14
movq (%rdi), %rax
callq *0x40(%rax)
movl %eax, %edx
leaq 0xf(,%rdx,4), %rsi
andq $-0x10, %rsi
movq %rsp, %rcx
subq %rsi, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
leaq 0xf(,%rdx,8), %rcx
andq $-0x10, %rcx
movq %rsp, %rbx
subq %rcx, %rbx
movq %rbx, %rsp
movq %rsp, %r13
movq %rsi, -0x50(%rbp)
subq %rsi, %r13
movq %r13, %rsp
testl %eax, %eax
movq %rdx, -0x48(%rbp)
jle 0x5b58
xorl %r12d, %r12d
movq (%r14), %rax
movq %r14, %rdi
movl %r12d, %esi
callq *0x60(%rax)
movl %eax, %r15d
movq (%r14), %rax
movq %r14, %rdi
movl %r12d, %esi
callq *0x60(%rax)
addl %r15d, %eax
movq -0x38(%rbp), %rcx
movl %eax, (%rcx,%r12,4)
movq (%r14), %rax
movq %r14, %rdi
movl %r12d, %esi
callq *0x70(%rax)
movl %eax, (%r13,%r12,4)
cltq
leaq (,%rax,4), %rdi
testl %eax, %eax
movq $-0x1, %rax
cmovsq %rax, %rdi
callq 0x2030
movq %rax, (%rbx,%r12,8)
movq (%r14), %rax
movq %r14, %rdi
movl %r12d, %esi
callq *0x98(%rax)
movq %rbx, %rdx
cmpl $0x0, (%r13,%r12,4)
jle 0x5b1e
movq %rax, %r15
xorl %ebx, %ebx
movq %r12, -0x40(%rbp)
movq (%r15), %rax
movl (%rax,%rbx,4), %esi
movq (%r14), %rax
movq %r14, %rdi
movq %rdx, %r12
callq *0x60(%rax)
movl %eax, -0x2c(%rbp)
movq (%r15), %rax
movl (%rax,%rbx,4), %esi
movq (%r14), %rax
movq %r14, %rdi
callq *0x60(%rax)
movq %r12, %rdx
movq -0x40(%rbp), %r12
addl -0x2c(%rbp), %eax
movq (%rdx,%r12,8), %rcx
movl %eax, (%rcx,%rbx,4)
incq %rbx
movslq (%r13,%r12,4), %rax
cmpq %rax, %rbx
jl 0x5adc
movq (%rdx,%r12,8), %r15
movq (%r14), %rax
movq %r14, %rdi
movl %r12d, %esi
movq %rdx, %rbx
callq *0x70(%rax)
movslq %eax, %rsi
movl $0x4, %edx
movq %r15, %rdi
leaq -0x179(%rip), %rcx      # 0x59ca
callq 0x23c0
incq %r12
movq -0x48(%rbp), %rdx
cmpq %rdx, %r12
jne 0x5a62
movq -0x38(%rbp), %rax
movq %rax, 0x13c1d(%rip)     # 0x19780
movq %rbx, %r12
movq %rbx, 0x13c1b(%rip)     # 0x19788
movq %r13, 0x13c1c(%rip)     # 0x19790
movq %rsp, %r13
movq -0x50(%rbp), %rax
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
testl %edx, %edx
jle 0x5b9d
xorl %eax, %eax
movl %eax, (%r13,%rax,4)
incq %rax
cmpq %rax, %rdx
jne 0x5b90
movq %r14, -0x38(%rbp)
movslq -0x48(%rbp), %r15
leaq -0x1d6(%rip), %rcx      # 0x59d6
movl $0x4, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x23c0
testl %r15d, %r15d
movq -0x48(%rbp), %r15
jle 0x5bd7
xorl %eax, %eax
movslq (%r13,%rax,4), %rcx
movl %eax, (%rbx,%rcx,4)
incq %rax
cmpq %rax, %r15
jne 0x5bc7
movq %rbx, -0x50(%rbp)
testl %r15d, %r15d
movq -0x58(%rbp), %r14
jle 0x5bfc
xorl %ebx, %ebx
movq (%r12,%rbx,8), %rdi
testq %rdi, %rdi
je 0x5bf4
callq 0x2290
incq %rbx
cmpq %rbx, %r15
jne 0x5be6
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
movq (%r14), %rcx
movq %r14, %rdi
movl %r15d, %esi
movl %eax, %edx
callq *0x18(%rcx)
testl %r15d, %r15d
jle 0x5cea
xorl %esi, %esi
movq -0x58(%rbp), %r14
movq -0x50(%rbp), %r13
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
movq %rsi, -0x40(%rbp)
callq *0xb8(%rax)
movq 0x8(%rax), %r12
subq (%rax), %r12
shrq $0x2, %r12
testl %r12d, %r12d
jle 0x5c80
movq %rax, %r15
movq -0x40(%rbp), %rax
movl (%r13,%rax,4), %eax
movl %eax, -0x2c(%rbp)
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %ebx, %ebx
movq (%r15), %rax
movslq (%rax,%rbx,4), %rax
movl (%r13,%rax,4), %edx
movq (%r14), %rax
movq %r14, %rdi
movl -0x2c(%rbp), %esi
callq *0x30(%rax)
incq %rbx
cmpq %rbx, %r12
jne 0x5c60
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
movq -0x40(%rbp), %rsi
callq *0xc0(%rax)
movq 0x8(%rax), %r12
subq (%rax), %r12
shrq $0x2, %r12
testl %r12d, %r12d
jle 0x5cd9
movq %rax, %r15
movq -0x40(%rbp), %rax
movl (%r13,%rax,4), %eax
movl %eax, -0x2c(%rbp)
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %ebx, %ebx
movq (%r15), %rax
movslq (%rax,%rbx,4), %rax
movl (%r13,%rax,4), %esi
movq (%r14), %rax
movq %r14, %rdi
movl -0x2c(%rbp), %edx
callq *0x30(%rax)
incq %rbx
cmpq %rbx, %r12
jne 0x5cb9
movq -0x40(%rbp), %rsi
incq %rsi
cmpq -0x48(%rbp), %rsi
jne 0x5c27
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)