def calculate_offset(address, base, shadow_base=0):
    """ Calculates an offset between two addresses, taking optional
        shadow base into consideration.

    Args:
        address (int): first address
        base (int): second address
        shadow_base (int): mask of shadow address that is applied to `base`

    Returns:
        The minimal non-negative offset between `address` and `base`
        calculated with and without applying the `shadow_base`.
    """

    return min(a - base for a in (address, (address | shadow_base), (address & ~shadow_base)) if a >= base)