// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: OutputBlobs(gaps::R3SurfelScene*, char const*)
static int
OutputBlobs(R3SurfelScene *scene, const char *directory_name)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  if (print_verbose) {
    printf("Outputing blobs to %s ...\n", directory_name);
    fflush(stdout);
  }

  // Create directory
  char cmd[1024];
  sprintf(cmd, "mkdir -p %s", directory_name);
  system(cmd);

  // Output blob file for each object
  for (int i = 0; i < scene->NObjects(); i++) {
    R3SurfelObject *object = scene->Object(i);

    // Get object label
    R3SurfelLabel *label = object->GroundTruthLabel();
    if (!label) label = object->HumanLabel();
    int label_identifier = (label) ? label->Identifier() : 0;

    // Create pointset
    R3SurfelPointSet *pointset = object->PointSet();
    if (!pointset) continue;
    if (pointset->NPoints() == 0) { 
      delete pointset; 
      continue; 
    }

    // Create filename
    char filename[1024];
    R3Point centroid = object->Centroid();
    sprintf(filename, "%s/%d_%.3f_%.3f_%.3f.xyz", directory_name,
      label_identifier, centroid.X(), centroid.Y(), centroid.Z());

    // Open file
    FILE *fp = fopen(filename, "w");
    if (!fp) {
      RNFail("Unable to open xyz file %s\n", filename);
      delete pointset;
      return 0;
    }

    // Write points to file
    for (int j = 0; j < pointset->NPoints(); j++) {
      R3SurfelPoint *point = pointset->Point(j);
      R3Point position = point->Position();
      // RNRgb rgb = point->Rgb();
      fprintf(fp, "%.6f %.6f %.6f\n", position.X(), position.Y(), position.Z());
    }

    // Close file
    fclose(fp);

    // Print message
    if (print_debug) {
      printf("%3d %8.3f %8.3f %8.3f : %6d %g\n", label_identifier, 
        centroid.X(), centroid.Y(), centroid.Z(),
        pointset->NPoints(), object->Complexity());
    }

    // Delete pointset
    delete pointset;
  }

  // Print statistics
  if (print_verbose) {
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Blobs = %d\n", scene->NObjects());
    fflush(stdout);
  }
  
  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x878, %rsp            # imm = 0x878
movq %rsi, %r12
movq %rdi, %r15
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0xc0cf4
movq %rbx, %rdi
xorl %esi, %esi
callq 0x15a10
cmpl $0x0, 0x14e47f(%rip)    # 0x16cdbc
je 0x1e95f
leaq 0xe946f(%rip), %rdi     # 0x107db5
movq %r12, %rsi
xorl %eax, %eax
callq 0x15080
movq 0x14d609(%rip), %rax    # 0x16bf60
movq (%rax), %rdi
callq 0x15750
leaq 0xe946a(%rip), %rsi     # 0x107dd0
leaq 0x470(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x15110
movq %rbx, %rdi
callq 0x15530
cmpl $0x0, 0x1c(%r15)
jle 0x1eb69
xorl %ebp, %ebp
leaq 0xe947a(%rip), %r14     # 0x107e11
movq %r15, 0x30(%rsp)
movq %r12, 0x28(%rsp)
movq 0x10(%r15), %rax
movq (%rax,%rbp,8), %rbx
movq %rbx, %rdi
callq 0x40a28
testq %rax, %rax
jne 0x1e9c7
movq %rbx, %rdi
callq 0x40a76
testq %rax, %rax
je 0x1eb5c
movq %rax, %rdi
callq 0x41d0e
movl %eax, 0xc(%rsp)
xorps %xmm0, %xmm0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x40746
testq %rax, %rax
je 0x1eb4a
movq %rax, %r13
cmpl $0x0, 0x10(%rax)
je 0x1eb40
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
callq 0x40318
movq (%rax), %rcx
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq *0x68(%rcx)
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
leaq 0xe93aa(%rip), %rsi     # 0x107ddc
movq %r12, %rdx
movl 0xc(%rsp), %ecx
movb $0x3, %al
callq 0x15110
movq %rbx, %rdi
leaq 0xf11da(%rip), %rsi     # 0x10fc24
callq 0x15760
testq %rax, %rax
je 0x1ebb2
movq %rax, %rbx
movq %rbp, 0x40(%rsp)
cmpl $0x0, 0x10(%r13)
leaq 0x48(%rsp), %rbp
jle 0x1eadf
movl $0x8, %r15d
xorl %r12d, %r12d
movq 0x8(%r13), %rax
movq -0x8(%rax,%r15), %rcx
movq (%rax,%r15), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
movq %rbp, %rdi
callq 0x9bed2
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movq %rbx, %rdi
movq %r14, %rsi
movb $0x3, %al
callq 0x158f0
incq %r12
movslq 0x10(%r13), %rax
addq $0x10, %r15
cmpq %rax, %r12
jl 0x1ea75
movq %rbx, %rdi
callq 0x15600
cmpl $0x0, 0x14e2d2(%rip)    # 0x16cdc0
je 0x1eb25
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movl 0x10(%r13), %edx
movq 0x38(%rsp), %rax
movsd 0x98(%rax), %xmm3
leaq 0xe9307(%rip), %rdi     # 0x107e21
movl 0xc(%rsp), %esi
movb $0x4, %al
callq 0x15080
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
movq 0x30(%rsp), %r15
movq 0x28(%rsp), %r12
movq 0x40(%rsp), %rbp
jmp 0x1eb4a
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
incq %rbp
movslq 0x1c(%r15), %rax
cmpq %rax, %rbp
jl 0x1e9a1
jmp 0x1eb69
movl $0x0, 0xc(%rsp)
jmp 0x1e9d3
movl $0x1, %ebx
cmpl $0x0, 0x14e247(%rip)    # 0x16cdbc
je 0x1ebd1
leaq 0x60(%rsp), %rdi
callq 0xc0d56
leaq 0xe8a2a(%rip), %rdi     # 0x1075b2
movb $0x1, %al
callq 0x15080
movl 0x1c(%r15), %esi
leaq 0xe92a7(%rip), %rdi     # 0x107e41
xorl %eax, %eax
callq 0x15080
movq 0x14d3b8(%rip), %rax    # 0x16bf60
movq (%rax), %rdi
callq 0x15750
jmp 0x1ebd1
leaq 0xe923c(%rip), %rdi     # 0x107df5
xorl %ebx, %ebx
leaq 0x70(%rsp), %rsi
xorl %eax, %eax
callq 0xc3e02
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
movl %ebx, %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop