// File: /workspace/github_cpp_makefile_star2_O1/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 0x4160
movl %eax, %ebp
testl %eax, %eax
jns 0xb9ac
leaq 0x20bf(%rip), %rdi      # 0xda66
callq 0xa912
leaq 0xa8(%rsp), %rsi
movl %ebp, %edi
callq 0x4310
testl %eax, %eax
je 0xb9cb
leaq 0x20b9(%rip), %rdi      # 0xda7f
callq 0xa912
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 0x4350
movq %rax, %r15
testq %rax, %rax
jne 0xba07
leaq 0x2090(%rip), %rdi      # 0xda92
callq 0xa912
movq %r15, 0x18(%rbx)
movq %r14, 0x20(%rbx)
movl %ebp, 0x28(%rbx)
leaq 0x208c(%rip), %rsi      # 0xdaa5
movl $0x2, %edx
movq %r15, %rdi
callq 0x40e0
testl %eax, %eax
je 0xba36
leaq 0x2077(%rip), %rdi      # 0xdaa8
callq 0xa8ec
leaq 0x2(%r15), %rax
movq %rsp, %rdi
movq %rax, (%rdi)
addq %r15, %r14
leaq 0x20(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0xbb19
leaq 0x205c(%rip), %rsi      # 0xdab6
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
xorl %eax, %eax
callq 0x4200
cmpl $0x2, %eax
je 0xba7f
leaq 0x202e(%rip), %rdi      # 0xdaa8
callq 0xa8ec
movq %rsp, %rdi
leaq 0x20(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0xbb19
leaq 0x1f53(%rip), %rsi      # 0xd9ec
leaq 0xc(%rsp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x4200
cmpl $0x1, %eax
je 0xbab9
leaq 0x1ff4(%rip), %rdi      # 0xdaa8
callq 0xa8ec
cmpl $0xff, 0xc(%rsp)
je 0xbacf
leaq 0x1ff4(%rip), %rdi      # 0xdabe
callq 0xa8ec
movq 0x18(%rsp), %rax
imulq 0x10(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq (%rsp), %r15
subq %r15, %r14
cmpq %rax, %r14
je 0xbaf6
leaq 0x1fea(%rip), %rdi      # 0xdadb
callq 0xa8ec
movq 0x18(%rsp), %rax
movq %rax, (%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rbx)
movq %r15, 0x10(%rbx)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r14
popq %r15
popq %rbp
retq