// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateConnectedPointSet(gaps::R3SurfelPointGraph*, int)
R3SurfelPointSet *
CreateConnectedPointSet(R3SurfelPointGraph *graph, int seed_index)
{
  // Initialize result
  R3SurfelPointSet *connected_pointset = new R3SurfelPointSet();
  if (graph->NPoints() == 0) return connected_pointset;

  // Find seed point 
  R3SurfelPoint *seed_point = graph->Point(seed_index);

  // Initialize marks
  for (int i = 0; i < graph->NPoints(); i++) {
    R3SurfelPoint *point = graph->Point(i);
    point->SetMark(FALSE);
  }

  // Flood fill from seed point
  RNArray<R3SurfelPoint *> stack;
  stack.Insert(seed_point);
  seed_point->SetMark(TRUE);
  while (!stack.IsEmpty()) {
    R3SurfelPoint *point = stack.Tail();
    stack.RemoveTail();
    connected_pointset->InsertPoint(*point);
    int point_index = graph->PointIndex(point);
    for (int i = 0; i < graph->NNeighbors(point_index); i++) {
      R3SurfelPoint *neighbor = graph->Neighbor(point_index, i);
      if (neighbor->IsMarked()) continue;
      stack.InsertTail(neighbor);
      neighbor->SetMark(TRUE);
    }
  }

  // Return connected pointset
  return connected_pointset;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movl %esi, 0xa4(%rsp)
movl $0x60, %edi
callq 0x13570
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
callq 0x55df0
jmp 0x395c2
movq 0x40(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x43e20
cmpl $0x0, %eax
jne 0x3961e
movq 0x98(%rsp), %rax
movq %rax, 0xb0(%rsp)
jmp 0x39838
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
movl $0x60, %esi
callq 0x13580
jmp 0x39848
movq 0xa8(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x43e40
movq %rax, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x43e20
movl %eax, %ecx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0x39690
movq 0xa8(%rsp), %rdi
movl 0x7c(%rsp), %esi
callq 0x43e40
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x55d10
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x39642
leaq 0x60(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x43e70
movq 0x30(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x43e80
jmp 0x396b3
movq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x55d10
jmp 0x396c7
jmp 0x396c9
leaq 0x60(%rsp), %rdi
callq 0x31da0
movl %eax, 0x2c(%rsp)
jmp 0x396d9
movl 0x2c(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x396ee
jmp 0x3981e
leaq 0x60(%rsp), %rdi
callq 0x43eb0
movq %rax, 0x20(%rsp)
jmp 0x396ff
movq 0x20(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x1c0c0
jmp 0x39715
movq 0x98(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
jmp 0x3972d
movq 0xa8(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x43ec0
movl %eax, 0x1c(%rsp)
jmp 0x39745
movl 0x1c(%rsp), %eax
movl %eax, 0x54(%rsp)
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0xa8(%rsp), %rdi
movl 0x54(%rsp), %esi
callq 0x43ef0
movl %eax, 0x18(%rsp)
jmp 0x39774
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
cmpl %ecx, %eax
jge 0x39819
movq 0xa8(%rsp), %rdi
movl 0x54(%rsp), %esi
movl 0x50(%rsp), %edx
callq 0x43f20
movq %rax, 0x8(%rsp)
jmp 0x397a0
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
callq 0x43f60
movl %eax, 0x4(%rsp)
jmp 0x397ba
movl 0x4(%rsp), %eax
cmpl $0x0, %eax
je 0x397e5
jmp 0x39809
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x43fb0
jmp 0x39848
movq 0x48(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x43f80
jmp 0x397f6
movq 0x48(%rsp), %rdi
movl $0x1, %esi
callq 0x55d10
jmp 0x39807
jmp 0x39809
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x39755
jmp 0x396c9
movq 0x98(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x43fb0
movq 0xb0(%rsp), %rax
addq $0xb8, %rsp
retq
movq 0x90(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)