// File: /workspace/github_cpp_makefile_star2_O1/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, 0x38(%rsp)
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x18(%rsi), %r14
addq $0x8, %rsi
movq %rsi, 0x30(%rsp)
cmpq %rsi, %r14
je 0x35ca1
leaq 0x48(%rsp), %r13
movl 0x20(%r14), %eax
movl %eax, 0xc(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%rbx), %r15
cmpq %r15, 0x8(%rbx)
je 0x35c31
movq %r14, 0x28(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
shlq $0x4, %rcx
leaq (%r15,%rcx), %r13
addq $0x8, %r13
movq 0x10(%r13), %rax
cmpq %r13, %rax
je 0x35b16
addq %rcx, %r15
movslq 0xc(%rsp), %rcx
movq 0x38(%rsp), %rdx
movq (%rdx), %rdx
leaq (%rcx,%rcx,2), %rcx
movq (%rdx,%rcx,8), %r14
movslq 0x20(%rax), %rcx
movsd (%r14,%rcx,8), %xmm0
ucomisd 0x40(%rsp), %xmm0
ja 0x35ada
movq %rax, %rdi
callq 0x44f0
cmpq %r13, %rax
jne 0x35ab9
jmp 0x35b16
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x132f6
movl %ebp, 0x48(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x35b04
movl %ebp, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x35b13
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x1422c
movb $0x1, %r12b
incl %ebp
movq (%rbx), %r15
movq 0x8(%rbx), %rax
subq %r15, %rax
sarq $0x4, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
cmpq %rbp, %rax
ja 0x35a81
testb $0x1, %r12b
movq 0x28(%rsp), %r14
leaq 0x48(%rsp), %r13
je 0x35c31
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cmpq $0x5, %rax
jb 0x35c74
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
cmpq $0x5, %rcx
jb 0x35bca
movl $0x1, %ecx
movl $0x2, %ebp
movslq (%rax), %rdx
movq (%rbx), %rsi
leaq (%rdx,%rdx,2), %rdi
shlq $0x4, %rdi
addq %rsi, %rdi
movslq (%rax,%rcx,4), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
leaq (%rsi,%rax), %rdx
addq $0x8, %rdx
movq 0x10(%rdx), %rsi
callq 0x3b90c
movl %ebp, %ecx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
incl %ebp
cmpq %rcx, %rdx
ja 0x35b82
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
cmpq $0x5, %rcx
movq 0x28(%rsp), %r14
leaq 0x48(%rsp), %r13
jb 0x35c74
movl $0x2, %r15d
xorl %r12d, %r12d
movslq -0x4(%rax,%r15,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x4, %rsi
addq (%rbx), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x3ba4e
movl %r15d, %ecx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
addq $-0x30, %r12
incq %r15
cmpq %rcx, %rdx
ja 0x35bf4
jmp 0x35c74
movl $0x0, 0x50(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x58(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movq %rcx, 0x70(%rsp)
movq %r13, %rdi
leaq 0xc(%rsp), %rsi
callq 0x132f6
movq %rbx, %rdi
movq %r13, %rsi
callq 0x38a4c
movq %r13, %rdi
callq 0xf168
movq %r14, %rdi
callq 0x44f0
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x35c96
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
cmpq 0x30(%rsp), %r14
jne 0x35a4f
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 0xf168
jmp 0x35ccb
jmp 0x35cc8
jmp 0x35cc8
jmp 0x35cc8
movq %rax, %r14
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x35ce2
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0xf4f6
movq %r14, %rdi
callq 0x4780