// File: /workspace/github_cpp_makefile_star2_O3/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 0x40f3b(%rip), %rsi     # 0xa25ab
movq %r15, %rdi
callq 0xe530
testq %rax, %rax
je 0x61704
movq %rax, %r14
movq %rax, %rdi
callq 0xe720
cmpl $0x50, %eax
jne 0x616fb
movq %r14, %rdi
callq 0xe720
cmpl $0x66, %eax
jne 0x616fb
movq %r14, %rdi
callq 0xe720
cmpl $0xa, %eax
jne 0x616fb
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r14, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x616f2
cmpl $0x20, %eax
je 0x6172b
cmpl $0xa, %eax
je 0x6172b
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x617a4
movslq %r12d, %rcx
incl %r12d
movb %al, 0x210(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x616b3
jmp 0x61739
leaq 0x47e23(%rip), %rdi     # 0xa9525
jmp 0x6170b
leaq 0x47e00(%rip), %rdi     # 0xa950b
xorl %ebx, %ebx
movq %r15, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq %r12d, %rax
movb $0x0, 0x210(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x61754
leaq 0x47e0a(%rip), %rdi     # 0xa955c
jmp 0x6170b
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r14, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x6179b
cmpl $0x20, %eax
je 0x617b0
cmpl $0xa, %eax
je 0x617b0
movl %eax, %ecx
addl $-0x30, %ecx
cmpl $0xa, %ecx
jae 0x61837
movslq %r12d, %rcx
incl %r12d
movb %al, 0x110(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x6175c
jmp 0x617be
leaq 0x47d93(%rip), %rdi     # 0xa953e
jmp 0x6183e
movslq %r12d, %rax
movb $0x0, 0x110(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x617dc
leaq 0x47dbf(%rip), %rdi     # 0xa9596
jmp 0x6170b
xorl %r12d, %r12d
movl $0x100, %ebp            # imm = 0x100
movq %r14, %rdi
callq 0xe720
movl %eax, %ecx
xorl $0x20, %ecx
orl %r12d, %ecx
movl $0x0, %ecx
je 0x6182e
cmpl $0x20, %eax
je 0x61851
cmpl $0xa, %eax
je 0x61851
leal -0x3a(%rax), %ecx
cmpl $-0xa, %ecx
setae %cl
movl %eax, %edx
addl $-0x2f, %edx
cmpl $-0x2, %edx
setae %dl
orb %cl, %dl
je 0x61a4d
movslq %r12d, %rcx
incl %r12d
movb %al, 0x10(%rsp,%rcx)
movl %r12d, %ecx
movl %ecx, %r12d
decl %ebp
jne 0x617e4
jmp 0x6185c
leaq 0x47d39(%rip), %rdi     # 0xa9577
xorl %ebx, %ebx
movl %eax, %esi
movq %r15, %rdx
xorl %eax, %eax
callq 0x719d2
jmp 0x61717
movslq %r12d, %rax
movb $0x0, 0x10(%rsp,%rax)
movl %r12d, %ecx
testl %ecx, %ecx
setne %al
cmpl $0x81, %ecx
setl %cl
testb %cl, %al
jne 0x6187a
leaq 0x47daa(%rip), %rdi     # 0xa961f
jmp 0x6170b
leaq 0x210(%rsp), %rdi
callq 0xe700
movl %eax, %r12d
leaq 0x110(%rsp), %rdi
callq 0xe700
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0xe580
cvtsd2ss %xmm0, %xmm0
ucomiss 0x47af2(%rip), %xmm0    # 0xa93a0
jne 0x618c0
jp 0x618c0
leaq 0x47d82(%rip), %rdi     # 0xa963b
xorl %eax, %eax
callq 0x719d2
movl %ebp, 0x8(%rsp)
movl %r12d, 0xc(%rsp)
imull %r12d, %ebp
movslq %ebp, %r13
leaq (,%r13,4), %rax
testl %r13d, %r13d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r12
testl %r13d, %r13d
jle 0x6191e
movl %ebp, %r13d
movl %ebp, %eax
subl %r13d, %eax
cltq
leaq (%r12,%rax,4), %rdi
movl %r13d, %edx
movl $0x4, %esi
movq %r14, %rcx
callq 0xe6c0
testl %eax, %eax
jle 0x61a5d
subl %eax, %r13d
jg 0x618f6
movl 0xc(%rsp), %eax
movl %eax, 0xc0(%rbx)
movl 0x8(%rsp), %ecx
movl %ecx, 0xc4(%rbx)
movl %eax, 0xc8(%rbx)
movl %ebp, 0xcc(%rbx)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xb0(%rbx)
leaq 0x60(%rbx), %rdi
leaq 0x4865fe(%rip), %r13    # 0x4e7f58
leaq 0x8(%r13), %r15
movq %r15, %rsi
callq 0x66bbc
movl 0x50(%r13), %eax
movl %eax, 0xa8(%rbx)
leaq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0x66bbc
movl 0x50(%r13), %eax
movl %eax, 0x50(%rbx)
movq 0xb8(%rbx), %rdi
testq %rdi, %rdi
je 0x61994
callq 0xe5b0
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 0x61a32
leaq 0x841cf(%rip), %rcx     # 0xe5b98
movsd (%rcx), %xmm0
movapd 0x4064b(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
xorl %ecx, %ecx
movsd 0x4793d(%rip), %xmm2    # 0xa9320
movsd 0x415cd(%rip), %xmm3    # 0xa2fb8
xorps %xmm4, %xmm4
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
cmpq %rcx, %r15
jne 0x619eb
movq %r12, %rdi
callq 0xe5b0
movq %r14, %rdi
callq 0xe430
movl 0xcc(%rbx), %ebx
jmp 0x61717
leaq 0x47bac(%rip), %rdi     # 0xa9600
xorl %ebx, %ebx
movl %eax, %esi
jmp 0x61842
leaq 0x47a60(%rip), %rdi     # 0xa94c4
jmp 0x6170b
nop