// File: /workspace/github_cpp_makefile_star2_O3/riyadparvez[P]louvain-method/community.cpp
// Function: Community::one_level()
bool
Community::one_level() {
  bool improvement=false ;
  int nb_moves;
  int nb_pass_done = 0;
  double new_mod   = modularity();
  double cur_mod   = new_mod;

  vector<int> random_order(size);
  for (int i=0 ; i<size ; i++)
    random_order[i]=i;
  for (int i=0 ; i<size-1 ; i++) {
    int rand_pos = rand()%(size-i)+i;
    int tmp      = random_order[i];
    random_order[i] = random_order[rand_pos];
    random_order[rand_pos] = tmp;
  }

  // repeat while 
  //   there is an improvement of modularity
  //   or there is an improvement of modularity greater than a given epsilon 
  //   or a predefined number of pass have been done
  do {
    cur_mod = new_mod;
    nb_moves = 0;
    nb_pass_done++;

    // for each node: remove the node from its community and insert it in the best community
    for (int node_tmp=0 ; node_tmp<size ; node_tmp++) {
//      int node = node_tmp;
      int node = random_order[node_tmp];
      int node_comm     = n2c[node];
      double w_degree = g.weighted_degree(node);

      // computation of all neighboring communities of current node
      neigh_comm(node);
      // remove node from its current community
      remove(node, node_comm, neigh_weight[node_comm]);

      // compute the nearest community for node
      // default choice for future insertion is the former community
      int best_comm        = node_comm;
      double best_nblinks  = 0.;
      double best_increase = 0.;
      for (unsigned int i=0 ; i<neigh_last ; i++) {
        double increase = modularity_gain(node, neigh_pos[i], neigh_weight[neigh_pos[i]], w_degree);
        if (increase>best_increase) {
          best_comm     = neigh_pos[i];
          best_nblinks  = neigh_weight[neigh_pos[i]];
          best_increase = increase;
        }
      }

      // insert node in the nearest community
      insert(node, best_comm, best_nblinks);
     
      if (best_comm!=node_comm)
        nb_moves++;
    }

    double total_tot=0;
    double total_in=0;
    for (unsigned int i=0 ; i<tot.size() ;i++) {
      total_tot+=tot[i];
      total_in+=in[i];
    }

    new_mod = modularity();
    if (nb_moves>0)
      improvement=true;
    
  } while (nb_moves>0 && new_mod-cur_mod>min_modularity);

  return improvement;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
callq 0x47c6
movsd %xmm0, 0x10(%rsp)
movslq 0xb0(%rbx), %rsi
leaq 0x20(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x5ac2
movl 0xb0(%rbx), %eax
testl %eax, %eax
jle 0x51e4
movq 0x20(%rsp), %rcx
xorl %edx, %edx
movl %edx, (%rcx,%rdx,4)
incq %rdx
movslq 0xb0(%rbx), %rax
cmpq %rax, %rdx
jl 0x5187
cmpl $0x2, %eax
jl 0x51e4
xorl %r14d, %r14d
xorl %r15d, %r15d
callq 0x2080
movl 0xb0(%rbx), %ecx
addl %r14d, %ecx
cltd
idivl %ecx
movq 0x20(%rsp), %rax
movl (%rax,%r15,4), %ecx
movslq %edx, %rdx
addq %r15, %rdx
movl (%rax,%rdx,4), %esi
movl %esi, (%rax,%r15,4)
movl %ecx, (%rax,%rdx,4)
incq %r15
movslq 0xb0(%rbx), %rax
leaq -0x1(%rax), %rcx
decl %r14d
cmpq %rcx, %r15
jl 0x51a4
testl %eax, %eax
jle 0x53db
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x20(%rsp), %rax
movl (%rax,%r14,4), %r15d
cmpl %r15d, 0x38(%rbx)
jbe 0x541d
movq 0x98(%rbx), %rax
cmpq %rax, 0xa0(%rbx)
je 0x5234
testq %r15, %r15
je 0x524b
leal -0x1(%r15), %ecx
movq 0x50(%rbx), %rdx
movq (%rdx,%rcx,8), %rsi
leaq (%rax,%rsi,4), %rax
jmp 0x5254
testq %r15, %r15
je 0x528b
movq 0x50(%rbx), %rcx
movq (%rcx,%r15,8), %rax
leal -0x1(%r15), %edx
subq (%rcx,%rdx,8), %rax
jmp 0x5292
movq 0x50(%rbx), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorpd %xmm1, %xmm1
xorl %esi, %esi
testq %r15, %r15
je 0x5269
movq (%rdx,%r15,8), %rdi
subq (%rdx,%rcx,8), %rdi
jmp 0x526b
movl (%rdx), %edi
movl %edi, %edi
cmpq %rdi, %rsi
jae 0x5283
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rsi,4), %xmm0
addsd %xmm0, %xmm1
incq %rsi
jmp 0x525a
movsd %xmm1, 0x18(%rsp)
jmp 0x52a2
movq 0x50(%rbx), %rax
movq (%rax), %rax
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x18(%rsp)
movslq %r15d, %rax
movq 0xb8(%rbx), %rcx
movl (%rcx,%rax,4), %r13d
movslq %r13d, %r12
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4554
movq (%rbx), %rax
movsd (%rax,%r12,8), %xmm0
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x5568
movl 0x30(%rbx), %eax
testq %rax, %rax
je 0x5367
testl %r15d, %r15d
js 0x543c
cmpl %r15d, 0xb0(%rbx)
jle 0x543c
movl %ebp, %r12d
movq (%rbx), %rcx
movq 0x18(%rbx), %rdx
movq 0xe8(%rbx), %rsi
movsd 0x48(%rbx), %xmm1
xorpd %xmm2, %xmm2
xorl %edi, %edi
xorpd %xmm3, %xmm3
movl %r13d, %ebp
movsd 0x18(%rsp), %xmm6
movslq (%rdx,%rdi,4), %r8
movl %r8d, %r9d
movsd (%rcx,%r9,8), %xmm0
movsd (%rsi,%r8,8), %xmm5
mulsd %xmm6, %xmm5
divsd %xmm1, %xmm5
movapd %xmm0, %xmm4
subsd %xmm5, %xmm4
ucomisd %xmm2, %xmm4
cmoval %r8d, %ebp
ja 0x5351
movapd %xmm3, %xmm0
maxsd %xmm2, %xmm4
incq %rdi
movapd %xmm4, %xmm2
movapd %xmm0, %xmm3
cmpq %rdi, %rax
jne 0x5320
jmp 0x5371
movl %ebp, %r12d
xorpd %xmm0, %xmm0
movl %r13d, %ebp
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x571e
xorl %eax, %eax
cmpl %r13d, %ebp
setne %al
movl %r12d, %ebp
addl %eax, %ebp
incq %r14
movslq 0xb0(%rbx), %r15
cmpq %r15, %r14
jl 0x51fa
testl %ebp, %ebp
je 0x53e2
movq %rbx, %rdi
callq 0x47c6
movapd %xmm0, %xmm1
subsd 0x10(%rsp), %xmm1
ucomisd 0x108(%rbx), %xmm1
jbe 0x53e6
movb $0x1, %al
movq %rax, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
testl %r15d, %r15d
jg 0x51f5
xorl %ebx, %ebx
movb $0x1, %r14b
jmp 0x53ed
xorl %r14d, %r14d
xorl %ebx, %ebx
jmp 0x53ed
xorl %ebx, %ebx
jmp 0x53e8
movb $0x1, %bl
movq 0x8(%rsp), %r14
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5404
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
orb %bl, %r14b
andb $0x1, %r14b
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2c57(%rip), %rdi      # 0x807b
leaq 0x2c69(%rip), %rsi      # 0x8094
leaq 0x2d90(%rip), %rcx      # 0x81c2
movl $0x74, %edx
callq 0x2130
leaq 0x2dd4(%rip), %rdi      # 0x8217
leaq 0x2de2(%rip), %rsi      # 0x822c
leaq 0x2e3b(%rip), %rcx      # 0x828c
movl $0x7a, %edx
callq 0x2130
jmp 0x545d
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x5477
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2370
nop