// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::ReadDelphiFile(char const*)
int R3Grid::
ReadDelphiFile(const char *filename)
{
  // Delphi writes phi files as follows.
  // Each fortran write appears as <leading_record_size> data <trailing_record_size>
  // write(14)'now starting phimap '
  // write(14)nxtlbl,toplbl
  // write(14)phimap
  // write(14)' end of phimap  '
  // write(14)scale,oldmid,igrid
 
  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open delphi file %s\n", filename);
    return 0;
  }

  // Read leading record size for uplbl
  int uplbl_record_size;
  if (fread(&uplbl_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read uplbl_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (uplbl_record_size != 20) {
    RNFail("Wrong uplbl record size (%d) in delphi file: %s\n", uplbl_record_size, filename);
    return 0;
  }

  // Read uplbl from file 
  char uplbl[32];
  if (fread(uplbl, sizeof(char), 20, fp) != 20) {
    RNFail("Unable to read uplbl from delphi file: %s\n", filename);
    return 0;
  }

  // Read trailing record size for uplbl
  if (fread(&uplbl_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read uplbl_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (uplbl_record_size != 20) {
    RNFail("Wrong uplbl record size (%d) in delphi file: %s\n", uplbl_record_size, filename);
    return 0;
  }

  // Read leading record size for nextlbl and toplbl
  int nextlbl_record_size;
  if (fread(&nextlbl_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read nextlbl_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (nextlbl_record_size != 70) {
    RNFail("Wrong nextlbl record size (%d) in delphi file: %s\n", nextlbl_record_size, filename);
    return 0;
  }

  // Read nextlbl from file 
  char nextlbl[32];
  if (fread(nextlbl, sizeof(char), 10, fp) != 10) {
    RNFail("Unable to read nextlbl from delphi file: %s\n", filename);
    return 0;
  }

  // Read toplbl from file 
  char toplbl[64];
  if (fread(toplbl, sizeof(char), 60, fp) != 60) {
    RNFail("Unable to read toplbl from delphi file: %s\n", filename);
    return 0;
  }

  // Read trailing record size for nextlbl and toplbl
  if (fread(&nextlbl_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read nextlbl_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (nextlbl_record_size != 70) {
    RNFail("Wrong nextlbl record size (%d) in delphi file: %s\n", nextlbl_record_size, filename);
    return 0;
  }

  // Read leading record size for grid values
  int res = 65;
  unsigned int values_record_size;
  if (fread(&values_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read values_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (values_record_size != res*res*res*sizeof(float)) {
    RNFail("Wrong values record size (%d) in delphi file: %s\n", values_record_size, filename);
    return 0;
  }

  // Re-allocate grid values
  int new_size = res * res * res;
  if (!grid_values || (new_size > grid_size)) { 
    if (grid_values) delete [] grid_values;
    grid_values = new RNScalar [ new_size ];
    assert(grid_values);
  }
  // Update grid resolution variables
  grid_resolution[0] = res;
  grid_resolution[1] = res;
  grid_resolution[2] = res;
  grid_row_size = grid_resolution[0];
  grid_sheet_size = grid_row_size * grid_resolution[1];
  grid_size = grid_sheet_size * grid_resolution[2];
  if (grid_size <= 0) {
    RNFail("Invalid grid size (%d) in file", grid_size);
    return 0;
  }

  // Read grid values
  RNScalar *grid_valuesp = grid_values;
  for (int i = 0; i < grid_size; i++) {
    float value;
    if (fread(&value, sizeof(float), 1, fp) != 1) {
      RNFail("Unable to read grid value %d of %d from file", i, grid_size);
      return 0;
    }
    else {
      *(grid_valuesp++) = (RNScalar) value;
    }
  }

  // Read trailing record size for grid values
  if (fread(&values_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read values_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (values_record_size != res*res*res*sizeof(float)) {
    RNFail("Wrong values record size (%d) in delphi file: %s\n", values_record_size, filename);
    return 0;
  }

  // Read leading record size for trailer text
  int trailer_record_size;
  if (fread(&trailer_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read trailer_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (trailer_record_size != 16) {
    RNFail("Wrong trailer record size (%d) in delphi file: %s\n", trailer_record_size, filename);
    return 0;
  }

  // Read trailer from file 
  char trailer[128];
  if (fread(trailer, sizeof(char), 16, fp) != 16) {
    RNFail("Unable to read trailer from delphi file: %s\n", filename);
    return 0;
  }

  // Read trailing record size for trailer text
  if (fread(&trailer_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read trailer_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (trailer_record_size != 16) {
    RNFail("Wrong trailer record size (%d) in delphi file: %s\n", trailer_record_size, filename);
    return 0;
  }

  // Read leading record size for scale, center, and igrid
  int scale_record_size;
  if (fread(&scale_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read scale_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (scale_record_size != 20) {
    RNFail("Wrong scale record size (%d) in delphi file: %s\n", scale_record_size, filename);
    return 0;
  }

  // Read scale
  float scale;
  if (fread(&scale, sizeof(float), 1, fp) != 1) {
    RNFail("Unable to read scale from delphi file");
    return 0;
  }

  // Read center
  float center[3];
  if (fread(center, sizeof(float), 3, fp) != 3) {
    RNFail("Unable to read center from delphi file");
    return 0;
  }

  // Read grid resolution
  int igrid;
  if (fread(&igrid, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read igrid from delphi file");
    return 0;
  }

  // Check grid resolution
  if (igrid != 65) {
    RNFail("Phi grid file has invalid igrid: %d\n", igrid);
    return 0;
  }

  // Read trailing record size for scale, center, and igrid
  if (fread(&scale_record_size, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read scale_record_size from delphi file: %s", filename);
    return 0;
  }
  else if (scale_record_size != 20) {
    RNFail("Wrong scale record size (%d) in delphi file: %s\n", scale_record_size, filename);
    return 0;
  }

  // Make sure at end of file
  int count = 0;
  char dummytrailer;
  while (fread(&dummytrailer, sizeof(char), 1, fp) == 1) count++;
  if (count != 0) {
    RNFail("%d bytes of extra data at end of delphi file %s\n", count, filename);
    return 0;
  }

  // Determine world-grid transformation
  R3Point start_point(center[0] - 32 / scale, center[1] - 32 / scale, center[2] - 32 / scale);
  R3Point end_point(center[0] + 32 / scale, center[1] + 32 / scale, center[2] + 32 / scale);
  R3Box world_box(start_point, end_point);
  SetWorldToGridTransformation(world_box);

  // Close file
  fclose(fp);

  // Return number of grid values read
  return grid_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x61903(%rip), %rsi     # 0x9355b
movq %rbx, %rdi
callq 0xe550
testq %rax, %rax
je 0x31e4a
movq %rax, %r15
leaq 0x1c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x31e53
movl 0x1c(%rsp), %esi
cmpl $0x14, %esi
jne 0x31e7a
leaq 0xd0(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x14
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x14, %rax
jne 0x31e8f
leaq 0x1c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x31e53
movl 0x1c(%rsp), %esi
cmpl $0x14, %esi
jne 0x31e7a
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x31e98
movl 0x18(%rsp), %esi
cmpl $0x46, %esi
jne 0x31ea1
leaq 0xb0(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0xa
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0xa, %rax
jne 0x31eaa
leaq 0x170(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x3c
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x3c, %rax
jne 0x31eb3
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x31e98
movl 0x18(%rsp), %esi
cmpl $0x46, %esi
jne 0x31ea1
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x32055
movl 0x14(%rsp), %esi
cmpl $0x10c304, %esi         # imm = 0x10C304
jne 0x32061
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x31dba
cmpl $0x430c0, 0x24c(%r14) # imm = 0x430C0
jg 0x31ddb
testq %r12, %r12
je 0x31dc7
movq %r12, %rdi
callq 0xe5d0
movl $0x218608, %edi         # imm = 0x218608
callq 0xe030
movq %rax, %r12
movq %rax, 0x230(%r14)
movaps 0x6213e(%rip), %xmm0    # 0x93f20
movups %xmm0, 0x238(%r14)
movabsq $0x430c100001081, %rax  # imm = 0x430C100001081
movq %rax, 0x248(%r14)
movl $0x430c1, %eax          # imm = 0x430C1
xorl %r13d, %r13d
pushq $0x1
popq %rbp
cmpl %eax, %r13d
jge 0x31ebc
leaq 0xf0(%rsp), %rdi
pushq $0x4
popq %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x3206d
cvtss2sd 0xf0(%rsp), %xmm0
movsd %xmm0, (%r12,%r13,8)
movl 0x24c(%r14), %eax
incq %r13
jmp 0x31e06
leaq 0x66b69(%rip), %rdi     # 0x989ba
jmp 0x31e5a
leaq 0x66b7f(%rip), %rdi     # 0x989d9
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebp, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66b8e(%rip), %rdi     # 0x98a0f
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x31e66
leaq 0x66baa(%rip), %rdi     # 0x98a40
jmp 0x31e5a
leaq 0x66bcc(%rip), %rdi     # 0x98a6b
jmp 0x31e5a
leaq 0x66bfb(%rip), %rdi     # 0x98aa3
jmp 0x31e81
leaq 0x66c25(%rip), %rdi     # 0x98ad6
jmp 0x31e5a
leaq 0x66c49(%rip), %rdi     # 0x98b03
jmp 0x31e5a
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x32055
movl 0x14(%rsp), %esi
cmpl $0x10c304, %esi         # imm = 0x10C304
jne 0x32061
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x3208c
movl 0x10(%rsp), %esi
cmpl $0x10, %esi
jne 0x32098
leaq 0xf0(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x10
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x10, %rax
jne 0x320a4
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x3208c
movl 0x10(%rsp), %esi
cmpl $0x10, %esi
jne 0x32098
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x320b0
movl 0xc(%rsp), %esi
cmpl $0x14, %esi
jne 0x320bc
leaq 0x38(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x320c8
leaq 0x3c(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x3
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x3, %rax
jne 0x320d1
leaq 0x34(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x320da
movl 0x34(%rsp), %esi
cmpl $0x41, %esi
jne 0x320ef
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0xe6f0
cmpq $0x1, %rax
jne 0x320b0
movl 0xc(%rsp), %esi
cmpl $0x14, %esi
jne 0x320bc
pushq $-0x1
popq %r12
leaq 0x23(%rsp), %r13
pushq $0x1
popq %rbp
movq %r13, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0xe6f0
incl %r12d
cmpq $0x1, %rax
je 0x32021
testl %r12d, %r12d
je 0x32104
leaq 0x66ce4(%rip), %rdi     # 0x98d2f
xorl %ebp, %ebp
movl %r12d, %esi
jmp 0x31e83
leaq 0x66ad3(%rip), %rdi     # 0x98b2f
jmp 0x31e5a
leaq 0x66afe(%rip), %rdi     # 0x98b66
jmp 0x31e81
movl 0x24c(%r14), %edx
leaq 0x667c6(%rip), %rdi     # 0x98841
xorl %ebp, %ebp
movl %r13d, %esi
xorl %eax, %eax
callq 0x681ca
jmp 0x31e66
leaq 0x66b05(%rip), %rdi     # 0x98b98
jmp 0x31e5a
leaq 0x66b31(%rip), %rdi     # 0x98bd0
jmp 0x31e81
leaq 0x66b58(%rip), %rdi     # 0x98c03
jmp 0x31e5a
leaq 0x66b79(%rip), %rdi     # 0x98c30
jmp 0x31e5a
leaq 0x66ba3(%rip), %rdi     # 0x98c66
jmp 0x31e81
leaq 0x66bc8(%rip), %rdi     # 0x98c97
jmp 0x320e1
leaq 0x66be5(%rip), %rdi     # 0x98cbd
jmp 0x320e1
leaq 0x66c03(%rip), %rdi     # 0x98ce4
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x681ca
jmp 0x31e66
leaq 0x66c14(%rip), %rdi     # 0x98d0a
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x681ca
jmp 0x31e66
movss 0x61e6c(%rip), %xmm3    # 0x93f78
divss 0x38(%rsp), %xmm3
movss %xmm3, 0x24(%rsp)
movss 0x3c(%rsp), %xmm0
movss %xmm0, 0x30(%rsp)
movss 0x40(%rsp), %xmm1
movss %xmm1, 0x2c(%rsp)
subss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm0
subss %xmm3, %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x44(%rsp), %xmm2
movss %xmm2, 0x28(%rsp)
subss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0x98(%rsp), %rbx
movq %rbx, %rdi
callq 0x37b4c
movss 0x24(%rsp), %xmm2
movss 0x30(%rsp), %xmm0
addss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x2c(%rsp), %xmm1
addss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
addss 0x28(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0x80(%rsp), %r12
movq %r12, %rdi
callq 0x37b4c
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x24b90
movq %r14, %rdi
movq %r13, %rsi
callq 0x2b4b0
movq %r15, %rdi
callq 0xe440
movl 0x24c(%r14), %ebp
leaq 0x48(%rsp), %rdi
callq 0x25832
jmp 0x31e66
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x25832
movq %rbx, %rdi
callq 0xe810
nop