// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateObject(gaps::R3SurfelScene*, gaps::R3SurfelPointSet*, gaps::R3SurfelObject*, char const*, gaps::R3SurfelNode*, char const*, int, int)
R3SurfelObject *
CreateObject(R3SurfelScene *scene, 
  R3SurfelPointSet *pointset, 
  R3SurfelObject *parent_object, const char *object_name, 
  R3SurfelNode *parent_node, const char *node_name, 
  RNBoolean copy_surfels, RNBoolean release_copied_blocks)
{
  // Get parent object
  if (!parent_object) parent_object = scene->RootObject();

  // Create object
  R3SurfelObject *object = new R3SurfelObject(object_name);
    
  // Insert object into scene
  scene->InsertObject(object, parent_object);

  // Check if should create copy of surfels for new node
  if (copy_surfels) {
    // Create node
    R3SurfelNode *node = CreateNode(scene, pointset,
      parent_node, node_name, copy_surfels, release_copied_blocks);
    if (!node) { delete object; return NULL; }
    
    // Insert node into object
    object->InsertNode(node);
  }
  else {
    // Get surfel tree
    R3SurfelTree *tree = scene->Tree();
    if (!tree) return NULL;
    
    // Split nodes
    RNArray<R3SurfelNode *> nodes;
    tree->SplitNodes(*pointset, &nodes);
    if (nodes.IsEmpty()) { delete object; return NULL; }
    
    // Move nodes into object
    for (int i = 0; i < nodes.NEntries(); i++) {
      R3SurfelNode *node = nodes.Kth(i);
      R3SurfelObject *old_object = node->Object();
      if (old_object) old_object->RemoveNode(node);
      object->InsertNode(node);
    }
  }

  // Update properties
  object->UpdateProperties();

  // Return object
  return object;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, 0x10(%rsp)
movq %r8, %r12
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r13
testq %rdx, %rdx
jne 0x222d5
movq 0x10(%r13), %rax
movq (%rax), %rbp
movl $0xf8, %edi
callq 0x134f0
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x16360
movl 0x50(%rsp), %r15d
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq *0x18(%rax)
testl %r15d, %r15d
je 0x2233b
movl 0x58(%rsp), %r9d
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movl %r15d, %r8d
callq 0x2186c
testq %rax, %rax
je 0x223b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x17274
jmp 0x223d0
movq 0x8(%r13), %r15
testq %r15, %r15
je 0x223da
movq %rsp, %r12
movq %r12, %rdi
callq 0x9a7ae
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
je 0x223bb
xorl %r15d, %r15d
movl %ebp, %eax
cltq
cmpq %rax, %r15
jge 0x223c4
movq (%rsp), %rax
movq (%rax,%r15,8), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x338b4
testq %rax, %rax
je 0x2239c
movq %rax, %rdi
movq %r14, %rsi
callq 0x1677a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x17274
incq %r15
movl 0xc(%rsp), %eax
jmp 0x22371
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
jmp 0x223da
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %rsp, %rdi
callq 0x9a81c
testl %ebp, %ebp
je 0x223da
movq %rbx, %rdi
callq 0x1775a
jmp 0x223dc
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x22402
movq %rax, %r14
movl $0xf8, %esi
movq %rbx, %rdi
callq 0x13500
jmp 0x2240d
movq %rax, %r14
movq %rsp, %rdi
callq 0x9a81c
movq %r14, %rdi
callq 0x138f0