def unrolled(i, init_x, func, num_iter, return_last_two=False):
    """Repeatedly apply a function using a regular python loop.

    Args:
        init_x: The initial values fed to `func`.
        func (callable): The function which is repeatedly called.
        num_iter (int): The number of times to apply the function `func`.
        return_last_two (bool, optional): When `True`, return the two last
            outputs of `func`.

    Returns:
        The last output of `func`, or, if `return_last_two` is `True`, a tuple
        with the last two outputs of `func`.
    """
    x = init_x
    x_old = None
    for _ in range(num_iter):
        x, x_old = func(i, x), x
        i = i + 1
    if return_last_two:
        return i, x, x_old
    else:
        return i, x
