// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::ReadInsightFile(char const*)
int R3Grid::
ReadInsightFile(const char *filename)
{
  // Delphi writes insight files as follows.
  // Each fortran write appears as <leading_record_size> data <trailing_record_size>
  // write(14)toplbl
  // write(14)ivary,nbyte,intdat,extent,extent,extent,
  //   xang,yang,zang,xstart,xend,ystart,yend,zstart,
  //   zend,intx,inty,intz
  // do 9041 k = 1,igrid
  //   do 9042 j = 1,igrid
  //     write(14)(phimap(i,j,k),i=1,igrid)

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

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

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

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

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

  // Read ivary (0 means x varies most quickly)
  int ivary;
  if (fread(&ivary, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read ivary from insight file");
    return 0;
  }

  // Read nbyte (# of bytes in data (per entry?)
  int nbyte;
  if (fread(&nbyte, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read nbyte from insight file");
    return 0;
  }

  // Read intdat (0 means floating point)
  int intdat;
  if (fread(&intdat, sizeof(int), 1, fp) != 1) {
    RNFail("Unable to read intdat from insight file");
    return 0;
  }

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

  // Read unit cell angle (90,90,90)
  float unit_cell_angle[3];
  if (fread(unit_cell_angle, sizeof(float), 3, fp) != 3) {
    RNFail("Unable to read cell angle from insight file");
    return 0;
  }

  // Read start and end values
  float start[3], end[3];
  for (int i = 0; i < 3; i++) {
    if (fread(&start[i], sizeof(float), 1, fp) != 1) {
      RNFail("Unable to read start coordinate from insight file");
      return 0;
    }
    if (fread(&end[i], sizeof(float), 1, fp) != 1) {
      RNFail("Unable to read end coordinate from insight file");
      return 0;
    }
  }

  // Read grid resolution
  int intervals[3];
  if (fread(intervals, sizeof(int), 3, fp) != 3) {
    RNFail("Unable to read number of intervals from insight file");
    return 0;
  }

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

  // Determine resolution
  int res[3];
  res[0] = intervals[0]+1;
  res[1] = intervals[1]+1;
  res[2] = intervals[2]+1;

  // Check standard values
  if ((ivary != 0) || (nbyte != 4) || (intdat != 0) ||
      (unit_cell_angle[0] != 90) || (unit_cell_angle[1] != 90) || (unit_cell_angle[2] != 90)) {
    RNFail("Problem with header info in insight file: %d %d %d (should be 0 4 0 90 90 90)\n", 
           ivary, nbyte, intdat, unit_cell_angle[0], unit_cell_angle[1], unit_cell_angle[2]);
    return 0;
  }

#if 0
  printf("%d %d %d   (%g %g %g)   (%g %g %g)    (%g %g %g) (%g %g %g)    (%d %d %d)   (%d %d %d)\n",
    ivary, nbyte, intdat,
    extent[0], extent[1], extent[2],
    unit_cell_angle[0], unit_cell_angle[1], unit_cell_angle[2],
    start[0], start[1], start[2],
    end[0], end[1], end[2],
    intervals[0], intervals[1], intervals[2], 
    res[0], res[1], res[2]);
#endif

  // Re-allocate grid values
  int new_size = res[0] * res[1] * res[2];
  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[0];
  grid_resolution[1] = res[1];
  grid_resolution[2] = res[2];
  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 k = 0; k < grid_resolution[2]; k++) {
    for (int j = 0; j < grid_resolution[1]; j++) {
      // Read leading record size for grid row
      unsigned int row_record_size;
      if (fread(&row_record_size, sizeof(int), 1, fp) != 1) {
        RNFail("Unable to read row_record_size from insight file: %s", filename);
        return 0;
      }
      else if (row_record_size != grid_resolution[0]*sizeof(float)) {
        RNFail("Wrong row record size (%d) in insight file: %s\n", row_record_size, filename);
        return 0;
      }

      // Read row
      for (int i = 0; i < grid_resolution[0]; 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 row
      if (fread(&row_record_size, sizeof(int), 1, fp) != 1) {
        RNFail("Unable to read row_record_size from insight file: %s", filename);
        return 0;
      }
      else if (row_record_size != grid_resolution[0]*sizeof(float)) {
        RNFail("Wrong row record size (%d) in insight file: %s\n", row_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(start[0]*extent[0], start[1]*extent[1], start[2]*extent[2]);
  R3Point end_point(end[0]*extent[0], end[1]*extent[1], end[2]*extent[2]);
  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 $0x158, %rsp            # imm = 0x158
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x6c067(%rip), %rsi     # 0xa25ab
movq %rbx, %rdi
callq 0xe530
testq %rax, %rax
je 0x369cc
movq %rax, %r15
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x369d5
movl 0x10(%rsp), %esi
cmpl $0x3c, %esi
jne 0x369fc
leaq 0xd0(%rsp), %rdi
movl $0x1, %esi
movl $0x3c, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3c, %rax
jne 0x36a11
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x369d5
movl 0x10(%rsp), %esi
cmpl $0x3c, %esi
jne 0x369fc
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36a1a
movl 0xc(%rsp), %esi
cmpl $0x48, %esi
jne 0x36a23
leaq 0x20(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36a2c
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36a35
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36a3e
leaq 0x54(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x36a47
leaq 0x48(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x36a50
xorl %r12d, %r12d
leaq (%rsp,%r12), %rdi
addq $0x3c, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36a59
leaq (%rsp,%r12), %rdi
addq $0x30, %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36a62
addq $0x4, %r12
cmpq $0xc, %r12
jne 0x366ae
leaq 0x24(%rsp), %rdi
movl $0x4, %esi
movl $0x3, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x3, %rax
jne 0x36a6b
leaq 0xc(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36a1a
movl 0xc(%rsp), %esi
cmpl $0x48, %esi
jne 0x36a23
movl 0x20(%rsp), %esi
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %ecx
movss 0x48(%rsp), %xmm0
movss 0x4c(%rsp), %xmm1
movss 0x50(%rsp), %xmm2
testl %esi, %esi
jne 0x36a80
cmpl $0x4, %edx
jne 0x36a80
testl %ecx, %ecx
jne 0x36a80
ucomiss 0x6c83f(%rip), %xmm0    # 0xa2fcc
jne 0x36a80
jp 0x36a80
ucomiss 0x6c82c(%rip), %xmm1    # 0xa2fcc
jne 0x36a80
jp 0x36a80
ucomiss 0x6c819(%rip), %xmm2    # 0xa2fcc
jne 0x36a80
jp 0x36a80
movl 0x24(%rsp), %ebp
movl 0x28(%rsp), %r13d
incl %ebp
incl %r13d
movl 0x2c(%rsp), %ecx
movq %rcx, 0x60(%rsp)
incl %ecx
movl %r13d, 0x8(%rsp)
imull %ebp, %r13d
movl %r13d, (%rsp)
movl %ecx, 0x4(%rsp)
imull %ecx, %r13d
movq 0x230(%r14), %r12
testq %r12, %r12
je 0x36802
cmpl 0x24c(%r14), %r13d
jle 0x36836
testq %r12, %r12
je 0x3680f
movq %r12, %rdi
callq 0xe5b0
movslq %r13d, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r12
movq %rax, 0x230(%r14)
movl %ebp, 0x238(%r14)
movl 0x8(%rsp), %eax
movl %eax, 0x23c(%r14)
movl 0x4(%rsp), %eax
movl %eax, 0x240(%r14)
movl %ebp, 0x244(%r14)
movl (%rsp), %eax
movl %eax, 0x248(%r14)
movl %r13d, 0x24c(%r14)
testl %r13d, %r13d
jle 0x36a9c
cmpl $0x0, 0x60(%rsp)
js 0x3698c
movl $0x0, (%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x3697a
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x1, %edx
leaq 0x68(%rsp), %rdi
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36bab
movl 0x68(%rsp), %esi
movslq 0x238(%r14), %rax
leaq (,%rax,4), %rcx
cmpq %rsi, %rcx
jne 0x36bc1
testl %eax, %eax
jle 0x3691d
xorl %r13d, %r13d
movl $0x4, %esi
movl $0x1, %edx
leaq 0xa0(%rsp), %rdi
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36ab4
xorps %xmm0, %xmm0
cvtss2sd 0xa0(%rsp), %xmm0
movsd %xmm0, (%r12)
addq $0x8, %r12
incl %r13d
cmpl 0x238(%r14), %r13d
jl 0x368d7
movl $0x4, %esi
movl $0x1, %edx
leaq 0x68(%rsp), %rdi
movq %r15, %rcx
callq 0xe6c0
cmpq $0x1, %rax
jne 0x36bab
movl 0x68(%rsp), %esi
movslq 0x238(%r14), %rax
shlq $0x2, %rax
cmpq %rsi, %rax
jne 0x36bc1
movl %ebp, %ecx
incl %ecx
movl 0x23c(%r14), %eax
movl %eax, 0x8(%rsp)
movl %ecx, %ebp
cmpl %eax, %ecx
jl 0x36893
movl 0x240(%r14), %eax
movl %eax, 0x4(%rsp)
movl (%rsp), %eax
incl %eax
movl %eax, (%rsp)
cmpl 0x4(%rsp), %eax
jl 0x36886
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq 0x17(%rsp), %r13
movl $0x1, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0xe6c0
incl %r12d
cmpq $0x1, %rax
je 0x36997
testl %r12d, %r12d
je 0x36ad3
leaq 0x713ba(%rip), %rdi     # 0xa7d7f
xorl %ebp, %ebp
movl %r12d, %esi
jmp 0x36a05
leaq 0x71555(%rip), %rdi     # 0xa7f28
jmp 0x369dc
leaq 0x7156c(%rip), %rdi     # 0xa7f48
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebp, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7157d(%rip), %rdi     # 0xa7f80
xorl %ebp, %ebp
movq %rbx, %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x369e8
leaq 0x7159b(%rip), %rdi     # 0xa7fb3
jmp 0x369dc
leaq 0x715c1(%rip), %rdi     # 0xa7fe2
jmp 0x369dc
leaq 0x715ef(%rip), %rdi     # 0xa8019
jmp 0x36a03
leaq 0x71618(%rip), %rdi     # 0xa804b
jmp 0x36a72
leaq 0x71636(%rip), %rdi     # 0xa8072
jmp 0x36a72
leaq 0x71654(%rip), %rdi     # 0xa8099
jmp 0x36a72
leaq 0x71673(%rip), %rdi     # 0xa80c1
jmp 0x36a72
leaq 0x71692(%rip), %rdi     # 0xa80e9
jmp 0x36a72
leaq 0x716b5(%rip), %rdi     # 0xa8115
jmp 0x36a72
leaq 0x716de(%rip), %rdi     # 0xa8147
jmp 0x36a72
leaq 0x71705(%rip), %rdi     # 0xa8177
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x719d2
jmp 0x369e8
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
leaq 0x71719(%rip), %rdi     # 0xa81ac
movb $0x3, %al
callq 0x719d2
jmp 0x36acc
leaq 0x70d9c(%rip), %rdi     # 0xa783f
xorl %ebp, %ebp
movl %r13d, %esi
xorl %eax, %eax
callq 0x719d2
jmp 0x369e8
movl 0x24c(%r14), %edx
leaq 0x70dcf(%rip), %rdi     # 0xa7891
movl %r13d, %esi
xorl %eax, %eax
callq 0x719d2
xorl %ebp, %ebp
jmp 0x369e8
movss 0x54(%rsp), %xmm1
movss %xmm1, 0x8(%rsp)
movss 0x58(%rsp), %xmm2
movss %xmm2, 0x4(%rsp)
movss 0x3c(%rsp), %xmm0
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x40(%rsp), %xmm1
mulss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
movss 0x5c(%rsp), %xmm3
movss %xmm3, (%rsp)
movss 0x44(%rsp), %xmm2
mulss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
callq 0x3c532
movss 0x8(%rsp), %xmm0
mulss 0x30(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x4(%rsp), %xmm1
mulss 0x34(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movss (%rsp), %xmm2
mulss 0x38(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
callq 0x3c532
leaq 0x68(%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 0x68(%rsp), %rdi
callq 0x280f4
jmp 0x369e8
leaq 0x71649(%rip), %rdi     # 0xa81fb
movq %rbx, %rsi
xorl %eax, %eax
callq 0x719d2
jmp 0x36acc
leaq 0x71668(%rip), %rdi     # 0xa8230
movq %rbx, %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x36acc
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x280f4
movq %rbx, %rdi
callq 0xe7e0