// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::ConnectedComponents(double, int, int*, int*, int*)
int R3Grid::
ConnectedComponents(RNScalar isolevel, int max_components, int *seeds, int *sizes, int *grid_components)
{
  // Allocate array of component identifiers
  int *components = grid_components;
  if (!grid_components) {
    components = new int [ grid_size ];
    assert(components);
  }

  // Initialize array of components
  for (int i = 0; i < grid_size; i++) 
    components[i] = -1;

  // Find connected components
  int ncomponents = 0;
  int next_seed = 0;
  while (TRUE){
    // Find unmarked seed
    int seed = next_seed;
    while (seed < grid_size) { 
      if ((components[seed] < 0) && (grid_values[seed] > isolevel)) break;
      seed++;
    }

    // Check if found a seed
    if (seed >= grid_size) break;

    // Flood fill marking all grid entries 6-connected to seed
    int x, y, z, neighbor;
    int size = 0;
    RNArray<RNScalar *> stack;
    stack.Insert(&grid_values[seed]);
    components[seed] = ncomponents;
    while (!stack.IsEmpty()) {
      // Pop top of stack
      RNScalar *c = stack.Tail();
      stack.RemoveTail();

      // Add grid entry to component
      int index = c - grid_values;
      assert(index >= 0);
      assert(index < grid_size);
      assert(components[index] == ncomponents);
      assert(grid_values[index] > isolevel);

      // Add neighbors to stack
      IndexToIndices(index, x, y, z);
      if (x > 0) {
        IndicesToIndex(x-1, y, z, neighbor);
        if ((components[neighbor] < 0) && (grid_values[neighbor] > isolevel)) {
          components[neighbor] = ncomponents;
          stack.Insert(&grid_values[neighbor]);
        }
      }
      if (x < XResolution()-1) {
        IndicesToIndex(x+1, y, z, neighbor);
        if ((components[neighbor] < 0) && (grid_values[neighbor] > isolevel)) {
          components[neighbor] = ncomponents;
          stack.Insert(&grid_values[neighbor]);
        }
      }
      if (y > 0) {
        IndicesToIndex(x, y-1, z, neighbor);
        if ((components[neighbor] < 0) && (grid_values[neighbor] > isolevel)) {
          components[neighbor] = ncomponents;
          stack.Insert(&grid_values[neighbor]);
        }
      }
      if (y < YResolution()-1) {
        IndicesToIndex(x, y+1, z, neighbor);
        if ((components[neighbor] < 0) && (grid_values[neighbor] > isolevel)) { 
          components[neighbor] = ncomponents;
          stack.Insert(&grid_values[neighbor]);
        }
      }
      if (z > 0) {
        IndicesToIndex(x, y, z-1, neighbor);
        if ((components[neighbor] < 0) && (grid_values[neighbor] > isolevel)) {
          components[neighbor] = ncomponents;
          stack.Insert(&grid_values[neighbor]);
        }
      }
      if (z < ZResolution()-1) {
        IndicesToIndex(x, y, z+1, neighbor);
        if ((components[neighbor] < 0) && (grid_values[neighbor] > isolevel)) {
          components[neighbor] = ncomponents;
          stack.Insert(&grid_values[neighbor]);
        }
      }

      // Increment component_size
      size++;
    }

    // Update output variables
    if (ncomponents < max_components) {
      if (seeds) seeds[ncomponents] = seed;
      if (sizes) sizes[ncomponents] = size;
    }

    // Increment the number of components
    ncomponents++;
  }


  // Delete components
  if (!grid_components) delete [] components;

  // Return number of connected components found
  return ncomponents;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, 0x30(%rsp)
movq %rdx, 0x40(%rsp)
movl %esi, 0x28(%rsp)
movq %rdi, %r12
movq %r8, %r13
movq %r8, 0x38(%rsp)
testq %r8, %r8
movsd %xmm0, 0x18(%rsp)
jne 0x3a00e
movslq 0x24c(%r12), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movsd 0x18(%rsp), %xmm0
movq %rax, %r13
cmpl $0x0, 0x24c(%r12)
jle 0x3a034
xorl %eax, %eax
movl $0xffffffff, (%r13,%rax,4) # imm = 0xFFFFFFFF
incq %rax
movslq 0x24c(%r12), %rcx
cmpq %rcx, %rax
jl 0x3a01b
xorl %r14d, %r14d
movslq 0x24c(%r12), %rax
testq %rax, %rax
setg %dl
jle 0x3a071
movq 0x230(%r12), %rcx
xorl %r15d, %r15d
cmpl $0x0, (%r13,%r15,4)
jns 0x3a066
movsd (%rcx,%r15,8), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3a07c
incq %r15
cmpq %rax, %r15
setb %dl
jne 0x3a052
xorl %eax, %eax
testb %al, %al
jne 0x3a037
jmp 0x3a3b6
testb $0x1, %dl
je 0x3a071
leaq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x705ec
movl %r15d, %ebx
leaq (,%rbx,8), %rsi
addq 0x230(%r12), %rsi
movl 0x14(%rsp), %edx
movq %rbp, %rdi
callq 0x70682
movq %r14, 0x20(%rsp)
movl %r14d, (%r13,%rbx,4)
movl 0x14(%rsp), %eax
xorl %edx, %edx
testl %eax, %eax
je 0x3a36f
movl %edx, 0x2c(%rsp)
movq 0x8(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r14
decl %esi
leaq 0x8(%rsp), %rdi
callq 0x707f6
movq 0x230(%r12), %rcx
subq %rcx, %r14
shrq $0x3, %r14
movl 0x248(%r12), %esi
movl %r14d, %eax
cltd
idivl %esi
movl %eax, %ebp
movl %edx, %eax
movl 0x244(%r12), %edi
cltd
idivl %edi
movl %eax, %ebx
movl %r14d, %eax
cltd
idivl %edi
movl %edx, %r14d
testl %edx, %edx
movsd 0x18(%rsp), %xmm1
jle 0x3a168
imull %ebp, %esi
imull %ebx, %edi
addl %r14d, %esi
leal (%rdi,%rsi), %eax
decl %eax
cltq
cmpl $0x0, (%r13,%rax,4)
jns 0x3a168
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a168
leaq (%rcx,%rax,8), %rsi
movq 0x20(%rsp), %rcx
movl %ecx, (%r13,%rax,4)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0x70682
movsd 0x18(%rsp), %xmm1
movl 0x238(%r12), %eax
decl %eax
cmpl %eax, %r14d
jge 0x3a1d3
movl 0x248(%r12), %eax
imull %ebp, %eax
addl %r14d, %eax
movl 0x244(%r12), %ecx
imull %ebx, %ecx
addl %ecx, %eax
incl %eax
cltq
cmpl $0x0, (%r13,%rax,4)
jns 0x3a1d3
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a1d3
leaq (%rcx,%rax,8), %rsi
movq 0x20(%rsp), %rcx
movl %ecx, (%r13,%rax,4)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0x70682
movsd 0x18(%rsp), %xmm1
testl %ebx, %ebx
jle 0x3a233
leal -0x1(%rbx), %eax
movl 0x248(%r12), %ecx
imull %ebp, %ecx
imull 0x244(%r12), %eax
addl %r14d, %ecx
addl %eax, %ecx
movslq %ecx, %rax
cmpl $0x0, (%r13,%rax,4)
jns 0x3a233
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a233
leaq (%rcx,%rax,8), %rsi
movq 0x20(%rsp), %rcx
movl %ecx, (%r13,%rax,4)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0x70682
movsd 0x18(%rsp), %xmm1
movl 0x23c(%r12), %eax
decl %eax
cmpl %eax, %ebx
jge 0x3a29e
movl %ebx, %eax
incl %eax
movl 0x248(%r12), %ecx
imull %ebp, %ecx
imull 0x244(%r12), %eax
addl %r14d, %ecx
addl %eax, %ecx
movslq %ecx, %rax
cmpl $0x0, (%r13,%rax,4)
jns 0x3a29e
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a29e
leaq (%rcx,%rax,8), %rsi
movq 0x20(%rsp), %rcx
movl %ecx, (%r13,%rax,4)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0x70682
movsd 0x18(%rsp), %xmm1
testl %ebp, %ebp
jle 0x3a2fd
leal -0x1(%rbp), %eax
imull 0x248(%r12), %eax
movl 0x244(%r12), %ecx
imull %ebx, %ecx
addl %r14d, %eax
addl %ecx, %eax
cltq
cmpl $0x0, (%r13,%rax,4)
jns 0x3a2fd
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a2fd
leaq (%rcx,%rax,8), %rsi
movq 0x20(%rsp), %rcx
movl %ecx, (%r13,%rax,4)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0x70682
movsd 0x18(%rsp), %xmm1
movl 0x240(%r12), %eax
decl %eax
cmpl %eax, %ebp
jge 0x3a360
incl %ebp
imull 0x248(%r12), %ebp
movl 0x244(%r12), %eax
imull %ebx, %eax
addl %r14d, %ebp
addl %eax, %ebp
movslq %ebp, %rax
cmpl $0x0, (%r13,%rax,4)
jns 0x3a360
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3a360
leaq (%rcx,%rax,8), %rsi
movq 0x20(%rsp), %rcx
movl %ecx, (%r13,%rax,4)
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0x70682
movl 0x2c(%rsp), %edx
incl %edx
movl 0x14(%rsp), %eax
jmp 0x3a0bd
movq 0x20(%rsp), %r14
cmpl 0x28(%rsp), %r14d
jge 0x3a39c
movslq %r14d, %rax
movq 0x40(%rsp), %rcx
testq %rcx, %rcx
je 0x3a38c
movl %r15d, (%rcx,%rax,4)
cmpq $0x0, 0x30(%rsp)
je 0x3a39c
movq 0x30(%rsp), %rcx
movl %edx, (%rcx,%rax,4)
incl %r14d
leaq 0x8(%rsp), %rdi
callq 0x70656
movsd 0x18(%rsp), %xmm0
movb $0x1, %al
jmp 0x3a073
cmpq $0x0, 0x38(%rsp)
jne 0x3a3c6
movq %r13, %rdi
callq 0xe5b0
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3a3dc
jmp 0x3a3dc
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x70656
movq %rbx, %rdi
callq 0xe7e0
nop