// File: /workspace/github_cpp_makefile_star2_O1/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 0x426c0
leaq 0xf96d4(%rip), %rcx     # 0x117273
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %rcx, %r8
callq 0x4548c
testl %eax, %eax
je 0x1e01d
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x1df79
cmpq $0x0, 0x10(%rax)
je 0x1df8d
movq 0x68(%rsp), %rcx
testq %rcx, %rcx
je 0x1dfaf
movq 0x10(%rcx), %rdx
movq %rdx, 0x18(%rsp)
testq %rdx, %rdx
je 0x1df9e
movslq 0x24(%rax), %rbx
testq %rbx, %rbx
je 0x1df9e
movq %rcx, 0x28(%rsp)
movq %rax, 0x20(%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 0x34fb4
addq $-0x60, %r15
addq $0x60, %rbx
cmpq %r15, %r13
jne 0x1dc54
movq 0x28(%rsp), %rcx
cmpl $0x0, 0x24(%rcx)
jle 0x1de41
xorl %edx, %edx
leaq 0x250(%rsp), %r13
movq 0x18(%rcx), %rax
movq %rdx, 0x38(%rsp)
movq (%rax,%rdx,8), %rbx
cmpl $0x0, 0x4c(%rbx)
jle 0x1de27
xorl %r14d, %r14d
movq %rbx, 0x40(%rsp)
movq 0x40(%rbx), %rax
movq (%rax,%r14,8), %r12
cmpl $0x0, 0xb4(%r12)
jne 0x1dcd0
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r12, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x1dcd8
incl 0xb4(%r12)
movq %r14, 0x48(%rsp)
cmpl $0x0, 0x8(%r12)
jle 0x1dda9
xorl %r15d, %r15d
xorl %ebp, %ebp
movq (%r12), %rbx
addq %r15, %rbx
movq (%r12), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r15), %xmm0
addsd 0x10(%r12), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax,%r15), %xmm1
addsd 0x18(%r12), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax,%r15), %xmm2
addsd 0x20(%r12), %xmm2
movq %r13, %rdi
callq 0x9d282
movq 0x20(%rsp), %rdi
movq %r13, %rsi
movsd 0x58(%rsp), %xmm0
callq 0x1802c
testq %rax, %rax
je 0x1dd94
movq %r12, %rsi
movslq 0x20(%rax), %r13
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
callq 0x349c0
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 0x34a6c
leaq 0x250(%rsp), %r13
incq %rbp
movslq 0x8(%r12), %rax
addq $0x30, %r15
cmpq %rax, %rbp
jl 0x1dcee
cmpl $0x1, 0xb4(%r12)
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r14
jne 0x1ddda
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r12, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x1de17
decl 0xb4(%r12)
jne 0x1de17
testb $0x2, 0x89(%r12)
je 0x1de17
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *0x18(%rax)
testq %r12, %r12
je 0x1de17
movq %r12, %rdi
callq 0x2f42a
movl $0xc8, %esi
movq %r12, %rdi
callq 0x15620
incq %r14
movslq 0x4c(%rbx), %rax
cmpq %rax, %r14
jl 0x1dca1
movq 0x38(%rsp), %rdx
incq %rdx
movq 0x28(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
jl 0x1dc82
leaq 0x8(%rsp), %rdi
callq 0xc2904
movq 0x20(%rsp), %r15
cmpl $0x0, 0x24(%r15)
jle 0x1de7f
xorl %r14d, %r14d
leaq 0x8(%rsp), %rbx
movq 0x18(%r15), %rax
movq (%rax,%r14,8), %rsi
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0xc299a
incq %r14
movslq 0x24(%r15), %rax
cmpq %rax, %r14
jl 0x1de5f
cmpl $0x0, 0x14(%rsp)
jle 0x1dfc0
movq (%rsp), %rax
leaq 0x18(%rax), %r14
leaq 0x250(%rsp), %r15
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
movq (%rax,%r12,8), %rbp
cmpl $0x0, 0x3c(%rbp)
jle 0x1dec2
incq %r12
movslq 0x14(%rsp), %rax
addq $0x60, %r14
cmpq %rax, %r12
jl 0x1de9d
jmp 0x1dfc0
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
cmpq (%rax), %rbp
je 0x1deac
movl (%r14), %ebx
testl %ebx, %ebx
jle 0x1deac
movq 0x28(%rbp), %r13
testq %r13, %r13
je 0x1deac
movq %rbp, %rdi
callq 0x38e82
leaq 0xebd72(%rip), %rdi     # 0x109c61
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 0x1df29
movq %rbx, %rdi
leaq 0xebd4a(%rip), %rsi     # 0x109c6a
xorl %eax, %eax
callq 0x15110
jmp 0x1df3e
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 0x4d050
testq %rax, %rax
je 0x1deac
movq (%rbp), %rcx
movq %rbp, %rdi
movq %rax, %rsi
callq *0x20(%rcx)
jmp 0x1deac
xorl %r14d, %r14d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x4560c
jmp 0x1e01d
xorl %r14d, %r14d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x4560c
jmp 0x1e01d
xorl %r14d, %r14d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x4560c
jmp 0x1e01d
xorl %r14d, %r14d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x4560c
jmp 0x1e01d
movq (%rsp), %r12
movq (%r12), %rcx
movq %rcx, %rax
shlq $0x5, %rax
leaq (%rax,%rax,2), %rbx
addq $0x8, %rbx
testq %rcx, %rcx
je 0x1dff6
leaq (%rax,%rax,2), %r14
movq %r12, %r15
addq $-0x58, %r15
leaq (%r15,%r14), %rdi
callq 0x35130
addq $-0x60, %r14
jne 0x1dfe7
movq %r12, %rdi
movq %rbx, %rsi
callq 0x15c30
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x4560c
leaq 0x8(%rsp), %rdi
callq 0xc296e
movl $0x1, %r14d
leaq 0x60(%rsp), %rdi
callq 0x42b42
movl %r14d, %eax
addq $0x658, %rsp            # imm = 0x658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1e050
jmp 0x1e0a4
jmp 0x1e0a4
jmp 0x1e0a4
jmp 0x1e0a4
jmp 0x1e050
jmp 0x1e0a4
jmp 0x1e050
jmp 0x1e0a4
jmp 0x1e0a4
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xc296e
jmp 0x1e0a7
movq %rax, %rbx
testq %r15, %r15
je 0x1e081
negq %r15
movq (%rsp), %r12
addq $-0x58, %r12
leaq (%r12,%r15), %rdi
callq 0x35130
addq $-0x60, %r15
jne 0x1e072
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x15c30
jmp 0x1e0a7
jmp 0x1e0a4
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x34a6c
jmp 0x1e0a7
jmp 0x1e0a4
jmp 0x1e0a4
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x42b42
movq %rbx, %rdi
callq 0x15ae0