Make insert_lines promises that are generated by temlate code unique.

Otherwise, locks will kick in when a second identical line is
supposted to be generated.

Also re-apply commit eb829195c4bdd56f631884f29dce5b4a57a0658c to
fix lock-name generation.