// File: /workspace/github_cpp_makefile_star2_O1/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 0x77420
xorpd %xmm0, %xmm0
movq %r15, %rdi
callq 0x7a79e
movq 0x230(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0x8(%rsp), %rdi
callq 0xaaf34
cmpl $0x0, 0x24c(%rbx)
jle 0x268c8
xorl %r13d, %r13d
xorpd %xmm1, %xmm1
movsd 0xb792a(%rip), %xmm2    # 0xde168
movsd 0xb78fa(%rip), %xmm3    # 0xde140
leaq 0x8(%rsp), %r15
movq 0x18(%rsp), %r12
movq 0x230(%r14), %rax
movsd (%rax,%r13,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x26865
jnp 0x268b5
movq 0x230(%rbx), %rax
movsd (%rax,%r13,8), %xmm0
ucomisd %xmm3, %xmm0
movapd %xmm2, %xmm0
jbe 0x268a7
movl 0x14(%rsp), %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0xaafca
movsd 0xb78ad(%rip), %xmm3    # 0xde140
movsd 0xb78cd(%rip), %xmm2    # 0xde168
xorpd %xmm1, %xmm1
movsd 0xb78a9(%rip), %xmm0    # 0xde150
movq 0x258(%rsp), %rax
movsd %xmm0, (%rax,%r13,8)
incq %r13
movslq 0x24c(%rbx), %rax
addq $0x8, %r12
cmpq %rax, %r13
jl 0x26850
movl 0x14(%rsp), %eax
testl %eax, %eax
je 0x26a34
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r12
decl %esi
leaq 0x8(%rsp), %rdi
callq 0xab13e
subq 0x18(%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, %edi
movl %r12d, %eax
cltd
idivl %ecx
movl %edx, %r12d
movl $0x6, %r13d
movl %edi, 0x24(%rsp)
xorl %eax, %eax
cmpl $0x5, %r13d
sete %al
cmpl $0x6, %r13d
cmovel %r14d, %eax
addl %r12d, %eax
js 0x26a1f
cmpl 0x238(%rbx), %eax
jge 0x26a1f
xorl %ecx, %ecx
cmpl $0x3, %r13d
sete %cl
cmpl $0x4, %r13d
cmovel %r14d, %ecx
addl %edi, %ecx
js 0x26a1f
cmpl 0x23c(%rbx), %ecx
jge 0x26a1f
xorl %edx, %edx
cmpl $0x1, %r13d
sete %dl
cmpl $0x2, %r13d
cmovel %r14d, %edx
addl %r15d, %edx
js 0x26a1f
cmpl 0x240(%rbx), %edx
jge 0x26a1f
imull 0x248(%rbx), %edx
imull 0x244(%rbx), %ecx
addl %eax, %edx
addl %ecx, %edx
movq 0x258(%rsp), %rax
movslq %edx, %rbp
movsd (%rax,%rbp,8), %xmm0
ucomisd 0xb79c4(%rip), %xmm0    # 0xde388
jne 0x26a1f
jp 0x26a1f
movq 0x230(%rbx), %rcx
movsd (%rcx,%rbp,8), %xmm0
ucomisd 0xb7764(%rip), %xmm0    # 0xde140
jbe 0x26a11
movq 0x18(%rsp), %rax
leaq (%rax,%rbp,8), %rsi
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0xaafca
movq 0x258(%rsp), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax,%rbp,8)
movl 0x24(%rsp), %edi
jmp 0x26a1f
movabsq $-0x4010000000000000, %rcx # imm = 0xBFF0000000000000
movq %rcx, (%rax,%rbp,8)
decl %r13d
jne 0x26929
movl 0x14(%rsp), %eax
testl %eax, %eax
jne 0x268da
leaq 0x28(%rsp), %rdi
movsd 0xb76ff(%rip), %xmm0    # 0xde140
movsd 0xb7707(%rip), %xmm2    # 0xde150
xorpd %xmm1, %xmm1
callq 0x78ea4
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x7adb6
leaq 0x8(%rsp), %rdi
callq 0xaaf9e
leaq 0x28(%rsp), %rdi
callq 0x7747c
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x26a87
movq %rax, %rbx
jmp 0x26a9f
jmp 0x26a92
jmp 0x26a92
jmp 0x26a92
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xaaf9e
leaq 0x28(%rsp), %rdi
callq 0x7747c
movq %rbx, %rdi
callq 0x138b0