// File: /workspace/github_cpp_makefile_star2_O3/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
movq $0x0, (%rsp)
leal (%r15,%r15), %esi
movq %rax, %rdi
xorl %eax, %eax
callq 0x4150
movl %eax, %ebp
testl %eax, %eax
jns 0xbc24
leaq 0x1ebf(%rip), %rdi      # 0xdade
callq 0xab82
leaq 0xa8(%rsp), %rsi
movl %ebp, %edi
callq 0x4300
testl %eax, %eax
je 0xbc43
leaq 0x1eb9(%rip), %rdi      # 0xdaf7
callq 0xab82
movq 0xd8(%rsp), %r14
movzbl %r15b, %eax
leal 0x1(,%rax,2), %edx
xorl %edi, %edi
movq %r14, %rsi
movl $0x1, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x4340
movq %rax, %r15
testq %rax, %rax
jne 0xbc7f
leaq 0x1e90(%rip), %rdi      # 0xdb0a
callq 0xab82
movq %r15, 0x18(%rbx)
movq %r14, 0x20(%rbx)
movl %ebp, 0x28(%rbx)
cmpb $0x50, (%r15)
jne 0xbc97
cmpb $0x36, 0x1(%r15)
je 0xbca3
leaq 0x1e7f(%rip), %rdi      # 0xdb1d
callq 0xab5c
leaq 0x2(%r15), %rax
movq %rsp, %rdi
movq %rax, (%rdi)
addq %r15, %r14
leaq 0x20(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0xbd8c
leaq 0x1e64(%rip), %rsi      # 0xdb2b
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
xorl %eax, %eax
callq 0x41f0
cmpl $0x2, %eax
je 0xbcec
leaq 0x1e36(%rip), %rdi      # 0xdb1d
callq 0xab5c
movq %rsp, %rdi
leaq 0x20(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0xbd8c
leaq 0x1d5e(%rip), %rsi      # 0xda64
leaq 0xc(%rsp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x41f0
cmpl $0x1, %eax
je 0xbd26
leaq 0x1dfc(%rip), %rdi      # 0xdb1d
callq 0xab5c
cmpl $0xff, 0xc(%rsp)
je 0xbd3c
leaq 0x1dfc(%rip), %rdi      # 0xdb33
callq 0xab5c
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 0xbd73
leaq 0x1dec(%rip), %rdi      # 0xdb50
callq 0xab5c
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