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

  // Create map from colors to category identifiers
  RNMap<unsigned int, int> encoded_color_to_category_identifier;
  for (int i = 0; i < scene->NLabels(); i++) {
    R3SurfelLabel *label = scene->Label(i);
    int category_identifier = label->Identifier();
    RNRgb color = label->Color();
    unsigned int r = 255.0 * color.R();
    unsigned int g = 255.0 * color.G();
    unsigned int b = 255.0 * color.B();
    unsigned int encoded_color = 0;
    encoded_color |= r & 0xFF;
    encoded_color |= (g & 0xFF) << 8;
    encoded_color |= (b & 0xFF) << 16;
    encoded_color_to_category_identifier.Insert(encoded_color, category_identifier);
  }
  
  // Overwrite category identifiers by reverse mapping from colors
  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++) {
        const R3Surfel *surfel = block->Surfel(k);
        unsigned int r = surfel->R();
        unsigned int g = surfel->G();
        unsigned int b = surfel->B();
        unsigned int encoded_color = 0;
        encoded_color |= r & 0xFF;
        encoded_color |= (g & 0xFF) << 8;
        encoded_color |= (b & 0xFF) << 16;
        unsigned int new_category_identifier = 255;
        int category_identifier = 255;
        if (encoded_color_to_category_identifier.Find(encoded_color, &category_identifier)) 
          new_category_identifier = category_identifier;
        unsigned int old_attribute = surfel->Attribute();
        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 $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq 0x118(%rsp), %rdi
callq 0x26780
movq %rax, 0x110(%rsp)
cmpq $0x0, 0x110(%rsp)
jne 0x2252f
movl $0x0, 0x124(%rsp)
jmp 0x229f8
movq 0x110(%rsp), %rdi
callq 0x26790
movq %rax, 0x108(%rsp)
cmpq $0x0, 0x108(%rsp)
jne 0x2255f
movl $0x0, 0x124(%rsp)
jmp 0x229f8
leaq 0x100(%rsp), %rdi
callq 0x283d0
movl $0x0, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0x118(%rsp), %rdi
callq 0x266e0
movl %eax, %ecx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x2272f
movq 0x118(%rsp), %rdi
movl 0xfc(%rsp), %esi
callq 0x26af0
movq %rax, 0x58(%rsp)
jmp 0x225b8
movq 0x58(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x6f0e0
movl %eax, 0x54(%rsp)
jmp 0x225d8
movl 0x54(%rsp), %eax
movl %eax, 0xe0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x28450
movq %rax, 0x48(%rsp)
jmp 0x225f7
movq 0x48(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x16e4b0
jmp 0x2260b
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x26f40
movq 0x38(%rsp), %rdi
movsd 0x1d5a0e(%rip), %xmm1   # 0x1f8038
movsd %xmm1, 0x40(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, 0xc4(%rsp)
callq 0x26f50
movq 0x38(%rsp), %rdi
movsd 0x40(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, 0xc0(%rsp)
callq 0x26f60
movsd 0x40(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, 0xbc(%rsp)
movl $0x0, 0xb8(%rsp)
movzbl 0xc4(%rsp), %ecx
movl 0xb8(%rsp), %eax
orl %ecx, %eax
movl %eax, 0xb8(%rsp)
movzbl 0xc0(%rsp), %ecx
shll $0x8, %ecx
movl 0xb8(%rsp), %eax
orl %ecx, %eax
movl %eax, 0xb8(%rsp)
movzbl 0xbc(%rsp), %ecx
shll $0x10, %ecx
movl 0xb8(%rsp), %eax
orl %ecx, %eax
movl %eax, 0xb8(%rsp)
movl 0xb8(%rsp), %esi
movl 0xe0(%rsp), %edx
leaq 0x100(%rsp), %rdi
callq 0x28460
jmp 0x226f1
jmp 0x226f3
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x22577
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x28560
jmp 0x22a07
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x110(%rsp), %rdi
callq 0x26330
movl %eax, 0x34(%rsp)
jmp 0x22758
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0x229e0
movq 0x110(%rsp), %rdi
movl 0xb4(%rsp), %esi
callq 0x26350
movq %rax, 0x28(%rsp)
jmp 0x22783
movq 0x28(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x26380
cmpl $0x0, %eax
jle 0x227a7
jmp 0x229ca
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x27710
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x229c8
movq 0xa8(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x27730
movq %rax, 0x18(%rsp)
jmp 0x227f3
movq 0x18(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x108(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x27890
jmp 0x22817
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x98(%rsp), %rdi
callq 0x267d0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x22999
movq 0x98(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x267e0
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
callq 0x284a0
movb %al, 0x13(%rsp)
jmp 0x22877
movb 0x13(%rsp), %al
movzbl %al, %eax
movl %eax, 0x84(%rsp)
movq 0x88(%rsp), %rdi
callq 0x284b0
movb %al, 0x12(%rsp)
jmp 0x22898
movb 0x12(%rsp), %al
movzbl %al, %eax
movl %eax, 0x80(%rsp)
movq 0x88(%rsp), %rdi
callq 0x284c0
movb %al, 0x11(%rsp)
jmp 0x228b9
movb 0x11(%rsp), %al
movzbl %al, %eax
movl %eax, 0x7c(%rsp)
movl $0x0, 0x78(%rsp)
movzbl 0x84(%rsp), %ecx
movl 0x78(%rsp), %eax
orl %ecx, %eax
movl %eax, 0x78(%rsp)
movzbl 0x80(%rsp), %ecx
shll $0x8, %ecx
movl 0x78(%rsp), %eax
orl %ecx, %eax
movl %eax, 0x78(%rsp)
movzbl 0x7c(%rsp), %ecx
shll $0x10, %ecx
movl 0x78(%rsp), %eax
orl %ecx, %eax
movl %eax, 0x78(%rsp)
movl $0xff, 0x74(%rsp)
movl $0xff, 0x70(%rsp)
movl 0x78(%rsp), %esi
leaq 0x100(%rsp), %rdi
leaq 0x70(%rsp), %rdx
callq 0x284d0
movl %eax, 0xc(%rsp)
jmp 0x22931
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0x22942
movl 0x70(%rsp), %eax
movl %eax, 0x74(%rsp)
movq 0x88(%rsp), %rdi
callq 0x282e0
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
movzbl 0x74(%rsp), %ecx
orl %ecx, %eax
movl %eax, 0x68(%rsp)
movq 0x98(%rsp), %rdi
movl 0x94(%rsp), %esi
movl 0x68(%rsp), %edx
callq 0x4aa30
jmp 0x22981
jmp 0x22983
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x22822
movq 0x108(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x26820
jmp 0x229b0
jmp 0x229b2
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x227b2
jmp 0x229ca
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x2273a
movl $0x1, 0x124(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x28560
movl 0x124(%rsp), %eax
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xe8(%rsp), %rdi
callq 0x16ca0
nopw %cs:(%rax,%rax)