// File: /workspace/github_cpp_makefile_star2_O3/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 0x480ed
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x992f3(%rip), %rsi     # 0xe1293
movq %rbx, %rdi
callq 0x21a90
testq %rax, %rax
je 0x480fa
movq %rax, %r15
movq (%r14), %rax
movl 0x14(%rax), %eax
leaq 0xc(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22260
cmpq $0x1, %rax
jne 0x480f1
movl 0x14(%r14), %eax
leaq 0x8(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22260
cmpq $0x1, %rax
jne 0x480f1
movl 0x14(%r14), %eax
testl %eax, %eax
jle 0x48073
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %r13
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x21470
addq $0x8, %r13
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
movq %r13, %rsi
callq 0x21410
movb $0x0, 0x8f(%rsp)
movl $0x1, %esi
movl $0x80, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x22260
cmpq $0x80, %rax
jne 0x4812a
incq %rbp
movslq 0x14(%r14), %rax
cmpq %rax, %rbp
jl 0x48013
movq (%r14), %rcx
cmpl $0x0, 0x14(%rcx)
jle 0x480de
xorl %r13d, %r13d
leaq 0x10(%rsp), %r12
testl %eax, %eax
jle 0x480d2
xorl %ebp, %ebp
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %rax
movq 0x410(%rax), %rax
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%r13,8), %xmm0
movss %xmm0, 0x10(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x22260
cmpq $0x1, %rax
jne 0x48121
incq %rbp
movslq 0x14(%r14), %rax
cmpq %rax, %rbp
jl 0x4808a
movq (%r14), %rcx
incq %r13
movslq 0x14(%rcx), %rdx
cmpq %rdx, %r13
jl 0x48084
movq %r15, %rdi
callq 0x218a0
movl $0x1, %ebp
jmp 0x4810d
xorl %ebp, %ebp
jmp 0x4810d
leaq 0x992a6(%rip), %rdi     # 0xe139e
jmp 0x48101
leaq 0x98ff0(%rip), %rdi     # 0xe10f1
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8e88e
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9924c(%rip), %rdi     # 0xe1374
jmp 0x48101
leaq 0x9928e(%rip), %rdi     # 0xe13bf
jmp 0x48101
nop