LLM-Assisted Reverse Engineering and Rewriting of Legacy Systems: A Practical Workflow
This article explains how large language models can be combined with conventional reverse-engineering tools to understand binaries and rewrite legacy systems whose source code has been lost. It covers asset inventory, decompilation and information extraction, chunked prompting, behavior alignment and regression testing, engineering practices, and the compliance boundaries that must be addressed before any work begins.
In recent years, large language models (LLMs) have made rapid progress in code understanding and generation. “Asking AI to rewrite an old system whose source code is lost” has moved from a joke to a realistic engineering path. Many enterprises and teams hold legacy assets of this kind: early client software, internal business systems, discontinued games or tools. The source code is gone, and all that remains is a binary that still runs. This article examines, from an engineering perspective, the technical route, key stages, and compliance boundaries of using LLMs to assist reverse analysis and rewrite legacy systems.
Background: What Losing Source Code Means
A software system without source code usually faces three problems:
- It cannot be maintained. Any functional change is difficult to start, even changing a single line of text.
- It cannot be migrated. Old programs are often tied to old operating systems and old runtime libraries, and may not even start in a new environment.
- It cannot be audited. Security issues, license compliance, hidden backdoors, and similar concerns cannot be verified.
Traditional reverse engineering relies heavily on expert experience: reading assembly, recovering data structures, and manually reconstructing control flow. A medium-sized client program may require person-years of work. The emergence of LLMs significantly lowers the barrier in the “understanding and translation” part of this work.
Where LLMs Actually Fit in the Reverse-Engineering Chain
One point must be clear first: an LLM cannot “one-click decompile” a binary. It is not good at parsing at the binary level. What it is good at is understanding and rewriting “semi-structured intermediate artifacts.” A reasonable division of labor is therefore:
- Traditional toolchains handle mechanical work: disassembly, decompilation, extraction of strings and resources, identification of function signatures and dependency libraries.
- LLMs handle cognitive work: explaining decompiled pseudo-code, recovering variable names and types, summarizing module responsibilities, and ultimately rewriting readable, compilable new code.
In one sentence: tools produce intermediate results that are “visible but hard to understand,” and the model completes the transition from “hard to understand” to “maintainable.”
End-to-End Workflow
1. Preparation and Asset Inventory
- Confirm legal and authorization boundaries. This is the first and most important step; see the compliance section below.
- Collect the program itself, accompanying resource files, old documentation, and community materials.
- Set up an isolated analysis environment, such as a virtual machine, to avoid the risks of executing unknown binaries.
2. Decompilation and Information Extraction
Use standard reverse-engineering tools to:
- Extract import and export tables and map the external capabilities the program depends on.
- Decompile to obtain function-level pseudo-code.
- Extract string constants, configuration key names, and UI text. These are important clues for understanding business logic.
- Recover resource files such as images, audio, scripts, and data tables. These can often be reused as-is.
3. Chunked Input and Logic Reconstruction
Decompiled output can easily reach hundreds of thousands of lines. Sending it directly to a model is neither practical nor necessary. The correct approach is divide and conquer:
- Split the output into logical units by function or call-graph clustering.
- First ask the model to produce a “responsibility summary” for each unit and output a module specification.
- Then, based on the specification, ask the model to rewrite the unit in a modern language.
A practical prompt structure can be:
The following is decompiled pseudo-code for a function. Please:1. Infer the function's purpose and the semantics of its inputs and outputs.2. Give variables and functions readable names and annotate their types.3. Rewrite it in the target language while preserving the original behavior.4. List any assumptions you are unsure about.
The fourth point, the “assumption list,” is especially important. It makes the model’s uncertainty explicit and provides checkpoints for later human verification.
4. Behavior Alignment and Verification Loop
Correctness of a rewrite cannot rely on the model’s self-assessment. A “dual-run comparison” mechanism must be established:
- Design the same set of input cases for the old program and the new implementation.
- Compare outputs: numeric values, state, files, and screen output.
- Trace differences back to the corresponding module and have a human confirm whether the difference is hidden behavior in the old program or a deviation in the new implementation.
- Freeze every confirmed behavior into a regression test.
This loop of “rewrite—compare—correct—freeze” is the quality guarantee for the whole project. It is also the essential difference between this approach and “asking AI to generate some code that happens to run.”
Engineering Practices
- Version control and traceability: Manage decompiled intermediate artifacts, module specifications, and rewritten code in separate repositories so the process remains traceable.
- Progressive replacement: Rewrite peripheral modules first, such as configuration parsing and resource loading. Leave core logic for last, when understanding of the system is most complete.
- Clear human-machine division: The model handles translation and first drafts. Architecture decisions, difficult assembly analysis, and behavior adjudication must be reviewed by humans.
- Use elastic cloud compute: Batch analysis tasks can run on cloud container platforms, with object storage for intermediate artifacts and logging services for analysis trails, so local resources do not become a bottleneck.
Non-Negotiable Compliance Boundaries
Technical feasibility does not equal legal permission. Before starting, three questions must be answered:
- Copyright: Software code is protected by copyright. Is reverse engineering permitted under an exception in local law, such as interoperability research or personal study?
- License agreement: Does the end-user agreement prohibit reverse engineering?
- Purpose of use: Is the rewritten result for internal compatibility research or for external distribution? The latter carries significantly higher risk.
For scenarios with a legitimate need, such as lost source code for self-developed software or authorization from the rights holder, keep complete ownership proof and authorization documents, and store rewritten artifacts separately from the original binary. Responsible AI tools often refuse clear infringement requests. This is not excessive moralizing; it is reasonable risk control.
Conclusion
“Using LLMs to rewrite legacy systems” is a real and usable technical path, but it is not magic. Toolchains perform mechanical parsing, models perform understanding and translation, and humans perform verification and decision-making. All three are indispensable. At the same time, reverse rewriting must be built on a clear authorization and compliance foundation. For teams troubled by legacy systems, this methodology offers more than a code migration. It is an opportunity to turn “unmaintainable black-box assets” back into “evolvable engineering assets”—provided the team takes the right technical path and the right legal path.
