// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x6f820
movq %r15, %rdi
xorl %esi, %esi
callq 0xe760
movq 0xd4402(%rip), %r13     # 0xe4f70
movq (%r13), %r15
cmpb $0x1, 0xd509f(%rip)     # 0xe5c18
jne 0x10c07
testq %r14, %r14
je 0x10b9f
leaq 0x96399(%rip), %rsi     # 0xa6f24
movq %r14, %rdi
callq 0xe530
movq %rax, %r15
testq %rax, %rax
je 0x10d08
cmpl $0x0, 0xc(%rbx)
jle 0x10bf1
xorl %ebp, %ebp
leaq 0x91499(%rip), %r12     # 0xa2047
movq (%rbx), %rax
movq (%rax,%rbp,8), %rax
xorps %xmm0, %xmm0
cvtsd2ss (%rax), %xmm0
xorps %xmm1, %xmm1
cvtsd2ss 0x8(%rax), %xmm1
xorps %xmm2, %xmm2
cvtsd2ss 0x10(%rax), %xmm2
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
movq %r15, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0xe670
incq %rbp
movslq 0xc(%rbx), %rax
cmpq %rax, %rbp
jl 0x10bae
testq %r14, %r14
je 0x10c8d
movq %r15, %rdi
callq 0xe430
jmp 0x10c8d
testq %r14, %r14
je 0x10c27
leaq 0x9144a(%rip), %rsi     # 0xa205d
movq %r14, %rdi
callq 0xe530
movq %rax, %r15
testq %rax, %rax
je 0x10d08
cmpl $0x0, 0xc(%rbx)
jle 0x10c8d
xorl %ebp, %ebp
movq %rsp, %r12
movq (%rbx), %rax
movq (%rax,%rbp,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, (%rsp)
movupd 0x20(%rax), %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, 0x10(%rsp)
movl $0x4, %esi
movl $0x6, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0xe8b0
cmpq $0x6, %rax
jne 0x10d11
incq %rbp
movslq 0xc(%rbx), %rax
cmpq %rax, %rbp
jl 0x10c32
movl $0x1, %ebp
cmpb $0x1, 0xd4f83(%rip)     # 0xe5c1c
jne 0x10d24
movq (%r13), %rdi
testq %r14, %r14
leaq 0x913f4(%rip), %rdx     # 0xa20a1
cmovneq %r14, %rdx
leaq 0x913d1(%rip), %rsi     # 0xa2089
xorl %eax, %eax
callq 0xe670
leaq 0x18(%rsp), %rdi
callq 0x6f882
leaq 0x913d8(%rip), %rdi     # 0xa20a8
movb $0x1, %al
callq 0xe070
movl 0xc(%rbx), %esi
leaq 0x913de(%rip), %rdi     # 0xa20bf
xorl %eax, %eax
callq 0xe070
movzbl 0xd4f29(%rip), %esi     # 0xe5c18
leaq 0x913da(%rip), %rdi     # 0xa20d0
xorl %eax, %eax
callq 0xe070
movq (%r13), %rdi
callq 0xe520
jmp 0x10d24
leaq 0x91319(%rip), %rdi     # 0xa2028
jmp 0x10d18
leaq 0x91348(%rip), %rdi     # 0xa2060
xorl %ebp, %ebp
movq %r14, %rsi
xorl %eax, %eax
callq 0x71f82
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq