// File: /workspace/github_cpp_makefile_star2_O2/matthewarcus[P]mmps/image.cpp
// Function: Image::Read(char const*, bool)
void Image::Read (const char* filename, bool writeable)
{
  int fd = -1;
  struct stat filestat;
  size_t filesize = 0;
  char *filebuffer = NULL;
  char *fileend = NULL;
  char *fileptr = NULL;
  char linebuffer[LINELENGTH];
  long w; long h; int ncolors; long datasize;

  fd = open(filename, writeable ? O_RDWR : O_RDONLY);
  if (fd < 0) {
    syserror("Couldn't open input file");
  }
  if (fstat(fd, &filestat) != 0) {
    syserror("Couldn't stat file");
  }
  filesize = filestat.st_size;
  // Now try to mmap the file
  int prot = PROT_READ;
  if (writeable) prot |= PROT_WRITE;
  filebuffer = static_cast<char *>(mmap(0, filesize, prot, MAP_SHARED, fd, 0));
  if (filebuffer == NULL) {
    syserror("Couldn't mmap file");
  }
  mmapdata = filebuffer;
  mmapsize = filesize;
  mmapfd = fd;

  if (strncmp(filebuffer,"P6",2) != 0) {
    error("Invalid file\n");
  }
  fileptr = filebuffer + 2;
  fileend = filebuffer + filesize;
  GetLine(fileptr, fileend, linebuffer, LINELENGTH);
  if (sscanf(linebuffer, "%ld %ld", &w, &h) != 2){
    error("Invalid file\n");
  }
  GetLine(fileptr, fileend, linebuffer, LINELENGTH);
  if (sscanf(linebuffer, "%d", &ncolors) != 1){
    error("Invalid file\n");
  }
  if (ncolors != 255){
    error("Unexpected number of colors\n");
  }
  datasize = (long) w*h*3;
  // Now we hope that fileptr is pointing at the rgb data
  if (fileend - fileptr != datasize) {
    error("Not enough data in file\n");
  }
  width = w;
  height = h;
  data = fileptr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %edx, %r15d
movq %rsi, %rax
movq %rdi, %rbx
andq $0x0, (%rsp)
leal (%r15,%r15), %esi
movq %rax, %rdi
xorl %eax, %eax
callq 0x4160
movl %eax, %ebp
testl %eax, %eax
jns 0xad89
leaq 0x1cfa(%rip), %rdi      # 0xca7e
callq 0x9e44
leaq 0xa8(%rsp), %rsi
movl %ebp, %edi
callq 0x4330
testl %eax, %eax
je 0xada8
leaq 0x1cf4(%rip), %rdi      # 0xca97
callq 0x9e44
movq 0xd8(%rsp), %r14
movzbl %r15b, %eax
leal 0x1(,%rax,2), %edx
pushq $0x1
popq %rcx
xorl %edi, %edi
movq %r14, %rsi
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x4370
movq %rax, %r15
testq %rax, %rax
jne 0xade2
leaq 0x1ccd(%rip), %rdi      # 0xcaaa
callq 0x9e44
movq %r15, 0x18(%rbx)
movq %r14, 0x20(%rbx)
movl %ebp, 0x28(%rbx)
cmpb $0x50, (%r15)
jne 0xadfa
cmpb $0x36, 0x1(%r15)
je 0xae06
leaq 0x1cbc(%rip), %rdi      # 0xcabd
callq 0x9e20
leaq 0x2(%r15), %rax
movq %rsp, %rdi
movq %rax, (%rdi)
addq %r15, %r14
leaq 0x20(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0xaeef
leaq 0x1ca1(%rip), %rsi      # 0xcacb
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
xorl %eax, %eax
callq 0x4200
cmpl $0x2, %eax
je 0xae4f
leaq 0x1c73(%rip), %rdi      # 0xcabd
callq 0x9e20
movq %rsp, %rdi
leaq 0x20(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0xaeef
leaq 0x1b9b(%rip), %rsi      # 0xca04
leaq 0xc(%rsp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x4200
cmpl $0x1, %eax
je 0xae89
leaq 0x1c39(%rip), %rdi      # 0xcabd
callq 0x9e20
cmpl $0xff, 0xc(%rsp)
je 0xae9f
leaq 0x1c39(%rip), %rdi      # 0xcad3
callq 0x9e20
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, %rdx
imulq %rcx, %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rsp), %r15
subq %r15, %r14
cmpq %rdx, %r14
je 0xaed6
leaq 0x1c29(%rip), %rdi      # 0xcaf0
callq 0x9e20
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, (%rbx)
movq %rcx, 0x8(%rbx)
movq %r15, 0x10(%rbx)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r14
popq %r15
popq %rbp
retq