// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::WriteRawFile(char const*, char const*) const
int R3Grid::
WriteRawFile(const char *filename, const char *format) const
{
  // Get size file name
  char size_name[2048];
  strncpy(size_name, filename, 2047);
  char *ext = strrchr(size_name, '.');
  if (ext) *ext = '\0';
  strncat(size_name, ".size", 2047);

  // Get transformation file name
  char transformation_name[2048];
  strncpy(transformation_name, filename, 2047);
  ext = strrchr(transformation_name, '.');
  if (ext) *ext = '\0';
  strncat(transformation_name, ".xf", 2047);

  // Write size file
  if (!WriteRawSizeFile(size_name, grid_resolution[0], grid_resolution[1], grid_resolution[2], format)) return 0;

  // Write transformation file
  if (!WriteRawTransformationFile(transformation_name, world_to_grid_transform)) return 0;

  // Open raw file
  FILE *fp = fopen(filename, "wb");
  if (!fp) {
    RNFail("Unable to open raw file %s", filename);
    return 0;
  }

  // Write raw grid values
  if (!strcmp(format, "Double64") || !strcmp(format, "double64")) {
    if (!WriteRawValues<RNScalar64>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Float64") || !strcmp(format, "float64")) {
    if (!WriteRawValues<RNScalar64>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Float32") || !strcmp(format, "float32")) {
    if (!WriteRawValues<RNScalar32>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Uint64") || !strcmp(format, "Uint64")) {
    if (!WriteRawValues<RNUInt64>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Int64") || !strcmp(format, "int64")) {
    if (!WriteRawValues<RNInt64>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Uint32") || !strcmp(format, "uint32")) {
    if (!WriteRawValues<RNUInt32>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Int32") || !strcmp(format, "int32")) {
    if (!WriteRawValues<RNInt32>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Uint16") || !strcmp(format, "uint16")) {
    if (!WriteRawValues<RNUInt16>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Int16") || !strcmp(format, "int16")) {
    if (!WriteRawValues<RNInt16>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Uchar8") || !strcmp(format, "uchar8")) {
    if (!WriteRawValues<RNUChar8>(fp, grid_values, grid_size)) return 0;
  } else if (!strcmp(format, "Char8") || !strcmp(format, "char8")) {
    if (!WriteRawValues<RNChar8>(fp, grid_values, grid_size)) return 0;
  } else {
    RNFail("Unrecognized format %s in %s\n", format, size_name);
    return 0;
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}

// Assembly:
subq $0x1048, %rsp           # imm = 0x1048
movq %rdi, 0x1038(%rsp)
movq %rsi, 0x1030(%rsp)
movq %rdx, 0x1028(%rsp)
movq 0x1038(%rsp), %rax
movq %rax, (%rsp)
leaq 0x820(%rsp), %rdi
movq 0x1030(%rsp), %rsi
movl $0x7ff, %edx            # imm = 0x7FF
callq 0xf260
leaq 0x820(%rsp), %rdi
movl $0x2e, %esi
callq 0xf580
movq %rax, 0x818(%rsp)
cmpq $0x0, 0x818(%rsp)
je 0x665c5
movq 0x818(%rsp), %rax
movb $0x0, (%rax)
leaq 0x820(%rsp), %rdi
leaq 0xcde9e(%rip), %rsi     # 0x134472
movl $0x7ff, %edx            # imm = 0x7FF
callq 0xf330
leaq 0x10(%rsp), %rdi
movq 0x1030(%rsp), %rsi
movl $0x7ff, %edx            # imm = 0x7FF
callq 0xf260
leaq 0x10(%rsp), %rdi
movl $0x2e, %esi
callq 0xf580
movq %rax, 0x818(%rsp)
cmpq $0x0, 0x818(%rsp)
je 0x66622
movq 0x818(%rsp), %rax
movb $0x0, (%rax)
leaq 0x10(%rsp), %rdi
leaq 0xcde4a(%rip), %rsi     # 0x134478
movl $0x7ff, %edx            # imm = 0x7FF
callq 0xf330
movq (%rsp), %rax
leaq 0x820(%rsp), %rdi
movl 0x238(%rax), %esi
movl 0x23c(%rax), %edx
movl 0x240(%rax), %ecx
movq 0x1028(%rsp), %r8
callq 0x67ce0
cmpl $0x0, %eax
jne 0x66678
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
movq (%rsp), %rsi
leaq 0x10(%rsp), %rdi
addq $0x110, %rsi            # imm = 0x110
callq 0x67d80
cmpl $0x0, %eax
jne 0x666a2
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
movq 0x1030(%rsp), %rdi
leaq 0xcd409(%rip), %rsi     # 0x133aba
callq 0xf5e0
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x666e9
movq 0x1030(%rsp), %rsi
leaq 0xcddca(%rip), %rdi     # 0x13449c
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
movq 0x1028(%rsp), %rdi
leaq 0xcddbf(%rip), %rsi     # 0x1344b7
callq 0xf700
cmpl $0x0, %eax
je 0x6671b
movq 0x1028(%rsp), %rdi
leaq 0xcddaf(%rip), %rsi     # 0x1344c0
callq 0xf700
cmpl $0x0, %eax
jne 0x66750
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x68090
cmpl $0x0, %eax
jne 0x6674b
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b8f
movq 0x1028(%rsp), %rdi
leaq 0xcdd6a(%rip), %rsi     # 0x1344c9
callq 0xf700
cmpl $0x0, %eax
je 0x66782
movq 0x1028(%rsp), %rdi
leaq 0xcdd59(%rip), %rsi     # 0x1344d1
callq 0xf700
cmpl $0x0, %eax
jne 0x667b7
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x68090
cmpl $0x0, %eax
jne 0x667b2
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b8d
movq 0x1028(%rsp), %rdi
leaq 0xcdb2f(%rip), %rsi     # 0x1342f5
callq 0xf700
cmpl $0x0, %eax
je 0x667e9
movq 0x1028(%rsp), %rdi
leaq 0xcdcfa(%rip), %rsi     # 0x1344d9
callq 0xf700
cmpl $0x0, %eax
jne 0x6681e
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x68110
cmpl $0x0, %eax
jne 0x66819
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b8b
movq 0x1028(%rsp), %rdi
leaq 0xcdcb4(%rip), %rsi     # 0x1344e1
callq 0xf700
cmpl $0x0, %eax
je 0x66850
movq 0x1028(%rsp), %rdi
leaq 0xcdc9b(%rip), %rsi     # 0x1344e1
callq 0xf700
cmpl $0x0, %eax
jne 0x66885
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x681a0
cmpl $0x0, %eax
jne 0x66880
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b89
movq 0x1028(%rsp), %rdi
leaq 0xcdc54(%rip), %rsi     # 0x1344e8
callq 0xf700
cmpl $0x0, %eax
je 0x668b7
movq 0x1028(%rsp), %rdi
leaq 0xcdc35(%rip), %rsi     # 0x1344e2
callq 0xf700
cmpl $0x0, %eax
jne 0x668ec
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x68250
cmpl $0x0, %eax
jne 0x668e7
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b87
movq 0x1028(%rsp), %rdi
leaq 0xcdbf3(%rip), %rsi     # 0x1344ee
callq 0xf700
cmpl $0x0, %eax
je 0x6691e
movq 0x1028(%rsp), %rdi
leaq 0xcdbe1(%rip), %rsi     # 0x1344f5
callq 0xf700
cmpl $0x0, %eax
jne 0x66953
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x682d0
cmpl $0x0, %eax
jne 0x6694e
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b85
movq 0x1028(%rsp), %rdi
leaq 0xcdb9a(%rip), %rsi     # 0x1344fc
callq 0xf700
cmpl $0x0, %eax
je 0x66985
movq 0x1028(%rsp), %rdi
leaq 0xcdb74(%rip), %rsi     # 0x1344ef
callq 0xf700
cmpl $0x0, %eax
jne 0x669ba
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x68360
cmpl $0x0, %eax
jne 0x669b5
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b83
movq 0x1028(%rsp), %rdi
leaq 0xcdb39(%rip), %rsi     # 0x134502
callq 0xf700
cmpl $0x0, %eax
je 0x669ec
movq 0x1028(%rsp), %rdi
leaq 0xcdb27(%rip), %rsi     # 0x134509
callq 0xf700
cmpl $0x0, %eax
jne 0x66a21
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x683e0
cmpl $0x0, %eax
jne 0x66a1c
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b81
movq 0x1028(%rsp), %rdi
leaq 0xcdae0(%rip), %rsi     # 0x134510
callq 0xf700
cmpl $0x0, %eax
je 0x66a53
movq 0x1028(%rsp), %rdi
leaq 0xcdaba(%rip), %rsi     # 0x134503
callq 0xf700
cmpl $0x0, %eax
jne 0x66a88
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x68470
cmpl $0x0, %eax
jne 0x66a83
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b7f
movq 0x1028(%rsp), %rdi
leaq 0xcda7f(%rip), %rsi     # 0x134516
callq 0xf700
cmpl $0x0, %eax
je 0x66aba
movq 0x1028(%rsp), %rdi
leaq 0xcda6d(%rip), %rsi     # 0x13451d
callq 0xf700
cmpl $0x0, %eax
jne 0x66aef
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x68500
cmpl $0x0, %eax
jne 0x66aea
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b7d
movq 0x1028(%rsp), %rdi
leaq 0xcda26(%rip), %rsi     # 0x134524
callq 0xf700
cmpl $0x0, %eax
je 0x66b21
movq 0x1028(%rsp), %rdi
leaq 0xcda00(%rip), %rsi     # 0x134517
callq 0xf700
cmpl $0x0, %eax
jne 0x66b50
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
movq 0x230(%rax), %rsi
movl 0x24c(%rax), %edx
callq 0x68580
cmpl $0x0, %eax
jne 0x66b4e
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b7b
movq 0x1028(%rsp), %rsi
leaq 0x820(%rsp), %rdx
leaq 0xcd9c3(%rip), %rdi     # 0x13452a
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x1044(%rsp)
jmp 0x66ba4
jmp 0x66b7d
jmp 0x66b7f
jmp 0x66b81
jmp 0x66b83
jmp 0x66b85
jmp 0x66b87
jmp 0x66b89
jmp 0x66b8b
jmp 0x66b8d
jmp 0x66b8f
movq 0x8(%rsp), %rdi
callq 0xf4b0
movl $0x1, 0x1044(%rsp)
movl 0x1044(%rsp), %eax
addq $0x1048, %rsp           # imm = 0x1048
retq
nopw %cs:(%rax,%rax)