// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %r15
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
callq 0xc2368
movq %rbx, %rdi
xorl %esi, %esi
callq 0x15a20
cmpl $0x0, 0x1501fd(%rip)    # 0x16edcc
je 0x1ebf1
leaq 0xeb1e6(%rip), %rdi     # 0x109dbe
movq %r14, %rsi
xorl %eax, %eax
callq 0x15080
movq 0x14f377(%rip), %rax    # 0x16df60
movq (%rax), %rdi
callq 0x15760
leaq 0xeb1e1(%rip), %rsi     # 0x109dd9
leaq 0x470(%rsp), %rbx
movq %rbx, %rdi
movq %r14, 0x30(%rsp)
movq %r14, %rdx
xorl %eax, %eax
callq 0x15110
movq %rbx, %rdi
callq 0x15530
cmpl $0x0, 0x1c(%r15)
jle 0x1ee0c
leaq 0xeb1ee(%rip), %r12     # 0x109e1a
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
movq 0x10(%r15), %rax
movq (%rax,%rbp,8), %r13
movq %r13, %rdi
callq 0x40ed0
testq %rax, %rax
jne 0x1ec50
movq %r13, %rdi
callq 0x40f22
testq %rax, %rax
je 0x1ec63
movq %rax, %rdi
callq 0x421f0
movl %eax, 0xc(%rsp)
jmp 0x1ec6b
movl $0x0, 0xc(%rsp)
xorps %xmm0, %xmm0
movq %r13, %rdi
xorl %esi, %esi
callq 0x40b92
movl $0x4, %ebx
testq %rax, %rax
je 0x1edf7
movq %rax, %r14
cmpl $0x0, 0x10(%rax)
je 0x1edee
movq %r13, 0x40(%rsp)
movq %r13, %rdi
callq 0x4076a
movq (%rax), %rcx
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
callq *0x68(%rcx)
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
leaq 0x70(%rsp), %r13
movq %r13, %rdi
leaq 0xeb116(%rip), %rsi     # 0x109de5
movq 0x30(%rsp), %rdx
movl 0xc(%rsp), %ecx
movb $0x3, %al
callq 0x15110
movq %r13, %rdi
leaq 0xf2f4f(%rip), %rsi     # 0x111c38
callq 0x15770
testq %rax, %rax
je 0x1edd8
movq %rax, %rbx
movq %rbp, 0x38(%rsp)
cmpl $0x0, 0x10(%r14)
movq %r12, %r15
leaq 0x48(%rsp), %rbp
jle 0x1ed81
movl $0x8, %r13d
xorl %r12d, %r12d
movq 0x8(%r14), %rax
movq -0x8(%rax,%r13), %rcx
movq (%rax,%r13), %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 0x9d282
movsd 0x48(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movq %rbx, %rdi
movq %r15, %rsi
movb $0x3, %al
callq 0x15900
incq %r12
movslq 0x10(%r14), %rax
addq $0x10, %r13
cmpq %rax, %r12
jl 0x1ed17
movq %r15, %r12
movq %rbx, %rdi
callq 0x15600
xorl %ebx, %ebx
cmpl $0x0, 0x15003b(%rip)    # 0x16edd0
movq 0x38(%rsp), %rbp
je 0x1edd1
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
movl 0x10(%r14), %edx
movq 0x40(%rsp), %rax
movsd 0x98(%rax), %xmm3
leaq 0xeb064(%rip), %rdi     # 0x109e2a
movl 0xc(%rsp), %esi
movb $0x4, %al
callq 0x15080
movq 0x10(%rsp), %r15
jmp 0x1edee
leaq 0xeb01f(%rip), %rdi     # 0x109dfe
movq %r13, %rsi
xorl %eax, %eax
callq 0xc5782
movl $0x1, %ebx
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
testb $0x3, %bl
jne 0x1ee55
incq %rbp
movslq 0x1c(%r15), %rax
cmpq %rax, %rbp
jl 0x1ec33
movl $0x1, %ebx
cmpl $0x0, 0x14ffb4(%rip)    # 0x16edcc
je 0x1ee57
leaq 0x60(%rsp), %rdi
callq 0xc23ca
leaq 0xea78e(%rip), %rdi     # 0x1095b9
movb $0x1, %al
callq 0x15080
movl 0x1c(%r15), %esi
leaq 0xeb00d(%rip), %rdi     # 0x109e4a
xorl %eax, %eax
callq 0x15080
movq 0x14f115(%rip), %rax    # 0x16df60
movq (%rax), %rdi
callq 0x15760
jmp 0x1ee57
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop