// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: OverwriteSurfelCategoryIdentifiers(gaps::R3SurfelScene*, char const*)
static int
OverwriteSurfelCategoryIdentifiers(R3SurfelScene *scene, const char *filename)
{
  // Get convenient variables
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return 0;
  R3SurfelDatabase *database = tree->Database();
  if (!database) return 0;

  // Open file
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open surfel category identifier file %s\n", filename);
    return 0;
  }

  // Read categories
  unsigned int c;
  std::vector<unsigned int> category_identifiers;
  while (fscanf(fp, "%u", &c) == (unsigned int) 1) {
    category_identifiers.push_back(c);
  }

  // Close file
  fclose(fp);

  // Overwrite category identifiers
  for (int i = 0; i < tree->NNodes(); i++) {
    R3SurfelNode *node = tree->Node(i);
    if (node->NParts() > 0) continue;
    for (int j = 0; j < node->NBlocks(); j++) {
      R3SurfelBlock *block = node->Block(j);
      database->ReadBlock(block);
      for (int k = 0; k < block->NSurfels(); k++) {
        unsigned int surfel_identifier = block->SurfelIdentifier(k);
        if (surfel_identifier < 0) continue;
        if (surfel_identifier >= category_identifiers.size()) continue;
        unsigned int old_attribute = block->SurfelAttribute(k);
        unsigned int new_category_identifier = category_identifiers[surfel_identifier];
        unsigned int new_attribute = (old_attribute & 0xFFFFFF00) | (new_category_identifier & 0xFF);
        block->SetSurfelAttribute(k, new_attribute);
      }
      database->ReleaseBlock(block);
    }
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
xorl %ebp, %ebp
testq %rdi, %rdi
je 0x1c9c8
movq %rdi, %rbx
movq 0x10(%rdi), %r14
testq %r14, %r14
je 0x1c9c8
movq %rsi, %r12
leaq 0xf8ab0(%rip), %rsi     # 0x115273
movq %r12, %rdi
callq 0x15760
testq %rax, %rax
je 0x1c9b5
movq %rax, %r15
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r12
movaps %xmm0, (%r12)
movq $0x0, 0x10(%r12)
leaq 0xf8675(%rip), %r13     # 0x114e69
leaq 0xc(%rsp), %rbp
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x15b60
cmpl $0x1, %eax
jne 0x1c838
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x1c82b
movl 0xc(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x1c7f9
movq %r12, %rdi
movq %rbp, %rdx
callq 0x1ebe6
jmp 0x1c7f9
movq %r15, %rdi
callq 0x15600
movl 0x24(%rbx), %eax
testl %eax, %eax
jle 0x1c997
xorl %edx, %edx
movq %rbx, 0x28(%rsp)
movq 0x18(%rbx), %rcx
movq (%rcx,%rdx,8), %rbp
cmpl $0x0, 0x3c(%rbp)
jg 0x1c988
cmpl $0x0, 0x4c(%rbp)
jle 0x1c988
movq %rdx, 0x30(%rsp)
xorl %ebx, %ebx
movq 0x40(%rbp), %rax
movq (%rax,%rbx,8), %r15
movl 0xb4(%r15), %eax
testl %eax, %eax
jne 0x1c8aa
movq (%r14), %rax
movq 0x8(%r14), %rdx
movl 0x28(%r14), %ecx
movq %r14, %rdi
movq %r15, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x1c8b3
movl 0xb4(%r15), %eax
incl %eax
movl %eax, 0xb4(%r15)
movl 0x8(%r15), %eax
testl %eax, %eax
jle 0x1c907
movl $0x24, %r13d
xorl %r12d, %r12d
movq (%r15), %rcx
movl -0x4(%rcx,%r13), %esi
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
subq %rdi, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0x1c8f8
movl (%rcx,%r13), %edx
movb (%rdi,%rsi,4), %dl
movq %r15, %rdi
movl %r12d, %esi
callq 0x2fdc6
movl 0x8(%r15), %eax
incq %r12
movslq %eax, %rcx
addq $0x30, %r13
cmpq %rcx, %r12
jl 0x1c8c4
movl 0xb4(%r15), %eax
cmpl $0x1, %eax
jne 0x1c935
movq (%r14), %rax
movq 0x8(%r14), %rdx
movl 0x28(%r14), %ecx
movq %r14, %rdi
movq %r15, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x1c96b
movl 0xb4(%r15), %eax
decl %eax
movl %eax, 0xb4(%r15)
jne 0x1c96b
testb $0x2, 0x89(%r15)
je 0x1c96b
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x18(%rax)
movq %r15, %rdi
callq 0x2ef26
movl $0xc8, %esi
movq %r15, %rdi
callq 0x15620
incq %rbx
movslq 0x4c(%rbp), %rax
cmpq %rax, %rbx
jl 0x1c875
movq 0x28(%rsp), %rbx
movl 0x24(%rbx), %eax
movq 0x30(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x1c852
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1c9ae
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
movl $0x1, %ebp
jmp 0x1c9c8
leaq 0xeb15c(%rip), %rdi     # 0x107b18
xorl %ebp, %ebp
movq %r12, %rsi
xorl %eax, %eax
callq 0xc3e02
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1c9dd
jmp 0x1c9dd
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1c9f7
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
movq %rbx, %rdi
callq 0x15ad0