// File: /workspace/github_cpp_makefile_star2_O3/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 0x99617(%rip), %rsi     # 0xe1293
movq %rbx, %rdi
callq 0x21a90
testq %rax, %rax
je 0x47dd1
movq %rax, %r15
leaq 0x18(%rsp), %rdi
movw $0x5950, 0x4(%rdi)      # imm = 0x5950
movl $0x4d554e93, (%rdi)     # imm = 0x4D554E93
movl $0x1, %esi
movl $0x6, %edx
movq %rax, %rcx
callq 0x22260
cmpq $0x6, %rax
jne 0x47de6
leaq 0x16(%rsp), %rdi
movw $0x1, (%rdi)
movl $0x1, %esi
movl $0x2, %edx
movq %r15, %rcx
callq 0x22260
cmpq $0x2, %rax
jne 0x47def
movq (%r14), %rax
movl 0x14(%rax), %edx
movl 0x14(%r14), %ecx
leaq 0x995f1(%rip), %rsi     # 0xe12e5
xorl %ebp, %ebp
leaq 0x20(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x21130
movq %r12, %rdi
callq 0x21370
leaq 0x10(%rsp), %rdi
movl %eax, (%rdi)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x22260
cmpq $0x1, %rax
jne 0x47e13
movl 0x10(%rsp), %r12d
leaq 0x20(%rsp), %rdi
movl $0x1, %esi
movq %r12, %rdx
movq %r15, %rcx
callq 0x22260
cmpq %r12, %rax
jne 0x47e1c
movq (%r14), %rcx
cmpl $0x0, 0x14(%rcx)
jle 0x47dc2
movl 0x14(%r14), %eax
xorl %r13d, %r13d
leaq 0xc(%rsp), %r12
testl %eax, %eax
jle 0x47db6
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, 0xc(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x22260
cmpq $0x1, %rax
jne 0x47e25
incq %rbp
movslq 0x14(%r14), %rax
cmpq %rax, %rbp
jl 0x47d6e
movq (%r14), %rcx
incq %r13
movslq 0x14(%rcx), %rdx
cmpq %rdx, %r13
jl 0x47d68
movq %r15, %rdi
callq 0x218a0
movl $0x1, %ebp
jmp 0x47dff
leaq 0x991be(%rip), %rdi     # 0xe0f96
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8e88e
jmp 0x47dff
leaq 0x994a9(%rip), %rdi     # 0xe1296
jmp 0x47df6
leaq 0x994c9(%rip), %rdi     # 0xe12bf
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x8e88e
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x99509(%rip), %rdi     # 0xe1323
jmp 0x47df8
leaq 0x9952c(%rip), %rdi     # 0xe134f
jmp 0x47df6
leaq 0x99548(%rip), %rdi     # 0xe1374
jmp 0x47dd8