// File: riyadparvez[P]louvain-method/community.cpp
// Function: Community::partition2graph_binary()
Graph
Community::partition2graph_binary() {
  // Renumber communities
  vector<int> renumber(size, -1);
  for (int node=0 ; node<size ; node++) {
    renumber[n2c[node]]++;
  }

  int final=0;
  for (int i=0 ; i<size ; i++)
    if (renumber[i]!=-1)
      renumber[i]=final++;

  // Compute communities
  vector<vector<int> > comm_nodes(final);
  vector<vector<int> > communities(final);
  //printf("%s %d \n", __FILE__, __LINE__);
  for (int node=0 ; node<size ; node++) {
    //TODO add node handling
    vector<int>& comm = communities[renumber[n2c[node]]];  
    comm.insert(comm.end(), g.nodes[node].begin(), g.nodes[node].end());
    comm_nodes[renumber[n2c[node]]].push_back(node);
  }
  /*
  for (size_t i=0; i<g.nodes.size(); i++) {
    for (size_t j=0; j<g.nodes[i].size(); j++) {
      printf("%d n ", g.nodes[i][j]);
    }
    printf("n \n");
  }
  */
  /*
  for (size_t i=0; i<communities.size(); i++) {
    for (size_t j=0; j<communities[i].size(); j++) {
      printf("%d c ", communities[i][j]);
    }
    printf("i: %d \n", i);
  }
  printf("%s %d \n", __FILE__, __LINE__);
  */
  Graph g2(communities);  
  
  g2.nb_nodes = comm_nodes.size();
  g2.degrees.resize(comm_nodes.size());

  int comm_deg = comm_nodes.size();
  for (int comm=0 ; comm<comm_deg ; comm++) {
    map<int,float> m;
    map<int,float>::iterator it;

    int comm_size = comm_nodes[comm].size();
    for (int node=0 ; node<comm_size ; node++) {
      pair<vector<unsigned int>::iterator, vector<float>::iterator> p = g.neighbors(comm_nodes[comm][node]);
      int deg = g.nb_neighbors(comm_nodes[comm][node]);
      for (int i=0 ; i<deg ; i++) {
    int neigh        = *(p.first+i);
    int neigh_comm   = renumber[n2c[neigh]];
    double neigh_weight = (g.weights.size()==0)?1.:*(p.second+i);

    it = m.find(neigh_comm);
    if (it==m.end())
      m.insert(make_pair(neigh_comm, neigh_weight));
    else
      it->second+=neigh_weight;
      }
    }
    g2.degrees[comm]=(comm==0)?m.size():g2.degrees[comm-1]+m.size();
    g2.nb_links+=m.size();

    
    for (it = m.begin() ; it!=m.end() ; it++) {
      g2.total_weight  += it->second;
      g2.links.push_back(it->first);
      g2.weights.push_back(it->second);
    }
  }

  return g2;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x1c0(%rbp)
movq %rdi, %rax
movq %rax, -0x1b8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movslq 0xb0(%rax), %rax
movq %rax, -0x1a8(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
leaq -0x2d(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x4130
movq -0x1a8(%rbp), %rsi
movq -0x1a0(%rbp), %rcx
leaq -0x28(%rbp), %rdi
leaq -0x2c(%rbp), %rdx
callq 0xa5d0
jmp 0x8fc1
leaq -0x2d(%rbp), %rdi
callq 0x7290
movl $0x0, -0x40(%rbp)
movq -0x1b0(%rbp), %rcx
movl -0x40(%rbp), %eax
cmpl 0xb0(%rcx), %eax
jge 0x9032
movq -0x1b0(%rbp), %rdi
addq $0xb8, %rdi
movslq -0x40(%rbp), %rsi
callq 0x3ab0
movslq (%rax), %rsi
leaq -0x28(%rbp), %rdi
callq 0x3ab0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x8fd1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x2d(%rbp), %rdi
callq 0x7290
jmp 0x990a
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x1b0(%rbp), %rcx
movl -0x48(%rbp), %eax
cmpl 0xb0(%rcx), %eax
jge 0x9095
movslq -0x48(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x3ab0
cmpl $-0x1, (%rax)
je 0x9088
movl -0x44(%rbp), %eax
movl %eax, -0x1c4(%rbp)
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movslq -0x48(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x3ab0
movl -0x1c4(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x908a
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x9040
movslq -0x44(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x3f20
movq -0x1d8(%rbp), %rsi
movq -0x1d0(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0xa650
jmp 0x90c9
leaq -0x61(%rbp), %rdi
callq 0x4630
movslq -0x44(%rbp), %rax
movq %rax, -0x1e8(%rbp)
leaq -0x81(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x3f20
movq -0x1e8(%rbp), %rsi
movq -0x1e0(%rbp), %rdx
leaq -0x80(%rbp), %rdi
callq 0xa650
jmp 0x9109
leaq -0x81(%rbp), %rdi
callq 0x4630
movl $0x0, -0x88(%rbp)
movq -0x1b0(%rbp), %rcx
movl -0x88(%rbp), %eax
cmpl 0xb0(%rcx), %eax
jge 0x92c6
movq -0x1b0(%rbp), %rdi
addq $0xb8, %rdi
movslq -0x88(%rbp), %rsi
callq 0x3ab0
movslq (%rax), %rsi
leaq -0x28(%rbp), %rdi
callq 0x3ab0
movslq (%rax), %rsi
leaq -0x80(%rbp), %rdi
callq 0x3a70
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x68b0
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xa770
movq -0x1b0(%rbp), %rdi
addq $0x68, %rdi
movq %rdi, -0x200(%rbp)
movslq -0x88(%rbp), %rsi
callq 0x3a70
movq %rax, %rdi
callq 0x6a60
movq -0x200(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movslq -0x88(%rbp), %rsi
callq 0x3a70
movq %rax, %rdi
callq 0x68b0
movq -0x1f8(%rbp), %rdi
movq %rax, -0xb0(%rbp)
movq -0x98(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq -0xb0(%rbp), %rcx
callq 0xa6c0
movq %rax, -0x1f0(%rbp)
jmp 0x9217
movq -0x1b0(%rbp), %rdi
movq -0x1f0(%rbp), %rax
movq %rax, -0xb8(%rbp)
addq $0xb8, %rdi
movslq -0x88(%rbp), %rsi
callq 0x3ab0
movslq (%rax), %rsi
leaq -0x28(%rbp), %rdi
callq 0x3ab0
movslq (%rax), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3a70
movq %rax, %rdi
leaq -0x88(%rbp), %rsi
callq 0xa7b0
jmp 0x9268
jmp 0x926a
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x911f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x4630
jmp 0x9901
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x81(%rbp), %rdi
callq 0x4630
jmp 0x98f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x98ef
movq -0x1c0(%rbp), %rdi
movb $0x0, -0xb9(%rbp)
leaq -0x80(%rbp), %rsi
callq 0x2a60
jmp 0x92df
leaq -0x60(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x3a40
movq -0x210(%rbp), %rdi
movq %rax, %rcx
movq -0x1c0(%rbp), %rax
movl %ecx, (%rax)
addq $0x18, %rax
movq %rax, -0x208(%rbp)
callq 0x3a40
movq -0x208(%rbp), %rdi
movq %rax, %rsi
callq 0x3370
jmp 0x9323
leaq -0x60(%rbp), %rdi
callq 0x3a40
movl %eax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0xc0(%rbp), %eax
jge 0x989c
leaq -0xf8(%rbp), %rdi
callq 0xa820
leaq -0x100(%rbp), %rdi
callq 0xa840
movslq -0xc4(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3a70
movq %rax, %rdi
callq 0x3a90
movl %eax, -0x104(%rbp)
movl $0x0, -0x108(%rbp)
movl -0x108(%rbp), %eax
cmpl -0x104(%rbp), %eax
jge 0x96c4
movq -0x1b0(%rbp), %rax
addq $0x38, %rax
movq %rax, -0x228(%rbp)
movslq -0xc4(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3a70
movq %rax, %rdi
movslq -0x108(%rbp), %rsi
callq 0x3ab0
movq -0x228(%rbp), %rdi
movl (%rax), %esi
callq 0x3ad0
movq %rdx, -0x220(%rbp)
movq %rax, -0x218(%rbp)
jmp 0x93ef
movq -0x1b0(%rbp), %rax
movq -0x220(%rbp), %rcx
movq -0x218(%rbp), %rdx
movq %rdx, -0x118(%rbp)
movq %rcx, -0x110(%rbp)
addq $0x38, %rax
movq %rax, -0x238(%rbp)
movslq -0xc4(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3a70
movq %rax, %rdi
movslq -0x108(%rbp), %rsi
callq 0x3ab0
movq -0x238(%rbp), %rdi
movl (%rax), %esi
callq 0x3c80
movl %eax, -0x22c(%rbp)
jmp 0x9452
movl -0x22c(%rbp), %eax
movl %eax, -0x11c(%rbp)
movl $0x0, -0x120(%rbp)
movl -0x120(%rbp), %eax
cmpl -0x11c(%rbp), %eax
jge 0x96ae
leaq -0x118(%rbp), %rdi
movslq -0x120(%rbp), %rsi
callq 0x3d40
movq %rax, -0x130(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x3d80
movq -0x1b0(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x124(%rbp)
addq $0xb8, %rdi
movslq -0x124(%rbp), %rsi
callq 0x3ab0
movslq (%rax), %rsi
leaq -0x28(%rbp), %rdi
callq 0x3ab0
movq -0x1b0(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x134(%rbp)
addq $0x38, %rdi
addq $0x60, %rdi
callq 0x3da0
cmpq $0x0, %rax
jne 0x9502
movsd 0xbc90(%rip), %xmm0     # 0x15188
movsd %xmm0, -0x240(%rbp)
jmp 0x953c
leaq -0x118(%rbp), %rdi
addq $0x8, %rdi
movslq -0x120(%rbp), %rsi
callq 0x3dc0
movq %rax, -0x148(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x3e00
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x240(%rbp)
movsd -0x240(%rbp), %xmm0
movsd %xmm0, -0x140(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x134(%rbp), %rsi
callq 0xa860
movq %rax, -0x248(%rbp)
jmp 0x9568
movq -0x248(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xa8c0
movq %rax, -0x158(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0xa890
testb $0x1, %al
jne 0x95b3
jmp 0x965e
leaq -0x134(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0xa930
movsd %xmm0, -0x258(%rbp)
movl %eax, -0x24c(%rbp)
jmp 0x95d6
movsd -0x258(%rbp), %xmm0
movl -0x24c(%rbp), %eax
movl %eax, -0x168(%rbp)
movsd %xmm0, -0x160(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0xa8f0
movb %dl, -0x261(%rbp)
movq %rax, -0x260(%rbp)
jmp 0x9614
movb -0x261(%rbp), %al
movq -0x260(%rbp), %rcx
movq %rcx, -0x178(%rbp)
movb %al, -0x170(%rbp)
jmp 0x9698
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x98e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xaae0
jmp 0x98e3
movsd -0x140(%rbp), %xmm0
movsd %xmm0, -0x270(%rbp)
leaq -0x100(%rbp), %rdi
callq 0xa960
movsd -0x270(%rbp), %xmm1
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x4(%rax)
jmp 0x969a
movl -0x120(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x120(%rbp)
jmp 0x9468
jmp 0x96b0
movl -0x108(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x108(%rbp)
jmp 0x938e
cmpl $0x0, -0xc4(%rbp)
jne 0x96e2
leaq -0xf8(%rbp), %rdi
callq 0xa980
movq %rax, -0x278(%rbp)
jmp 0x9728
movq -0x1c0(%rbp), %rdi
addq $0x18, %rdi
movl -0xc4(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x3400
movq (%rax), %rax
movq %rax, -0x280(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xa980
movq %rax, %rcx
movq -0x280(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x278(%rbp)
movq -0x1c0(%rbp), %rdi
movq -0x278(%rbp), %rax
movq %rax, -0x288(%rbp)
addq $0x18, %rdi
movslq -0xc4(%rbp), %rsi
callq 0x3400
movq -0x288(%rbp), %rcx
movq %rcx, (%rax)
leaq -0xf8(%rbp), %rdi
callq 0xa980
movq %rax, %rcx
movq -0x1c0(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
leaq -0xf8(%rbp), %rdi
callq 0xa9a0
movq %rax, -0x180(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xa8c0
movq %rax, -0x188(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0xa9d0
testb $0x1, %al
jne 0x97c5
jmp 0x987c
leaq -0x100(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0xa960
movq -0x298(%rbp), %rdi
movq %rax, %rcx
movq -0x1c0(%rbp), %rax
movss 0x4(%rcx), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x10(%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rax)
addq $0x48, %rax
movq %rax, -0x290(%rbp)
callq 0xa960
movq -0x290(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x18c(%rbp)
leaq -0x18c(%rbp), %rsi
callq 0xaa00
jmp 0x982d
movq -0x1c0(%rbp), %rax
addq $0x60, %rax
movq %rax, -0x2a0(%rbp)
leaq -0x100(%rbp), %rdi
callq 0xa960
movq -0x2a0(%rbp), %rdi
movq %rax, %rsi
addq $0x4, %rsi
callq 0xaa30
jmp 0x9860
jmp 0x9862
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
callq 0xaaa0
movq %rax, -0x198(%rbp)
jmp 0x9796
leaq -0xf8(%rbp), %rdi
callq 0xaae0
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x933c
movb $0x1, -0xb9(%rbp)
testb $0x1, -0xb9(%rbp)
jne 0x98b8
movq -0x1c0(%rbp), %rdi
callq 0x9f30
leaq -0x80(%rbp), %rdi
callq 0x3880
leaq -0x60(%rbp), %rdi
callq 0x3880
leaq -0x28(%rbp), %rdi
callq 0x3640
movq -0x1b8(%rbp), %rax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
movq -0x1c0(%rbp), %rdi
callq 0x9f30
leaq -0x80(%rbp), %rdi
callq 0x3880
leaq -0x60(%rbp), %rdi
callq 0x3880
leaq -0x28(%rbp), %rdi
callq 0x3640
movq -0x38(%rbp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)