anchor.diff:  -export([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
   
   -include("lmq.hrl").
  --record(state, {refs=gb_sets:empty(), queue=queue:new()}).
  +-record(state, {waiting=queue:new(), monitors=gb_sets:empty()}).
   
   start_link() ->
       gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  
positive.code: 
  handle_call({push, Data}, _From, S=#state{}) ->
      R = lmq_lib:enqueue(Data),
      case queue:is_empty(S#state.queue) of
          true  -> {reply, R, S};
          false -> {reply, R, S, lmq_lib:waittime()}
      end;
  handle_call(pull, From={Pid, _}, S=#state{refs=R, queue=Q}) ->
      Ref = erlang:monitor(process, Pid),
      NewState = S#state{refs=gb_sets:add(Ref, R), queue=queue:in({From, Ref}, Q)},
      {noreply, NewState, lmq_lib:waittime()};
  handle_call({complete, UUID}, _From, S=#state{}) ->
      R = lmq_lib:complete(UUID),
      {reply, R, S};
  handle_call({alive, UUID}, _From, S=#state{}) ->
      R = lmq_lib:reset_timeout(UUID),
      {reply, R, S};
  handle_call({return, UUID}, _From, S=#state{}) ->
      R = lmq_lib:return(UUID),
      {reply, R, S};
  handle_call(stop, _From, S=#state{}) ->
      {stop, normal, ok, S}.
  
  handle_cast(Msg, S=#state{}) ->
      io:format("Unknown message received: ~p~n", [Msg]),
      {noreply, S}.
  
  handle_info(timeout, S=#state{}) ->
      NewState = maybe_push_message(S),
      case queue:is_empty(NewState#state.queue) of
          true  -> {noreply, NewState};
          false -> {noreply, NewState, lmq_lib:waittime()}
      end;
  handle_info({'DOWN', Ref, process, _Pid, _}, S=#state{refs=R, queue=Q}) ->
      case gb_sets:is_member(Ref, R) of
          true ->
              erlang:demonitor(Ref, [flush]),
              NewQueue = queue:filter(
                  fun({_, V}) when V =:= Ref -> false;
                     (_) -> true
                  end, Q),
              {noreply, S#state{refs=gb_sets:delete(Ref, R), queue=NewQueue}};
          false ->
              {noreply, S}
      end;
  handle_info(Msg, S=#state{}) ->
      io:format("Unknown message received: ~p~n", [Msg]),
      {noreply, S}.
  
  code_change(_OldVsn, State, _Extra) ->
      {ok, State}.
  
negative.code: terminate(_Reason, _State) ->
      ok.
  
  maybe_push_message(S=#state{waiting=Waiting}) ->
      case queue:is_empty(Waiting) of
          true -> S;
          false ->
              case lmq_lib:dequeue() of
                  empty -> S;
                  Msg ->
                      case queue:out(Waiting) of
                          {{value, {From, Ref}}, NewWaiting} ->
                              erlang:demonitor(Ref, [flush]),
                              Monitors = gb_sets:delete(Ref, S#state.monitors),
                              gen_server:reply(From, Msg),
                              S#state{waiting=NewWaiting, monitors=Monitors};
                          {empty, Waiting} ->
                              S
                      end
              end
  