// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
subq $0xf8, %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 0x628b0
movl 0xc0(%rbx), %esi
movl %esi, 0xc8(%rbx)
imull 0xc4(%rbx), %esi
movl %esi, 0xcc(%rbx)
testl %esi, %esi
jle 0x628b9
leaq 0x60(%rsp), %rdi
movl $0x8, %esi
movl $0x9, %edx
movq %r14, %rcx
callq 0xe6c0
cmpq $0x9, %rax
jne 0x628cc
leaq 0x58(%rbx), %r15
leaq 0x8(%rsp), %r12
leaq 0x60(%rsp), %rsi
movq %r12, %rdi
callq 0x66202
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x65f1e
movq %r15, %rdi
callq 0x65ca6
movsd %xmm0, 0xb0(%rbx)
leaq 0x60(%rbx), %rsi
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
callq 0x6654c
movl 0xa8(%rbx), %edx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x65c5e
leaq 0x8(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0x66bbc
leaq 0x8(%rsp), %rdi
movl 0x50(%rdi), %eax
movl %eax, 0x50(%rbx)
callq 0x65f5c
movslq 0xcc(%rbx), %r15
leaq (,%r15,8), %rax
testq %r15, %r15
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0xb8(%rbx)
testq %r15, %r15
jle 0x628e6
movslq 0xcc(%rbx), %rdi
movslq %r15d, %rax
subq %rax, %rdi
shlq $0x3, %rdi
addq 0xb8(%rbx), %rdi
movl %eax, %edx
movl $0x8, %esi
movq %r14, %rcx
callq 0xe6c0
testl %eax, %eax
js 0x628d5
subl %eax, %r15d
jg 0x6280f
movslq 0xcc(%rbx), %r15
testq %r15, %r15
jle 0x628e6
movq 0xb8(%rbx), %rax
leaq 0x83337(%rip), %rcx     # 0xe5b98
movsd (%rcx), %xmm0
xorl %edx, %edx
movsd 0x46ab1(%rip), %xmm1    # 0xa9320
movapd 0x3f7a9(%rip), %xmm2    # 0xa2020
movabsq $-0x3f8bf00000000000, %rsi # imm = 0xC074100000000000
movsd (%rax,%rdx,8), %xmm3
addsd %xmm1, %xmm3
movapd %xmm0, %xmm4
xorpd %xmm2, %xmm4
ucomisd %xmm4, %xmm3
jb 0x628a6
ucomisd %xmm3, %xmm0
jb 0x628a6
movq %rsi, (%rax,%rdx,8)
movsd (%rcx), %xmm0
incq %rdx
cmpq %rdx, %r15
jne 0x62881
jmp 0x628e6
leaq 0x46dea(%rip), %rdi     # 0xa96a1
jmp 0x628dc
leaq 0x44f7f(%rip), %rdi     # 0xa783f
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x719d2
jmp 0x628e6
leaq 0x46df7(%rip), %rdi     # 0xa96ca
jmp 0x628dc
leaq 0x46e1d(%rip), %rdi     # 0xa96f9
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x719d2
movl %r15d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x65f5c
movq %rbx, %rdi
callq 0xe7e0
nop