<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def insert_tag(tag, before, root):
 """ Insert `tag` before `before` tag if present. If not, insert it into `root`. Args: tag (obj):
 HTMLElement instance. before (obj):
 HTMLElement instance. root (obj):
 HTMLElement instance. """

    if not before:
        root.childs.append(tag)
        tag.parent = root
        return

    if type(before) in [tuple, list]:
        before = first(before)

    # check that `before` is double linked
    if not hasattr(before, "parent"):
        raise ValueError("Input must be double-linked!")

    # put it before first existing identifier
    parent = before.parent
    parent.childs.insert(
        parent.childs.index(before),
        tag
    )
    tag.parent = parent