// File: /workspace/github_cpp_makefile_star2_O2/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, 0x28(%rsp)
movq %rdx, 0x38(%rsp)
movl %esi, %ebx
movsd %xmm0, 0x8(%rsp)
movq %rdi, %r12
movq %r8, %r13
testq %r8, %r8
jne 0x35257
movslq 0x24c(%r12), %rax
leaq (,%rax,4), %rcx
testq %rax, %rax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
movq %r8, %r14
callq 0xe030
movq %r14, %r8
movq %rax, %r13
xorl %ecx, %ecx
movslq 0x24c(%r12), %rax
cmpq %rax, %rcx
jge 0x35271
orl $-0x1, (%r13,%rcx,4)
incq %rcx
jmp 0x35259
movslq %ebx, %rcx
movq %rcx, 0x40(%rsp)
xorl %edx, %edx
movq $0x0, (%rsp)
movq 0x230(%r12), %rcx
testl %eax, %eax
cmovlel %edx, %eax
shlq $0x3, %rax
xorl %ebp, %ebp
xorl %ebx, %ebx
movq %r13, %r14
cmpq %rbx, %rax
je 0x355d1
cmpl $0x0, (%r14)
jns 0x352b7
movsd (%rcx,%rbx), %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0x352c3
addq $0x4, %r14
addq $0x8, %rbx
incl %ebp
jmp 0x3529b
movq %r8, 0x30(%rsp)
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x6630a
addq 0x230(%r12), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x22bfc
movq (%rsp), %rax
movl %eax, (%r14)
xorl %edx, %edx
movslq 0x1c(%rsp), %rax
testq %rax, %rax
je 0x35574
movl %edx, 0x24(%rsp)
movq 0x10(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r14
leaq 0x10(%rsp), %rdi
callq 0x21340
movq 0x230(%r12), %rcx
subq %rcx, %r14
shrq $0x3, %r14
movl 0x248(%r12), %esi
movl %r14d, %eax
cltd
idivl %esi
movl %eax, %r15d
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
jle 0x35390
imull %r15d, %esi
imull %ebx, %edi
addl %r14d, %esi
leal (%rdi,%rsi), %eax
decl %eax
cltq
cmpl $0x0, (%r13,%rax,4)
jns 0x35390
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x35390
leaq (%rcx,%rax,8), %rsi
movq (%rsp), %rcx
movl %ecx, (%r13,%rax,4)
leaq 0x10(%rsp), %rdi
callq 0x22bfc
movl 0x238(%r12), %eax
decl %eax
cmpl %eax, %r14d
jge 0x353f3
movl 0x248(%r12), %eax
imull %r15d, %eax
addl %r14d, %eax
movl 0x244(%r12), %ecx
imull %ebx, %ecx
addl %ecx, %eax
incl %eax
cltq
cmpl $0x0, (%r13,%rax,4)
jns 0x353f3
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x353f3
leaq (%rcx,%rax,8), %rsi
movq (%rsp), %rcx
movl %ecx, (%r13,%rax,4)
leaq 0x10(%rsp), %rdi
callq 0x22bfc
testl %ebx, %ebx
jle 0x3544b
leal -0x1(%rbx), %eax
movl 0x248(%r12), %ecx
imull %r15d, %ecx
imull 0x244(%r12), %eax
addl %r14d, %ecx
addl %eax, %ecx
movslq %ecx, %rax
cmpl $0x0, (%r13,%rax,4)
jns 0x3544b
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x3544b
leaq (%rcx,%rax,8), %rsi
movq (%rsp), %rcx
movl %ecx, (%r13,%rax,4)
leaq 0x10(%rsp), %rdi
callq 0x22bfc
movl 0x23c(%r12), %eax
decl %eax
cmpl %eax, %ebx
jge 0x354ae
movl %ebx, %eax
incl %eax
movl 0x248(%r12), %ecx
imull %r15d, %ecx
imull 0x244(%r12), %eax
addl %r14d, %ecx
addl %eax, %ecx
movslq %ecx, %rax
cmpl $0x0, (%r13,%rax,4)
jns 0x354ae
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x354ae
leaq (%rcx,%rax,8), %rsi
movq (%rsp), %rcx
movl %ecx, (%r13,%rax,4)
leaq 0x10(%rsp), %rdi
callq 0x22bfc
testl %r15d, %r15d
jle 0x35506
leal -0x1(%r15), %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 0x35506
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x35506
leaq (%rcx,%rax,8), %rsi
movq (%rsp), %rcx
movl %ecx, (%r13,%rax,4)
leaq 0x10(%rsp), %rdi
callq 0x22bfc
movl 0x240(%r12), %eax
decl %eax
cmpl %eax, %r15d
jge 0x35569
incl %r15d
imull 0x248(%r12), %r15d
movl 0x244(%r12), %eax
imull %ebx, %eax
addl %r14d, %r15d
addl %eax, %r15d
movslq %r15d, %rax
cmpl $0x0, (%r13,%rax,4)
jns 0x35569
movq 0x230(%r12), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x35569
leaq (%rcx,%rax,8), %rsi
movq (%rsp), %rcx
movl %ecx, (%r13,%rax,4)
leaq 0x10(%rsp), %rdi
callq 0x22bfc
movl 0x24(%rsp), %edx
incl %edx
jmp 0x352f1
movq (%rsp), %rax
cmpq 0x40(%rsp), %rax
jge 0x355ac
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x35590
movq (%rsp), %rcx
movl %ebp, (%rax,%rcx,4)
cmpq $0x0, 0x28(%rsp)
movq (%rsp), %rax
je 0x355ac
movq 0x28(%rsp), %rax
movq (%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
leaq 0x10(%rsp), %rdi
callq 0x66378
movl 0x24c(%r12), %eax
movq 0x30(%rsp), %r8
xorl %edx, %edx
jmp 0x35283
testq %r8, %r8
jne 0x355de
movq %r13, %rdi
callq 0xe5d0
movq (%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x355f5
jmp 0x355f5
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810