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

  // Read header line
  int ndimensions;
  double polygon_depth, sample_spacing;
  if (fscanf(fp, "%d %d %lf %lf", &ndimensions, &npoints, &polygon_depth, &sample_spacing) != 4) {
    RNFail("Unable to read polygon file header: %s\n", filename);
    return 0;
  }

  // Allocate points
  points = new R2Point [ npoints ];
  if (!points) {
    RNFail("Unable to allocate points for polygon: %s\n", filename);
    return 0;
  }

  // Read points
  for (int i = 0; i < npoints; i++) {
    double x, y;
    if (fscanf(fp, "%lf %lf", &x, &y) != 2) {
      RNFail("Unable to read point %d from polygon file %s\n", npoints, filename);
      return 0;
    }

    // Add point
    points[i].Reset(x,y);
    bbox.Union(points[i]);
  }

  // Close file
  fclose(fp);

  // Return success
  return npoints;
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x80(%rsp), %rdi
leaq 0x7b700(%rip), %rsi     # 0x138673
callq 0xf5e0
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0xbcfab
movq 0x80(%rsp), %rsi
leaq 0x79271(%rip), %rdi     # 0x136205
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x94(%rsp)
jmp 0xbd1c3
movq 0x38(%rsp), %rcx
movq 0x78(%rsp), %rdi
addq $0x10, %rcx
leaq 0x79266(%rip), %rsi     # 0x136226
leaq 0x74(%rsp), %rdx
leaq 0x68(%rsp), %r8
leaq 0x60(%rsp), %r9
movb $0x0, %al
callq 0xf910
cmpl $0x4, %eax
je 0xbd001
movq 0x80(%rsp), %rsi
leaq 0x7924a(%rip), %rdi     # 0x136234
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x94(%rsp)
jmp 0xbd1c3
movq 0x38(%rsp), %rax
movslq 0x10(%rax), %rax
movq %rax, 0x28(%rsp)
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x20(%rsp)
callq 0xf030
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x30(%rsp)
cmpq $0x0, %rax
je 0xbd08f
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rdx
shlq $0x4, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0xc3130
jmp 0xbd077
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
addq $0x10, %rax
cmpq %rcx, %rax
movq %rax, 0x18(%rsp)
jne 0xbd066
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0x8(%rax)
cmpq $0x0, 0x8(%rax)
jne 0xbd0ec
movq 0x80(%rsp), %rsi
leaq 0x791a9(%rip), %rdi     # 0x13625c
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x94(%rsp)
jmp 0xbd1c3
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
callq 0xf990
jmp 0xbd1d2
movl $0x0, 0x50(%rsp)
movq 0x38(%rsp), %rcx
movl 0x50(%rsp), %eax
cmpl 0x10(%rcx), %eax
jge 0xbd1aa
movq 0x78(%rsp), %rdi
leaq 0x7911a(%rip), %rsi     # 0x13622c
leaq 0x48(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movb $0x0, %al
callq 0xf910
cmpl $0x2, %eax
je 0xbd153
movq 0x38(%rsp), %rax
movl 0x10(%rax), %esi
movq 0x80(%rsp), %rdx
leaq 0x79148(%rip), %rdi     # 0x136287
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0x94(%rsp)
jmp 0xbd1c3
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rdi
movslq 0x50(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movsd 0x48(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
callq 0xbd320
movq 0x38(%rsp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movq 0x8(%rax), %rsi
movslq 0x50(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0xbe470
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0xbd0f4
movq 0x78(%rsp), %rdi
callq 0xf4b0
movq 0x38(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0xf890
nopl (%rax)