// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::ReadPFMFile(char const*)
int R2Grid::
ReadPFMFile(const char *filename)
{
  // Open file
  FILE *fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open image: %s\n", filename);
    return 0;
  }

  // Read magic header
  int c;
  c = fgetc(fp); if (c != 'P') { RNFail("Bad magic keyword in %s\n", filename); return 0; }
  c = fgetc(fp); if (c != 'f') { RNFail("Bad magic keyword in %s\n", filename); return 0; }
  c = fgetc(fp); if (c != '\n') { RNFail("Bad magic keyword in %s\n", filename); return 0; }

  // Read width
  int width_count = 0;
  char width_string[256];
  for (int i = 0; i < 256; i++) { 
    c = fgetc(fp); 
    if ((c == ' ') && (width_count == 0)) { continue; }
    else if ((c == ' ') || (c == '\n')) { width_string[width_count] = '\0'; break; }
    else if (!isdigit(c)) { RNFail("Bad width character %c in %s\n", c, filename); return 0; }
    else width_string[width_count++] = c;
  }

  // Check width
  if ((width_count == 0) || (width_count > 128)) {
    RNFail("Error reading width in %s\n", filename); 
    return 0; 
  }

  // Read height
  int height_count = 0;
  char height_string[256];
  for (int i = 0; i < 256; i++) { 
    c = fgetc(fp); 
    if ((c == ' ') && (height_count == 0)) { continue; }
    else if ((c == ' ') || (c == '\n')) { height_string[height_count] = '\0'; break; }
    else if (!isdigit(c)) { RNFail("Bad height character %c in %s\n", c, filename); return 0; }
    else height_string[height_count++] = c;
  }

  // Check height
  if ((height_count == 0) || (height_count > 128)) {
    RNFail("Error reading height in %s\n", filename); 
    return 0; 
  }

  // Read endian
  int endian_count = 0;
  char endian_string[256];
  for (int i = 0; i < 256; i++) { 
    c = fgetc(fp); 
    if ((c == ' ') && (endian_count == 0)) { continue; }
    else if ((c == ' ') || (c == '\n')) { endian_string[endian_count] = '\0'; break; }
    if (!isdigit(c) && (c != '.') && (c != '-')) { RNFail("Bad endian character %c in %s\n", c, filename); return 0; }
    endian_string[endian_count++] = c;
  }

  // Check endian
  if ((endian_count == 0) || (endian_count > 128)) {
    RNFail("Error reading endian in %s\n", filename); 
    return 0; 
  }

  // Parse values
  int width = atoi(width_string);
  int height = atoi(height_string);
  float endian = (float) atof(endian_string);
  if (endian == -999.0F) RNFail("Just trying to avoid compiler warning for unused variable\n");

  // Allocate pixels
  int npixels = width * height;
  float *pixels = new float [ npixels ];
  if (!pixels) {
    RNFail("Unable to allocate pixels for %s\n", filename);
    return 0;
  }

  // Read pixels
  int count = npixels;
  while (count > 0) {
    int n = fread(&pixels[npixels-count], sizeof(float), count, fp);
    if (n <= 0) { RNFail("Unable to read pixels from %s\n", filename); return 0; }
    count -= n;
  }

  // Fill in grid info
  grid_resolution[0] = width;
  grid_resolution[1] = height;
  grid_row_size = width;
  grid_size = npixels;
  world_to_grid_scale_factor = 1;
  world_to_grid_transform = R2identity_affine;
  grid_to_world_transform = R2identity_affine;
  if (grid_values) delete [] grid_values;
  grid_values = new RNScalar [ grid_size ];
  for (int i = 0; i < grid_size; i++) {
    if (RNIsEqual(pixels[i], R2_GRID_UNKNOWN_VALUE)) grid_values[i] = R2_GRID_UNKNOWN_VALUE;
    else grid_values[i] = pixels[i];
  }

  // Delete pixels
  delete [] pixels;

  // Close image
  fclose(fp);

  // Return success
  return grid_size;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x3a287(%rip), %rsi     # 0x9355b
