// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
cmpq $0x0, (%rdi)
je 0x429fd
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x88939(%rip), %rsi     # 0xcb26b
movq %rbx, %rdi
callq 0x21a90
testq %rax, %rax
je 0x42a0a
movq %rax, %r15
movq (%r14), %rax
movl 0x14(%rax), %eax
leaq 0xc(%rsp), %rdi
movl %eax, (%rdi)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x22290
cmpq $0x1, %rax
jne 0x42a01
movl 0x14(%r14), %eax
leaq 0x8(%rsp), %rdi
movl %eax, (%rdi)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x22290
cmpq $0x1, %rax
jne 0x42a01
movq %rbx, (%rsp)
xorl %ebx, %ebx
leaq 0x10(%rsp), %r12
pushq $0x1
popq %r13
movslq 0x14(%r14), %rax
cmpq %rax, %rbx
jge 0x42a31
movq 0x8(%r14), %rax
movq (%rax,%rbx,8), %rbp
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x21470
addq $0x8, %rbp
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
movq %rbp, %rsi
callq 0x21420
movb $0x0, 0x8f(%rsp)
movl $0x80, %edx
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x22290
cmpq $0x80, %rax
jne 0x42a93
incq %rbx
jmp 0x42999
xorl %ebp, %ebp
jmp 0x42a1d
leaq 0x8896e(%rip), %rdi     # 0xcb376
jmp 0x42a11
leaq 0x886b8(%rip), %rdi     # 0xcb0c9
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x82696
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
pushq $0x4
popq %r13
pushq $0x1
popq %rbp
movq (%r14), %rcx
movslq 0x14(%rcx), %rcx
cmpq %rcx, %r12
jge 0x42aae
xorl %ebx, %ebx
movslq %eax, %rcx
cmpq %rcx, %rbx
jge 0x42a8e
movq 0x8(%r14), %rax
movq (%rax,%rbx,8), %rax
movq 0x410(%rax), %rax
cvtsd2ss (%rax,%r12,8), %xmm0
movss %xmm0, 0x10(%rsp)
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x22290
cmpq $0x1, %rax
jne 0x42a9c
incq %rbx
movl 0x14(%r14), %eax
jmp 0x42a49
incq %r12
jmp 0x42a3b
leaq 0x888fd(%rip), %rdi     # 0xcb397
jmp 0x42aa3
leaq 0x888a9(%rip), %rdi     # 0xcb34c
xorl %ebp, %ebp
movq (%rsp), %rsi
jmp 0x42a16
movq %r15, %rdi
callq 0x218b0
pushq $0x1
popq %rbp
jmp 0x42a1d