// File: /workspace/github_cpp_makefile_star2_O3/Luthaf[P]zeopp/networkanalysis.cc
// Function: clusterElements(std::set<int, std::less<int>, std::allocator<int>>, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>, double)
vector< set<int> > clusterElements(set<int> cageNodes, vector< vector<double> > overlapFractions, double MIN_FRACTION){
  vector< set<int> > clusters;
  set<int>::iterator nodeIter = cageNodes.begin();
  
  while(nodeIter != cageNodes.end()) {
    int elementID = *nodeIter;
    bool merged = false;
    vector<int> clusterIDs;
    for(unsigned int i = 0; i < clusters.size(); i++){
      set<int>::iterator clusterIter = clusters[i].begin();
      while(clusterIter != clusters[i].end()){
	if(overlapFractions[elementID][*clusterIter] > MIN_FRACTION){
	  clusters[i].insert(elementID);
	  merged = true;
	  clusterIDs.push_back(i);
	  break;
	}
	clusterIter++;
      }
    }
    if(!merged){
      // Node not added to any clusters so form a new cluster
      set<int> newCluster; newCluster.insert(elementID);
      clusters.push_back(newCluster);
    }
    else if (clusterIDs.size() > 1){
      // Node was added to more than one cluster. Therefore,
      // join the resulting clusters into a single cluster

      for(unsigned int i = 1; i < clusterIDs.size(); i++){
	clusters[clusterIDs[0]].insert(clusters[clusterIDs[i]].begin(), clusters[clusterIDs[i]].end());
      }
      int removeCount = 0;
      for(unsigned int i = 1; i < clusterIDs.size(); i++){
	clusters.erase(clusters.begin() + clusterIDs[i] - removeCount);
	removeCount++;
      }
    }
    nodeIter++;
  }
  return clusters;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movsd %xmm0, 0x50(%rsp)
movq %rdx, 0x40(%rsp)
movq %rdi, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x18(%rsi), %rbx
addq $0x8, %rsi
movq %rsi, 0x30(%rsp)
cmpq %rsi, %rbx
je 0x358a8
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
movq %r13, 0x8(%rsp)
movq %rbx, 0x28(%rsp)
movl 0x20(%rbx), %eax
movl %eax, 0x4(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%r13), %r12
movq 0x8(%r13), %r13
cmpq %r12, %r13
je 0x35812
xorl %ebp, %ebp
xorl %ecx, %ecx
movq %rcx, 0x38(%rsp)
movslq 0x4(%rsp), %rax
movq %rax, 0x48(%rsp)
subq %r12, %r13
sarq $0x4, %r13
imulq %r14, %r13
leaq (,%rbp,2), %r15
addq %rbp, %r15
shlq $0x4, %r15
leaq (%r12,%r15), %r14
addq $0x8, %r14
movq 0x10(%r14), %rax
cmpq %r14, %rax
je 0x356cc
addq %r12, %r15
movq 0x40(%rsp), %rcx
movq (%rcx), %rcx
movq 0x48(%rsp), %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rbx
movslq 0x20(%rax), %rcx
movsd (%rbx,%rcx,8), %xmm0
ucomisd 0x50(%rsp), %xmm0
ja 0x356d5
movq %rax, %rdi
callq 0x44f0
cmpq %r14, %rax
jne 0x356ae
incl %ebp
cmpq %rbp, %r13
ja 0x35676
jmp 0x35742
movq %r15, %rdi
leaq 0x4(%rsp), %rsi
callq 0x1329a
movl %ebp, 0x58(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
movq 0x8(%rsp), %r13
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
je 0x3570e
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x3571d
leaq 0x10(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x14198
movq (%r13), %r12
movq 0x8(%r13), %r13
incl %ebp
movq %r13, %rax
subq %r12, %rax
sarq $0x4, %rax
imulq %r14, %rax
movb $0x1, %cl
cmpq %rbp, %rax
ja 0x3565c
jmp 0x3574d
testb $0x1, 0x38(%rsp)
je 0x35812
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rax
subq %r15, %rax
cmpq $0x5, %rax
jb 0x35869
movl $0x1, %eax
movl $0x2, %ebx
movq 0x8(%rsp), %r13
movslq (%r15), %rcx
movq (%r13), %rdx
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
addq %rdx, %rdi
movslq (%r15,%rax,4), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
addq %rax, %rdx
addq $0x8, %rdx
movq 0x10(%rdx), %rsi
callq 0x3b538
movl %ebx, %eax
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rcx
subq %r15, %rcx
movq %rcx, %rdx
sarq $0x2, %rdx
incl %ebx
cmpq %rax, %rdx
ja 0x35773
cmpq $0x5, %rcx
movq 0x28(%rsp), %r12
jb 0x35873
movl $0x2, %ebx
xorl %r14d, %r14d
movslq -0x4(%r15,%rbx,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x4, %rsi
addq (%r13), %rsi
addq %r14, %rsi
movq %r13, %rdi
callq 0x3b67a
movl %ebx, %eax
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rcx
subq %r15, %rcx
sarq $0x2, %rcx
addq $-0x30, %r14
incq %rbx
cmpq %rax, %rcx
ja 0x357d5
jmp 0x35873
movl $0x0, 0x60(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x68(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x70(%rsp)
movq %rax, 0x78(%rsp)
movq %rcx, 0x80(%rsp)
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4(%rsp), %rsi
movq 0x8(%rsp), %r13
callq 0x1329a
movq %r13, %rdi
movq %rbx, %rsi
callq 0x385a0
movq %rbx, %rdi
callq 0xf206
movq 0x10(%rsp), %r15
jmp 0x3586e
movq 0x8(%rsp), %r13
movq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x44f0
movq %rax, %rbx
testq %r15, %r15
je 0x35893
movq 0x20(%rsp), %rsi
subq %r15, %rsi
movq %r15, %rdi
callq 0x4470
cmpq 0x30(%rsp), %rbx
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
jne 0x35628
movq %r13, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0xf206
jmp 0x358dd
jmp 0x358da
jmp 0x358da
movq %rax, %r14
movq 0x8(%rsp), %r13
jmp 0x358dd
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x358f4
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %r13, %rdi
callq 0xf5a0
movq %r14, %rdi
callq 0x4780