movq %r15, %rdi
callq 0xe550
testq %rax, %rax
je 0x5935f
movq %rax, %r14
movq %rax, %rdi
callq 0xe750
cmpl $0x50, %eax
jne 0x59356
movq %r14, %rdi
callq 0xe750
cmpl $0x66, %eax
jne 0x59356
movq %r14, %rdi
callq 0xe750
cmpl $0xa, %eax
jne 0x59356
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x5938e
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x59351
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x59386
cmpl $0xa, %eax
je 0x59386
movl %eax, %edx
addl $-0x30, %edx
cmpl $0xa, %edx
jae 0x593f6
leal 0x1(%rcx), %edx
movb %al, 0x210(%rsp,%rcx)
movl %edx, %r12d
jmp 0x59313
leaq 0x41158(%rip), %rdi     # 0x9a4b5
jmp 0x59366
leaq 0x41135(%rip), %rdi     # 0x9a49b
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, 0x210(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x593ab
leaq 0x41143(%rip), %rdi     # 0x9a4ec
jmp 0x59366
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x5940a
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x593f1
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x59402
cmpl $0xa, %eax
je 0x59402
movl %eax, %edx
addl $-0x30, %edx
cmpl $0xa, %edx
jae 0x59482
leal 0x1(%rcx), %edx
movb %al, 0x110(%rsp,%rcx)
movl %edx, %r12d
jmp 0x593b3
leaq 0x410d1(%rip), %rdi     # 0x9a4ce
jmp 0x59489
movb $0x0, 0x110(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x5942a
leaq 0x41101(%rip), %rdi     # 0x9a526
jmp 0x59366
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
subl $0x1, %ebp
jb 0x594a1
movq %r14, %rdi
callq 0xe750
movl %eax, %ecx
xorl $0x20, %ecx
xorl %edx, %edx
orl %r12d, %ecx
je 0x5947d
movslq %r12d, %rcx
cmpl $0x20, %eax
je 0x5949c
cmpl $0xa, %eax
je 0x5949c
leal -0x3a(%rax), %edx
cmpl $-0xa, %edx
setae %dl
movl %eax, %esi
addl $-0x2f, %esi
cmpl $-0x2, %esi
setae %sil
orb %dl, %sil
je 0x5967d
leal 0x1(%rcx), %edx
movb %al, 0x10(%rsp,%rcx)
movl %edx, %r12d
jmp 0x59432
leaq 0x4107e(%rip), %rdi     # 0x9a507
xorl %ebx, %ebx
movl %eax, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x681ca
jmp 0x59372
movb $0x0, 0x10(%rsp,%rcx)
testl %r12d, %r12d
setne %al
cmpl $0x81, %r12d
setl %cl
testb %cl, %al
jne 0x594c1
leaq 0x410f3(%rip), %rdi     # 0x9a5af
jmp 0x59366
leaq 0x210(%rsp), %rdi
callq 0xe730
movl %eax, %ebp
leaq 0x110(%rsp), %rdi
callq 0xe730
movl %eax, %r13d
leaq 0x10(%rsp), %rdi
callq 0xe5a0
cvtsd2ss %xmm0, %xmm0
ucomiss 0x40e3b(%rip), %xmm0    # 0x9a330
jne 0x59507
jp 0x59507
leaq 0x410cb(%rip), %rdi     # 0x9a5cb
xorl %eax, %eax
callq 0x681ca
movl %r13d, 0x8(%rsp)
movl %ebp, 0xc(%rsp)
imull %ebp, %r13d
movslq %r13d, %rax
leaq (,%rax,4), %rdi
testl %eax, %eax
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0xe030
movq %rax, %r12
movl %r13d, %ebp
testl %ebp, %ebp
jle 0x59561
movl %r13d, %eax
subl %ebp, %eax
cltq
leaq (%r12,%rax,4), %rdi
movl %ebp, %edx
pushq $0x4
popq %rsi
movq %r14, %rcx
callq 0xe6f0
subl %eax, %ebp
testl %eax, %eax
jg 0x59533
leaq 0x40ef8(%rip), %rdi     # 0x9a454
jmp 0x59366
movl 0xc(%rsp), %eax
movl %eax, 0xc0(%rbx)
movl 0x8(%rsp), %ecx
movl %ecx, 0xc4(%rbx)
movl %eax, 0xc8(%rbx)
movl %r13d, 0xcc(%rbx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%rbx)
leaq 0x58(%rbx), %rdi
leaq 0x4839ca(%rip), %r15    # 0x4dcf68
movq %r15, %rsi
callq 0x5ac32
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5ac32
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x595c2
callq 0xe5d0
movslq 0xcc(%rbx), %r15
leaq (,%r15,8), %rdi
testq %r15, %r15
pushq $-0x1
popq %rax
cmovsq %rax, %rdi
callq 0xe030
movq %rax, 0xb8(%rbx)
leaq 0x815ba(%rip), %rcx     # 0xdaba8
movsd (%rcx), %xmm0
movapd 0x39a36(%rip), %xmm1    # 0x93030
xorpd %xmm0, %xmm1
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r15d, %edx
movsd 0x40cbc(%rip), %xmm2    # 0x9a2d0
movsd 0x3a94c(%rip), %xmm3    # 0x93f68
cmpq %rcx, %rdx
je 0x59662
cvtss2sd (%r12,%rcx,4), %xmm4
movaps %xmm4, %xmm5
addsd %xmm2, %xmm5
movapd %xmm1, %xmm6
cmpnlesd %xmm5, %xmm6
movapd %xmm6, %xmm7
andpd %xmm4, %xmm7
cmpnlesd %xmm0, %xmm5
andpd %xmm5, %xmm4
andnpd %xmm3, %xmm5
orpd %xmm4, %xmm5
andnpd %xmm5, %xmm6
orpd %xmm7, %xmm6
movlpd %xmm6, (%rax,%rcx,8)
incq %rcx
jmp 0x5961c
movq %r12, %rdi
callq 0xe5d0
movq %r14, %rdi
callq 0xe440
movl 0xcc(%rbx), %ebx
jmp 0x59372
leaq 0x40f0c(%rip), %rdi     # 0x9a590
xorl %ebx, %ebx
movl %eax, %esi
jmp 0x5948d
nop