// File: 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:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x26780
movq %rax, 0xa8(%rsp)
cmpq $0x0, 0xa8(%rsp)
jne 0x221e7
movl $0x0, 0xc4(%rsp)
jmp 0x224c9
movq 0xa8(%rsp), %rdi
callq 0x26790
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xa0(%rsp)
jne 0x22217
movl $0x0, 0xc4(%rsp)
jmp 0x224c9
movq 0xb0(%rsp), %rdi
leaq 0x1dffad(%rip), %rsi    # 0x2021d3
callq 0x168d0
movq %rax, 0x98(%rsp)
cmpq $0x0, 0x98(%rsp)
jne 0x22264
movq 0xb0(%rsp), %rsi
leaq 0x1d696e(%rip), %rdi    # 0x1f8bbb
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0xc4(%rsp)
jmp 0x224c9
leaq 0x78(%rsp), %rdi
callq 0x27810
movq 0x98(%rsp), %rdi
leaq 0x1dfb4c(%rip), %rsi    # 0x201dc9
xorl %eax, %eax
leaq 0x94(%rsp), %rdx
callq 0x16d50
movl %eax, 0x38(%rsp)
jmp 0x22292
movl 0x38(%rsp), %eax
cmpl $0x1, %eax
jne 0x222ce
leaq 0x78(%rsp), %rdi
leaq 0x94(%rsp), %rsi
callq 0x27820
jmp 0x222af
jmp 0x2226e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x279d0
jmp 0x224d8
movq 0x98(%rsp), %rdi
callq 0x16730
jmp 0x222dd
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x26330
movl %eax, 0x34(%rsp)
jmp 0x22300
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0x224b4
movq 0xa8(%rsp), %rdi
movl 0x68(%rsp), %esi
callq 0x26350
movq %rax, 0x28(%rsp)
jmp 0x22328
movq 0x28(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
callq 0x26380
cmpl $0x0, %eax
jle 0x22346
jmp 0x224a4
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x60(%rsp), %rdi
callq 0x27710
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x224a2
movq 0x60(%rsp), %rdi
movl 0x5c(%rsp), %esi
callq 0x27730
movq %rax, 0x18(%rsp)
jmp 0x22383
movq 0x18(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x27890
jmp 0x223a1
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x50(%rsp), %rdi
callq 0x267d0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x2247c
movq 0x50(%rsp), %rdi
movl 0x4c(%rsp), %esi
callq 0x27910
movl %eax, 0x10(%rsp)
jmp 0x223dd
movl 0x10(%rsp), %eax
movl %eax, 0x48(%rsp)
cmpl $0x0, 0x48(%rsp)
jae 0x223ee
jmp 0x2246c
movl 0x48(%rsp), %eax
movq %rax, 0x8(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x27950
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jb 0x22410
jmp 0x2246c
movq 0x50(%rsp), %rdi
movl 0x4c(%rsp), %esi
callq 0x27970
movl %eax, 0x4(%rsp)
jmp 0x22424
movl 0x4(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x48(%rsp), %eax
movl %eax, %esi
leaq 0x78(%rsp), %rdi
callq 0x279b0
movl (%rax), %eax
movl %eax, 0x40(%rsp)
movl 0x44(%rsp), %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
movzbl 0x40(%rsp), %ecx
orl %ecx, %eax
movl %eax, 0x3c(%rsp)
movq 0x50(%rsp), %rdi
movl 0x4c(%rsp), %esi
movl 0x3c(%rsp), %edx
callq 0x4aa30
jmp 0x2246a
jmp 0x2246c
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x223a9
movq 0xa0(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x26820
jmp 0x22490
jmp 0x22492
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x2234e
jmp 0x224a4
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x222e5
movl $0x1, 0xc4(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x279d0
movl 0xc4(%rsp), %eax
addq $0xc8, %rsp
retq
movq 0x70(%rsp), %rdi
callq 0x16ca0
nopw %cs:(%rax,%rax)