// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xb0, %rsp
movq %rsi, %r14
movq %rdi, %rbx
addq $0xc0, %rdi
pushq $0x4
popq %rsi
pushq $0x2
popq %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x2, %rax
jne 0x5a350
movl 0xc0(%rbx), %esi
movl %esi, 0xc8(%rbx)
imull 0xc4(%rbx), %esi
movl %esi, 0xcc(%rbx)
testl %esi, %esi
jle 0x5a359
leaq 0x60(%rsp), %rdi
pushq $0x8
popq %rsi
pushq $0x9
popq %rdx
movq %r14, %rcx
callq 0xe6f0
cmpq $0x9, %rax
jne 0x5a36b
leaq 0x58(%rbx), %r15
leaq 0x8(%rsp), %r12
leaq 0x60(%rsp), %rsi
movq %r12, %rdi
callq 0x5d8c4
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x5d5e2
movq %r15, %rdi
callq 0x5d378
movsd %xmm0, 0xb0(%rbx)
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x5ac5c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5ac32
leaq 0x8(%rsp), %rdi
callq 0x5d61e
movslq 0xcc(%rbx), %r12
leaq (,%r12,8), %rax
testq %r12, %r12
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, 0xb8(%rbx)
pushq $0x8
popq %r15
testl %r12d, %r12d
jle 0x5a38d
movslq 0xcc(%rbx), %rdi
movslq %r12d, %rax
subq %rax, %rdi
shlq $0x3, %rdi
addq 0xb8(%rbx), %rdi
movl %r12d, %edx
movq %r15, %rsi
movq %r14, %rcx
callq 0xe6f0
subl %eax, %r12d
testl %eax, %eax
jns 0x5a315
leaq 0x4033b(%rip), %rdi     # 0x9a689
jmp 0x5a372
leaq 0x402da(%rip), %rdi     # 0x9a631
jmp 0x5a372
leaq 0x3e48f(%rip), %rdi     # 0x987ef
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x681ca
jmp 0x5a37b
leaq 0x402e8(%rip), %rdi     # 0x9a65a
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x681ca
movl %ebp, %eax
addq $0xb0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl 0xcc(%rbx), %ebp
movq 0xb8(%rbx), %rax
xorl %ecx, %ecx
testl %ebp, %ebp
movl $0x0, %edx
cmovgl %ebp, %edx
leaq 0x807fb(%rip), %rsi     # 0xdaba8
movsd 0x3ff1b(%rip), %xmm0    # 0x9a2d0
movapd 0x38c73(%rip), %xmm1    # 0x93030
movabsq $-0x3f8bf00000000000, %rdi # imm = 0xC074100000000000
cmpq %rcx, %rdx
je 0x5a37b
movsd (%rsi), %xmm2
movsd (%rax,%rcx,8), %xmm3
addsd %xmm0, %xmm3
movapd %xmm2, %xmm4
xorpd %xmm1, %xmm4
ucomisd %xmm4, %xmm3
jb 0x5a3f1
ucomisd %xmm3, %xmm2
jb 0x5a3f1
movq %rdi, (%rax,%rcx,8)
incq %rcx
jmp 0x5a3c7
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5d61e
movq %rbx, %rdi
callq 0xe810
nop