// File: /workspace/github_cpp_makefile_star2_O1/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), %rdi
callq 0xc4b50
cmpl $0x0, 0x24(%rbx)
jle 0x2dc48
xorl %r15d, %r15d
leaq 0x8(%rsp), %r14
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rsi
movl 0x14(%rsi), %eax
cmpl 0x38(%rbx), %eax
jge 0x2dc3c
movl 0x14(%rsp), %edx
movq %r14, %rdi
callq 0xc4be6
incq %r15
movslq 0x24(%rbx), %rax
cmpq %rax, %r15
jl 0x2dc20
cmpl $0x2, 0x14(%rsp)
jl 0x2dd19
cmpl $0x2, 0x14(%rsp)
jl 0x2dc8b
movq 0x8(%rsp), %rax
movq (%rax), %r14
movl $0x1, %r15d
movq 0x8(%rsp), %rax
movq (%rax,%r15,8), %rsi
cmpq %r14, %rsi
je 0x2dc7e
movq %r14, %rdi
callq 0x2affa
incq %r15
movslq 0x14(%rsp), %rax
cmpq %rax, %r15
jl 0x2dc68
leaq 0x18(%rbx), %r14
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0xc4b58
movq %r14, %rdi
xorl %esi, %esi
callq 0xc4d90
cmpl $0x0, 0x24(%rsp)
jle 0x2dd0f
xorl %r12d, %r12d
movq 0x18(%rsp), %rax
movq (%rax,%r12,8), %r15
movq 0x18(%r15), %rdi
testq %rdi, %rdi
je 0x2dce6
movq %r15, %rsi
callq 0x2b372
testq %r15, %r15
je 0x2dd02
movq %r15, %rdi
callq 0x2ac1c
movl $0x140, %esi            # imm = 0x140
movq %r15, %rdi
callq 0x15620
jmp 0x2dd02
movq %rbx, 0x130(%r15)
movl 0x24(%rbx), %edx
movl %edx, 0x138(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0xc4be6
incq %r12
movslq 0x24(%rsp), %rax
cmpq %rax, %r12
jl 0x2dcb0
leaq 0x18(%rsp), %rdi
callq 0xc4bba
leaq 0x8(%rsp), %rdi
callq 0xc4bba
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x2dd3a
jmp 0x2dd49
jmp 0x2dd49
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0xc4bba
jmp 0x2dd4c
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xc4bba
movq %rbx, %rdi
callq 0x15ae0