// File: /workspace/github_cpp_makefile_star2_O3/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 0x6c7cb(%rip), %rsi     # 0xa25ab
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x361a8
movq %rax, %r15
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x361b1
movl 0x1c(%rsp), %esi
cmpl $0x14, %esi
jne 0x361d8
leaq 0xd0(%rsp), %rdi
movl $0x1, %esi
movl $0x14, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x14, %rax
jne 0x361ed
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x361b1
movl 0x1c(%rsp), %esi
cmpl $0x14, %esi
jne 0x361d8
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x361f6
movl 0x18(%rsp), %esi
cmpl $0x46, %esi
jne 0x361ff
leaq 0xb0(%rsp), %rdi
movl $0x1, %esi
movl $0xa, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0xa, %rax
jne 0x36208
leaq 0x170(%rsp), %rdi
movl $0x1, %esi
movl $0x3c, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3c, %rax
jne 0x36211
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x361f6
movl 0x18(%rsp), %esi
cmpl $0x46, %esi
jne 0x361ff
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3621a
movl 0x14(%rsp), %esi
cmpl $0x10c304, %esi         # imm = 0x10C304
jne 0x36223
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x35f62
cmpl $0x430c0, 0x24c(%r14) # imm = 0x430C0
jg 0x35f83
testq %r12, %r12
je 0x35f6f
movq %r12, %rdi
callq 0xe5b0
movl $0x218608, %edi         # imm = 0x218608
callq 0xe030
movq %rax, %r12
movq %rax, 0x230(%r14)
movaps 0x6cfe6(%rip), %xmm0    # 0xa2f70
movups %xmm0, 0x238(%r14)
movabsq $0x430c100001081, %rax  # imm = 0x430C100001081
movq %rax, 0x248(%r14)
xorl %r13d, %r13d
leaq 0xf0(%rsp), %rbp
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3622c
xorps %xmm0, %xmm0
cvtss2sd 0xf0(%rsp), %xmm0
movsd %xmm0, (%r12,%r13,8)
incq %r13
cmpl 0x24c(%r14), %r13d
jl 0x35fae
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3621a
movl 0x14(%rsp), %esi
cmpl $0x10c304, %esi         # imm = 0x10C304
jne 0x36223
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3624b
movl 0x10(%rsp), %esi
cmpl $0x10, %esi
jne 0x36257
leaq 0xf0(%rsp), %rdi
movl $0x1, %esi
movl $0x10, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x10, %rax
jne 0x36263
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3624b
movl 0x10(%rsp), %esi
cmpl $0x10, %esi
jne 0x36257
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3626f
movl 0xc(%rsp), %esi
cmpl $0x14, %esi
jne 0x3627b
leaq 0x38(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36287
leaq 0x3c(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x36290
leaq 0x34(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36299
movl 0x34(%rsp), %esi
cmpl $0x41, %esi
jne 0x362ae
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3626f
movl 0xc(%rsp), %esi
cmpl $0x14, %esi
jne 0x3627b
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x23(%rsp), %r13
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0xe6c0
incl %r12d
cmpq $0x1, %rax
je 0x36173
testl %r12d, %r12d
je 0x362c3
leaq 0x71bde(%rip), %rdi     # 0xa7d7f
xorl %ebp, %ebp
movl %r12d, %esi
jmp 0x361e1
leaq 0x7185b(%rip), %rdi     # 0xa7a0a
jmp 0x361b8
leaq 0x71871(%rip), %rdi     # 0xa7a29
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebp, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x71880(%rip), %rdi     # 0xa7a5f
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x361c4
leaq 0x7189c(%rip), %rdi     # 0xa7a90
jmp 0x361b8
leaq 0x718be(%rip), %rdi     # 0xa7abb
jmp 0x361b8
leaq 0x718ed(%rip), %rdi     # 0xa7af3
jmp 0x361df
leaq 0x71917(%rip), %rdi     # 0xa7b26
jmp 0x361b8
leaq 0x7193b(%rip), %rdi     # 0xa7b53
jmp 0x361b8
leaq 0x7195e(%rip), %rdi     # 0xa7b7f
jmp 0x361b8
leaq 0x7198c(%rip), %rdi     # 0xa7bb6
jmp 0x361df
movl 0x24c(%r14), %edx
leaq 0x71657(%rip), %rdi     # 0xa7891
xorl %ebp, %ebp
movl %r13d, %esi
xorl %eax, %eax
callq 0x719d2
jmp 0x361c4
leaq 0x71996(%rip), %rdi     # 0xa7be8
jmp 0x361b8
leaq 0x719c2(%rip), %rdi     # 0xa7c20
jmp 0x361df
leaq 0x719e9(%rip), %rdi     # 0xa7c53
jmp 0x361b8
leaq 0x71a0a(%rip), %rdi     # 0xa7c80
jmp 0x361b8
leaq 0x71a34(%rip), %rdi     # 0xa7cb6
jmp 0x361df
leaq 0x71a59(%rip), %rdi     # 0xa7ce7
jmp 0x362a0
leaq 0x71a76(%rip), %rdi     # 0xa7d0d
jmp 0x362a0
leaq 0x71a94(%rip), %rdi     # 0xa7d34
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x719d2
jmp 0x361c4
leaq 0x71aa5(%rip), %rdi     # 0xa7d5a
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x719d2
jmp 0x361c4
movss 0x6ccfd(%rip), %xmm3    # 0xa2fc8
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 0x3c532
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 0x3c532
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x27326
movq %r14, %rdi
movq %r13, %rsi
callq 0x2e67a
movq %r15, %rdi
callq 0xe430
movl 0x24c(%r14), %ebp
leaq 0x48(%rsp), %rdi
callq 0x280f4
jmp 0x361c4
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x280f4
movq %rbx, %rdi
callq 0xe7e0