simplify.txt
This works but it's hard to follow: <paste>

Simplify it. Behaviour must be identical.

Look for:
- Nesting that could be flattened with early returns.
- Conditions that could be named instead of inlined.
- Steps that happen in a confusing order.
- Flags and parameters that change what the function fundamentally is
  — those usually want to be two functions.

Tell me first, in two lines, what specifically makes it hard to
follow. Then the rewrite. Don't make it shorter at the cost of
clarity — I'd rather have more lines that read in order.

Clever and short is not the goal. The best version is the one a tired version of you understands at a glance in six months.