// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: CreateLoResNodes(gaps::R3SurfelScene*, char const*, char const*, double)
static int
CreateLoResNodes(R3SurfelScene *scene,
  const char *lores_scene_filename, const char *lores_database_filename,
  RNLength max_distance = FLT_MAX)
{
  // Open lores scene
  R3SurfelScene lores_scene;
  if (!lores_scene.OpenFile(lores_scene_filename, lores_database_filename, "r", "r"))
    return 0;

  // Get convenient variables
  R3SurfelTree *tree = scene->Tree();
  if (!tree) { lores_scene.CloseFile(); return 0; }
  R3SurfelDatabase *database = tree->Database();
  if (!database) { lores_scene.CloseFile(); return 0; }
  R3SurfelTree *lores_tree = lores_scene.Tree();
  if (!lores_tree) { lores_scene.CloseFile(); return 0; }
  R3SurfelDatabase *lores_database = lores_tree->Database();
  if (!lores_database) { lores_scene.CloseFile(); return 0; }

  // Allocate pointset for each node
  if (tree->NNodes() == 0) { lores_scene.CloseFile(); return 0; }
  R3SurfelPointSet *pointsets = new R3SurfelPointSet [ tree->NNodes() ];
  if (!pointsets) { lores_scene.CloseFile(); return 0; }
  
  // Associate each lores surfel with a leaf node
  for (int i = 0; i < lores_tree->NNodes(); i++) {
    R3SurfelNode *lores_node = lores_tree->Node(i);
    for (int j = 0; j < lores_node->NBlocks(); j++) {
      R3SurfelBlock *lores_block = lores_node->Block(j);

      // Read source block
      lores_database->ReadBlock(lores_block);

      // Update pointsets associated with leaf nodes
      for (int k = 0; k < lores_block->NSurfels(); k++) {
        const R3Surfel *lores_surfel = lores_block->Surfel(k);
        R3Point lores_position = lores_block->SurfelPosition(k);
        R3SurfelNode *closest_node = FindClosestLeafNode(tree, lores_position, max_distance);
        if (!closest_node) continue;
        int node_index = closest_node->TreeIndex();
        pointsets[node_index].InsertPoint(R3SurfelPoint(lores_block, lores_surfel));
      }

      // Release source block
      lores_database->ReleaseBlock(lores_block);
    }
  }
      
  // Create parents for leaf nodes with lores surfels
  RNArray<R3SurfelNode *> original_nodes;
  for (int i = 0; i < tree->NNodes(); i++) 
    original_nodes.Insert(tree->Node(i));
  for (int i = 0; i < original_nodes.NEntries(); i++) {
    R3SurfelNode *node = original_nodes.Kth(i);
    if (node->NParts() > 0) continue;
    if (node == tree->RootNode()) continue;
    if (pointsets[i].NPoints() <= 0) continue;
    R3SurfelNode *parent_node = node->Parent();
    if (!parent_node) continue;
    printf("%d %.0f\n", pointsets[i].NPoints(), node->Complexity());
    char node_name[1024] = { '\0' };
    if (node->Name()) sprintf(node_name, "%s_LORES", node->Name());
    else sprintf(node_name, "LORES");
    R3SurfelNode *interior_node = CreateNode(scene, &pointsets[i], parent_node, node_name, TRUE);
    if (!interior_node) continue;
    node->SetParent(interior_node);
  }

  // Delete pointsets
  delete [] pointsets;
  
  // Close lores scene file
  lores_scene.CloseFile();   

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x658, %rsp            # imm = 0x658
movsd %xmm0, 0x58(%rsp)
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r13
xorl %r14d, %r14d
leaq 0x60(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x421dc
leaq 0xf7977(%rip), %rcx     # 0x115273
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %rcx, %r8
callq 0x44f68
testl %eax, %eax
je 0x1dd8f
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x1dceb
cmpq $0x0, 0x10(%rax)
je 0x1dcff
movq 0x68(%rsp), %rcx
testq %rcx, %rcx
je 0x1dd21
movq 0x10(%rcx), %rdx
movq %rdx, 0x20(%rsp)
testq %rdx, %rdx
je 0x1dd10
movslq 0x24(%rax), %rbx
testq %rbx, %rbx
je 0x1dd10
movq %rcx, 0x18(%rsp)
movq %rax, 0x28(%rsp)
movq %r13, 0x30(%rsp)
movl $0x60, %ecx
movq %rbx, %rax
mulq %rcx
seto %cl
orq $0x8, %rax
movq $-0x1, %r14
testb %cl, %cl
cmoveq %rax, %r14
movq %r14, %rdi
callq 0x15040
movq %rbx, (%rax)
movq %rax, (%rsp)
addq $0x8, %rax
shlq $0x5, %rbx
leaq (%rbx,%rbx,2), %r13
negq %r13
xorl %r15d, %r15d
movq %rax, 0x50(%rsp)
movq %rax, %rbx
movq %rbx, %rdi
callq 0x34b40
addq $-0x60, %r15
addq $0x60, %rbx
cmpq %r15, %r13
jne 0x1d9b1
movq 0x18(%rsp), %rax
movl 0x24(%rax), %eax
testl %eax, %eax
jle 0x1dbb4
xorl %edx, %edx
leaq 0x250(%rsp), %r13
movq 0x18(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%rdx,8), %rbx
cmpl $0x0, 0x4c(%rbx)
jle 0x1dba5
movq %rdx, 0x38(%rsp)
xorl %r14d, %r14d
movq %rbx, 0x40(%rsp)
movq 0x40(%rbx), %rax
movq (%rax,%r14,8), %r12
movl 0xb4(%r12), %eax
testl %eax, %eax
jne 0x1da3c
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r12, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x1da46
movl 0xb4(%r12), %eax
incl %eax
movl %eax, 0xb4(%r12)
movq %r14, 0x48(%rsp)
cmpl $0x0, 0x8(%r12)
jle 0x1db13
xorl %r15d, %r15d
xorl %ebp, %ebp
movq (%r12), %rbx
xorps %xmm0, %xmm0
cvtss2sd (%rbx,%r15), %xmm0
addsd 0x10(%r12), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rbx,%r15), %xmm1
addsd 0x18(%r12), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rbx,%r15), %xmm2
addsd 0x20(%r12), %xmm2
movq %r13, %rdi
callq 0x9bed2
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movsd 0x58(%rsp), %xmm0
callq 0x17ffc
testq %rax, %rax
je 0x1dafe
movq %r12, %rsi
addq %r15, %rbx
movslq 0x20(%rax), %r13
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
callq 0x344fa
leaq (,%r13,2), %rax
addq %r13, %rax
shlq $0x5, %rax
movq 0x50(%rsp), %rcx
leaq (%rcx,%rax), %rdi
movq (%rcx,%rax), %rax
movq %r14, %rsi
callq *0x68(%rax)
movq %r14, %rdi
callq 0x345ba
leaq 0x250(%rsp), %r13
incq %rbp
movslq 0x8(%r12), %rax
addq $0x30, %r15
cmpq %rax, %rbp
jl 0x1da5c
movl 0xb4(%r12), %eax
cmpl $0x1, %eax
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r14
jne 0x1db4e
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r12, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x1db88
movl 0xb4(%r12), %eax
decl %eax
movl %eax, 0xb4(%r12)
jne 0x1db88
testb $0x2, 0x89(%r12)
je 0x1db88
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *0x18(%rax)
movq %r12, %rdi
callq 0x2ef26
movl $0xc8, %esi
movq %r12, %rdi
callq 0x15620
incq %r14
movslq 0x4c(%rbx), %rax
cmpq %rax, %r14
jl 0x1da04
movq 0x18(%rsp), %rax
movl 0x24(%rax), %eax
movq 0x38(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x1d9e0
leaq 0x8(%rsp), %rdi
callq 0xc1290
movq 0x28(%rsp), %r15
cmpl $0x0, 0x24(%r15)
jle 0x1dbf2
xorl %r14d, %r14d
leaq 0x8(%rsp), %rbx
movq 0x18(%r15), %rax
movq (%rax,%r14,8), %rsi
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0xc1326
incq %r14
movslq 0x24(%r15), %rax
cmpq %rax, %r14
jl 0x1dbd2
movl 0x14(%rsp), %eax
testl %eax, %eax
jle 0x1dd32
movq (%rsp), %rcx
leaq 0x18(%rcx), %r14
leaq 0x250(%rsp), %r15
xorl %r12d, %r12d
movq 0x8(%rsp), %rcx
movq (%rcx,%r12,8), %rbp
cmpl $0x0, 0x3c(%rbp)
jle 0x1dc34
incq %r12
movslq %eax, %rcx
addq $0x60, %r14
cmpq %rcx, %r12
jl 0x1dc11
jmp 0x1dd32
movq 0x28(%rsp), %rcx
movq 0x18(%rcx), %rcx
cmpq (%rcx), %rbp
je 0x1dc20
movl (%r14), %ebx
testl %ebx, %ebx
jle 0x1dc20
movq 0x28(%rbp), %r13
testq %r13, %r13
je 0x1dc20
movq %rbp, %rdi
callq 0x38ac2
leaq 0xe9ff7(%rip), %rdi     # 0x107c58
movl %ebx, %esi
movb $0x1, %al
callq 0x15080
movl $0x400, %edx            # imm = 0x400
movq %r15, %rbx
movq %r15, %rdi
xorl %esi, %esi
callq 0x15340
movq 0xb8(%rbp), %rdx
testq %rdx, %rdx
je 0x1dc9b
movq %rbx, %rdi
leaq 0xe9fcf(%rip), %rsi     # 0x107c61
xorl %eax, %eax
callq 0x15110
jmp 0x1dcb0
movw $0x53, 0x254(%rsp)
movl $0x45524f4c, 0x250(%rsp) # imm = 0x45524F4C
leaq -0x10(%r14), %rsi
movq 0x30(%rsp), %rdi
movq %r13, %rdx
movq %rbx, %rcx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0x4ca08
testq %rax, %rax
je 0x1dce2
movq (%rbp), %rcx
movq %rbp, %rdi
movq %rax, %rsi
callq *0x20(%rcx)
movl 0x14(%rsp), %eax
jmp 0x1dc20
xorl %r14d, %r14d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x450dc
jmp 0x1dd8f
xorl %r14d, %r14d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x450dc
jmp 0x1dd8f
xorl %r14d, %r14d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x450dc
jmp 0x1dd8f
xorl %r14d, %r14d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x450dc
jmp 0x1dd8f
movq (%rsp), %r12
movq (%r12), %rcx
movq %rcx, %rax
shlq $0x5, %rax
leaq (%rax,%rax,2), %rbx
addq $0x8, %rbx
testq %rcx, %rcx
je 0x1dd68
leaq (%rax,%rax,2), %r14
movq %r12, %r15
addq $-0x58, %r15
leaq (%r15,%r14), %rdi
callq 0x34cbc
addq $-0x60, %r14
jne 0x1dd59
movq %r12, %rdi
movq %rbx, %rsi
callq 0x15c20
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x450dc
leaq 0x8(%rsp), %rdi
callq 0xc12fa
movl $0x1, %r14d
leaq 0x60(%rsp), %rdi
callq 0x4265e
movl %r14d, %eax
addq $0x658, %rsp            # imm = 0x658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1ddc2
jmp 0x1de16
jmp 0x1de16
jmp 0x1de16
jmp 0x1de16
jmp 0x1ddc2
jmp 0x1de16
jmp 0x1ddc2
jmp 0x1de16
jmp 0x1de16
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xc12fa
jmp 0x1de19
movq %rax, %rbx
testq %r15, %r15
je 0x1ddf3
negq %r15
movq (%rsp), %r12
addq $-0x58, %r12
leaq (%r12,%r15), %rdi
callq 0x34cbc
addq $-0x60, %r15
jne 0x1dde4
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x15c20
jmp 0x1de19
jmp 0x1de16
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x345ba
jmp 0x1de19
jmp 0x1de16
jmp 0x1de16
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x4265e
movq %rbx, %rdi
callq 0x15ad0