Enron is another fascinating example, there is an interview with the former CFO where he talks about what he calls "legal fraud" - practices that are highly dubious but technically not illegal [1].
Legal/illegal is only a binary variable in theory. In practice it's things that are clearly white, clearly black and lots of grey in between. There are many concrete examples regarding accounting rules mentioned in the interview.
The question is where does a court draw the line, and as parent rightly points out, sometimes code/case law changes after the fact.