// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: LoadSurfels(gaps::R3SurfelScene*, char const*, char const*, char const*, char const*, char const*)
static R3SurfelNode *
LoadSurfels(R3SurfelScene *scene, const char *surfels_filename, 
  const char *object_name, const char *parent_object_name,
  const char *node_name, const char *parent_node_name)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

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

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

  // Find parent object
  R3SurfelObject *parent_object = NULL;
  if (parent_object_name && 
      strcmp(parent_object_name, "None") && 
      strcmp(parent_object_name, "none") && 
      strcmp(parent_object_name, "NONE")) {
    parent_object = scene->FindObjectByName(parent_object_name);
    if (!parent_object) {
      RNFail("Unable to find parent object with name %s\n", parent_object_name);
      return NULL;
    }
  }

  // Find parent node
  R3SurfelNode *parent_node = tree->FindNodeByName(parent_node_name);
  if (!parent_node) {
    RNFail("Unable to find parent node with name %s\n", parent_node_name);
    return NULL;
  }

  // Create node
  R3SurfelNode *node = new R3SurfelNode(node_name);
  if (!node) {
    RNFail("Unable to allocate node for %s\n", surfels_filename);
    return NULL;
  }

  // Insert node into tree
  tree->InsertNode(node, parent_node);

  // Create block
  R3SurfelBlock *block = new R3SurfelBlock();
  if (!block) {
    RNFail("Unable to allocate block for %s\n", surfels_filename);
    return NULL;
  }

  // Read block
  if (!block->ReadFile(surfels_filename)) {
    RNFail("Unable to read block from %s\n", surfels_filename);
    return NULL;
  }

  // Extract subset of surfels
  if (aerial_only || terrestrial_only) {
    // Create subset
    R3SurfelPointSet *subset = new R3SurfelPointSet();

    // Fill subset
    for (int i = 0; i < block->NSurfels(); i++) {
      const R3Surfel *surfel = block->Surfel(i);
      if (surfel->IsAerial() && terrestrial_only) continue;
      if (!surfel->IsAerial() && aerial_only) continue;
      R3SurfelPoint point(block, surfel);
      subset->InsertPoint(point);
    }

    // Replace block with subset
    // Note: it is important to delete subset first, since it references block
    R3SurfelBlock *subset_block = new R3SurfelBlock(subset);
    delete subset;
    delete block;
    block = subset_block;
  }

  // Update block properties
  block->UpdateProperties();

  // Insert block into database
  database->InsertBlock(block);

  // Insert block into node
  node->InsertBlock(block);

  // Update node properties
  node->UpdateProperties();

  // Create object
  if (object_name && 
      strcmp(object_name, "None") && 
      strcmp(object_name, "none") && 
      strcmp(object_name, "NONE") && 
      parent_object) {
    // Create object
    R3SurfelObject *object = new R3SurfelObject(object_name);
    if (!object) {
      RNFail("Unable to create object\n");
      return NULL;
    }

    // Insert object into scene
    scene->InsertObject(object, parent_object);

    // Insert node into object
    object->InsertNode(node);
      
    // Update object properties
    object->UpdateProperties();
  }

  // Release block
  database->ReleaseBlock(block);

  // Print statistics
  if (print_verbose) {
    printf("Loaded surfels from %s ...\n", surfels_filename);
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Surfels = %g\n", node->Complexity());
    fflush(stdout);
  }

  // Return node
  return node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbp
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, 0x20(%rsp)
movq %rsi, %r12
movq %rdi, %r13
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0xc2368
movq %r14, %rdi
xorl %esi, %esi
callq 0x15a20
movq %r13, 0x10(%rsp)
movq 0x8(%r13), %r13
testq %r13, %r13
je 0x1af27
movq 0x10(%r13), %r14
testq %r14, %r14
je 0x1b076
testq %r15, %r15
je 0x1af33
leaq 0xee89a(%rip), %rsi     # 0x109767
movq %r15, %rdi
callq 0x158c0
testl %eax, %eax
je 0x1af33
leaq 0xee88c(%rip), %rsi     # 0x10976c
movq %r15, %rdi
callq 0x158c0
testl %eax, %eax
je 0x1af33
leaq 0xee87e(%rip), %rsi     # 0x109771
movq %r15, %rdi
callq 0x158c0
testl %eax, %eax
je 0x1af33
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x42dac
movq %rax, (%rsp)
testq %rax, %rax
jne 0x1af3b
leaq 0xee820(%rip), %rdi     # 0x10973c
xorl %r14d, %r14d
movq %r15, %rsi
jmp 0x1b0a8
leaq 0xee7d2(%rip), %rdi     # 0x109700
jmp 0x1b07d
movq $0x0, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3aaf0
testq %rax, %rax
je 0x1b08c
movq %rax, %r15
movq %r14, 0x8(%rsp)
movl $0xd0, %edi
callq 0x15610
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x38bbc
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3a9e6
movl $0xc8, %edi
callq 0x15610
movq %rax, %rbp
movq %rax, %rdi
callq 0x2df68
movq %rbp, %rdi
movq %r12, %rsi
callq 0x30fe6
testl %eax, %eax
je 0x1b09b
movl 0x153e1d(%rip), %eax    # 0x16edc8
orl 0x153e13(%rip), %eax    # 0x16edc4
je 0x1b0b4
movq %r12, 0x18(%rsp)
movl $0x60, %edi
callq 0x15610
movq %rax, %r13
movq %rax, %rdi
callq 0x34fb4
cmpl $0x0, 0x8(%rbp)
jle 0x1b038
xorl %r15d, %r15d
leaq 0x28(%rsp), %rbx
xorl %r12d, %r12d
movq (%rbp), %rdx
movb 0x2f(%rdx,%r15), %al
testb $0x2, %al
je 0x1aff8
cmpl $0x0, 0x153dd2(%rip)    # 0x16edc8
jne 0x1b028
testb $0x2, %al
jne 0x1b005
cmpl $0x0, 0x153dc1(%rip)    # 0x16edc4
jne 0x1b028
addq %r15, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x349c0
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
callq *0x68(%rax)
movq %rbx, %rdi
callq 0x34a6c
incq %r12
movslq 0x8(%rbp), %rax
addq $0x30, %r15
cmpq %rax, %r12
jl 0x1afe2
movl $0xc8, %edi
callq 0x15610
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2e272
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
movq %rbp, %rdi
callq 0x2f42a
movl $0xc8, %esi
movq %rbp, %rdi
callq 0x15620
movq 0x18(%rsp), %r12
jmp 0x1b0b7
leaq 0xee73b(%rip), %rdi     # 0x1097b8
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xc5782
jmp 0x1b218
leaq 0xee680(%rip), %rdi     # 0x109713
xorl %r14d, %r14d
movq %rbp, %rsi
jmp 0x1b0a8
leaq 0xee72d(%rip), %rdi     # 0x1097cf
xorl %r14d, %r14d
movq %r12, %rsi
xorl %eax, %eax
callq 0xc5782
jmp 0x1b218
movq %rbp, %rbx
movq %rbx, %rdi
callq 0x30960
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
callq *0x10(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x28(%rax)
movq %r14, %rdi
callq 0x39c62
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x1b168
leaq 0xee675(%rip), %rsi     # 0x109767
movq %r15, %rdi
callq 0x158c0
testl %eax, %eax
je 0x1b168
leaq 0xee667(%rip), %rsi     # 0x10976c
movq %r15, %rdi
callq 0x158c0
testl %eax, %eax
je 0x1b168
leaq 0xee659(%rip), %rsi     # 0x109771
movq %r15, %rdi
callq 0x158c0
cmpq $0x0, (%rsp)
je 0x1b168
testl %eax, %eax
je 0x1b168
movl $0xf8, %edi
callq 0x15610
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x40264
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
movq (%rsp), %rdx
callq *0x18(%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0x41532
movq %r13, %rdi
callq 0x41aa4
cmpl $0x1, 0xb4(%rbx)
jne 0x1b18d
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %rbx, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x1b1c1
decl 0xb4(%rbx)
jne 0x1b1c1
testb $0x2, 0x89(%rbx)
je 0x1b1c1
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
callq *0x18(%rax)
movq %rbx, %rdi
callq 0x2f42a
movl $0xc8, %esi
movq %rbx, %rdi
callq 0x15620
cmpl $0x0, 0x153c04(%rip)    # 0x16edcc
je 0x1b218
leaq 0xee61c(%rip), %rdi     # 0x1097ed
movq %r12, %rsi
xorl %eax, %eax
callq 0x15080
leaq 0x38(%rsp), %rdi
callq 0xc23ca
leaq 0xee3cd(%rip), %rdi     # 0x1095b9
movb $0x1, %al
callq 0x15080
movq %r14, %rdi
callq 0x38e82
leaq 0xee607(%rip), %rdi     # 0x109809
movb $0x1, %al
callq 0x15080
movq 0x152d50(%rip), %rax    # 0x16df60
movq (%rax), %rdi
callq 0x15760
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movl $0xf8, %esi
jmp 0x1b249
movq %rax, %r15
movl $0xc8, %esi
movq %rbx, %rdi
jmp 0x1b266
movq %rax, %r15
movl $0x60, %esi
movq %r13, %rdi
jmp 0x1b266
movq %rax, %r15
movl $0xc8, %esi
movq %rbp, %rdi
jmp 0x1b266
movq %rax, %r15
movl $0xd0, %esi
movq %r14, %rdi
callq 0x15620
jmp 0x1b27a
movq %rax, %r15
leaq 0x28(%rsp), %rdi
callq 0x34a6c
movq %r15, %rdi
callq 0x15ae0