def _tabulate_dofs(ir):
    """Generate code for tabulate_dofs."""
    add = format['addition']
    iadd = format['iadd']
    multiply = format['multiply']
    assign = format['assign']
    component = format['component']
    entity_index = format['entity index']
    num_entities_format = format['num entities']
    unsigned_int = format['uint declaration']
    dofs_variable = format['argument dofs']
    if ir is None:
        return assign(component(dofs_variable, 0), 0)
    dofs_per_element, num_dofs_per_element, need_offset, fakes = ir
    code = []
    offset_name = '0'
    if need_offset:
        offset_name = 'offset'
        code.append(format['declaration'](unsigned_int, offset_name, 0))
    i = 0
    for no, num_dofs in enumerate(dofs_per_element):
        if fakes[no] and num_dofs_per_element[no] == 1:
            code.append(assign(component(dofs_variable, i), offset_name))
            if offset_name != '0':
                code.append(iadd(offset_name, 1))
            i += 1
            continue
        for dim, num in enumerate(num_dofs):
            if not num[0]:
                continue
            for k, dofs in enumerate(num):
                v = multiply([len(num[k]), component(entity_index, (dim, k))])
                for j, dof in enumerate(dofs):
                    value = add([offset_name, v, j])
                    code.append(assign(component(dofs_variable, dof + i),
                        value))
            if need_offset:
                addition = multiply([len(num[0]), component(
                    num_entities_format, dim)])
                code.append(iadd('offset', addition))
        i += num_dofs_per_element[no]
    return '\n'.join(code)
