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

  // Find parent object
  R3SurfelObject *parent_object = scene->RootObject();
  if (strcmp(parent_object_name, "Root")) {
    parent_object = scene->FindObjectByName(parent_object_name);
    if (!parent_object) {
      RNFail("Unable to find parent object with name %s\n", parent_object_name);
      return 0;
    }
  }

  // Find parent node
  R3SurfelNode *parent_node = tree->RootNode();
  if (strcmp(parent_node_name, "Root")) {
    parent_node = tree->FindNodeByName(parent_node_name);
    if (!parent_node) {
      RNFail("Unable to find parent node with name %s\n", parent_node_name);
      return 0;
    }
  }

  // Initialize identifier maps
  int max_surfel_identifier = scene->Tree()->Database()->MaxIdentifier();
  std::vector<int> label_identifiers(max_surfel_identifier+1);
  std::vector<int> instance_identifiers(max_surfel_identifier+1);
  label_identifiers.assign(max_surfel_identifier+1, -1);
  instance_identifiers.assign(max_surfel_identifier+1, -1);

  // Open csv file
  FILE *fp = fopen(csv_filename, "r");
  if (!fp) {
    RNFail("Unable to open CSV file: %s\n", csv_filename);
    return 0;
  }

  // Read csv file
  int surfel_identifier, label_identifier, instance_identifier;
  while (fscanf(fp, "%d,%d,%d", &surfel_identifier,
    &label_identifier, &instance_identifier) == (unsigned int) 3) {
    if (surfel_identifier < 0) continue;
    if (surfel_identifier > max_surfel_identifier) continue;
    // if (label_identifier == 255) label_identifier = -1; // TEMPORARY
    label_identifiers[surfel_identifier] = label_identifier;
    instance_identifiers[surfel_identifier] = instance_identifier;
  }

  // Close csv file
  fclose(fp);

  // Remove current objects
  RemoveObjects(scene);

  // Create new objects
  if (!CreateObjects(scene, instance_identifiers,
    parent_object, parent_node, FALSE)) return 0;

  // Assign object labels
  for (unsigned int i = 0; i < instance_identifiers.size(); i++) {
    // Find label
    int label_identifier = label_identifiers[i];
    if (label_identifier < 0) continue;
    R3SurfelLabel *label = scene->FindLabelByIdentifier(label_identifier);
    if (!label) continue;

    // Find object
    int instance_identifier = instance_identifiers[i];
    if (instance_identifier < 0) continue;
    R3SurfelObject *object = scene->FindObjectByIdentifier(instance_identifier);
    if (!object) continue;

    // Check assignment
    RNScalar confidence = 0.5;
    int originator = R3_SURFEL_MACHINE_ORIGINATOR;
    R3SurfelLabelAssignment *assignment = scene->FindLabelAssignment(object, label, confidence, originator);
    if (assignment) continue;

    // Create assignment
    assignment = new R3SurfelLabelAssignment(object, label, confidence, originator);
    scene->InsertLabelAssignment(assignment);
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, 0x20(%rsp)
movq 0x8(%rdi), %r15
xorl %ebp, %ebp
testq %r15, %r15
je 0x1e127
cmpq $0x0, 0x10(%r15)
je 0x1e127
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %r12
movq 0x20(%rsp), %rbx
movq 0x10(%rbx), %rax
movq (%rax), %rbp
leaq 0xf4dba(%rip), %rsi     # 0x112c2e
movq %rdx, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1de97
movq %rbx, %rdi
movq %r14, %rsi
callq 0x428ca
movq %rax, %rbp
testq %rax, %rax
je 0x1e0c3
movq 0x18(%r15), %rax
movq (%rax), %rbx
leaq 0xf4d89(%rip), %rsi     # 0x112c2e
movq %r13, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1dec8
movq %r15, %rdi
movq %r13, %rsi
callq 0x3a6a0
movq %rax, %rbx
testq %rax, %rax
je 0x1e114
movq %rbx, 0x50(%rsp)
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movslq 0xa0(%rax), %rbx
leaq 0x1(%rbx), %r14
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %r14, %rsi
callq 0x1f1ea
leaq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rdx
movq %r14, %rsi
callq 0x1f1ea
leaq 0x4(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x1f292
leaq 0x4(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x1f292
leaq 0xf7333(%rip), %rsi     # 0x115273
movq %r12, %rdi
callq 0x15760
testq %rax, %rax
je 0x1e0d1
movq %rax, %r15
movq %rbp, 0x48(%rsp)
leaq 0xe9d27(%rip), %r14     # 0x107c87
leaq 0x4(%rsp), %r12
leaq 0x2c(%rsp), %r13
leaq 0x28(%rsp), %rbp
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x15b60
cmpl $0x3, %eax
jne 0x1dfb7
movslq 0x4(%rsp), %rax
testq %rax, %rax
js 0x1df6f
cmpl %ebx, %eax
jg 0x1df6f
movl 0x2c(%rsp), %ecx
movq 0x30(%rsp), %rdx
movl %ecx, (%rdx,%rax,4)
movl 0x28(%rsp), %eax
movslq 0x4(%rsp), %rcx
movq 0x8(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
jmp 0x1df6f
movq %r15, %rdi
callq 0x15600
movq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x4dce1
leaq 0x8(%rsp), %rsi
xorl %ebp, %ebp
movq %r13, %rdi
movq 0x48(%rsp), %rdx
movq 0x50(%rsp), %rcx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x4d886
testl %eax, %eax
je 0x1e0e4
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x1e0bc
movl $0x1, %ebp
xorl %ebx, %ebx
movq 0x30(%rsp), %rax
movl (%rax,%rbx,4), %esi
testl %esi, %esi
js 0x1e0a1
movq %r13, %rdi
callq 0x429bc
movq %rax, %r14
testq %rax, %rax
je 0x1e0a1
movq 0x8(%rsp), %rax
movl (%rax,%rbx,4), %esi
testl %esi, %esi
js 0x1e0a1
movq %r13, %rdi
callq 0x4291a
movq %rax, %r15
testq %rax, %rax
je 0x1e0a1
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
movsd 0xe8ff6(%rip), %xmm0    # 0x107050
movl $0x1, %ecx
callq 0x42a46
testq %rax, %rax
jne 0x1e0a1
movl $0x50, %edi
callq 0x15610
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
movsd 0xe8fc9(%rip), %xmm0    # 0x107050
movl $0x1, %ecx
callq 0x420bc
movq (%r13), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x80(%rax)
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
movl %ebp, %ebx
sarq $0x2, %rax
incl %ebp
cmpq %rbx, %rax
ja 0x1e00d
movl $0x1, %ebp
jmp 0x1e0e4
leaq 0xe966b(%rip), %rdi     # 0x107735
xorl %ebp, %ebp
movq %r14, %rsi
jmp 0x1e120
xorl %ebp, %ebp
leaq 0xe9b90(%rip), %rdi     # 0x107c6a
movq %r12, %rsi
xorl %eax, %eax
callq 0xc3e02
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1e0fb
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x1e127
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
jmp 0x1e127
leaq 0xe95f1(%rip), %rdi     # 0x10770c
xorl %ebp, %ebp
movq %r13, %rsi
xorl %eax, %eax
callq 0xc3e02
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1e15d
movq %rax, %rbx
movl $0x50, %esi
movq %r12, %rdi
callq 0x15620
jmp 0x1e160
jmp 0x1e15d
jmp 0x1e15d
movq %rax, %rbx
jmp 0x1e177
jmp 0x1e15d
jmp 0x1e15d
jmp 0x1e15d
jmp 0x1e15d
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1e177
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x1e18e
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
movq %rbx, %rdi
callq 0x15ad0