// File: matthewarcus[P]mmps/image.cpp
// Function: Image::Write(char const*) const
void Image::Write (const char* filename) const
{
  if (mmapdata != NULL) {
    fprintf(stderr, "mmapped image - skipping WriteImage\n");
  } else {
    int fd = 1; // stdout
    char header[128];
    size_t datasize;
    if (filename != NULL) {
      fd = creat(filename, 0666);
      if (fd < 0) {
	syserror("Can't open output file");
      }
    }
#if defined O_BINARY
    // Ghastly hack to ensure CR/LF substitution not done, eg. on cygwin
    // Set the output type to binary, not text.
    setmode(fd,O_BINARY);
#endif
    sprintf(header, "P6\n%ld %ld\n255\n", width, height);
    if (write(fd, header, strlen(header)) < 0) {
      syserror("write failed");
    }
    datasize = (long) width * height * 3;
    char *dataptr = data;
    while (datasize > 0) {
      size_t MAXDATA = 64UL*1024*1024;
      ssize_t written = write(fd, dataptr, std::min(MAXDATA,datasize));
      fprintf(stderr,"%zd bytes written\n", written);
      if (written < 0) {
	syserror("write failed");
      }
      datasize -= written;
      dataptr += written;
    }
    if (filename != NULL) {
      close(fd);
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, 0x18(%rax)
je 0x13602
movq 0x99f4(%rip), %rax      # 0x1cfe0
movq (%rax), %rdi
leaq 0x2470(%rip), %rsi      # 0x15a66
movb $0x0, %al
callq 0x5370
jmp 0x1379e
movl $0x1, -0x14(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x13635
movq -0x10(%rbp), %rdi
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x51a0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x13633
leaq 0x245d(%rip), %rdi      # 0x15a8b
callq 0xfee0
jmp 0x13635
movq -0xc8(%rbp), %rax
leaq -0xa0(%rbp), %rdi
movq (%rax), %rdx
movq 0x8(%rax), %rcx
leaq 0x2451(%rip), %rsi      # 0x15aa2
movb $0x0, %al
callq 0x5070
movl -0x14(%rbp), %eax
movl %eax, -0xd4(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5100
movl -0xd4(%rbp), %edi
movq -0xd0(%rbp), %rsi
movq %rax, %rdx
callq 0x50b0
cmpq $0x0, %rax
jge 0x136a2
leaq 0x2415(%rip), %rdi      # 0x15ab2
callq 0xfee0
movq -0xc8(%rbp), %rax
movq (%rax), %rcx
imulq 0x8(%rax), %rcx
imulq $0x3, %rcx, %rcx
movq %rcx, -0xa8(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xa8(%rbp)
jbe 0x1378d
movq $0x4000000, -0xb8(%rbp) # imm = 0x4000000
movl -0x14(%rbp), %eax
movl %eax, -0xe4(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x115e0
movl -0xe4(%rbp), %edi
movq -0xe0(%rbp), %rsi
movq (%rax), %rdx
callq 0x50b0
movq %rax, -0xc0(%rbp)
movq 0x98b3(%rip), %rax      # 0x1cfe0
movq (%rax), %rdi
movq -0xc0(%rbp), %rdx
leaq 0x2381(%rip), %rsi      # 0x15abf
movb $0x0, %al
callq 0x5370
cmpq $0x0, -0xc0(%rbp)
jge 0x1375b
leaq 0x235c(%rip), %rdi      # 0x15ab2
callq 0xfee0
movq -0xc0(%rbp), %rcx
movq -0xa8(%rbp), %rax
subq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xc0(%rbp), %rax
addq -0xb0(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x136c7
cmpq $0x0, -0x10(%rbp)
je 0x1379c
movl -0x14(%rbp), %edi
callq 0x5480
jmp 0x1379e
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)