'Write buf to memory start at an address
Args:
- address: start address to write (Int)
- buf: data to write (raw bytes)
Returns:
- number of written bytes (Int)'

def writemem(self, address, buf):

    out = None
    if (not buf):
        return 0
    if self.getpid():
        tmp = tmpfile(is_binary_file=True)
        tmp.write(buf)
        tmp.flush()
        out = self.execute_redirect(('restore    %s    binary    0x%x' % (tmp.name, address)))
        tmp.close()
    if (not out):
        for i in range(len(buf)):
            if (not self.execute(('set    {char}0x%x    =    0x%x' % ((address + i), ord(buf[i]))))):
                return i
        return (i + 1)
    elif ('error' in out):
        for i in range(0, len(buf), 1):
            if (not self.is_address((address + i))):
                return i
    else:
        return len(buf)
