import re


def pow2mult(instr):
    """Power to multiplications

    Substitutes x**5 or pow(x, 5) by x*x*x*x*x

    Parameters
    ----------
    instr : str
        The input string

    Returns
    -------
    outstr : str
        The output string

    """
    old_new = []
    for p in re.findall(r'\w+\*\*\d+', instr):
        var, exp = p.split('**')
        new = '(' + '*'.join([var]*int(exp)) + ')'
        old_new.append([p, new])
    # putting longer patterns first to avoid wrong
    # substitutions
    old_new = sorted(old_new, key=lambda x: len(x[0]))[::-1]
    outstr = instr
    for old, new in old_new:
        outstr = outstr.replace(old, new)

    old_new = []
    for p in re.findall(r'pow\(\w+,\s*\w+\)', instr):
        var, exp = p.split('pow')[1].split('(')[1].split(')')[0].split(',')
        new = '(' + '*'.join([var]*int(exp)) + ')'
        old_new.append([p, new])
    for old, new in old_new:
        outstr = outstr.replace(old, new)

    return outstr