Skip to content

The Decompiler

Beside the Listing, the CodeBrowser keeps a second pane that shows the current function rebuilt as C. It is the same facts at a higher level: where the Listing says call, compare, branch, the decompiler says if. Reading the two side by side — Listing for ground truth, decompiler for speed — is the basic working posture of the whole craft. Here is check_code as Ghidra 12.2's decompiler rendered it in the guide's lab (real output, kept in examples/headless-gatekeeper.txt):

bool check_code(undefined4 param_1)
{
  int iVar1;

  iVar1 = scramble(param_1);
  return iVar1 == 0x191b2;
}

Four honest observations before using it:

  • The shape is exactly right — scramble the attempt, compare with 0x191b2, answer true or false — and it took one glance instead of fifteen lines.
  • The names are placeholders. param_1 is the argument (the source called it attempt); iVar1 is a local the decompiler christened itself. Nothing in the binary remembered the real names, because the source's names were never in it. What names exist — check_code, scramble — came from the symbol table, not the decompiler.
  • The types are guesses. It decided the return is bool (fair: the function only ever produces 0 or 1) and the argument an undefined4 — a four-byte something it won't commit about. Guesses can be wrong, and chapter 7 is about correcting them by hand when it matters.
  • It can also be blunter than the source. Here is its rendering of scramble (real output, examples/headless-scramble-and-main.txt):
int scramble(uint param_1)
{
  return ((int)(param_1 ^ 0x3c5a) >> 0xd | (param_1 ^ 0x3c5a) * 8) + 0x3039;
}

The source wrote the middle step as (result << 3) | (result >> 13). The decompiler shows the left shift as * 8 and keeps the right shift as >> 0xd. Same arithmetic, plainer clothes — a reminder that this pane is a translation, not a recovery.

Solving the gate

Now the specimen pays off. The decompiled pair says, in full: the gate opens when scramble(your number) equals 0x191b2, and scramble mixes its input with 0x3c5a, shifts it, and adds 0x3039. Nothing anywhere stores the answer — but the mechanism is short enough to run backwards by brute force: try every plausible code, scramble it the same way, and keep the one that lands on 0x191b2. The guide did exactly that with a few lines of Python transcribing the decompiled formula. Across every code from 0 to 99,999, precisely one qualifies: 4213.

And the proof is not the arithmetic; it is the program itself, run:

$ ./gatekeeper 4213
The gate swings open.
$ ./gatekeeper 1234
The gate stays shut.

That closing of the loop — read, deduce, run, confirm — is the habit this guide exists to teach. Every claim the decompiler inspires should meet the same standard somewhere it can be checked.

One more function, with strings attached

main, decompiled the same way, shows the decompiler earning its keep on context: it recognised the library calls atoi and puts by name (their names ride in with the program's imports), and it even names the pointer behind the keeper's note — puts(keeper_note) — because the symbol table named that string's home. It also shows the placeholder habit at full stretch: argc and argv arrive as param_1 and param_2, and the return type is the noncommittal undefined8. The full body ships in examples/headless-scramble-and-main.txt; the shape of the program — usage message and exit 2 without exactly one argument, the open/shut verdict with it — reads off it in seconds.

Check: the decompiler has done its job when you can say, in plain words, what each of the specimen's three functions does and what evidence would prove you wrong. For this specimen the proof was one run of the program — the best kind there is.