// File: /workspace/github_cpp_makefile_star2_O3/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 0x7667a
xorpd %xmm0, %xmm0
movq %r15, %rdi
callq 0x79b14
movq 0x230(%rbx), %rax
movq %rax, 0x18(%rsp)
leaq 0x8(%rsp), %rdi
callq 0xa9ca8
movl 0x24c(%rbx), %eax
testl %eax, %eax
jle 0x2659f
xorl %r13d, %r13d
xorpd %xmm1, %xmm1
movsd 0xb6c26(%rip), %xmm2    # 0xdd138
movabsq $0x3ff0000000000000, %rbp # imm = 0x3FF0000000000000
movabsq $-0x4010000000000000, %r15 # imm = 0xBFF0000000000000
movq 0x18(%rsp), %r12
movq 0x230(%r14), %rcx
movsd (%rcx,%r13,8), %xmm0
ucomisd %xmm1, %xmm0
jne 0x26540
jnp 0x26590
movq 0x230(%rbx), %rcx
movsd (%rcx,%r13,8), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x26584
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0xa9d3e
movq 0x258(%rsp), %rax
movq %rbp, (%rax,%r13,8)
movl 0x24c(%rbx), %eax
xorpd %xmm1, %xmm1
movsd 0xb6bb6(%rip), %xmm2    # 0xdd138
jmp 0x26590
movq 0x258(%rsp), %rcx
movq %r15, (%rcx,%r13,8)
incq %r13
movslq %eax, %rcx
addq $0x8, %r12
cmpq %rcx, %r13
jl 0x2652b
movl 0x14(%rsp), %eax
testl %eax, %eax
je 0x2670b
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 0xa9ec8
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 0x266f6
cmpl 0x238(%rbx), %eax
jge 0x266f6
xorl %ecx, %ecx
cmpl $0x3, %r13d
sete %cl
cmpl $0x4, %r13d
cmovel %r14d, %ecx
addl %edi, %ecx
js 0x266f6
cmpl 0x23c(%rbx), %ecx
jge 0x266f6
xorl %edx, %edx
cmpl $0x1, %r13d
sete %dl
cmpl $0x2, %r13d
cmovel %r14d, %edx
addl %r15d, %edx
js 0x266f6
cmpl 0x240(%rbx), %edx
jge 0x266f6
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 0xb6ced(%rip), %xmm0    # 0xdd388
jne 0x266f6
jp 0x266f6
movq 0x230(%rbx), %rcx
movsd (%rcx,%rbp,8), %xmm0
ucomisd 0xb6a85(%rip), %xmm0    # 0xdd138
jbe 0x266e8
movq 0x18(%rsp), %rax
leaq (%rax,%rbp,8), %rsi
movl 0x14(%rsp), %edx
leaq 0x8(%rsp), %rdi
callq 0xa9d3e
movq 0x258(%rsp), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax,%rbp,8)
movl 0x24(%rsp), %edi
jmp 0x266f6
movabsq $-0x4010000000000000, %rcx # imm = 0xBFF0000000000000
movq %rcx, (%rax,%rbp,8)
decl %r13d
jne 0x26600
movl 0x14(%rsp), %eax
testl %eax, %eax
jne 0x265b1
leaq 0x28(%rsp), %rdi
movsd 0xb6a20(%rip), %xmm0    # 0xdd138
movsd 0xb6a28(%rip), %xmm2    # 0xdd148
xorpd %xmm1, %xmm1
callq 0x781e4
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x7a13a
leaq 0x8(%rsp), %rdi
callq 0xa9d12
leaq 0x28(%rsp), %rdi
callq 0x766d6
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2675e
movq %rax, %rbx
jmp 0x26776
jmp 0x26769
jmp 0x26769
jmp 0x26769
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xa9d12
leaq 0x28(%rsp), %rdi
callq 0x766d6
movq %rbx, %rdi
callq 0x138b0