// File: /workspace/github_cpp_makefile_star2_O3/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 0x237bf
movq 0x10(%r13), %rax
movq (%rax), %rbp
movl $0xf8, %edi
callq 0x134e0
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x16b68
movl 0x50(%rsp), %r15d
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq *0x18(%rax)
testl %r15d, %r15d
je 0x23822
movl 0x58(%rsp), %r9d
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movl %r15d, %r8d
callq 0x22bfc
testq %rax, %rax
je 0x238a5
movq %rbx, %rdi
movq %rax, %rsi
callq 0x17dd6
jmp 0x2389b
movq 0x8(%r13), %r15
testq %r15, %r15
je 0x238c1
movq %rsp, %r12
movq %r12, %rdi
callq 0xa9ca8
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
cmpl $0x0, 0xc(%rsp)
je 0x238b0
jle 0x23893
xorl %r15d, %r15d
movq (%rsp), %rax
movq (%rax,%r15,8), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x376f4
testq %rax, %rax
je 0x2387b
movq %rax, %rdi
movq %r14, %rsi
callq 0x16f82
movq %rbx, %rdi
movq %r14, %rsi
callq 0x17dd6
incq %r15
movslq 0xc(%rsp), %rax
cmpq %rax, %r15
jl 0x23857
movq %rsp, %rdi
callq 0xa9d12
movq %rbx, %rdi
callq 0x18324
jmp 0x238c3
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
jmp 0x238c1
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %rsp, %rdi
callq 0xa9d12
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x238e9
movq %rax, %r14
movl $0xf8, %esi
movq %rbx, %rdi
callq 0x134f0
jmp 0x238f4
movq %rax, %r14
movq %rsp, %rdi
callq 0xa9d12
movq %r14, %rdi
callq 0x138b0