// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshPropertySet.cpp
// Function: gaps::R3MeshPropertySet::WriteBinary(char const*) const
int R3MeshPropertySet::
WriteBinary(const char *filename) const
{
  // Just checking
  if (!mesh) return 0;
  
  // Open file
  FILE *fp = fopen(filename, "wb");
  if (!fp) {
    RNFail("Unable to open binary file: %s\n", filename);
    return 0;
  }

  // Write number of vertices
  int vertex_count = mesh->NVertices();
  if (fwrite(&vertex_count, sizeof(int), 1, fp) != (unsigned int) 1) {
    RNFail("Unable to write binary file: %s\n", filename);
    return 0;
  }
  
  // Write number of properties
  int property_count = NProperties();
  if (fwrite(&property_count, sizeof(int), 1, fp) != (unsigned int) 1) {
    RNFail("Unable to write binary file: %s\n", filename);
    return 0;
  }
  
  // Write property names
  for (int j = 0; j < NProperties(); j++) {
    R3MeshProperty *property = Property(j);
    char property_name[1024];
    memset(property_name, 0, 1024);
    if (property->Name()) strncpy(property_name, property->Name(), 1024);
    property_name[127] = '\0';
    if (fwrite(property_name, sizeof(char), 128, fp) != (unsigned int) 128) {
      RNFail("Unable to write property name to binary file: %s\n", filename);
      return 0;
    }    
  }

  // Write data and assign property values
  for (int i = 0; i < mesh->NVertices(); i++) {
    for (int j = 0; j < NProperties(); j++) {
      RNScalar32 value = Property(j)->VertexValue(i);
      if (fwrite(&value, sizeof(RNScalar32), 1, fp) != (unsigned int) 1) {
        RNFail("Unable to write value to binary file: %s\n", filename);
        return 0;
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x468, %rsp            # imm = 0x468
movq %rdi, 0x458(%rsp)
movq %rsi, 0x450(%rsp)
movq 0x458(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, (%rax)
jne 0x6bcea
movl $0x0, 0x464(%rsp)
jmp 0x6bfe2
movq 0x450(%rsp), %rdi
leaq 0x12c632(%rip), %rsi    # 0x19832b
callq 0x22bf0
movq %rax, 0x448(%rsp)
cmpq $0x0, 0x448(%rsp)
jne 0x6bd37
movq 0x450(%rsp), %rsi
leaq 0x12c412(%rip), %rdi    # 0x198132
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6bfe2
movq 0x18(%rsp), %rax
movq (%rax), %rdi
callq 0x2abd0
movl %eax, 0x444(%rsp)
movq 0x448(%rsp), %rcx
leaq 0x444(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x23450
cmpq $0x1, %rax
je 0x6bd96
movq 0x450(%rsp), %rsi
leaq 0x12c6b9(%rip), %rdi    # 0x198438
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6bfe2
movq 0x18(%rsp), %rdi
callq 0x2ae90
movl %eax, 0x440(%rsp)
movq 0x448(%rsp), %rcx
leaq 0x440(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x23450
cmpq $0x1, %rax
je 0x6bdf2
movq 0x450(%rsp), %rsi
leaq 0x12c65d(%rip), %rdi    # 0x198438
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6bfe2
movl $0x0, 0x43c(%rsp)
movq 0x18(%rsp), %rdi
movl 0x43c(%rsp), %eax
movl %eax, 0x14(%rsp)
callq 0x2ae90
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x6bef0
movq 0x18(%rsp), %rdi
movl 0x43c(%rsp), %esi
callq 0x2b330
movq %rax, 0x430(%rsp)
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x224d0
movq 0x430(%rsp), %rdi
callq 0x2adf0
cmpq $0x0, %rax
je 0x6be86
leaq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x430(%rsp), %rdi
callq 0x2adf0
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x22490
movb $0x0, 0xaf(%rsp)
leaq 0x30(%rsp), %rdi
movq 0x448(%rsp), %rcx
movl $0x1, %esi
movl $0x80, %edx
callq 0x23450
cmpq $0x80, %rax
je 0x6bed8
movq 0x450(%rsp), %rsi
leaq 0x12c598(%rip), %rdi    # 0x198459
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6bfe2
jmp 0x6beda
movl 0x43c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x43c(%rsp)
jmp 0x6bdfd
movl $0x0, 0x2c(%rsp)
movq 0x18(%rsp), %rax
movl 0x2c(%rsp), %ecx
movl %ecx, 0x4(%rsp)
movq (%rax), %rdi
callq 0x2abd0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x6bfca
movl $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rdi
movl 0x28(%rsp), %eax
movl %eax, (%rsp)
callq 0x2ae90
movl %eax, %ecx
movl (%rsp), %eax
cmpl %ecx, %eax
jge 0x6bfb8
movq 0x18(%rsp), %rdi
movl 0x28(%rsp), %esi
callq 0x2b330
movq %rax, %rdi
movl 0x2c(%rsp), %esi
callq 0x2b0a0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x448(%rsp), %rcx
leaq 0x24(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x23450
cmpq $0x1, %rax
je 0x6bfa6
movq 0x450(%rsp), %rsi
leaq 0x12c47c(%rip), %rdi    # 0x19840e
movb $0x0, %al
callq 0xfbc30
movl $0x0, 0x464(%rsp)
jmp 0x6bfe2
jmp 0x6bfa8
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x6bf23
jmp 0x6bfba
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x6bef8
movq 0x448(%rsp), %rdi
callq 0x229c0
movl $0x1, 0x464(%rsp)
movl 0x464(%rsp), %eax
addq $0x468, %rsp            # imm = 0x468
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)