def get_env(base_vars=None, lib_dir=None):
    """
    Return a dictionary of environment variables for command execution with
    appropriate LD paths. Use the optional `base_vars` environment variables
    dictionary as a base if provided. Note: if `base_vars`  contains LD
    variables these will be overwritten.
    Add `lib_dir` as a proper "LD_LIBRARY_PATH"-like path if provided.
    """
    env_vars = {}
    if base_vars:
        env_vars.update(base_vars)

    # Create and add LD environment variables
    if lib_dir:
        path_var = '%(lib_dir)s' % locals()
        # on Linux/posix
        env_vars['LD_LIBRARY_PATH'] = path_var
        # on Mac
        env_vars['DYLD_LIBRARY_PATH'] = path_var

    # ensure that we use bytes, not unicode
    def to_bytes(s):
        return s if isinstance(s, bytes) else s.encode('utf-8')

    env_vars = {to_bytes(k): to_bytes(v) for k, v in env_vars.items()}
    return env_vars