// File: /workspace/github_cpp_makefile_star2_O1/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 0x1e3b5
cmpq $0x0, 0x10(%r15)
je 0x1e3b5
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %r12
movq 0x20(%rsp), %rbx
movq 0x10(%rbx), %rax
movq (%rax), %rbp
leaq 0xf6b3c(%rip), %rsi     # 0x114c3e
movq %rdx, %rdi
callq 0x158c0
testl %eax, %eax
je 0x1e125
movq %rbx, %rdi
movq %r14, %rsi
callq 0x42dac
movq %rax, %rbp
testq %rax, %rax
je 0x1e351
movq 0x18(%r15), %rax
movq (%rax), %rbx
leaq 0xf6b0b(%rip), %rsi     # 0x114c3e
movq %r13, %rdi
callq 0x158c0
testl %eax, %eax
je 0x1e156
movq %r15, %rdi
movq %r13, %rsi
callq 0x3aaf0
movq %rax, %rbx
testq %rax, %rax
je 0x1e3a2
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 0x1f466
leaq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rdx
movq %r14, %rsi
callq 0x1f466
leaq 0x4(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x1f50c
leaq 0x4(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x1f50c
leaq 0xf90a5(%rip), %rsi     # 0x117273
movq %r12, %rdi
callq 0x15770
testq %rax, %rax
je 0x1e35f
movq %rax, %r15
movq %rbp, 0x48(%rsp)
leaq 0xebaa2(%rip), %r14     # 0x109c90
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 0x15b70
cmpl $0x3, %eax
jne 0x1e245
movslq 0x4(%rsp), %rax
testq %rax, %rax
js 0x1e1fd
cmpl %ebx, %eax
jg 0x1e1fd
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 0x1e1fd
movq %r15, %rdi
callq 0x15600
movq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x4e37e
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 0x4df2a
testl %eax, %eax
je 0x1e372
movq 0x10(%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x1e34a
movl $0x1, %ebp
xorl %ebx, %ebx
movq 0x30(%rsp), %rax
movl (%rax,%rbx,4), %esi
testl %esi, %esi
js 0x1e32f
movq %r13, %rdi
callq 0x42ed0
movq %rax, %r14
testq %rax, %rax
je 0x1e32f
movq 0x8(%rsp), %rax
movl (%rax,%rbx,4), %esi
testl %esi, %esi
js 0x1e32f
movq %r13, %rdi
callq 0x42e10
movq %rax, %r15
testq %rax, %rax
je 0x1e32f
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
movsd 0xead68(%rip), %xmm0    # 0x109050
movl $0x1, %ecx
callq 0x42f68
testq %rax, %rax
jne 0x1e32f
movl $0x50, %edi
callq 0x15610
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r14, %rdx
movsd 0xead3b(%rip), %xmm0    # 0x109050
movl $0x1, %ecx
callq 0x425a0
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 0x1e29b
movl $0x1, %ebp
jmp 0x1e372
leaq 0xeb3e4(%rip), %rdi     # 0x10973c
xorl %ebp, %ebp
movq %r14, %rsi
jmp 0x1e3ae
xorl %ebp, %ebp
leaq 0xeb90b(%rip), %rdi     # 0x109c73
movq %r12, %rsi
xorl %eax, %eax
callq 0xc5782
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1e389
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x1e3b5
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
jmp 0x1e3b5
leaq 0xeb36a(%rip), %rdi     # 0x109713
xorl %ebp, %ebp
movq %r13, %rsi
xorl %eax, %eax
callq 0xc5782
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1e3eb
movq %rax, %rbx
movl $0x50, %esi
movq %r12, %rdi
callq 0x15620
jmp 0x1e3ee
jmp 0x1e3eb
jmp 0x1e3eb
movq %rax, %rbx
jmp 0x1e405
jmp 0x1e3eb
jmp 0x1e3eb
jmp 0x1e3eb
jmp 0x1e3eb
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x1e405
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x1e41c
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x15620
movq %rbx, %rdi
callq 0x15ae0