// File: /workspace/github_cpp_makefile_star2_O1/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 0x70604
movq %r15, %rdi
xorl %esi, %esi
callq 0xe760
movq 0xd53e2(%rip), %r13     # 0xe5f70
movq (%r13), %r15
cmpb $0x1, 0xd607f(%rip)     # 0xe6c18
jne 0x10c27
testq %r14, %r14
je 0x10bbf
leaq 0x9737d(%rip), %rsi     # 0xa7f28
movq %r14, %rdi
callq 0xe530
movq %rax, %r15
testq %rax, %rax
je 0x10d28
cmpl $0x0, 0xc(%rbx)
jle 0x10c11
xorl %ebp, %ebp
leaq 0x92479(%rip), %r12     # 0xa3047
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 0x10bce
testq %r14, %r14
je 0x10cad
movq %r15, %rdi
callq 0xe430
jmp 0x10cad
testq %r14, %r14
je 0x10c47
leaq 0x9242a(%rip), %rsi     # 0xa305d
movq %r14, %rdi
callq 0xe530
movq %rax, %r15
testq %rax, %rax
je 0x10d28
cmpl $0x0, 0xc(%rbx)
jle 0x10cad
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 0x10d31
incq %rbp
movslq 0xc(%rbx), %rax
cmpq %rax, %rbp
jl 0x10c52
movl $0x1, %ebp
cmpb $0x1, 0xd5f63(%rip)     # 0xe6c1c
jne 0x10d44
movq (%r13), %rdi
testq %r14, %r14
leaq 0x923d4(%rip), %rdx     # 0xa30a1
cmovneq %r14, %rdx
leaq 0x923b1(%rip), %rsi     # 0xa3089
xorl %eax, %eax
callq 0xe670
leaq 0x18(%rsp), %rdi
callq 0x70666
leaq 0x923b8(%rip), %rdi     # 0xa30a8
movb $0x1, %al
callq 0xe070
movl 0xc(%rbx), %esi
leaq 0x923be(%rip), %rdi     # 0xa30bf
xorl %eax, %eax
callq 0xe070
movzbl 0xd5f09(%rip), %esi     # 0xe6c18
leaq 0x923ba(%rip), %rdi     # 0xa30d0
xorl %eax, %eax
callq 0xe070
movq (%r13), %rdi
callq 0xe520
jmp 0x10d44
leaq 0x922f9(%rip), %rdi     # 0xa3028
jmp 0x10d38
leaq 0x92328(%rip), %rdi     # 0xa3060
xorl %ebp, %ebp
movq %r14, %rsi
xorl %eax, %eax
callq 0x72fd2
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq