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

  // Write magic string
  unsigned char magic[] = { 0x93, 'N', 'U', 'M', 'P', 'Y' };
  unsigned int magic_length = sizeof(magic) / sizeof(unsigned char);
  if (fwrite(magic, 1, magic_length, fp) != magic_length) {
    RNFail("Unable to write first line to numpy file");
    return 0;
  }

  // Write version
  unsigned char version[] = { 0x01, 0x00 };
  unsigned int version_length = sizeof(version) / sizeof(unsigned char);
  if (fwrite(version, 1, version_length, fp) != version_length) {
    RNFail("Unable to write version to numpy file");
    return 0;
  }

  // Create header
  char header[1024];
  sprintf(header, "{'descr': '<f4', 'fortran_order': False, 'shape': (%d, %d), }", mesh->NVertices(), NProperties());
  unsigned int header_length = strlen(header);
 
  // Write header length
  if (fwrite(&header_length, sizeof(unsigned short), 1, fp) != (unsigned int) 1) {
    RNFail("Unable to write header length to numpy file");
    return 0;
  }

  // Write header
  if (fwrite(header, sizeof(char), header_length, fp) != header_length) {
    RNFail("Unable to write header to numpy file");
    return 0;
  }

  // Write 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 $0x428, %rsp            # imm = 0x428
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x88c33(%rip), %rsi     # 0xcb26b
movq %rbx, %rdi
callq 0x21a90
testq %rax, %rax
je 0x42761
movq %rax, %r15
leaq 0x18(%rsp), %rdi
movw $0x5950, 0x4(%rdi)      # imm = 0x5950
movl $0x4d554e93, (%rdi)     # imm = 0x4D554E93
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
movq %rax, %rcx
callq 0x22290
cmpq $0x6, %rax
jne 0x42776
leaq 0x16(%rsp), %rdi
movw $0x1, (%rdi)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
movq %r15, %rcx
callq 0x22290
cmpq $0x2, %rax
jne 0x4277f
movq (%r14), %rax
movl 0x14(%rax), %edx
movl 0x14(%r14), %ecx
leaq 0x88c15(%rip), %rsi     # 0xcb2bd
xorl %ebp, %ebp
leaq 0x20(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x21130
movq %r12, %rdi
callq 0x21360
leaq 0x10(%rsp), %rdi
movl %eax, (%rdi)
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x22290
cmpq $0x1, %rax
jne 0x427a3
movl 0x10(%rsp), %r12d
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x22290
cmpq %r12, %rax
jne 0x427ac
xorl %r12d, %r12d
pushq $0x1
popq %rbp
movq (%r14), %rax
movslq 0x14(%rax), %rax
cmpq %rax, %r12
jge 0x427be
xorl %r13d, %r13d
movslq 0x14(%r14), %rax
cmpq %rax, %r13
jge 0x4275c
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rax
movq 0x410(%rax), %rax
cvtsd2ss (%rax,%r12,8), %xmm0
movss %xmm0, 0xc(%rsp)
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x22290
cmpq $0x1, %rax
jne 0x427b5
incq %r13
jmp 0x4271a
incq %r12
jmp 0x42707
leaq 0x88806(%rip), %rdi     # 0xcaf6e
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x82696
jmp 0x4278f
leaq 0x88af1(%rip), %rdi     # 0xcb26e
jmp 0x42786
leaq 0x88b11(%rip), %rdi     # 0xcb297
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x82696
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x88b51(%rip), %rdi     # 0xcb2fb
jmp 0x42788
leaq 0x88b74(%rip), %rdi     # 0xcb327
jmp 0x42786
leaq 0x88b90(%rip), %rdi     # 0xcb34c
jmp 0x42768
movq %r15, %rdi
callq 0x218b0
pushq $0x1
popq %rbp
jmp 0x4278f
nop