// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movsd %xmm0, 0x40(%rsp)
movq %rdx, %r14
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq 0x18(%rsi), %r15
addq $0x8, %rsi
movq %rsi, 0x38(%rsp)
cmpq 0x38(%rsp), %r15
je 0x2822b
movq %r15, 0x30(%rsp)
movl 0x20(%r15), %eax
andq $0x0, 0x20(%rsp)
movl %eax, 0x8(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorl %ebp, %ebp
movl $0x0, 0xc(%rsp)
movl %ebp, %ecx
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
cqto
pushq $0x30
popq %rdi
idivq %rdi
cmpq %rcx, %rax
jbe 0x28110
imulq $0x30, %rcx, %rax
leaq (%rsi,%rax), %r12
leaq (%rsi,%rax), %r13
addq $0x8, %r13
movq 0x10(%r13), %rax
movslq 0x8(%rsp), %rcx
imulq $0x18, %rcx, %r15
cmpq %r13, %rax
je 0x28109
movq (%r14), %rcx
movslq 0x20(%rax), %rdx
movq (%rcx,%r15), %rcx
movsd (%rcx,%rdx,8), %xmm0
ucomisd 0x40(%rsp), %xmm0
ja 0x280e3
movq %rax, %rdi
callq 0x4510
jmp 0x280bc
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xf334
movl %ebp, 0x48(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x10324
movb $0x1, %al
movl %eax, 0xc(%rsp)
incl %ebp
jmp 0x28086
testb $0x1, 0xc(%rsp)
je 0x28186
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq $0x5, %rdx
movq 0x30(%rsp), %r13
jb 0x281d0
pushq $0x1
popq %rdx
movl %edx, %r15d
movl %r15d, %r15d
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %r15, %rdx
jbe 0x281ea
movslq (%rax), %rcx
movq (%rbx), %rdx
imulq $0x30, %rcx, %rdi
addq %rdx, %rdi
movslq (%rax,%r15,4), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdx
addq $0x8, %rdx
movq 0x10(%rdx), %rsi
callq 0x2c620
incl %r15d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
jmp 0x2813c
andl $0x0, 0x50(%rsp)
andq $0x0, 0x58(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x60(%rsp)
movq %rax, 0x68(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0x48(%rsp), %r15
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0xf334
movq 0x30(%rsp), %r13
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2a3f8
movq %r15, %rdi
callq 0xafbe
movq %r13, %rdi
callq 0x4510
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0xb290
jmp 0x28054
xorl %r15d, %r15d
pushq $0x1
popq %rdx
movl %edx, %r12d
movl %r12d, %r12d
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r12, %rcx
jbe 0x281d0
movslq (%rax,%r12,4), %rax
imulq $0x30, %rax, %rsi
addq (%rbx), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0x2a424
incl %r12d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
addq $-0x30, %r15
jmp 0x281f3
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0xafbe
jmp 0x28255
jmp 0x28252
jmp 0x28252
jmp 0x28252
movq %rax, %r14
leaq 0x10(%rsp), %rdi
callq 0xb290
movq %rbx, %rdi
callq 0xb494
movq %r14, %rdi
callq 0x4780
nop