// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::SplitObject(gaps::R3SurfelObject*, gaps::R3SurfelConstraint const*, gaps::R3SurfelObject**, gaps::R3SurfelObject**)
int
SplitObject(R3SurfelObject *object, const R3SurfelConstraint *constraint,
  R3SurfelObject **resultA, R3SurfelObject **resultB)
{
  // Get useful variables
  if (!object) return 0;
  if (!constraint) return 0;
  if (object->NNodes() == 0) return 0;
  assert(strcmp(object->Name(), "Root"));
  R3SurfelScene *scene = object->Scene();
  if (!scene) return 0;
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return 0;

  // Create constraint
  R3SurfelMultiConstraint multi_constraint;
  R3SurfelObjectConstraint object_constraint(object);
  multi_constraint.InsertConstraint(constraint);
  multi_constraint.InsertConstraint(&object_constraint);

  // Create array of nodes
  RNArray<R3SurfelNode *> nodes;
  for (int i = 0; i < object->NNodes(); i++) {
    R3SurfelNode *node = object->Node(i);
    nodes.Insert(node);
  }

  // Split all nodes
  RNArray<R3SurfelNode *> nodesA, nodesB;
  for (int i = 0; i < nodes.NEntries(); i++) {
    R3SurfelNode *node = nodes.Kth(i);
    tree->SplitLeafNodes(node, *constraint, &nodesA, &nodesB);
  }

  // Check nodesA
  if (nodesA.NEntries() == 0) {
    if (resultA) *resultA = NULL;
    if (resultB) *resultB = object; 
    return 0;
  }
    
  // Check nodesB
  if (nodesB.NEntries() == 0) {
    if (resultA) *resultA = object;
    if (resultB) *resultB = NULL; 
    return 0;
  }

  // Create new objects
  R3SurfelObject *objectA = new R3SurfelObject();
  R3SurfelObject *objectB = new R3SurfelObject();
  if (!objectA || !objectB) return 0;
      
  // Remove nodes from object
  while (object->NNodes() > 0) {
    R3SurfelNode *node = object->Node(0);
    object->RemoveNode(node);
  }
  
  // Insert nodes into objectA
  for (int j = 0; j < nodesA.NEntries(); j++) {
    R3SurfelNode *nodeA = nodesA.Kth(j);
    if (nodeA->Object()) continue;
    objectA->InsertNode(nodeA);
  }
  
  // Insert nodes into objectB
  for (int j = 0; j < nodesB.NEntries(); j++) {
    R3SurfelNode *nodeB = nodesB.Kth(j);
    if (nodeB->Object()) continue;
    objectB->InsertNode(nodeB);
  }
      
  // Copy result
  if (resultA) *resultA = objectA;
  if (resultB) *resultB = objectB;
  
  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x22d59
movq %rdi, %r15
cmpl $0x0, 0x6c(%rdi)
je 0x22d59
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x22d59
movq 0x8(%rax), %r13
testq %r13, %r13
je 0x22d59
movq %rdx, %r14
movq %rsi, %r12
leaq 0x48(%rsp), %rdi
callq 0x2ea82
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2e782
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x2ea96
leaq 0x48(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x2ea96
movq %r14, 0x8(%rsp)
movq %rbx, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x9a7ae
xorl %r14d, %r14d
leaq 0x38(%rsp), %rbx
movslq 0x6c(%r15), %rax
cmpq %rax, %r14
jge 0x22bb8
movq 0x60(%r15), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0x1784c
incq %r14
jmp 0x22b9a
leaq 0x20(%rsp), %rdi
callq 0x9a7ae
leaq 0x10(%rsp), %rdi
callq 0x9a7ae
xorl %r14d, %r14d
leaq 0x20(%rsp), %rbp
leaq 0x10(%rsp), %rbx
movslq 0x44(%rsp), %rax
cmpq %rax, %r14
jge 0x22c04
movq 0x38(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq (%r13), %rax
movq %r13, %rdi
movq %r12, %rdx
movq %rbp, %rcx
movq %rbx, %r8
callq *0x30(%rax)
incq %r14
jmp 0x22bd9
cmpl $0x0, 0x2c(%rsp)
je 0x22cea
cmpl $0x0, 0x1c(%rsp)
movq 0x30(%rsp), %r14
je 0x22d0b
movl $0xf8, %edi
callq 0x134f0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0x16360
movl $0xf8, %edi
callq 0x134f0
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x16360
cmpl $0x0, 0x6c(%r15)
jle 0x22c65
movq 0x60(%r15), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0x1677a
jmp 0x22c4d
xorl %r15d, %r15d
movslq 0x2c(%rsp), %rax
cmpq %rax, %r15
jge 0x22c9c
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x338b4
testq %rax, %rax
jne 0x22c97
movq %r12, %rdi
movq %rbx, %rsi
callq 0x17274
incq %r15
jmp 0x22c68
xorl %r15d, %r15d
movslq 0x1c(%rsp), %rax
cmpq %rax, %r15
jge 0x22cd3
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x338b4
testq %rax, %rax
jne 0x22cce
movq %r13, %rdi
movq %rbx, %rsi
callq 0x17274
incq %r15
jmp 0x22c9f
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x22ce0
movq %r12, (%rax)
pushq $0x1
popq %rax
movq %r13, %r15
movl %eax, %ebp
jmp 0x22d04
movq 0x8(%rsp), %rax
testq %rax, %rax
movq 0x30(%rsp), %r14
je 0x22cfd
andq $0x0, (%rax)
xorl %eax, %eax
movl $0x0, %ebp
testq %r14, %r14
jne 0x22d22
jmp 0x22d27
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x22d18
movq %r15, (%rax)
xorl %eax, %eax
testq %r14, %r14
je 0x22d6a
xorl %r15d, %r15d
movq %r15, (%r14)
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0x9a81c
leaq 0x20(%rsp), %rdi
callq 0x9a81c
leaq 0x38(%rsp), %rdi
callq 0x9a81c
leaq 0x60(%rsp), %rdi
callq 0x2ec74
leaq 0x48(%rsp), %rdi
callq 0x28504
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x22d27
movq %rax, %rbx
movl $0xf8, %esi
movq %r13, %rdi
jmp 0x22d88
jmp 0x22daa
movq %rax, %rbx
movl $0xf8, %esi
movq %r12, %rdi
callq 0x13500
jmp 0x22dad
jmp 0x22daa
movq %rax, %rbx
jmp 0x22db7
jmp 0x22dc3
jmp 0x22d9f
movq %rax, %rbx
jmp 0x22dda
movq %rax, %rbx
jmp 0x22dd0
jmp 0x22daa
jmp 0x22daa
jmp 0x22daa
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x9a81c
leaq 0x20(%rsp), %rdi
callq 0x9a81c
jmp 0x22dc6
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x9a81c
leaq 0x60(%rsp), %rdi
callq 0x2ec74
leaq 0x48(%rsp), %rdi
callq 0x28504
movq %rbx, %rdi
callq 0x138f0