// File: /workspace/github_cpp_makefile_star2_O3/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 0xc352c
movl 0x24(%rbx), %eax
testl %eax, %eax
jle 0x2d7d5
xorl %r15d, %r15d
leaq 0x8(%rsp), %r14
movq 0x18(%rbx), %rcx
movq (%rcx,%r15,8), %rsi
movl 0x14(%rsi), %ecx
cmpl 0x38(%rbx), %ecx
jge 0x2d7ca
movl 0x14(%rsp), %edx
movq %r14, %rdi
callq 0xc35c2
movl 0x24(%rbx), %eax
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x2d7ab
movl 0x14(%rsp), %eax
cmpl $0x2, %eax
jl 0x2d89e
leaq 0x18(%rbx), %r14
movq 0x8(%rsp), %rcx
movq (%rcx), %r15
movl $0x1, %r12d
movq 0x8(%rsp), %rcx
movq (%rcx,%r12,8), %rsi
cmpq %r15, %rsi
je 0x2d80e
movq %r15, %rdi
callq 0x2acaa
movl 0x14(%rsp), %eax
incq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0x2d7f4
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0xc3534
movq %r14, %rdi
xorl %esi, %esi
callq 0xc378c
cmpl $0x0, 0x24(%rsp)
jle 0x2d894
xorl %r12d, %r12d
movq 0x18(%rsp), %rax
movq (%rax,%r12,8), %r15
movq 0x18(%r15), %rdi
testq %rdi, %rdi
je 0x2d86b
movq %r15, %rsi
callq 0x2b03a
movq %r15, %rdi
callq 0x2a8cc
movl $0x140, %esi            # imm = 0x140
movq %r15, %rdi
callq 0x15620
jmp 0x2d887
movq %rbx, 0x130(%r15)
movl 0x24(%rbx), %edx
movl %edx, 0x138(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0xc35c2
incq %r12
movslq 0x24(%rsp), %rax
cmpq %rax, %r12
jl 0x2d83a
leaq 0x18(%rsp), %rdi
callq 0xc3596
leaq 0x8(%rsp), %rdi
callq 0xc3596
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x2d8bd
jmp 0x2d8ce
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0xc3596
jmp 0x2d8d1
jmp 0x2d8ce
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xc3596
movq %rbx, %rdi
callq 0x15ad0
nop