anchor.diff:      No;
   
   failed_avp(Rec, Avps, Dict) ->
  -    [failed(Rec, [{'AVP', lists:reverse(Avps)}], Dict)].
  +    [failed(Rec, [{'AVP', Avps}], Dict)].
   
   %% Reply as name and tuple list ...
   failed([MsgName | Values], FailedAvp, Dict) ->
  
positive.code:     reply(Msg, Dict, TPid, Dict0, Fs, ReqPkt#diameter_packet{errors = []});
  
  reply(Msg, Dict, TPid, Dict0, Fs, ReqPkt) ->
      Pkt = encode(Dict, reset(make_answer_packet(Msg, ReqPkt), Dict), Fs),
      incr(send, Pkt, Dict, TPid, Dict0),  %% count outgoing result codes
      send(TPid, Pkt).
  
  %% reset/2
  
  %% Header/avps list: send as is.
  reset(#diameter_packet{msg = [#diameter_header{} | _]} = Pkt, _) ->
      Pkt;
  
  %% No errors to set or errors explicitly ignored.
  reset(#diameter_packet{errors = Es} = Pkt, _)
    when Es == [];
         Es == false ->
      Pkt;
  
  %% Otherwise possibly set Result-Code and/or Failed-AVP.
  reset(#diameter_packet{msg = Msg, errors = Es} = Pkt, Dict) ->
      Pkt#diameter_packet{msg = reset(Msg, Dict, Es)}.
  
  %% reset/3
  
  reset(Msg, Dict, Es)
    when is_list(Es) ->
      {E3, E5, Failed} = partition(Es),
      FailedAVP = failed_avp(Msg, Failed, Dict),
      reset(set(Msg, FailedAVP, Dict),
            Dict,
            choose(is_answer_message(Msg, Dict), E3, E5));
  
  reset(Msg, Dict, N)
    when is_integer(N) ->
      ResultCode = rc(Msg, {'Result-Code', N}, Dict),
      set(Msg, ResultCode, Dict);
  
  reset(Msg, _, _) ->
      Msg.
  
  partition(Es) ->
      lists:foldl(fun pacc/2, {false, false, []}, Es).
  
  %% Note that the errors list can contain not only integer() and
  %% {integer(), #diameter_avp{}} but also #diameter_avp{}. The latter
  %% isn't something that's returned by decode but can be set in a reply
  %% for encode.
  
  pacc({RC, #diameter_avp{} = A}, {E3, E5, Acc})
    when is_integer(RC) ->
      pacc(RC, {E3, E5, [A|Acc]});
  
  pacc(#diameter_avp{} = A, {E3, E5, Acc}) ->
      {E3, E5, [A|Acc]};
  
  pacc(RC, {false, E5, Acc})
    when 3 == RC div 1000 ->
      {RC, E5, Acc};
  
  pacc(RC, {E3, false, Acc})
    when 5 == RC div 1000 ->
      {E3, RC, Acc};
  
  pacc(_, Acc) ->
      Acc.
  
  eval_packet(Pkt, Fs) ->
      lists:foreach(fun(F) -> diameter_lib:eval([F,Pkt]) end, Fs).
  
negative.code: %% the arity is 1 or {0,1}. In other cases (which probably shouldn't
  %% exist in practise) we can't know what's appropriate.
  
  rc(_, B, _)
    when is_boolean(B) ->
      [];
  
  rc([MsgName | _], RC, Dict) ->
      K = 'Result-Code',
      case Dict:avp_arity(MsgName, K) of
          1     -> [{K, RC}];
          {0,1} -> [{K, [RC]}];
          _     -> []
      end;
  
  rc(Rec, RC, Dict) ->
      rc([Dict:rec2msg(element(1, Rec))], RC, Dict).
  
  %% failed_avp/3
  
  