// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: Mask(gaps::R3SurfelScene*, char const*, gaps::R3SurfelConstraint*)
static int
Mask(R3SurfelScene *scene, const char *node_name, R3SurfelConstraint *constraint)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Get useful variables
  R3SurfelTree *tree = scene->Tree();
  if (!tree) {
    RNFail("Scene has no surfel tree\n");
    return 0;
  }    

  // Get surfel database
  R3SurfelDatabase *database = tree->Database();
  if (!database) {
    RNFail("Tree has no surfel database\n");
    return 0;
  }    

  // Find node
  R3SurfelNode *node = tree->RootNode();
  if (strcmp(node_name, "All") && strcmp(node_name, "Root")) {
    node = tree->FindNodeByName(node_name);
    if (!node) {
      RNFail("Unable to find node with name %s\n", node_name);
      return 0;
    }
  }

  // Remove surfels not satistfying constraint
  RNArray<R3SurfelNode *> remove_nodes;
  tree->SplitLeafNodes(node, *constraint, NULL, &remove_nodes);
  for (int i = 0; i < remove_nodes.NEntries(); i++) {
    R3SurfelNode *node = remove_nodes.Kth(i);

    // Make array of blocks
    RNArray<R3SurfelBlock *> blocks;
    while (node->NBlocks() > 0) {
      R3SurfelBlock *block = node->Block(0);
      node->RemoveBlock(block);
      blocks.Insert(block);
    }

    // Remove/delete node
    tree->RemoveNode(node);
    delete node;

    // Remove/delete blocks
    for (int j = 0; j < blocks.NEntries(); j++) {
      R3SurfelBlock *block = blocks.Kth(j);
      database->RemoveBlock(block);
      delete block;
    }
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0xc0cf4
xorl %r14d, %r14d
movq %r13, %rdi
xorl %esi, %esi
callq 0x15a10
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
je 0x1c724
movq 0x10(%rbx), %r14
testq %r14, %r14
je 0x1c72d
movq 0x18(%rbx), %rax
movq (%rax), %r13
leaq 0xeb4fc(%rip), %rsi     # 0x107af2
movq %r12, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1c62c
leaq 0xf6625(%rip), %rsi     # 0x112c2e
movq %r12, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1c62c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3a6a0
movq %rax, %r13
testq %rax, %rax
je 0x1c750
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0xc1290
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
movq %r12, %r8
callq *0x30(%rax)
cmpl $0x0, 0x24(%rsp)
jle 0x1c712
xorl %ebp, %ebp
leaq 0x8(%rsp), %r15
movq 0x18(%rsp), %rax
movq (%rax,%rbp,8), %r12
movq %r15, %rdi
callq 0xc1290
cmpl $0x0, 0x4c(%r12)
jle 0x1c69e
movq (%r12), %rax
movq 0x40(%r12), %rcx
movq (%rcx), %r13
movq %r12, %rdi
movq %r13, %rsi
callq *0x30(%rax)
movl 0x14(%rsp), %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0xc1326
jmp 0x1c670
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3a6f0
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
cmpl $0x0, 0x14(%rsp)
jle 0x1c6f9
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq (%rax,%r13,8), %r12
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
callq *0x18(%rax)
testq %r12, %r12
je 0x1c6df
movq %r12, %rdi
callq 0x2ef26
movl $0xc8, %esi
movq %r12, %rdi
callq 0x15620
incq %r13
movslq 0x14(%rsp), %rax
cmpq %rax, %r13
jl 0x1c6bd
movq %r15, %rdi
callq 0xc12fa
incq %rbp
movslq 0x24(%rsp), %rax
cmpq %rax, %rbp
jl 0x1c65f
leaq 0x18(%rsp), %rdi
callq 0xc12fa
movl $0x1, %r14d
jmp 0x1c73e
leaq 0xeb390(%rip), %rdi     # 0x107abb
jmp 0x1c737
leaq 0xeb3a1(%rip), %rdi     # 0x107ad5
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xc3e02
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xeb39f(%rip), %rdi     # 0x107af6
xorl %r14d, %r14d
movq %r12, %rsi
xorl %eax, %eax
callq 0xc3e02
jmp 0x1c73e
jmp 0x1c76a
jmp 0x1c771
movq %rax, %rbx
jmp 0x1c77e
jmp 0x1c771
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xc12fa
leaq 0x18(%rsp), %rdi
callq 0xc12fa
movq %rbx, %rdi
callq 0x15ad0