def add_pen_up_moves(segments):
    """
    Takes a list of pen-down segments. Returns a list of (segment, pen_up)
    tuples, with additional segments added that pen-up move between segments.
    Also add segments at the beginning and end to do pen-up moves from and to
    the origin location.

    The output list should thus always have 2n+1 elements, where n is the
    length of the input list.
    """
    assert segments
    origin = 0, 0
    out_segments = []
    start_seg = [origin, segments[0][0]]
    out_segments.append((start_seg, True))
    count = len(segments)
    for n, seg in enumerate(segments, start=1):
        assert seg
        out_segments.append((seg, False))
        if n == count:
            next_seg_start = origin
        else:
            next_seg_start = segments[n][0]
        inter_seg = [seg[-1], next_seg_start]
        out_segments.append((inter_seg, True))
    return out_segments
