// File: jstaursky[P]hutch/src/Sleigh/loadimage.cc
// Function: RawLoadImage::loadFill(unsigned char*, int, Address const&)
void RawLoadImage::loadFill(uint1 *ptr,int4 size,const Address &addr)

{
  uintb curaddr = addr.getOffset();
  uintb offset = 0;
  uintb readsize;

  curaddr -= vma;		// Get relative offset of first byte
  while(size>0) {
    if (curaddr >= filesize) {
      if (offset == 0)		// Initial address not within file
	break;
      memset(ptr+offset,0,size); // Fill out the rest of the buffer with 0
      return;
    }
    readsize = size;
    if (curaddr + readsize > filesize) // Adjust to biggest possible read
      readsize = filesize - curaddr;
    thefile->seekg(curaddr);
    thefile->read((char *)(ptr+offset),readsize);
    offset += readsize;
    size -= readsize;
    curaddr += readsize;
  }
  if (size > 0) {
    ostringstream errmsg;
    errmsg << "Unable to load " << dec << size << " bytes at " << addr.getShortcut();
    addr.printRaw(errmsg);
    throw DataUnavailError(errmsg.str());
  }
}

// Assembly:
subq $0x248, %rsp            # imm = 0x248
movq %rdi, 0x240(%rsp)
movq %rsi, 0x238(%rsp)
movl %edx, 0x234(%rsp)
movq %rcx, 0x228(%rsp)
movq 0x240(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x228(%rsp), %rdi
callq 0xe03b0
movq %rax, %rcx
movq 0x48(%rsp), %rax
movq %rcx, 0x220(%rsp)
movq $0x0, 0x218(%rsp)
movq 0x28(%rax), %rcx
movq 0x220(%rsp), %rax
subq %rcx, %rax
movq %rax, 0x220(%rsp)
cmpl $0x0, 0x234(%rsp)
jle 0x15e13e
movq 0x48(%rsp), %rcx
movq 0x220(%rsp), %rax
cmpq 0x38(%rcx), %rax
jb 0x15e048
cmpq $0x0, 0x218(%rsp)
jne 0x15e024
jmp 0x15e13e
movq 0x238(%rsp), %rdi
addq 0x218(%rsp), %rdi
movslq 0x234(%rsp), %rdx
xorl %esi, %esi
callq 0xd36e0
jmp 0x15e2b8
movq 0x48(%rsp), %rcx
movslq 0x234(%rsp), %rax
movq %rax, 0x210(%rsp)
movq 0x220(%rsp), %rax
addq 0x210(%rsp), %rax
cmpq 0x38(%rcx), %rax
jbe 0x15e08c
movq 0x48(%rsp), %rax
movq 0x38(%rax), %rax
subq 0x220(%rsp), %rax
movq %rax, 0x210(%rsp)
movq 0x48(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x220(%rsp), %rsi
leaq 0x200(%rsp), %rdi
callq 0xd93e0
movq 0x40(%rsp), %rdi
movq 0x200(%rsp), %rsi
movq 0x208(%rsp), %rdx
callq 0xdb200
movq 0x48(%rsp), %rax
movq 0x30(%rax), %rdi
movq 0x238(%rsp), %rsi
addq 0x218(%rsp), %rsi
movq 0x210(%rsp), %rdx
callq 0xdb050
movq 0x210(%rsp), %rax
addq 0x218(%rsp), %rax
movq %rax, 0x218(%rsp)
movq 0x210(%rsp), %rcx
movslq 0x234(%rsp), %rax
subq %rcx, %rax
movl %eax, 0x234(%rsp)
movq 0x210(%rsp), %rax
addq 0x220(%rsp), %rax
movq %rax, 0x220(%rsp)
jmp 0x15dff3
cmpl $0x0, 0x234(%rsp)
jle 0x15e2b8
leaq 0x88(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xdbe90
movq 0x30(%rsp), %rdi
leaq 0x2b77a(%rip), %rsi     # 0x1898e4
callq 0xd8300
movq %rax, 0x38(%rsp)
jmp 0x15e176
movq 0x38(%rsp), %rdi
movq 0x69c16(%rip), %rsi     # 0x1c7d98
callq 0xd1180
movq %rax, 0x28(%rsp)
jmp 0x15e18e
movq 0x28(%rsp), %rdi
movl 0x234(%rsp), %esi
callq 0xe03e0
movq %rax, 0x20(%rsp)
jmp 0x15e1a6
movq 0x20(%rsp), %rdi
leaq 0x2b742(%rip), %rsi     # 0x1898f4
callq 0xd8300
movq %rax, 0x18(%rsp)
jmp 0x15e1be
movq 0x228(%rsp), %rdi
callq 0xd5b50
movb %al, 0x17(%rsp)
jmp 0x15e1d1
movq 0x18(%rsp), %rdi
movb 0x17(%rsp), %al
movsbl %al, %esi
callq 0xda0c0
jmp 0x15e1e4
movq 0x228(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0xd82c0
jmp 0x15e1fb
movb $0x1, 0x57(%rsp)
movl $0x20, %edi
callq 0xd2b40
movq %rax, 0x8(%rsp)
leaq 0x58(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0xd8840
jmp 0x15e223
movq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xdaae0
jmp 0x15e234
movq 0x8(%rsp), %rdi
movb $0x0, 0x57(%rsp)
movq 0x69c93(%rip), %rsi     # 0x1c7ed8
movq 0x69a34(%rip), %rdx     # 0x1c7c80
callq 0xe01f0
jmp 0x15e2cd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x15e2a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x15e294
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x58(%rsp), %rdi
callq 0xd4550
testb $0x1, 0x57(%rsp)
jne 0x15e29d
jmp 0x15e2a7
movq 0x8(%rsp), %rdi
callq 0xd5400
jmp 0x15e2a9
leaq 0x88(%rsp), %rdi
callq 0xd0160
jmp 0x15e2c0
addq $0x248, %rsp            # imm = 0x248
retq
movq 0x80(%rsp), %rdi
callq 0xe0be0
nopl (%rax)