// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/grd2pts/grd2pts.cpp
// Function: WritePoints(gaps::RNArray<Point*> const&, char const*)
RNBoolean 
WritePoints(const RNArray<Point *>& points, const char *filename)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Check if ascii output
  if (ascii) {
    // Open file
    FILE *fp = stdout;
    if (filename) {
      fp = fopen(filename, "w");
      if (!fp) {
        RNFail("Unable to open output file %s\n", filename);
        return FALSE;
      }
    }

    // Write points
    float coordinates[6];
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points[i];
      coordinates[0] = point->position.X();
      coordinates[1] = point->position.Y();
      coordinates[2] = point->position.Z();
      coordinates[3] = point->normal.X();
      coordinates[4] = point->normal.Y();
      coordinates[5] = point->normal.Z();
      fprintf(fp, "%12.6g %12.6g %12.6g\n", coordinates[0], coordinates[1], coordinates[2]);
      // fprintf(fp, "%12.6g %12.6g %12.6g  %12.6g %12.6g %12.6g\n", 
      //         coordinates[0], coordinates[1], coordinates[2], 
      //         coordinates[3], coordinates[4], coordinates[5]);
    }

    // Close file
    if (filename) fclose(fp);
  }
  else {
    // Open file
    FILE *fp = stdout;
    if (filename) {
      fp = fopen(filename, "wb");
      if (!fp) {
        RNFail("Unable to open output file %s\n", filename);
        return FALSE;
      }
    }

    // Write points
    float coordinates[6];
    for (int i = 0; i < points.NEntries(); i++) {
      Point *point = points[i];
      coordinates[0] = point->position.X();
      coordinates[1] = point->position.Y();
      coordinates[2] = point->position.Z();
      coordinates[3] = point->normal.X();
      coordinates[4] = point->normal.Y();
      coordinates[5] = point->normal.Z();
      if (fwrite(coordinates, sizeof(float), 6, fp) != (unsigned int) 6) {
        RNFail("Unable to write point to output file %s\n", filename);
        return FALSE;
      }
    }
  }

  // Print message
  if (print_verbose) {
    fprintf(stdout, "Wrote points to %s ...\n", (filename) ? filename : "stdout");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Points = %d\n", points.NEntries());
    printf("  Ascii = %d\n", ascii);
    fflush(stdout);
  }

  // Return success
  return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x66372
movq %r15, %rdi
callq 0x111cc
movq 0xcab34(%rip), %r13     # 0xdaf80
movq (%r13), %r15
cmpb $0x1, 0xcb7d1(%rip)     # 0xdbc28
jne 0x104c6
testq %r14, %r14
je 0x10479
leaq 0x88a9f(%rip), %rsi     # 0x98f04
movq %r14, %rdi
callq 0xe550
movq %rax, %r15
testq %rax, %rax
je 0x10574
movq %r14, %rbp
xorl %r14d, %r14d
leaq 0x83bc1(%rip), %r12     # 0x94047
movslq 0xc(%rbx), %rax
cmpq %rax, %r14
jge 0x10562
movq (%rbx), %rax
movq (%rax,%r14,8), %rax
cvtsd2ss (%rax), %xmm0
cvtsd2ss 0x8(%rax), %xmm1
cvtsd2ss 0x10(%rax), %xmm2
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
movq %r15, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0xe6a0
incq %r14
jmp 0x10486
testq %r14, %r14
je 0x104e6
leaq 0x83b8b(%rip), %rsi     # 0x9405d
movq %r14, %rdi
callq 0xe550
movq %rax, %r15
testq %rax, %rax
je 0x10574
movq %r14, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0x10(%rsp), %r12
pushq $0x4
popq %r13
pushq $0x6
popq %rbp
movslq 0xc(%rbx), %rax
cmpq %rax, %r14
jge 0x10589
movq (%rbx), %rax
movq (%rax,%r14,8), %rax
movupd (%rax), %xmm0
movupd 0x10(%rax), %xmm1
cvtpd2ps %xmm1, %xmm1
cvtpd2ps %xmm0, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movapd %xmm0, 0x10(%rsp)
movupd 0x20(%rax), %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, 0x20(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0xe8e0
incq %r14
cmpq $0x6, %rax
je 0x104fa
leaq 0x83b07(%rip), %rdi     # 0x94060
xorl %ebp, %ebp
movq 0x8(%rsp), %rsi
jmp 0x10580
movq %rbp, %r14
testq %rbp, %rbp
je 0x10595
movq %r15, %rdi
callq 0xe440
jmp 0x10595
leaq 0x83aad(%rip), %rdi     # 0x94028
xorl %ebp, %ebp
movq %r14, %rsi
xorl %eax, %eax
callq 0x6877a
jmp 0x10608
movq 0xca9f0(%rip), %r13     # 0xdaf80
movq 0x8(%rsp), %r14
pushq $0x1
popq %rbp
cmpb $0x1, 0xcb68d(%rip)     # 0xdbc2c
jne 0x10608
movq (%r13), %rdi
testq %r14, %r14
leaq 0x83af2(%rip), %rdx     # 0x940a1
cmovneq %r14, %rdx
leaq 0x83acf(%rip), %rsi     # 0x94089
xorl %eax, %eax
callq 0xe6a0
leaq 0x28(%rsp), %rdi
callq 0x663ac
leaq 0x83ad6(%rip), %rdi     # 0x940a8
movb $0x1, %al
callq 0xe070
movl 0xc(%rbx), %esi
leaq 0x83adc(%rip), %rdi     # 0x940bf
xorl %eax, %eax
callq 0xe070
movzbl 0xcb637(%rip), %esi     # 0xdbc28
leaq 0x83ad8(%rip), %rdi     # 0x940d0
xorl %eax, %eax
callq 0xe070
movq (%r13), %rdi
callq 0xe540
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq