Rewriting is totally ok. In every possible context.
Because the opposite (blocking rewriting by some legal mechanism) would mean a past employer could slap a court order stopping a programmer from working ever again in any other company due to IP infringement since he could 'just copy his previous work and hence company ip'.