// File: tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: SplitSurfelTreeNodes(gaps::R3SurfelScene*, char const*, int, int, double, double, double, double, int)
static int
SplitSurfelTreeNodes(R3SurfelScene *scene, const char *node_name,
  int max_parts_per_node, int max_blocks_per_node, 
  RNScalar max_node_complexity, RNScalar max_block_complexity,
  RNLength max_leaf_extent, RNLength max_block_extent, 
  int max_levels)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Get surfel tree
  R3SurfelTree *tree = scene->Tree();
  if (!tree) {
    RNFail("Scene has no surfel tree\n");
    return 0;
  }    

  // Find node
  R3SurfelNode *node = NULL;
  if (strcmp(node_name, "All")) {
    node = tree->FindNodeByName(node_name);
    if (!node) {
      RNFail("Unable to find node with name %s\n", node_name);
      return 0;
    }
  }

  // Check node
  if (node) {
    // Split nodes
    tree->SplitNodes(node,
      max_parts_per_node, max_blocks_per_node, 
      max_node_complexity, max_block_complexity, 
      max_leaf_extent, max_block_extent, 
      max_levels);

    // Print statistics
    if (print_verbose) {
      printf("Split nodes starting at %s ...\n", node_name);
      printf("  Time = %.2f seconds\n", start_time.Elapsed());
      printf("  # Nodes = %d\n", tree->NNodes());
      printf("  # Blocks = %d\n", tree->Database()->NBlocks());
      fflush(stdout);
    }
  }
  else {
    // Split nodes
    tree->SplitNodes(
      max_parts_per_node, max_blocks_per_node, 
      max_node_complexity, max_block_complexity, 
      max_leaf_extent, max_block_extent, 
      max_levels);

    // Print statistics
    if (print_verbose) {
      printf("Split all nodes  ...\n");
      printf("  Time = %.2f seconds\n", start_time.Elapsed());
      printf("  # Nodes = %d\n", tree->NNodes());
      printf("  # Blocks = %d\n", tree->Database()->NBlocks());
      fflush(stdout);
    }
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movl %edx, 0x4c(%rsp)
movl %ecx, 0x48(%rsp)
movsd %xmm0, 0x40(%rsp)
movsd %xmm1, 0x38(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm3, 0x28(%rsp)
movl %r8d, 0x24(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x16dd30
leaq 0x10(%rsp), %rdi
callq 0x263f0
movq 0x58(%rsp), %rdi
callq 0x26780
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x24e69
leaq 0x1d3d09(%rip), %rdi    # 0x1f8b5e
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0x64(%rsp)
jmp 0x25039
movq $0x0, (%rsp)
movq 0x50(%rsp), %rdi
leaq 0x1d3d18(%rip), %rsi    # 0x1f8b95
callq 0x16a80
cmpl $0x0, %eax
je 0x24ec3
movq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x60280
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
jne 0x24ec1
movq 0x50(%rsp), %rsi
leaq 0x1d3cec(%rip), %rdi    # 0x1f8b99
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0x64(%rsp)
jmp 0x25039
jmp 0x24ec3
cmpq $0x0, (%rsp)
je 0x24f86
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movl 0x4c(%rsp), %edx
movl 0x48(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x28(%rsp), %xmm3
movl 0x24(%rsp), %r8d
movq (%rdi), %rax
callq *0x18(%rax)
cmpl $0x0, 0x23f8b7(%rip)    # 0x2647c0
je 0x24f81
movq 0x50(%rsp), %rsi
leaq 0x1d3f3a(%rip), %rdi    # 0x1f8e51
movb $0x0, %al
callq 0x16090
leaq 0x10(%rsp), %rdi
callq 0x16ddf0
leaq 0x1d3650(%rip), %rdi    # 0x1f857f
movb $0x1, %al
callq 0x16090
movq 0x8(%rsp), %rdi
callq 0x26330
movl %eax, %esi
leaq 0x1d3774(%rip), %rdi    # 0x1f86bd
movb $0x0, %al
callq 0x16090
movq 0x8(%rsp), %rdi
callq 0x26790
movq %rax, %rdi
callq 0x267a0
movl %eax, %esi
leaq 0x1d3762(%rip), %rdi    # 0x1f86cd
movb $0x0, %al
callq 0x16090
movq 0x236017(%rip), %rax    # 0x25af90
movq (%rax), %rdi
callq 0x168c0
jmp 0x25031
movq 0x8(%rsp), %rdi
movl 0x4c(%rsp), %esi
movl 0x48(%rsp), %edx
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x28(%rsp), %xmm3
movl 0x24(%rsp), %ecx
movq (%rdi), %rax
callq *0x20(%rax)
cmpl $0x0, 0x23f804(%rip)    # 0x2647c0
je 0x2502f
leaq 0x1d3eac(%rip), %rdi    # 0x1f8e71
movb $0x0, %al
callq 0x16090
leaq 0x10(%rsp), %rdi
callq 0x16ddf0
leaq 0x1d35a2(%rip), %rdi    # 0x1f857f
movb $0x1, %al
callq 0x16090
movq 0x8(%rsp), %rdi
callq 0x26330
movl %eax, %esi
leaq 0x1d36c6(%rip), %rdi    # 0x1f86bd
movb $0x0, %al
callq 0x16090
movq 0x8(%rsp), %rdi
callq 0x26790
movq %rax, %rdi
callq 0x267a0
movl %eax, %esi
leaq 0x1d36b4(%rip), %rdi    # 0x1f86cd
movb $0x0, %al
callq 0x16090
movq 0x235f69(%rip), %rax    # 0x25af90
movq (%rax), %rdi
callq 0x168c0
jmp 0x25031
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)