// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3Segmentation::MergeSmallClusters()
int R3Segmentation::
MergeSmallClusters(void)
{
  // Find small clusters
  RNArray<R3SegmentationCluster *> small_clusters;
  for (int i = 0; i < clusters.NEntries(); i++) {
    R3SegmentationCluster *cluster = clusters.Kth(i);
    assert(!cluster->parent);
    if (cluster->points.NEntries() >= min_cluster_points) continue;
    small_clusters.Insert(cluster);
  }

  // Check small clusters
  if (small_clusters.NEntries() < 2) return 1;

  // Merge n-1 small clusters into first one
  R3SegmentationCluster *cluster0 = small_clusters.Kth(0);
  for (int i = 1; i < small_clusters.NEntries(); i++) {
    R3SegmentationCluster *cluster = small_clusters.Kth(i);
    if (cluster == cluster0) continue;
    cluster0->InsertChild(cluster);
    assert(cluster->parent == cluster0);
    assert(cluster0->points.NEntries() > 0);
    assert(cluster->points.NEntries() == 0);
  }

  // Rebuild list of clusters
  RNArray<R3SegmentationCluster *> all_clusters = clusters;
  clusters.Empty();
  for (int i = 0; i < all_clusters.NEntries(); i++) {
    R3SegmentationCluster *cluster = all_clusters.Kth(i);
    if (!cluster->parent) {
      assert((cluster == cluster0) || (cluster->points.NEntries() >= min_cluster_points));
      cluster->segmentation = this;
      cluster->segmentation_index = clusters.NEntries();    
      clusters.Insert(cluster);
    }
    else {
      assert(cluster->points.NEntries() == 0);
      cluster->parent->RemoveChild(cluster);
      delete cluster;
    }
  }

  // Return success
  return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xb1eda
leaq 0x18(%rbx), %r14
xorl %r12d, %r12d
movslq 0x24(%rbx), %rax
cmpq %rax, %r12
jge 0x2b5b8
movq 0x18(%rbx), %rax
movq (%rax,%r12,8), %rsi
movl 0x14(%rsi), %eax
cmpl 0x38(%rbx), %eax
jge 0x2b5b3
movq %r15, %rdi
callq 0x1a04c
incq %r12
jmp 0x2b592
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
jl 0x2b678
movq 0x8(%rsp), %rcx
movq (%rcx), %r15
pushq $0x1
popq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jge 0x2b5f8
movq 0x8(%rsp), %rcx
movq (%rcx,%r12,8), %rsi
cmpq %r15, %rsi
je 0x2b5f3
movq %r15, %rdi
callq 0x28dc2
movl 0x14(%rsp), %eax
incq %r12
jmp 0x2b5d1
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0xb1ee2
movq %r14, %rdi
xorl %esi, %esi
callq 0xb20f8
xorl %r12d, %r12d
movslq 0x24(%rsp), %rax
cmpq %rax, %r12
jge 0x2b66e
movq 0x18(%rsp), %rax
movq (%rax,%r12,8), %r15
movq 0x18(%r15), %rdi
testq %rdi, %rdi
je 0x2b64d
movq %r15, %rsi
callq 0x290e2
movq %r15, %rdi
callq 0x28a24
movl $0x140, %esi            # imm = 0x140
movq %r15, %rdi
callq 0x15630
jmp 0x2b669
movq %rbx, 0x130(%r15)
movl 0x24(%rbx), %eax
movl %eax, 0x138(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1a04c
incq %r12
jmp 0x2b612
leaq 0x18(%rsp), %rdi
callq 0xb1f48
leaq 0x8(%rsp), %rdi
callq 0xb1f48
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x2b697
jmp 0x2b6a6
jmp 0x2b6a6
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0xb1f48
jmp 0x2b6a9
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xb1f48
movq %rbx, %rdi
callq 0x15af0
nop