// File: /workspace/github_cpp_makefile_star2_O1/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 0x6c5c0(%rip), %rsi     # 0xa25ae
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x363d8
movq %rax, %r15
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x363e1
movl 0x1c(%rsp), %esi
cmpl $0x14, %esi
jne 0x36408
leaq 0xd0(%rsp), %rdi
movl $0x1, %esi
movl $0x14, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x14, %rax
jne 0x3641d
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x363e1
movl 0x1c(%rsp), %esi
cmpl $0x14, %esi
jne 0x36408
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36426
movl 0x18(%rsp), %esi
cmpl $0x46, %esi
jne 0x3642f
leaq 0xb0(%rsp), %rdi
movl $0x1, %esi
movl $0xa, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0xa, %rax
jne 0x36438
leaq 0x170(%rsp), %rdi
movl $0x1, %esi
movl $0x3c, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3c, %rax
jne 0x36441
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36426
movl 0x18(%rsp), %esi
cmpl $0x46, %esi
jne 0x3642f
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3644a
movl 0x14(%rsp), %esi
cmpl $0x10c304, %esi         # imm = 0x10C304
jne 0x36453
movq 0x230(%r14), %rdi
testq %rdi, %rdi
je 0x36170
cmpl $0x430c0, 0x24c(%r14) # imm = 0x430C0
jg 0x3618b
testq %rdi, %rdi
je 0x3617a
callq 0xe5b0
movl $0x218608, %edi         # imm = 0x218608
callq 0xe030
movq %rax, 0x230(%r14)
movaps 0x6cdde(%rip), %xmm0    # 0xa2f70
movups %xmm0, 0x238(%r14)
movabsq $0x430c100001081, %rax  # imm = 0x430C100001081
movq %rax, 0x248(%r14)
movq 0x230(%r14), %r12
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x1, %edx
leaq 0xf0(%rsp), %rdi
movq %r15, %rcx
callq 0xe6c0
movq %rax, %r13
cmpq $0x1, %rax
jne 0x361ef
xorps %xmm0, %xmm0
cvtss2sd 0xf0(%rsp), %xmm0
movsd %xmm0, (%r12)
addq $0x8, %r12
jmp 0x36206
movl 0x24c(%r14), %edx
leaq 0x71698(%rip), %rdi     # 0xa7895
movl %ebp, %esi
xorl %eax, %eax
callq 0x72a1e
cmpq $0x1, %r13
jne 0x3645c
incl %ebp
cmpl 0x24c(%r14), %ebp
jl 0x361b4
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x3644a
movl 0x14(%rsp), %esi
cmpl $0x10c304, %esi         # imm = 0x10C304
jne 0x36453
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36460
movl 0x10(%rsp), %esi
cmpl $0x10, %esi
jne 0x3646c
leaq 0xf0(%rsp), %rdi
movl $0x1, %esi
movl $0x10, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x10, %rax
jne 0x36475
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36460
movl 0x10(%rsp), %esi
cmpl $0x10, %esi
jne 0x3646c
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36481
movl 0xc(%rsp), %esi
cmpl $0x14, %esi
jne 0x3648d
leaq 0x38(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36499
leaq 0x3c(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x364a2
leaq 0x34(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x364ab
movl 0x34(%rsp), %esi
cmpl $0x41, %esi
jne 0x364c0
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36481
movl 0xc(%rsp), %esi
cmpl $0x14, %esi
jne 0x3648d
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 0x363a3
testl %r12d, %r12d
je 0x364d5
leaq 0x719b2(%rip), %rdi     # 0xa7d83
xorl %ebp, %ebp
movl %r12d, %esi
jmp 0x36411
leaq 0x7162f(%rip), %rdi     # 0xa7a0e
jmp 0x363e8
leaq 0x71645(%rip), %rdi     # 0xa7a2d
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x72a1e
movl %ebp, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x71654(%rip), %rdi     # 0xa7a63
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0x72a1e
jmp 0x363f4
leaq 0x71670(%rip), %rdi     # 0xa7a94
jmp 0x363e8
leaq 0x71692(%rip), %rdi     # 0xa7abf
jmp 0x363e8
leaq 0x716c1(%rip), %rdi     # 0xa7af7
jmp 0x3640f
leaq 0x716eb(%rip), %rdi     # 0xa7b2a
jmp 0x363e8
leaq 0x7170f(%rip), %rdi     # 0xa7b57
jmp 0x363e8
leaq 0x71732(%rip), %rdi     # 0xa7b83
jmp 0x363e8
leaq 0x71760(%rip), %rdi     # 0xa7bba
jmp 0x3640f
xorl %ebp, %ebp
jmp 0x363f4
leaq 0x71785(%rip), %rdi     # 0xa7bec
jmp 0x363e8
leaq 0x717b1(%rip), %rdi     # 0xa7c24
jmp 0x3640f
leaq 0x717db(%rip), %rdi     # 0xa7c57
jmp 0x363e8
leaq 0x717fc(%rip), %rdi     # 0xa7c84
jmp 0x363e8
leaq 0x71826(%rip), %rdi     # 0xa7cba
jmp 0x3640f
leaq 0x7184b(%rip), %rdi     # 0xa7ceb
jmp 0x364b2
leaq 0x71868(%rip), %rdi     # 0xa7d11
jmp 0x364b2
leaq 0x71886(%rip), %rdi     # 0xa7d38
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x72a1e
jmp 0x363f4
leaq 0x71897(%rip), %rdi     # 0xa7d5e
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x72a1e
jmp 0x363f4
movss 0x6caeb(%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 0x3ca26
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 0x3ca26
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x27542
movq %r14, %rdi
movq %r13, %rsi
callq 0x2e962
movq %r15, %rdi
callq 0xe430
movl 0x24c(%r14), %ebp
leaq 0x48(%rsp), %rdi
callq 0x28348
jmp 0x363f4
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x28348
movq %rbx, %rdi
callq 0xe7e0