anchor.diff:      {Reg,reverse(Is)};
   take_last_dead_1(_) -> none.
   
  -save_recipe(Ks, Trim, Moves, {MaxCost,Acc}=Recipes) ->
  -    case recipe_cost(Ks, Moves) of
  -        Cost when Cost =< MaxCost ->
  -            %% The price is right.
  -            {MaxCost,[{Ks,Trim,Moves}|Acc]};
  -	_Cost ->
  -            %% Too expensive.
  -            Recipes
  +%% Is trimming too expensive?
  +is_too_expensive({Ks,_,Moves}, NumOrigKills, IsTooExpensive) ->
  +    NumKills = num_kills(Ks, 0),
  +    NumMoves = length(Moves),
  +    IsTooExpensive(NumKills, NumMoves, NumOrigKills).
  +
  +num_kills([{kill,_}|T], Acc) ->
  +    num_kills(T, Acc+1);
  +num_kills([_|T], Acc) ->
  +    num_kills(T, Acc);
  +num_kills([], Acc) -> Acc.
  +
  +is_too_expensive_fun(true) ->
  +    %% This call is not recursive (because it is a call to a BIF).
  +    %% Here we should avoid trimming if the trimming sequence is
  +    %% likely to be more expensive than the original sequence.
  +    fun(NumKills, NumMoves, NumOrigKills) ->
  +            Penalty =
  +                if
  +                    %% Slightly penalize the use of any `move`
  +                    %% instruction to avoid replacing two `kill`
  +                    %% instructions with a `move` and a `trim`.
  +                    NumMoves =/= 0 -> 1;
  +                    true -> 0
  +                end,
  +            1 + Penalty + NumKills + NumMoves > NumOrigKills
  +    end;
  +is_too_expensive_fun(false) ->
  +    %% This call **may** be recursive. In a recursive function that
  +    %% builds up a huge stack, having unused stack slots will be very
  +    %% expensive. Therefore, we want to be biased towards trimming.
  +    %% We will do that by not counting the `trim` instruction in
  +    %% the formula below.
  +    fun(NumKills, NumMoves, NumOrigKills) ->
  +            NumKills + NumMoves > NumOrigKills
       end.
  -
  -recipe_cost(Ks, Moves) ->
  -    %% We estimate that a {move,{y,_},{y,_}} instruction is roughly twice as
  -    %% expensive as a {kill,{y,_}} instruction. A {trim,_} instruction is
  -    %% roughly as expensive as a {kill,{y,_}} instruction.
  -
  -    recipe_cost_1(Ks, 1+2*length(Moves)).
  -
  -recipe_cost_1([{kill,_}|Ks], Cost) ->
  -    recipe_cost_1(Ks, Cost+1);
  -recipe_cost_1([_|Ks], Cost) ->
  -    recipe_cost_1(Ks, Cost);
  -recipe_cost_1([], Cost) -> Cost.
   
   %% try_remap([Recipe], [Instruction], FrameSize) ->
   %%           {[Instruction],[TrimInstruction]}.
   %%  Try to renumber Y registers in the instruction stream. The
  -%%  first rececipe that works will be used.
  +%%  first recipe that works will be used.
   %%
   %%  This function will issue a `not_possible` exception if none
   %%  of the recipes were possible to apply.
  
positive.code: trim([], _, Acc) ->
      reverse(Acc).
  
  %% trim_recipes([{kill,R}|{live,R}|{dead,R}]) -> [Recipe].
  %%      Recipe = {Kills,NumberToTrim,Moves}
  %%      Kills = [{kill,Y}]
  
negative.code: %%  Calculate how to best trim the stack and kill the correct
  %%  Y registers. Return a list of possible recipes. The best
  %%  recipe (the one that trims the most) is first in the list.
  
  trim_recipes(Layout, IsNotRecursive) ->
      Recipes = construct_recipes(Layout, 0, [], []),
      NumOrigKills = length([I || {kill,_}=I <- Layout]),
      IsTooExpensive = is_too_expensive_fun(IsNotRecursive),
      [R || R <- Recipes,
            not is_too_expensive(R, NumOrigKills, IsTooExpensive)].
  
  construct_recipes([{kill,{y,Trim0}}|Ks], Trim0, Moves, Acc) ->
      Trim = Trim0 + 1,
      Recipe = {Ks,Trim,Moves},
      construct_recipes(Ks, Trim, Moves, [Recipe|Acc]);
  construct_recipes([{dead,{y,Trim0}}|Ks], Trim0, Moves, Acc) ->
      Trim = Trim0 + 1,
      Recipe = {Ks,Trim,Moves},
      construct_recipes(Ks, Trim, Moves, [Recipe|Acc]);
  construct_recipes([{live,{y,Trim0}=Src}|Ks0], Trim0, Moves0, Acc) ->
      case take_last_dead(Ks0) of
  	none ->
              %% No more recipes are possible.
              Acc;
  	{Dst,Ks} ->
  	    Trim = Trim0 + 1,
  	    Moves = [{move,Src,Dst}|Moves0],
              Recipe = {Ks,Trim,Moves},
              construct_recipes(Ks, Trim, Moves, [Recipe|Acc])
      end;
  construct_recipes([], _, _, Acc) -> Acc.
  
  take_last_dead(L) ->
      take_last_dead_1(reverse(L)).
  