// File: /workspace/github_cpp_makefile_star2_O1/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 0x4875f
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x9ac93(%rip), %rsi     # 0xe32a3
movq %rbx, %rdi
callq 0x21aa0
testq %rax, %rax
je 0x4876c
movq %rax, %r14
movq (%r15), %rax
movl 0x14(%rax), %eax
leaq 0xc(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x22270
cmpq $0x1, %rax
jne 0x48763
movl 0x14(%r15), %eax
leaq 0x8(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x22270
cmpq $0x1, %rax
jne 0x48763
cmpl $0x0, 0x14(%r15)
jle 0x486e2
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movq 0x8(%r15), %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 %r14, %rcx
callq 0x22270
cmpq $0x80, %rax
jne 0x487bc
incq %rbp
movslq 0x14(%r15), %rax
cmpq %rax, %rbp
jl 0x48682
movq (%r15), %rax
cmpl $0x0, 0x14(%rax)
setg %al
jle 0x487ad
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movb %al, 0x7(%rsp)
cmpl $0x0, 0x14(%r15)
jle 0x4874b
xorl %r13d, %r13d
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rax
movq 0x410(%rax), %rax
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rbp,8), %xmm0
movss %xmm0, 0x10(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x22270
cmpq $0x1, %rax
jne 0x48793
incq %r13
movslq 0x14(%r15), %rax
cmpq %rax, %r13
jl 0x48707
incq %rbp
movq (%r15), %rax
movslq 0x14(%rax), %rax
cmpq %rax, %rbp
setl %al
jl 0x486f9
jmp 0x487ad
xorl %ebp, %ebp
jmp 0x4877f
leaq 0x9ac44(%rip), %rdi     # 0xe33ae
jmp 0x48773
leaq 0x9a98e(%rip), %rdi     # 0xe3101
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8fd7a
movl %ebp, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9abea(%rip), %rdi     # 0xe3384
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8fd7a
testb $0x1, 0x7(%rsp)
jne 0x4877f
movq %r14, %rdi
callq 0x218a0
movl $0x1, %ebp
jmp 0x4877f
leaq 0x9ac0c(%rip), %rdi     # 0xe33cf
jmp 0x48773
nopl (%rax)