// File: tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: LoadLabelList(gaps::R3SurfelScene*, char const*, char const*)
static int
LoadLabelList(R3SurfelScene *scene, const char *list_filename, const char *root_name)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  int original_count = scene->NLabels();
  int read_count = 0;
  int create_count = 0;
  
  // Open file
  FILE *fp = fopen(list_filename, "r");
  if (!fp) {
    RNFail("Unable to open %s\n", list_filename);
    return 0;
  }

  // Find root
  R3SurfelLabel *root = NULL;
  if (root_name && strcmp(root_name, "Null")) {
    root = scene->FindLabelByName(root_name);
    if (!root) {
      RNFail("Unable to find root label %s\n", root_name);
      return 0;
    }
  }
 
  // Read labels from file with list
  double r, g, b;
  int identifier, visibility;
  char assignment_keystroke[64];
  char label_name[4096], parent_name[4096], buffer[16384];
  while (fgets(buffer, 16384, fp)) {
    char *bufferp = buffer;
    while (*bufferp && isspace(*bufferp)) bufferp++;
    if (*bufferp == '\0') continue;
    if (*bufferp == '#') continue;
    if (sscanf(buffer, "%s%d%s%s%d%lf%lf%lf",
      label_name, &identifier, assignment_keystroke,
      parent_name, &visibility, &r, &g, &b) != (unsigned int) 8) {
      RNFail("Invalid format for label %d in %s\n", read_count, list_filename);
      return 0;
    }
          
    // Check if label already exists
    R3SurfelLabel *label = scene->FindLabelByName(label_name);
    if (!label) {
      // Create label
      label = new R3SurfelLabel(label_name);
      if (!label) continue;
      
      // Find parent
      R3SurfelLabel *parent = NULL;
      if (!strcmp(parent_name, "Null")) parent = root;
      else {
        parent = scene->FindLabelByName(parent_name);
        if (!parent) {
          RNFail("Unable to find label's parent (%s) in label %d of %s\n", parent_name, read_count, list_filename);
          return 0;
        }
      }
    
      // Insert label into scene
      scene->InsertLabel(label, parent);

      // Update stats
      create_count++;
    }

    // Adjust color if out of range
    if (RNIsGreater(r,1) || RNIsGreater(g,1) || RNIsGreater(b,1)) {
      r /= 255.0; g /= 255.0; b /= 255.0;
    }

    // Set label properties
    if (assignment_keystroke[0] != '-') label->SetAssignmentKeystroke(assignment_keystroke[0]);
    label->SetIdentifier(identifier);
    label->SetColor(RNRgb(r, g, b));

    // Update stats
    read_count++;
  }

  // Close file
  fclose(fp);

  // Print statistics
  if (print_verbose) {
    printf("Loaded labels from %s ...\n", list_filename);
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Original Labels = %d\n", original_count);
    printf("  # Final Labels = %d\n", scene->NLabels());
    printf("  # Read Labels = %d\n", read_count);
    printf("  # Created Labels = %d\n", create_count);
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbx
subq $0x6130, %rsp           # imm = 0x6130
movq %rdi, 0x6120(%rsp)
movq %rsi, 0x6118(%rsp)
movq %rdx, 0x6110(%rsp)
leaq 0x6100(%rsp), %rdi
callq 0x16dd30
leaq 0x6100(%rsp), %rdi
callq 0x263f0
movq 0x6120(%rsp), %rdi
callq 0x266e0
movl %eax, 0x60fc(%rsp)
movl $0x0, 0x60f8(%rsp)
movl $0x0, 0x60f4(%rsp)
movq 0x6118(%rsp), %rdi
leaq 0x1e30f0(%rip), %rsi    # 0x2021d3
callq 0x168d0
movq %rax, 0x60e8(%rsp)
cmpq $0x0, 0x60e8(%rsp)
jne 0x1f121
movq 0x6118(%rsp), %rsi
leaq 0x1d9791(%rip), %rdi    # 0x1f889b
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0x612c(%rsp)
jmp 0x1f61e
movq $0x0, 0x60e0(%rsp)
cmpq $0x0, 0x6110(%rsp)
je 0x1f1a1
movq 0x6110(%rsp), %rdi
leaq 0x1d9777(%rip), %rsi    # 0x1f88be
callq 0x16a80
cmpl $0x0, %eax
je 0x1f1a1
movq 0x6120(%rsp), %rdi
movq 0x6110(%rsp), %rsi
callq 0x70ea0
movq %rax, 0x60e0(%rsp)
cmpq $0x0, 0x60e0(%rsp)
jne 0x1f19f
movq 0x6110(%rsp), %rsi
leaq 0x1d973b(%rip), %rdi    # 0x1f88c3
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0x612c(%rsp)
jmp 0x1f61e
jmp 0x1f1a1
jmp 0x1f1a3
leaq 0x80(%rsp), %rdi
movq 0x60e8(%rsp), %rdx
movl $0x4000, %esi           # imm = 0x4000
callq 0x16b90
cmpq $0x0, %rax
je 0x1f55d
leaq 0x80(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rcx
xorl %eax, %eax
cmpb $0x0, (%rcx)
movb %al, 0x3f(%rsp)
je 0x1f1fb
movq 0x78(%rsp), %rax
movsbl (%rax), %edi
callq 0x160b0
cmpl $0x0, %eax
setne %al
movb %al, 0x3f(%rsp)
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x1f205
jmp 0x1f215
movq 0x78(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x78(%rsp)
jmp 0x1f1d4
movq 0x78(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x1f227
jmp 0x1f1a3
movq 0x78(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0x1f239
jmp 0x1f1a3
leaq 0x80(%rsp), %rdi
leaq 0x5080(%rsp), %rdx
leaq 0x6080(%rsp), %r8
leaq 0x4080(%rsp), %r9
leaq 0x1d9681(%rip), %rsi    # 0x1f88e1
leaq 0x60c4(%rsp), %rcx
leaq 0x60c0(%rsp), %rbx
leaq 0x60d8(%rsp), %r11
leaq 0x60d0(%rsp), %r10
leaq 0x60c8(%rsp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movb $0x0, %al
callq 0x16710
cmpl $0x8, %eax
je 0x1f2d4
movl 0x60f8(%rsp), %esi
movq 0x6118(%rsp), %rdx
leaq 0x1d9638(%rip), %rdi    # 0x1f88f5
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0x612c(%rsp)
jmp 0x1f61e
movq 0x6120(%rsp), %rdi
leaq 0x5080(%rsp), %rsi
callq 0x70ea0
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x1f40a
movl $0x98, %edi
callq 0x16740
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x30(%rsp)
leaq 0x5080(%rsp), %rsi
callq 0x6ea70
jmp 0x1f31e
movq 0x30(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x1f357
jmp 0x1f1a3
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
movl $0x98, %esi
callq 0x16750
jmp 0x1f62e
movq $0x0, 0x58(%rsp)
leaq 0x4080(%rsp), %rdi
leaq 0x1d954f(%rip), %rsi    # 0x1f88be
callq 0x16a80
cmpl $0x0, %eax
jne 0x1f388
movq 0x60e0(%rsp), %rax
movq %rax, 0x58(%rsp)
jmp 0x1f3e1
movq 0x6120(%rsp), %rdi
leaq 0x4080(%rsp), %rsi
callq 0x70ea0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x1f3df
leaq 0x4080(%rsp), %rsi
movl 0x60f8(%rsp), %edx
movq 0x6118(%rsp), %rcx
leaq 0x1d9550(%rip), %rdi    # 0x1f8918
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0x612c(%rsp)
jmp 0x1f61e
jmp 0x1f3e1
movq 0x6120(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq (%rdi), %rax
callq *0x30(%rax)
movl 0x60f4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60f4(%rsp)
movsd 0x60d8(%rsp), %xmm0
leaq 0x24530e(%rip), %rax    # 0x264728
movsd (%rax), %xmm2
movsd 0x1d8c0a(%rip), %xmm1   # 0x1f8030
callq 0x269c0
cmpl $0x0, %eax
jne 0x1f47c
movsd 0x60d0(%rsp), %xmm0
leaq 0x2452e8(%rip), %rax    # 0x264728
movsd (%rax), %xmm2
movsd 0x1d8be4(%rip), %xmm1   # 0x1f8030
callq 0x269c0
cmpl $0x0, %eax
jne 0x1f47c
movsd 0x60c8(%rsp), %xmm0
leaq 0x2452c2(%rip), %rax    # 0x264728
movsd (%rax), %xmm2
movsd 0x1d8bbe(%rip), %xmm1   # 0x1f8030
callq 0x269c0
cmpl $0x0, %eax
je 0x1f4d6
movsd 0x60d8(%rsp), %xmm0
movsd 0x1d8bab(%rip), %xmm1   # 0x1f8038
divsd %xmm1, %xmm0
movsd %xmm0, 0x60d8(%rsp)
movsd 0x60d0(%rsp), %xmm0
movsd 0x1d8b8d(%rip), %xmm1   # 0x1f8038
divsd %xmm1, %xmm0
movsd %xmm0, 0x60d0(%rsp)
movsd 0x60c8(%rsp), %xmm0
movsd 0x1d8b6f(%rip), %xmm1   # 0x1f8038
divsd %xmm1, %xmm0
movsd %xmm0, 0x60c8(%rsp)
movsbl 0x6080(%rsp), %eax
cmpl $0x2d, %eax
je 0x1f4f6
movq 0x70(%rsp), %rdi
movsbl 0x6080(%rsp), %esi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x70(%rsp), %rdi
movl 0x60c4(%rsp), %esi
movq (%rdi), %rax
callq *0x20(%rax)
movq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movsd 0x60d8(%rsp), %xmm0
movsd 0x60d0(%rsp), %xmm1
movsd 0x60c8(%rsp), %xmm2
leaq 0x40(%rsp), %rdi
callq 0x16e4f0
movq 0x28(%rsp), %rdi
movq (%rdi), %rax
leaq 0x40(%rsp), %rsi
callq *0x30(%rax)
movl 0x60f8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60f8(%rsp)
jmp 0x1f1a3
movq 0x60e8(%rsp), %rdi
callq 0x16730
cmpl $0x0, 0x24524f(%rip)    # 0x2647c0
je 0x1f613
movq 0x6118(%rsp), %rsi
leaq 0x1d93c8(%rip), %rdi    # 0x1f894e
movb $0x0, %al
callq 0x16090
leaq 0x6100(%rsp), %rdi
callq 0x16ddf0
leaq 0x1d8fde(%rip), %rdi    # 0x1f857f
movb $0x1, %al
callq 0x16090
movl 0x60fc(%rsp), %esi
leaq 0x1d93b3(%rip), %rdi    # 0x1f8969
movb $0x0, %al
callq 0x16090
movq 0x6120(%rsp), %rdi
callq 0x266e0
movl %eax, %esi
leaq 0x1d93b0(%rip), %rdi    # 0x1f8983
movb $0x0, %al
callq 0x16090
movl 0x60f8(%rsp), %esi
leaq 0x1d93b2(%rip), %rdi    # 0x1f899a
movb $0x0, %al
callq 0x16090
movl 0x60f4(%rsp), %esi
leaq 0x1d93b3(%rip), %rdi    # 0x1f89b0
movb $0x0, %al
callq 0x16090
movq 0x23b985(%rip), %rax    # 0x25af90
movq (%rax), %rdi
callq 0x168c0
movl $0x1, 0x612c(%rsp)
movl 0x612c(%rsp), %eax
addq $0x6130, %rsp           # imm = 0x6130
popq %rbx
retq
movq 0x68(%rsp), %rdi
callq 0x16ca0
nopl (%rax,%rax)