// File: /workspace/github_cpp_makefile_star2_O2/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 0xaf4d6
movq %r14, %rdi
callq 0x1d806
movq %r13, 0x10(%rsp)
movq 0x8(%r13), %r13
testq %r13, %r13
je 0x1c9a6
movq 0x10(%r13), %r14
testq %r14, %r14
je 0x1cab5
testq %r15, %r15
je 0x1c9b2
leaq 0xcfe03(%rip), %rsi     # 0xec74f
movq %r15, %rdi
callq 0x158e0
testl %eax, %eax
je 0x1c9b2
leaq 0xcfdf5(%rip), %rsi     # 0xec754
movq %r15, %rdi
callq 0x158e0
testl %eax, %eax
je 0x1c9b2
leaq 0xcfde7(%rip), %rsi     # 0xec759
movq %r15, %rdi
callq 0x158e0
testl %eax, %eax
je 0x1c9b2
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x3d7a4
movq %rax, (%rsp)
testq %rax, %rax
jne 0x1c9ba
leaq 0xcfd89(%rip), %rdi     # 0xec724
xorl %r14d, %r14d
movq %r15, %rsi
jmp 0x1cae7
leaq 0xcfd3b(%rip), %rdi     # 0xec6e8
jmp 0x1cabc
movq $0x0, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x36436
testq %rax, %rax
je 0x1cacb
movq %rax, %r15
movq %r14, 0x8(%rsp)
movl $0xd0, %edi
callq 0x15620
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x34858
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x36336
movl $0xc8, %edi
callq 0x15620
movq %rax, %rbp
movq %rax, %rdi
callq 0x2b438
movq %rbp, %rdi
movq %r12, %rsi
callq 0x2da34
testl %eax, %eax
je 0x1cada
movl 0x13d3ae(%rip), %eax    # 0x159dd8
orl 0x13d3a4(%rip), %eax    # 0x159dd4
je 0x1caf3
movq %r12, 0x18(%rsp)
pushq $0x60
popq %rdi
callq 0x15620
movq %rax, %r13
movq %rax, %rdi
callq 0x314d8
xorl %r15d, %r15d
leaq 0x28(%rsp), %rbx
xorl %r12d, %r12d
movslq 0x8(%rbp), %rax
cmpq %rax, %r12
jge 0x1caf8
movq (%rbp), %rdx
movb 0x2f(%rdx,%r15), %al
testb $0x2, %al
je 0x1ca7c
cmpl $0x0, 0x13d35e(%rip)    # 0x159dd8
jne 0x1caac
testb $0x2, %al
jne 0x1ca89
cmpl $0x0, 0x13d34d(%rip)    # 0x159dd4
jne 0x1caac
addq %r15, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x310c8
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
callq *0x68(%rax)
movq %rbx, %rdi
callq 0x31126
incq %r12
addq $0x30, %r15
jmp 0x1ca59
leaq 0xcfce4(%rip), %rdi     # 0xec7a0
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xb2096
jmp 0x1cc49
leaq 0xcfc29(%rip), %rdi     # 0xec6fb
xorl %r14d, %r14d
movq %rbp, %rsi
jmp 0x1cae7
leaq 0xcfcd6(%rip), %rdi     # 0xec7b7
xorl %r14d, %r14d
movq %r12, %rsi
xorl %eax, %eax
callq 0xb2096
jmp 0x1cc49
movq %rbp, %rbx
jmp 0x1cb34
movl $0xc8, %edi
callq 0x15620
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2b726
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
movq %rbp, %rdi
callq 0x2c522
movl $0xc8, %esi
movq %rbp, %rdi
callq 0x15630
movq 0x18(%rsp), %r12
movq %rbx, %rdi
callq 0x2d4bc
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 0x3570e
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x1cbe5
leaq 0xcfbe0(%rip), %rsi     # 0xec74f
movq %r15, %rdi
callq 0x158e0
testl %eax, %eax
je 0x1cbe5
leaq 0xcfbd2(%rip), %rsi     # 0xec754
movq %r15, %rdi
callq 0x158e0
testl %eax, %eax
je 0x1cbe5
leaq 0xcfbc4(%rip), %rsi     # 0xec759
movq %r15, %rdi
callq 0x158e0
cmpq $0x0, (%rsp)
je 0x1cbe5
testl %eax, %eax
je 0x1cbe5
movl $0xf8, %edi
callq 0x15620
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x3b19c
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
movq (%rsp), %rdx
callq *0x18(%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3c0b0
movq %r13, %rdi
callq 0x3c596
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1d858
cmpl $0x0, 0x13d1e3(%rip)    # 0x159ddc
je 0x1cc49
leaq 0xcfbd3(%rip), %rdi     # 0xec7d5
movq %r12, %rsi
xorl %eax, %eax
callq 0x15080
leaq 0x38(%rsp), %rdi
callq 0xaf510
leaq 0xcf984(%rip), %rdi     # 0xec5a1
movb $0x1, %al
callq 0x15080
movq %r14, %rdi
callq 0x34b20
leaq 0xcfbbe(%rip), %rdi     # 0xec7f1
movb $0x1, %al
callq 0x15080
movq 0x13c33f(%rip), %rax    # 0x158f80
movq (%rax), %rdi
callq 0x15750
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 0x1cc78
movq %rax, %r15
movl $0xc8, %esi
movq %rbx, %rdi
jmp 0x1cc95
movq %rax, %r15
pushq $0x60
popq %rsi
movq %r13, %rdi
jmp 0x1cc95
movq %rax, %r15
movl $0xc8, %esi
movq %rbp, %rdi
jmp 0x1cc95
movq %rax, %r15
movl $0xd0, %esi
movq %r14, %rdi
callq 0x15630
jmp 0x1cca9
movq %rax, %r15
leaq 0x28(%rsp), %rdi
callq 0x31126
movq %r15, %rdi
callq 0x15af0