// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
leaq 0x9afd5(%rip), %rsi     # 0xe32a3
movq %rbx, %rdi
callq 0x21aa0
testq %rax, %rax
je 0x48422
movq %rax, %r14
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 0x22270
cmpq $0x6, %rax
jne 0x48437
leaq 0x16(%rsp), %rdi
movw $0x1, (%rdi)
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0x22270
cmpq $0x2, %rax
jne 0x48440
movq (%r15), %rax
movl 0x14(%rax), %edx
movl 0x14(%r15), %ecx
leaq 0x9afaf(%rip), %rsi     # 0xe32f5
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 %r14, %rcx
callq 0x22270
cmpq $0x1, %rax
jne 0x48464
movl 0x10(%rsp), %r12d
leaq 0x20(%rsp), %rdi
movl $0x1, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x22270
cmpq %r12, %rax
jne 0x4846d
movq (%r15), %rax
cmpl $0x0, 0x14(%rax)
setg %al
jle 0x48490
xorl %ebp, %ebp
leaq 0xc(%rsp), %r12
movb %al, 0xb(%rsp)
cmpl $0x0, 0x14(%r15)
jle 0x4840e
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, 0xc(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x22270
cmpq $0x1, %rax
jne 0x48476
incq %r13
movslq 0x14(%r15), %rax
cmpq %rax, %r13
jl 0x483ca
incq %rbp
movq (%r15), %rax
movslq 0x14(%rax), %rax
cmpq %rax, %rbp
setl %al
jl 0x483bc
jmp 0x48490
leaq 0x9ab7d(%rip), %rdi     # 0xe2fa6
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8fd7a
jmp 0x48450
leaq 0x9ae68(%rip), %rdi     # 0xe32a6
jmp 0x48447
leaq 0x9ae88(%rip), %rdi     # 0xe32cf
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x8fd7a
movl %ebp, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9aec8(%rip), %rdi     # 0xe3333
jmp 0x48449
leaq 0x9aeeb(%rip), %rdi     # 0xe335f
jmp 0x48447
leaq 0x9af07(%rip), %rdi     # 0xe3384
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8fd7a
testb $0x1, 0xb(%rsp)
jne 0x48450
movq %r14, %rdi
callq 0x218a0
movl $0x1, %ebp
jmp 0x48450
nop