// File: 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:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xd1830
leaq 0x80(%rsp), %rdi
callq 0x13fe0
cmpl $0x0, 0x167020(%rip)    # 0x179558
je 0x126c1
movq 0x15da4b(%rip), %rax    # 0x16ff90
movq (%rax), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x90(%rsp)
je 0x125a1
movq 0x90(%rsp), %rdi
leaq 0x1224b7(%rip), %rsi    # 0x134a1e
callq 0xf5e0
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x1259f
movq 0x90(%rsp), %rsi
leaq 0x121a7c(%rip), %rdi    # 0x134004
movb $0x0, %al
callq 0xd5a50
movl $0x0, 0xa4(%rsp)
jmp 0x12904
jmp 0x125a1
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x98(%rsp), %rdi
callq 0x14000
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x126a7
movq 0x98(%rsp), %rdi
movl 0x5c(%rsp), %esi
callq 0x14010
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
callq 0x14030
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x60(%rsp)
movq 0x50(%rsp), %rdi
callq 0x14040
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x64(%rsp)
movq 0x50(%rsp), %rdi
callq 0x14050
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x68(%rsp)
movq 0x50(%rsp), %rdi
addq $0x18, %rdi
callq 0x14060
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x6c(%rsp)
movq 0x50(%rsp), %rdi
addq $0x18, %rdi
callq 0x14070
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x70(%rsp)
movq 0x50(%rsp), %rdi
addq $0x18, %rdi
callq 0x14080
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x74(%rsp)
movq 0x78(%rsp), %rdi
movss 0x60(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x64(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x68(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0x121993(%rip), %rsi    # 0x134023
movb $0x3, %al
callq 0xf730
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x125a9
cmpq $0x0, 0x90(%rsp)
je 0x126bc
movq 0x78(%rsp), %rdi
callq 0xf4b0
jmp 0x12842
movq 0x15d8c8(%rip), %rax    # 0x16ff90
movq (%rax), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x90(%rsp)
je 0x12724
movq 0x90(%rsp), %rdi
leaq 0x12194f(%rip), %rsi    # 0x134039
callq 0xf5e0
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
jne 0x12722
movq 0x90(%rsp), %rsi
leaq 0x1218f9(%rip), %rdi    # 0x134004
movb $0x0, %al
callq 0xd5a50
movl $0x0, 0xa4(%rsp)
jmp 0x12904
jmp 0x12724
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x98(%rsp), %rdi
callq 0x14000
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x12840
movq 0x98(%rsp), %rdi
movl 0x2c(%rsp), %esi
callq 0x14010
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
callq 0x14030
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x30(%rsp)
movq 0x20(%rsp), %rdi
callq 0x14040
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x34(%rsp)
movq 0x20(%rsp), %rdi
callq 0x14050
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x38(%rsp)
movq 0x20(%rsp), %rdi
addq $0x18, %rdi
callq 0x14060
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x3c(%rsp)
movq 0x20(%rsp), %rdi
addq $0x18, %rdi
callq 0x14070
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x40(%rsp)
movq 0x20(%rsp), %rdi
addq $0x18, %rdi
callq 0x14080
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x44(%rsp)
leaq 0x30(%rsp), %rdi
movq 0x48(%rsp), %rcx
movl $0x4, %esi
movl $0x6, %edx
callq 0xf980
cmpq $0x6, %rax
je 0x1282e
movq 0x90(%rsp), %rsi
leaq 0x121825(%rip), %rdi    # 0x13403c
movb $0x0, %al
callq 0xd5a50
movl $0x0, 0xa4(%rsp)
jmp 0x12904
jmp 0x12830
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1272c
jmp 0x12842
cmpl $0x0, 0x166d13(%rip)    # 0x17955c
je 0x128f9
movq 0x15d73a(%rip), %rax    # 0x16ff90
movq (%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x90(%rsp)
je 0x12878
movq 0x90(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0x12886
leaq 0x1217fe(%rip), %rax    # 0x13407d
movq %rax, 0x8(%rsp)
jmp 0x12886
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rdx
leaq 0x1217ce(%rip), %rsi    # 0x134065
movb $0x0, %al
callq 0xf730
leaq 0x80(%rsp), %rdi
callq 0xd18f0
leaq 0x1217d2(%rip), %rdi    # 0x134084
movb $0x1, %al
callq 0xf070
movq 0x98(%rsp), %rdi
callq 0x14000
movl %eax, %esi
leaq 0x1217cc(%rip), %rdi    # 0x13409b
movb $0x0, %al
callq 0xf070
movl 0x166c7c(%rip), %esi    # 0x179558
leaq 0x1217c9(%rip), %rdi    # 0x1340ac
movb $0x0, %al
callq 0xf070
movq 0x15d69f(%rip), %rax    # 0x16ff90
movq (%rax), %rdi
callq 0xf5d0
movl $0x1, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)