// File: /workspace/github_cpp_makefile_star2_O3/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 0x24372
movq %rdi, %r15
cmpl $0x0, 0x6c(%rdi)
je 0x24372
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x24372
movq 0x8(%rax), %r13
testq %r13, %r13
je 0x24372
movq %rdx, %r14
movq %rsi, %r12
leaq 0x48(%rsp), %rdi
callq 0x31b58
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x31816
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x31b6c
leaq 0x48(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x31b6c
movq %r14, 0x8(%rsp)
movq %rbx, 0x40(%rsp)
leaq 0x30(%rsp), %rdi
callq 0xa9ca8
cmpl $0x0, 0x6c(%r15)
jle 0x241a1
xorl %r14d, %r14d
leaq 0x30(%rsp), %rbx
movq 0x60(%r15), %rax
movq (%rax,%r14,8), %rsi
movl 0x3c(%rsp), %edx
movq %rbx, %rdi
callq 0xa9d3e
incq %r14
movslq 0x6c(%r15), %rax
cmpq %rax, %r14
jl 0x24181
leaq 0x20(%rsp), %rdi
callq 0xa9ca8
leaq 0x10(%rsp), %rdi
callq 0xa9ca8
cmpl $0x0, 0x3c(%rsp)
jle 0x241f2
xorl %r14d, %r14d
leaq 0x20(%rsp), %rbp
leaq 0x10(%rsp), %rbx
movq 0x30(%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
movslq 0x3c(%rsp), %rax
cmpq %rax, %r14
jl 0x241c9
cmpl $0x0, 0x2c(%rsp)
je 0x242e7
cmpl $0x0, 0x1c(%rsp)
movq 0x40(%rsp), %r14
je 0x2430b
movl $0xf8, %edi
callq 0x134e0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0x16b68
movl $0xf8, %edi
callq 0x134e0
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x16b68
cmpl $0x0, 0x6c(%r15)
jle 0x24253
movq 0x60(%r15), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0x16f82
jmp 0x2423b
cmpl $0x0, 0x2c(%rsp)
jle 0x2428f
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x376f4
testq %rax, %rax
jne 0x24282
movq %r12, %rdi
movq %rbx, %rsi
callq 0x17dd6
incq %r15
movslq 0x2c(%rsp), %rax
cmpq %rax, %r15
jl 0x2425d
cmpl $0x0, 0x1c(%rsp)
jle 0x242cb
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x376f4
testq %rax, %rax
jne 0x242be
movq %r13, %rdi
movq %rbx, %rsi
callq 0x17dd6
incq %r15
movslq 0x1c(%rsp), %rax
cmpq %rax, %r15
jl 0x24299
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x242d8
movq %r12, (%rax)
movl $0x1, %eax
movq %r13, %r15
movl $0x1, %ebp
jmp 0x24304
movq 0x8(%rsp), %rax
testq %rax, %rax
movq 0x40(%rsp), %r14
je 0x242fd
movq $0x0, (%rax)
xorl %eax, %eax
movl $0x0, %ebp
testq %r14, %r14
jne 0x24322
jmp 0x24327
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x24318
movq %r15, (%rax)
xorl %eax, %eax
testq %r14, %r14
je 0x24383
xorl %r15d, %r15d
movq %r15, (%r14)
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0xa9d12
leaq 0x20(%rsp), %rdi
callq 0xa9d12
leaq 0x30(%rsp), %rdi
callq 0xa9d12
leaq 0x60(%rsp), %rdi
callq 0x31dba
leaq 0x10a2c2(%rip), %rax    # 0x12e618
addq $0x10, %rax
leaq 0x50(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0xa9d12
leaq 0x48(%rsp), %rdi
callq 0x31dba
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x24327
movq %rax, %rbx
movl $0xf8, %esi
movq %r13, %rdi
jmp 0x243a1
jmp 0x243c3
movq %rax, %rbx
movl $0xf8, %esi
movq %r12, %rdi
callq 0x134f0
jmp 0x243c6
jmp 0x243c3
movq %rax, %rbx
jmp 0x243d0
jmp 0x243dc
jmp 0x243b8
movq %rax, %rbx
jmp 0x243f3
movq %rax, %rbx
jmp 0x243e9
jmp 0x243c3
jmp 0x243c3
jmp 0x243c3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xa9d12
leaq 0x20(%rsp), %rdi
callq 0xa9d12
jmp 0x243df
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa9d12
leaq 0x60(%rsp), %rdi
callq 0x31dba
leaq 0x10a21e(%rip), %rax    # 0x12e618
addq $0x10, %rax
leaq 0x50(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0xa9d12
leaq 0x48(%rsp), %rdi
callq 0x31dba
movq %rbx, %rdi
callq 0x138b0