// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::ReadGridStream(_IO_FILE*)
int R2Grid::
ReadGridStream(FILE *fp)
{
  // Read grid resolution from file
  if (fread(&grid_resolution, sizeof(int), 2, fp) != 2) {
    RNFail("Unable to read resolution from grid file");
    return 0;
  }

  // Update grid resolution variables
  grid_row_size = grid_resolution[0];
  grid_size = grid_row_size * grid_resolution[1];
  if (grid_size <= 0) {
    RNFail("Invalid grid size (%d) in file", grid_size);
    return 0;
  }

  // Read world_to_grid transformation from file
  RNScalar m[9];
  if (fread(m, sizeof(RNScalar), 9, fp) != 9) {
    RNFail("Unable to read transformation matrix from file");
    return 0;
  }

  // Update transformation variables
  world_to_grid_transform.Reset(R3Matrix(m));
  world_to_grid_scale_factor = world_to_grid_transform.ScaleFactor();
  grid_to_world_transform = world_to_grid_transform.Inverse();

  // Allocate grid values
  grid_values = new RNScalar [ grid_size ];
  assert(grid_values);

  // Read values
  int count = grid_size;
  while (count > 0) {
    int n = fread(&grid_values[grid_size-count], sizeof(RNScalar), count, fp);
    if (n < 0) { RNFail("Unable to read pixels from grid\n"); return 0; }
    count -= n;
  }

  // Just to be sure
  for (int i = 0; i < grid_size; i++) {
    if (RNIsEqual(grid_values[i], R2_GRID_UNKNOWN_VALUE)) grid_values[i] = R2_GRID_UNKNOWN_VALUE;
  }

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

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf0, %rsp
movq %rsi, %r14
movq %rdi, %rbx
addq $0xc0, %rdi
movl $0x4, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x2, %rax
jne 0x63691
movl 0xc0(%rbx), %esi
movl %esi, 0xc8(%rbx)
imull 0xc4(%rbx), %esi
movl %esi, 0xcc(%rbx)
testl %esi, %esi
jle 0x6369a
leaq 0x60(%rsp), %rdi
movl $0x8, %esi
movl $0x9, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x9, %rax
jne 0x636ad
leaq 0x58(%rbx), %r15
leaq 0x8(%rsp), %r12
leaq 0x60(%rsp), %rsi
movq %r12, %rdi
callq 0x66f8a
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x66ca6
movq %r15, %rdi
callq 0x66a2e
movsd %xmm0, 0xb0(%rbx)
leaq 0x60(%rbx), %rsi
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
callq 0x672d4
movl 0xa8(%rbx), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x669e6
leaq 0x8(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0x67944
leaq 0x8(%rsp), %rdi
movl 0x50(%rdi), %eax
movl %eax, 0x50(%rbx)
callq 0x66ce4
movslq 0xcc(%rbx), %r13
leaq (,%r13,8), %rax
testq %r13, %r13
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0xb8(%rbx)
leaq 0x460ba(%rip), %r15     # 0xa96f9
testl %r13d, %r13d
jle 0x636d2
movslq 0xcc(%rbx), %rdi
movslq %r13d, %rax
subq %rax, %rdi
shlq $0x3, %rdi
addq 0xb8(%rbx), %rdi
movl %r13d, %edx
movl $0x8, %esi
movq %r14, %rcx
callq 0xe6c0
movq %rax, %r12
testl %r12d, %r12d
js 0x6367d
subl %r12d, %r13d
jmp 0x63687
movq %r15, %rdi
xorl %eax, %eax
callq 0x72a1e
testl %r12d, %r12d
jns 0x6363f
xorl %r14d, %r14d
jmp 0x636be
leaq 0x46009(%rip), %rdi     # 0xa96a1
jmp 0x636b4
leaq 0x441a2(%rip), %rdi     # 0xa7843
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x72a1e
jmp 0x636be
leaq 0x46016(%rip), %rdi     # 0xa96ca
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x72a1e
movl %r14d, %eax
addq $0xf0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movslq 0xcc(%rbx), %r14
testq %r14, %r14
jle 0x636be
movq 0xb8(%rbx), %rax
xorl %ecx, %ecx
leaq 0x824aa(%rip), %rdx     # 0xe5b98
movsd 0x45c2a(%rip), %xmm0    # 0xa9320
movapd 0x3e922(%rip), %xmm1    # 0xa2020
movabsq $-0x3f8bf00000000000, %rsi # imm = 0xC074100000000000
movsd (%rdx), %xmm2
movsd (%rax,%rcx,8), %xmm3
addsd %xmm0, %xmm3
movapd %xmm2, %xmm4
xorpd %xmm1, %xmm4
ucomisd %xmm4, %xmm3
jb 0x6372d
ucomisd %xmm3, %xmm2
jb 0x6372d
movq %rsi, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x63708
jmp 0x636be
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x66ce4
movq %rbx, %rdi
callq 0xe7e0