// File: 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
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movb %dl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x180(%rbp)
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0xb0(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %cl
xorl %esi, %esi
movl $0x2, %eax
testb $0x1, %cl
cmovnel %eax, %esi
movb $0x0, %al
callq 0x51d0
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x132a8
leaq 0x2735(%rip), %rdi      # 0x159d8
callq 0xfee0
movl -0x18(%rbp), %edi
leaq -0xa8(%rbp), %rsi
callq 0x53f0
cmpl $0x0, %eax
je 0x132c8
leaq 0x272e(%rip), %rdi      # 0x159f1
callq 0xfee0
movq -0x78(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl $0x1, -0x174(%rbp)
testb $0x1, -0x11(%rbp)
je 0x132f2
movl -0x174(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x174(%rbp)
movq -0xb0(%rbp), %rsi
movl -0x174(%rbp), %edx
movl -0x18(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %ecx
movq %r9, %rdi
callq 0x5440
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
jne 0x13332
leaq 0x26d7(%rip), %rdi      # 0x15a04
callq 0xfee0
movq -0x180(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0xb0(%rbp), %rcx
movq %rcx, 0x20(%rax)
movl -0x18(%rbp), %ecx
movl %ecx, 0x28(%rax)
movq -0xb8(%rbp), %rdi
leaq 0x26b4(%rip), %rsi      # 0x15a17
movl $0x2, %edx
callq 0x5120
cmpl $0x0, %eax
je 0x1337e
leaq 0x26a1(%rip), %rdi      # 0x15a1a
callq 0xfea0
movq -0xb8(%rbp), %rax
addq $0x2, %rax
movq %rax, -0xc8(%rbp)
movq -0xb8(%rbp), %rax
addq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rsi
leaq -0x150(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
movl $0x80, %ecx
callq 0x134d0
leaq -0x150(%rbp), %rdi
leaq 0x2656(%rip), %rsi      # 0x15a28
leaq -0x158(%rbp), %rdx
leaq -0x160(%rbp), %rcx
movb $0x0, %al
callq 0x5290
cmpl $0x2, %eax
je 0x133f8
leaq 0x2627(%rip), %rdi      # 0x15a1a
callq 0xfea0
movq -0xc0(%rbp), %rsi
leaq -0x150(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
movl $0x80, %ecx
callq 0x134d0
leaq -0x150(%rbp), %rdi
leaq 0x2539(%rip), %rsi      # 0x1595e
leaq -0x164(%rbp), %rdx
movb $0x0, %al
callq 0x5290
cmpl $0x1, %eax
je 0x13444
leaq 0x25db(%rip), %rdi      # 0x15a1a
callq 0xfea0
cmpl $0xff, -0x164(%rbp)
je 0x1345c
leaq 0x25d9(%rip), %rdi      # 0x15a30
callq 0xfea0
movq -0x158(%rbp), %rax
imulq -0x160(%rbp), %rax
imulq $0x3, %rax, %rax
movq %rax, -0x170(%rbp)
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rcx
subq %rcx, %rax
cmpq -0x170(%rbp), %rax
je 0x1349c
leaq 0x25b6(%rip), %rdi      # 0x15a4d
callq 0xfea0
movq -0x180(%rbp), %rax
movq -0x158(%rbp), %rcx
movq %rcx, (%rax)
movq -0x160(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0xc8(%rbp), %rcx
movq %rcx, 0x10(%rax)
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopl (%rax)