// File: /workspace/github_cpp_makefile_star2_O3/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 %r8, %r12
movq %rcx, %rbp
movq %rdx, 0x40(%rsp)
movl %esi, %ebx
movsd %xmm0, 0x18(%rsp)
movq %rdi, %r13
movslq 0x24c(%rdi), %r15
movq %r8, %r14
testq %r8, %r8
jne 0x39c09
leaq (,%r15,4), %rax
testl %r15d, %r15d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
testl %r15d, %r15d
jle 0x39f6c
xorl %ecx, %ecx
movl $0xffffffff, (%r14,%rcx,4) # imm = 0xFFFFFFFF
incq %rcx
movslq 0x24c(%r13), %rax
cmpq %rax, %rcx
jl 0x39c14
testl %eax, %eax
jle 0x39f6c
movq %rbp, 0x28(%rsp)
movslq %ebx, %rbx
xorl %r15d, %r15d
movq 0x230(%r13), %rcx
movl %eax, %eax
xorl %ebp, %ebp
cmpl $0x0, (%r14,%rbp,4)
jns 0x39c5d
movsd (%rcx,%rbp,8), %xmm0
ucomisd 0x18(%rsp), %xmm0
ja 0x39c6a
incq %rbp
cmpq %rbp, %rax
jne 0x39c49
jmp 0x39f6f
movq %rbx, 0x38(%rsp)
movq %rsp, %rdi
callq 0x6f80c
movl %ebp, %ebx
leaq (,%rbx,8), %rsi
addq 0x230(%r13), %rsi
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x6f8a2
movq %r12, 0x30(%rsp)
movq %r15, 0x10(%rsp)
movl %r15d, (%r14,%rbx,4)
movl 0xc(%rsp), %eax
xorl %ecx, %ecx
testl %eax, %eax
je 0x39f1d
movl %ecx, 0x24(%rsp)
movq (%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %rbx
decl %esi
movq %rsp, %rdi
callq 0x6fa2c
movq 0x230(%r13), %rcx
subq %rcx, %rbx
shrq $0x3, %rbx
movl 0x248(%r13), %esi
movl %ebx, %eax
cltd
idivl %esi
movl %eax, %r12d
movl %edx, %eax
movl 0x244(%r13), %edi
cltd
idivl %edi
movl %eax, %r15d
movl %ebx, %eax
cltd
idivl %edi
movl %edx, %ebx
testl %edx, %edx
jle 0x39d3f
imull %r12d, %esi
imull %r15d, %edi
addl %ebx, %esi
leal (%rdi,%rsi), %eax
decl %eax
cltq
cmpl $0x0, (%r14,%rax,4)
jns 0x39d3f
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x39d3f
leaq (%rcx,%rax,8), %rsi
movq 0x10(%rsp), %rcx
movl %ecx, (%r14,%rax,4)
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x6f8a2
movl 0x238(%r13), %eax
decl %eax
cmpl %eax, %ebx
jge 0x39d9e
movl 0x248(%r13), %eax
imull %r12d, %eax
addl %ebx, %eax
movl 0x244(%r13), %ecx
imull %r15d, %ecx
addl %ecx, %eax
incl %eax
cltq
cmpl $0x0, (%r14,%rax,4)
jns 0x39d9e
movq 0x230(%r13), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x39d9e
leaq (%rcx,%rax,8), %rsi
movq 0x10(%rsp), %rcx
movl %ecx, (%r14,%rax,4)
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x6f8a2
testl %r15d, %r15d
jle 0x39df5
leal -0x1(%r15), %eax
movl 0x248(%r13), %ecx
imull %r12d, %ecx
imull 0x244(%r13), %eax
addl %ebx, %ecx
addl %eax, %ecx
movslq %ecx, %rax
cmpl $0x0, (%r14,%rax,4)
jns 0x39df5
movq 0x230(%r13), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x39df5
leaq (%rcx,%rax,8), %rsi
movq 0x10(%rsp), %rcx
movl %ecx, (%r14,%rax,4)
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x6f8a2
movl 0x23c(%r13), %eax
decl %eax
cmpl %eax, %r15d
jge 0x39e56
movl %r15d, %eax
incl %eax
movl 0x248(%r13), %ecx
imull %r12d, %ecx
imull 0x244(%r13), %eax
addl %ebx, %ecx
addl %eax, %ecx
movslq %ecx, %rax
cmpl $0x0, (%r14,%rax,4)
jns 0x39e56
movq 0x230(%r13), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x39e56
leaq (%rcx,%rax,8), %rsi
movq 0x10(%rsp), %rcx
movl %ecx, (%r14,%rax,4)
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x6f8a2
testl %r12d, %r12d
jle 0x39ead
leal -0x1(%r12), %eax
imull 0x248(%r13), %eax
movl 0x244(%r13), %ecx
imull %r15d, %ecx
addl %ebx, %eax
addl %ecx, %eax
cltq
cmpl $0x0, (%r14,%rax,4)
jns 0x39ead
movq 0x230(%r13), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x39ead
leaq (%rcx,%rax,8), %rsi
movq 0x10(%rsp), %rcx
movl %ecx, (%r14,%rax,4)
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x6f8a2
movl 0x240(%r13), %eax
decl %eax
cmpl %eax, %r12d
jge 0x39f0e
incl %r12d
imull 0x248(%r13), %r12d
movl 0x244(%r13), %eax
imull %r15d, %eax
addl %ebx, %r12d
addl %eax, %r12d
movslq %r12d, %rax
cmpl $0x0, (%r14,%rax,4)
jns 0x39f0e
movq 0x230(%r13), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x39f0e
leaq (%rcx,%rax,8), %rsi
movq 0x10(%rsp), %rcx
movl %ecx, (%r14,%rax,4)
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0x6f8a2
movl 0x24(%rsp), %ecx
incl %ecx
movl 0xc(%rsp), %eax
jmp 0x39ca8
movq 0x10(%rsp), %r15
movq 0x38(%rsp), %rbx
cmpq %rbx, %r15
movq 0x30(%rsp), %r12
jge 0x39f50
movq 0x40(%rsp), %rax
testq %rax, %rax
je 0x39f3f
movl %ebp, (%rax,%r15,4)
cmpq $0x0, 0x28(%rsp)
je 0x39f50
movq 0x28(%rsp), %rax
movl %ecx, (%rax,%r15,4)
incq %r15
movq %rsp, %rdi
callq 0x6f876
movl 0x24c(%r13), %eax
testl %eax, %eax
jg 0x39c3e
jmp 0x39f6f
xorl %r15d, %r15d
testq %r12, %r12
jne 0x39f7c
movq %r14, %rdi
callq 0xe5b0
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x39f92
jmp 0x39f92
movq %rax, %rbx
movq %rsp, %rdi
callq 0x6f876
movq %rbx, %rdi
callq 0xe7e0
nop