// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::MaskToSeededConnectedComponent(gaps::R3Grid&, gaps::R3Grid const&, double)
static int 
MaskToSeededConnectedComponent(R3Grid& grid, const R3Grid& seeds, RNScalar isolevel)
{
  // Allocate grid of marks
  R3Grid marks(grid);
  marks.Clear(0);

  // Initialize stack 
  const RNScalar *grid_values = grid.GridValues();
  RNArray<const RNScalar *> stack;
  for (int i = 0; i < grid.NEntries(); i++) {
    RNScalar seed_value = seeds.GridValue(i);
    if (seed_value == 0) continue;
    RNScalar grid_value = grid.GridValue(i);
    if (grid_value > isolevel) {
      stack.Insert(&grid_values[i]);
      marks.SetGridValue(i, 1.0);
    }
    else {
      marks.SetGridValue(i, -1.0);
    }
  }

  // Find connected grid cells
  while (!stack.IsEmpty()) {
    // Pop tail off stack
    const RNScalar *grid_valuesp = stack.Tail();
    stack.RemoveTail();

    // Get grid index
    int grid_index = grid_valuesp - grid_values;
    assert(grid_index >= 0);
    assert(grid_index < grid.NEntries());
    assert(marks.GridValue(grid_index) == 1.0);
    assert(grid.GridValue(grid_index) > isolevel);

    // Mark connected neighbors
    int cx, cy, cz, neighbor_index;
    grid.IndexToIndices(grid_index, cx, cy, cz);
    for (int i = 0; i < 6; i++) {
      int ix = (i == 0) ? cx-1 : ((i == 1) ? cx+1 : cx);
      if ((ix >= 0) && (ix < grid.XResolution())) {
        int iy = (i == 2) ? cy-1 : ((i == 3) ? cy+1 : cy);
        if ((iy >= 0) && (iy < grid.YResolution())) {
          int iz = (i == 4) ? cz-1 : ((i == 5) ? cz+1 : cz);
          if ((iz >= 0) && (iz < grid.ZResolution())) {
            grid.IndicesToIndex(ix, iy, iz, neighbor_index);
            if (marks.GridValue(neighbor_index) == 0.0) {
              RNScalar neighbor_value = grid.GridValue(neighbor_index);
              if (neighbor_value > isolevel) {
                stack.Insert(&grid_values[neighbor_index]);
                marks.SetGridValue(neighbor_index, 1.0);
              }
              else {
                marks.SetGridValue(neighbor_index, -1.0);
              }
            }
          }
        }
      }
    }
  }

  // Mask grid
  marks.Threshold(0.5, 0.0, 1.0);
  grid.Mask(marks);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6c1d2
xorpd %xmm0, %xmm0
movq %r15, %rdi
callq 0x6eeec
movq 0x230(%rbx), %rbp
leaq 0x8(%rsp), %rdi
callq 0x9a7ae
xorl %r13d, %r13d
leaq 0x8(%rsp), %r15
movq %rbp, %r12
movslq 0x24c(%rbx), %rax
cmpq %rax, %r13
jge 0x24b4d
movq 0x230(%r14), %rax
movsd (%rax,%r13,8), %xmm0
ucomisd 0xa3880(%rip), %xmm0    # 0xc8380
jne 0x24b04
jnp 0x24b44
movq 0x230(%rbx), %rax
movsd (%rax,%r13,8), %xmm0
ucomisd 0xa361f(%rip), %xmm0    # 0xc8138
movsd 0xa363f(%rip), %xmm0    # 0xc8160
jbe 0x24b36
movq %r15, %rdi
movq %r12, %rsi
callq 0x1784c
movsd 0xa3612(%rip), %xmm0    # 0xc8148
movq 0x258(%rsp), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
addq $0x8, %r12
jmp 0x24adf
movq %rbp, 0x20(%rsp)
pushq $-0x1
popq %r14
movslq 0x14(%rsp), %rax
testq %rax, %rax
je 0x24c9e
movq 0x8(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r12
leaq 0x8(%rsp), %rdi
callq 0x17820
subq 0x20(%rsp), %r12
shrq $0x3, %r12
movl %r12d, %eax
cltd
idivl 0x248(%rbx)
movl %eax, %r15d
movl %edx, %eax
movl 0x244(%rbx), %ecx
cltd
idivl %ecx
movl %eax, %r8d
movl %r12d, %eax
cltd
idivl %ecx
movl %edx, %r12d
pushq $0x6
popq %r13
movl %r8d, 0x1c(%rsp)
movl %r13d, %ecx
subl $0x1, %r13d
jb 0x24b56
xorl %eax, %eax
cmpl $0x5, %ecx
sete %al
cmpl $0x6, %ecx
cmovel %r14d, %eax
addl %r12d, %eax
js 0x24bae
cmpl 0x238(%rbx), %eax
jge 0x24bae
xorl %edx, %edx
cmpl $0x3, %ecx
sete %dl
cmpl $0x4, %ecx
cmovel %r14d, %edx
addl %r8d, %edx
js 0x24bae
cmpl 0x23c(%rbx), %edx
jge 0x24bae
xorl %esi, %esi
cmpl $0x1, %ecx
sete %sil
cmpl $0x2, %ecx
cmovel %r14d, %esi
addl %r15d, %esi
js 0x24bae
cmpl 0x240(%rbx), %esi
jge 0x24bae
imull 0x248(%rbx), %esi
imull 0x244(%rbx), %edx
addl %eax, %esi
addl %edx, %esi
movq 0x258(%rsp), %rax
movslq %esi, %rbp
movsd (%rax,%rbp,8), %xmm0
ucomisd 0xa374a(%rip), %xmm0    # 0xc8380
jne 0x24bae
jp 0x24bae
movq 0x230(%rbx), %rcx
movsd (%rcx,%rbp,8), %xmm0
ucomisd 0xa34e2(%rip), %xmm0    # 0xc8138
jbe 0x24c8b
movq 0x20(%rsp), %rax
leaq (%rax,%rbp,8), %rsi
leaq 0x8(%rsp), %rdi
callq 0x1784c
movq 0x258(%rsp), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax,%rbp,8)
movl 0x1c(%rsp), %r8d
jmp 0x24bae
movabsq $-0x4010000000000000, %rcx # imm = 0xBFF0000000000000
movq %rcx, (%rax,%rbp,8)
jmp 0x24bae
leaq 0x28(%rsp), %rdi
movsd 0xa348d(%rip), %xmm0    # 0xc8138
movsd 0xa3495(%rip), %xmm2    # 0xc8148
xorps %xmm1, %xmm1
callq 0x6d878
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x6f406
leaq 0x8(%rsp), %rdi
callq 0x9a81c
leaq 0x28(%rsp), %rdi
callq 0x6c22a
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24cf0
movq %rax, %rbx
jmp 0x24d08
jmp 0x24cfb
jmp 0x24cfb
jmp 0x24cfb
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x9a81c
leaq 0x28(%rsp), %rdi
callq 0x6c22a
movq %rbx, %rdi
callq 0x138f0