def _unravel_loop_index(shape, uint_t='unsigned int'):
    """
    declare a multi-index array in_coord and unravel the 1D index, i into it.
    This code assumes that the array is a C-ordered array.
    """
    ndim = len(shape)
    code = [
        """
        {uint_t} in_coord[{ndim}];
        {uint_t} s, t, idx = i;""".format(uint_t=uint_t, ndim=ndim)]
    for j in range(ndim - 1, 0, -1):
        code.append("""
        s = {size};
        t = idx / s;
        in_coord[{j}] = idx - t * s;
        idx = t;""".format(j=j, size=shape[j]))
    code.append("""
        in_coord[0] = idx;""")
    return '\n'.join(code)