// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::RemoveInteriorNodes(gaps::R3SurfelScene*)
int
RemoveInteriorNodes(R3SurfelScene *scene)
{
  // Get surfel tree
  R3SurfelTree *tree = scene->Tree();
  if (!tree) {
    RNFail("Scene has no surfel tree\n");
    return 0;
  }    

  // Create copy of nodes (because will edit as traverse)
  RNArray<R3SurfelNode *> interior_nodes;
  for (int i = 0; i < tree->NNodes(); i++) {
    R3SurfelNode *node = tree->Node(i);
    if (node->Name() && !strcmp(node->Name(), "Root")) continue;
    if (!node->Parent()) continue;
    if (node->NParts() == 0) continue;
    if (node->Object()) continue;
    assert(node->Tree() == node->Parent()->Tree());
    interior_nodes.Insert(node);
  }

  // Remove interior nodes
  for (int i = 0; i < interior_nodes.NEntries(); i++) {
    R3SurfelNode *node = interior_nodes.Kth(i);
    R3SurfelNode *parent = node->Parent();

    // Move parts into parent
    while (node->NParts() > 0) {
      R3SurfelNode *part = node->Part(0);
      part->SetParent(parent);
    }

    // Remove node
    tree->RemoveNode(node);
    delete node;
  }

  // Remove blocks from root node
  R3SurfelNode *node = tree->RootNode();
  while (node->NBlocks() > 0) {
    R3SurfelBlock *block = node->Block(0);
    node->RemoveBlock(block);
    tree->Database()->RemoveBlock(block);
    delete block;
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x16ce0
movq %rax, 0xf0(%rsp)
cmpq $0x0, 0xf0(%rsp)
jne 0x34b1d
leaq 0x17002a(%rip), %rdi    # 0x1a4b30
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0x104(%rsp)
jmp 0x34ec0
leaq 0xe0(%rsp), %rdi
callq 0x1b960
movl $0x0, 0xdc(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0x88(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x16cf0
movl %eax, 0x8c(%rsp)
jmp 0x34b59
movl 0x88(%rsp), %eax
movl 0x8c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x34ca4
movq 0xf0(%rsp), %rdi
movl 0xdc(%rsp), %esi
callq 0x30a90
movq %rax, 0x80(%rsp)
jmp 0x34b8d
movq 0x80(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x31110
movq %rax, 0x78(%rsp)
jmp 0x34bb1
movq 0x78(%rsp), %rax
cmpq $0x0, %rax
je 0x34c11
movq 0xc0(%rsp), %rdi
callq 0x31110
movq %rax, 0x70(%rsp)
jmp 0x34bd0
movq 0x70(%rsp), %rdi
leaq 0x16f7fc(%rip), %rsi    # 0x1a43d8
callq 0x137e0
cmpl $0x0, %eax
jne 0x34c11
jmp 0x34c8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x1b9a0
jmp 0x34ecf
movq 0xc0(%rsp), %rdi
callq 0x31100
movq %rax, 0x68(%rsp)
jmp 0x34c25
movq 0x68(%rsp), %rax
cmpq $0x0, %rax
jne 0x34c32
jmp 0x34c8e
movq 0xc0(%rsp), %rdi
callq 0x31660
movl %eax, 0x64(%rsp)
jmp 0x34c45
movl 0x64(%rsp), %eax
cmpl $0x0, %eax
jne 0x34c50
jmp 0x34c8e
movq 0xc0(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x5f2c0
movq %rax, 0x58(%rsp)
jmp 0x34c68
movq 0x58(%rsp), %rax
cmpq $0x0, %rax
je 0x34c75
jmp 0x34c8e
movq 0xc0(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x1bee0
jmp 0x34c8c
jmp 0x34c8e
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x34b35
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x50(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x16d50
movl %eax, 0x54(%rsp)
jmp 0x34ccd
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x34dcf
movl 0xbc(%rsp), %esi
leaq 0xe0(%rsp), %rdi
callq 0x310e0
movq %rax, 0x48(%rsp)
jmp 0x34cf8
movq 0x48(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x31100
movq %rax, 0x40(%rsp)
jmp 0x34d19
movq 0x40(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x31660
movl %eax, 0x3c(%rsp)
jmp 0x34d39
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
jle 0x34d82
movq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x43680
movq %rax, 0x30(%rsp)
jmp 0x34d58
movq 0x30(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x34d80
jmp 0x34d26
movq 0xf0(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x62370
jmp 0x34d99
movq 0xb0(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x34db7
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x34db9
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x34caf
movq 0xf0(%rsp), %rdi
callq 0x31030
movq %rax, 0x20(%rsp)
jmp 0x34de3
movq 0x20(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0x186b0
movl %eax, 0x1c(%rsp)
jmp 0x34e03
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
jle 0x34ea8
movq 0x98(%rsp), %rdi
xorl %esi, %esi
callq 0x186d0
movq %rax, 0x10(%rsp)
jmp 0x34e26
movq 0x10(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq (%rdi), %rax
movq 0x30(%rax), %rax
callq *%rax
jmp 0x34e4e
movq 0xf0(%rsp), %rdi
callq 0x16d10
movq %rax, 0x8(%rsp)
jmp 0x34e62
movq 0x8(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0x34e7a
movq 0x90(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x34ea3
movq (%rsp), %rdi
callq 0x4a1c0
movq (%rsp), %rdi
movl $0xc8, %esi
callq 0x13580
jmp 0x34df0
movl $0x1, 0x104(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x1b9a0
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xd0(%rsp), %rdi
callq 0x13970
nopl (%rax)