def add_or_get_merge_anchor_index(lane, pos):
    """
    Add a merge anchor at pos if needed, return the index of the merge anchor
    """
    if not hasattr(lane, "merge_anchors"):
        lane.merge_anchors = []
    for ind, e in enumerate(lane.merge_anchors):
        if (e[1] is None and pos == lane.start) or e[1] == pos:
            return ind
    lane.merge_anchors.append([lane.anchor, None if pos == lane.start else pos])
    return len(lane.merge_anchors) - 1