# -*- generated by 1.0.2 -*-
import da
PatternExpr_231 = da.pat.TuplePattern([da.pat.ConstantPattern('request'), da.pat.FreePattern('p')])
PatternExpr_252 = da.pat.TuplePattern([da.pat.ConstantPattern('propose'), da.pat.FreePattern('s'), da.pat.BoundPattern('_BoundPattern257_')])
PatternExpr_275 = da.pat.TuplePattern([da.pat.ConstantPattern('decision'), da.pat.BoundPattern('_BoundPattern278_'), da.pat.FreePattern('p2')])
PatternExpr_312 = da.pat.TuplePattern([da.pat.ConstantPattern('propose'), da.pat.FreePattern('s'), da.pat.FreePattern(None)])
PatternExpr_335 = da.pat.TuplePattern([da.pat.ConstantPattern('decision'), da.pat.FreePattern('s'), da.pat.FreePattern(None)])
PatternExpr_382 = da.pat.TuplePattern([da.pat.ConstantPattern('propose'), da.pat.BoundPattern('_BoundPattern385_'), da.pat.FreePattern(None)])
PatternExpr_405 = da.pat.TuplePattern([da.pat.ConstantPattern('decision'), da.pat.BoundPattern('_BoundPattern408_'), da.pat.FreePattern(None)])
PatternExpr_442 = da.pat.TuplePattern([da.pat.ConstantPattern('decision'), da.pat.BoundPattern('_BoundPattern445_'), da.pat.FreePattern('p')])
PatternExpr_473 = da.pat.TuplePattern([da.pat.ConstantPattern('decision'), da.pat.FreePattern('s'), da.pat.BoundPattern('_BoundPattern477_')])
PatternExpr_537 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_542 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.FreePattern(None)]), da.pat.TuplePattern([da.pat.ConstantPattern('done')])])
PatternExpr_597 = da.pat.TuplePattern([da.pat.ConstantPattern('1b'), da.pat.BoundPattern('_BoundPattern600_'), da.pat.FreePattern(None)])
PatternExpr_604 = da.pat.FreePattern('a')
PatternExpr_640 = da.pat.TuplePattern([da.pat.ConstantPattern('1b'), da.pat.BoundPattern('_BoundPattern643_'), da.pat.FreePattern('accepted')])
PatternExpr_679 = da.pat.TuplePattern([da.pat.ConstantPattern('propose'), da.pat.FreePattern('s'), da.pat.FreePattern('p')])
PatternExpr_702 = da.pat.TuplePattern([da.pat.ConstantPattern('2a'), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern706_'), da.pat.FreePattern(None)])
PatternExpr_738 = da.pat.TuplePattern([da.pat.ConstantPattern('2b'), da.pat.BoundPattern('_BoundPattern741_'), da.pat.FreePattern('s'), da.pat.FreePattern('p')])
PatternExpr_765 = da.pat.TuplePattern([da.pat.ConstantPattern('2b'), da.pat.BoundPattern('_BoundPattern768_'), da.pat.BoundPattern('_BoundPattern769_'), da.pat.BoundPattern('_BoundPattern770_')])
PatternExpr_773 = da.pat.FreePattern('a')
PatternExpr_798 = da.pat.TuplePattern([da.pat.ConstantPattern('decision'), da.pat.BoundPattern('_BoundPattern801_'), da.pat.BoundPattern('_BoundPattern802_')])
PatternExpr_830 = da.pat.TuplePattern([da.pat.ConstantPattern('1b'), da.pat.FreePattern('b'), da.pat.FreePattern(None)])
PatternExpr_856 = da.pat.TuplePattern([da.pat.ConstantPattern('2b'), da.pat.FreePattern('b'), da.pat.FreePattern(None), da.pat.FreePattern(None)])
PatternExpr_888 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_908 = da.pat.TuplePattern([da.pat.ConstantPattern('1b'), da.pat.FreePattern('b'), da.pat.FreePattern(None)])
PatternExpr_933 = da.pat.TuplePattern([da.pat.ConstantPattern('2b'), da.pat.FreePattern('b'), da.pat.FreePattern(None), da.pat.FreePattern(None)])
PatternExpr_964 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_805 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.FreePattern(None)]), da.pat.TuplePattern([da.pat.ConstantPattern('decision'), da.pat.BoundPattern('_BoundPattern815_'), da.pat.BoundPattern('_BoundPattern816_')])])
PatternExpr_893 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.FreePattern(None)]), da.pat.TuplePattern([da.pat.ConstantPattern('done')])])
PatternExpr_969 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.FreePattern(None)]), da.pat.TuplePattern([da.pat.ConstantPattern('done')])])
PatternExpr_1038 = da.pat.TuplePattern([da.pat.ConstantPattern('1a'), da.pat.FreePattern(None)])
PatternExpr_1044 = da.pat.FreePattern('leader')
PatternExpr_1053 = da.pat.TuplePattern([da.pat.ConstantPattern('1a'), da.pat.FreePattern('b')])
PatternExpr_1075 = da.pat.TuplePattern([da.pat.ConstantPattern('2a'), da.pat.FreePattern('b'), da.pat.FreePattern(None), da.pat.FreePattern(None)])
PatternExpr_1120 = da.pat.TuplePattern([da.pat.ConstantPattern('2a'), da.pat.FreePattern('b'), da.pat.FreePattern('s'), da.pat.FreePattern('p')])
PatternExpr_1161 = da.pat.TuplePattern([da.pat.ConstantPattern('2a'), da.pat.FreePattern('b'), da.pat.FreePattern('s'), da.pat.FreePattern('p')])
PatternExpr_1172 = da.pat.FreePattern('leader')
PatternExpr_1181 = da.pat.TuplePattern([da.pat.ConstantPattern('1a'), da.pat.FreePattern('b')])
PatternExpr_1202 = da.pat.TuplePattern([da.pat.ConstantPattern('2a'), da.pat.FreePattern('b'), da.pat.FreePattern(None), da.pat.FreePattern(None)])
PatternExpr_1275 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_1280 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.FreePattern(None)]), da.pat.TuplePattern([da.pat.ConstantPattern('done')])])
PatternExpr_1383 = da.pat.TuplePattern([da.pat.ConstantPattern('response'), da.pat.FreePattern('cid'), da.pat.FreePattern('result')])
PatternExpr_1587 = da.pat.TuplePattern([da.pat.ConstantPattern('done')])
PatternExpr_1592 = da.pat.BoundPattern('_BoundPattern1593_')
PatternExpr_1594 = da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.TuplePattern([da.pat.FreePattern(None), da.pat.FreePattern(None), da.pat.BoundPattern('_BoundPattern1600_')]), da.pat.TuplePattern([da.pat.ConstantPattern('done')])])
_config_object = {}
import sys
import time
import random
NOPS = 10

def operation(i):
    return (lambda state: ((state + [i]), ['result', i, 'on', state]))
operations = {i: operation(i) for i in range(NOPS)}

class Replica(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._ReplicaReceivedEvent_0 = []
        self._ReplicaSentEvent_1 = []
        self._ReplicaReceivedEvent_2 = []
        self._ReplicaSentEvent_3 = []
        self._ReplicaReceivedEvent_4 = []
        self._ReplicaSentEvent_5 = []
        self._ReplicaReceivedEvent_6 = []
        self._ReplicaReceivedEvent_7 = []
        self._ReplicaReceivedEvent_8 = []
        self._ReplicaReceivedEvent_9 = []
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_ReplicaReceivedEvent_0', PatternExpr_231, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.SentEvent, '_ReplicaSentEvent_1', PatternExpr_252, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_ReplicaReceivedEvent_2', PatternExpr_275, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.SentEvent, '_ReplicaSentEvent_3', PatternExpr_312, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_ReplicaReceivedEvent_4', PatternExpr_335, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.SentEvent, '_ReplicaSentEvent_5', PatternExpr_382, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_ReplicaReceivedEvent_6', PatternExpr_405, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_ReplicaReceivedEvent_7', PatternExpr_442, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_ReplicaReceivedEvent_8', PatternExpr_473, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_ReplicaReceivedEvent_9', PatternExpr_537, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

    def setup(self, leaders, initial_state, **rest_1614):
        super().setup(leaders=leaders, initial_state=initial_state, **rest_1614)
        self._state.leaders = leaders
        self._state.initial_state = initial_state
        self._state.state = self._state.initial_state
        self._state.slot_num = 1

    def run(self):
        self.serve()

    def serve(self):
        while True:
            self.debug('### replica')
            super()._label('_st_label_228', block=False)
            p2 = s = p = None

            def ExistentialOpExpr_229():
                nonlocal p2, s, p
                for (_, _, (_ConstantPattern246_, p)) in self._ReplicaReceivedEvent_0:
                    if (_ConstantPattern246_ == 'request'):

                        def UniversalOpExpr_250(p):
                            nonlocal p2, s
                            for (_, _, (_ConstantPattern268_, s, _BoundPattern271_)) in self._ReplicaSentEvent_1:
                                if (_ConstantPattern268_ == 'propose'):
                                    if (_BoundPattern271_ == p):

                                        def ExistentialOpExpr_273(s, p):
                                            nonlocal p2
                                            for (_, _, (_ConstantPattern291_, _BoundPattern293_, p2)) in self._ReplicaReceivedEvent_2:
                                                if (_ConstantPattern291_ == 'decision'):
                                                    if (_BoundPattern293_ == s):
                                                        if (not (p2 == p)):
                                                            return True
                                            return False
                                        if (not ExistentialOpExpr_273(s=s, p=p)):
                                            return False
                            return True
                        if UniversalOpExpr_250(p=p):
                            return True
                return False
            p = None

            def ExistentialOpExpr_440():
                nonlocal p
                for (_, _, (_ConstantPattern457_, _BoundPattern459_, p)) in self._ReplicaReceivedEvent_7:
                    if (_ConstantPattern457_ == 'decision'):
                        if (_BoundPattern459_ == self._state.slot_num):
                            if True:
                                return True
                return False
            _st_label_228 = 0
            while (_st_label_228 == 0):
                _st_label_228 += 1
                if ExistentialOpExpr_229():
                    self.debug('### replica 1', 'p', p)
                    used_s = max((({s for (_, _, (_ConstantPattern327_, s, _)) in self._ReplicaSentEvent_3 if (_ConstantPattern327_ == 'propose')} | {s for (_, _, (_ConstantPattern350_, s, _)) in self._ReplicaReceivedEvent_4 if (_ConstantPattern350_ == 'decision')}) or {0}))
                    self.debug('used_s', used_s)

                    def ExistentialOpExpr_380(s):
                        for (_, _, (_ConstantPattern397_, _BoundPattern399_, _)) in self._ReplicaSentEvent_5:
                            if (_ConstantPattern397_ == 'propose'):
                                if (_BoundPattern399_ == s):
                                    if True:
                                        return True
                        return False

                    def ExistentialOpExpr_403(s):
                        for (_, _, (_ConstantPattern420_, _BoundPattern422_, _)) in self._ReplicaReceivedEvent_6:
                            if (_ConstantPattern420_ == 'decision'):
                                if (_BoundPattern422_ == s):
                                    if True:
                                        return True
                        return False
                    s = min({s for s in range(1, ((used_s + 1) + 1)) if (not (ExistentialOpExpr_380(s=s) or ExistentialOpExpr_403(s=s)))})
                    self.debug('s', s, 'p', p)
                    self.send(('propose', s, p), self._state.leaders)
                    _st_label_228 += 1
                elif ExistentialOpExpr_440():
                    self.debug('### replica 2', 'p', p)
                    s = None

                    def ExistentialOpExpr_471():
                        nonlocal s
                        for (_, _, (_ConstantPattern488_, s, _BoundPattern491_)) in self._ReplicaReceivedEvent_8:
                            if (_ConstantPattern488_ == 'decision'):
                                if (_BoundPattern491_ == p):
                                    if (s < self._state.slot_num):
                                        return True
                        return False
                    if (not ExistentialOpExpr_471()):
                        (client, cmd_id, op) = p
                        (self._state.state, result) = operations[op](self._state.state)
                        self.debug('### replica 2.1', 'state', self._state.state, 'result', result)
                        self.send(('response', cmd_id, result), client)
                    self._state.slot_num += 1
                    self.debug('slot_num', self._state.slot_num)
                    _st_label_228 += 1
                elif PatternExpr_542.match_iter(self._ReplicaReceivedEvent_9, SELF_ID=self._id):
                    self.output('terminating')
                    break
                    _st_label_228 += 1
                else:
                    super()._label('_st_label_228', block=True)
                    _st_label_228 -= 1
            else:
                if (_st_label_228 != 2):
                    continue
            if (_st_label_228 != 2):
                break

class Leader(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._LeaderReceivedEvent_0 = []
        self._LeaderReceivedEvent_1 = []
        self._LeaderReceivedEvent_2 = []
        self._LeaderSentEvent_3 = []
        self._LeaderReceivedEvent_4 = []
        self._LeaderReceivedEvent_5 = []
        self._LeaderSentEvent_6 = []
        self._LeaderReceivedEvent_7 = []
        self._LeaderReceivedEvent_8 = []
        self._LeaderReceivedEvent_9 = []
        self._LeaderReceivedEvent_10 = []
        self._LeaderReceivedEvent_11 = []
        self._LeaderReceivedEvent_12 = []
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_0', PatternExpr_597, sources=[PatternExpr_604], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_1', PatternExpr_640, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_2', PatternExpr_679, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.SentEvent, '_LeaderSentEvent_3', PatternExpr_702, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_4', PatternExpr_738, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_5', PatternExpr_765, sources=[PatternExpr_773], destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.SentEvent, '_LeaderSentEvent_6', PatternExpr_798, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_7', PatternExpr_830, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_8', PatternExpr_856, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_9', PatternExpr_888, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_10', PatternExpr_908, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_11', PatternExpr_933, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_LeaderReceivedEvent_12', PatternExpr_964, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

    def setup(self, acceptors, replicas, **rest_1614):
        super().setup(acceptors=acceptors, replicas=replicas, **rest_1614)
        self._state.acceptors = acceptors
        self._state.replicas = replicas
        self._state.ballot = (0, self._id)

    def run(self):
        self.lead()

    def lead(self):
        while True:
            self.debug('### leader')
            self.send(('1a', self._state.ballot), self._state.acceptors)
            super()._label('_st_label_592', block=False)
            b = None

            def ExistentialOpExpr_906():
                nonlocal b
                for (_, _, (_ConstantPattern923_, b, _)) in self._LeaderReceivedEvent_10:
                    if (_ConstantPattern923_ == '1b'):
                        if (b > self._state.ballot):
                            return True
                return False
            b = None

            def ExistentialOpExpr_931():
                nonlocal b
                for (_, _, (_ConstantPattern949_, b, _, _)) in self._LeaderReceivedEvent_11:
                    if (_ConstantPattern949_ == '2b'):
                        if (b > self._state.ballot):
                            return True
                return False
            _st_label_592 = 0
            while (_st_label_592 == 0):
                _st_label_592 += 1
                if (len({a for (_, (_, _, a), (_ConstantPattern615_, _BoundPattern617_, _)) in self._LeaderReceivedEvent_0 if (_ConstantPattern615_ == '1b') if (_BoundPattern617_ == self._state.ballot)}) > (len(self._state.acceptors) / 2)):
                    self.debug('### leader 1')
                    for (s, p) in self.pmax({t for (_, _, (_ConstantPattern656_, _BoundPattern658_, accepted)) in self._LeaderReceivedEvent_1 if (_ConstantPattern656_ == '1b') if (_BoundPattern658_ == self._state.ballot) for t in accepted}):
                        self.send(('2a', self._state.ballot, s, p), self._state.acceptors)
                    while True:
                        super()._label('_st_label_676', block=False)
                        p = s = None

                        def ExistentialOpExpr_677():
                            nonlocal p, s
                            for (_, _, (_ConstantPattern694_, s, p)) in self._LeaderReceivedEvent_2:
                                if (_ConstantPattern694_ == 'propose'):

                                    def ExistentialOpExpr_700(s):
                                        for (_, _, (_ConstantPattern718_, _, _BoundPattern721_, _)) in self._LeaderSentEvent_3:
                                            if (_ConstantPattern718_ == '2a'):
                                                if (_BoundPattern721_ == s):
                                                    if True:
                                                        return True
                                        return False
                                    if (not ExistentialOpExpr_700(s=s)):
                                        return True
                            return False
                        p = s = a = None

                        def ExistentialOpExpr_736():
                            nonlocal p, s, a
                            for (_, _, (_ConstantPattern754_, _BoundPattern756_, s, p)) in self._LeaderReceivedEvent_4:
                                if (_ConstantPattern754_ == '2b'):
                                    if (_BoundPattern756_ == self._state.ballot):
                                        if ((len({a for (_, (_, _, a), (_ConstantPattern784_, _BoundPattern786_, _BoundPattern787_, _BoundPattern788_)) in self._LeaderReceivedEvent_5 if (_ConstantPattern784_ == '2b') if (_BoundPattern786_ == self._state.ballot) if (_BoundPattern787_ == s) if (_BoundPattern788_ == p)}) > (len(self._state.acceptors) / 2)) and (not PatternExpr_805.match_iter(self._LeaderSentEvent_6, _BoundPattern815_=s, _BoundPattern816_=p, SELF_ID=self._id))):
                                            return True
                            return False
                        b = None

                        def ExistentialOpExpr_828():
                            nonlocal b
                            for (_, _, (_ConstantPattern846_, b, _)) in self._LeaderReceivedEvent_7:
                                if (_ConstantPattern846_ == '1b'):
                                    if (b > self._state.ballot):
                                        return True
                            return False
                        b = None

                        def ExistentialOpExpr_854():
                            nonlocal b
                            for (_, _, (_ConstantPattern872_, b, _, _)) in self._LeaderReceivedEvent_8:
                                if (_ConstantPattern872_ == '2b'):
                                    if (b > self._state.ballot):
                                        return True
                            return False
                        _st_label_676 = 0
                        while (_st_label_676 == 0):
                            _st_label_676 += 1
                            if ExistentialOpExpr_677():
                                self.debug('### leader 1.1')
                                self.send(('2a', self._state.ballot, s, p), self._state.acceptors)
                                _st_label_676 += 1
                            elif ExistentialOpExpr_736():
                                self.debug('### leader 1.2')
                                self.send(('decision', s, p), self._state.replicas)
                                _st_label_676 += 1
                            elif (ExistentialOpExpr_828() or ExistentialOpExpr_854()):
                                self.debug('### leader 1.3')
                                break
                                _st_label_676 += 1
                            elif PatternExpr_893.match_iter(self._LeaderReceivedEvent_9, SELF_ID=self._id):
                                break
                                _st_label_676 += 1
                            else:
                                super()._label('_st_label_676', block=True)
                                _st_label_676 -= 1
                        else:
                            if (_st_label_676 != 2):
                                continue
                        if (_st_label_676 != 2):
                            break
                    _st_label_592 += 1
                elif (ExistentialOpExpr_906() or ExistentialOpExpr_931()):
                    self.output('### leader 2')
                    pass
                    _st_label_592 += 1
                elif PatternExpr_969.match_iter(self._LeaderReceivedEvent_12, SELF_ID=self._id):
                    self.output('terminating')
                    break
                    _st_label_592 += 1
                else:
                    super()._label('_st_label_592', block=True)
                    _st_label_592 -= 1
            else:
                if (_st_label_592 != 2):
                    continue
            if (_st_label_592 != 2):
                break
            self._state.ballot = ((self._state.ballot[0] + 1), self._id)

    def pmax(self, pvals):

        def UniversalOpExpr_1007(s, b):
            for (b2, _BoundPattern1012_, _) in pvals:
                if (_BoundPattern1012_ == s):
                    if (not (b2 <= b)):
                        return False
            return True
        return {(s, p) for (b, s, p) in pvals if UniversalOpExpr_1007(s=s, b=b)}

class Acceptor(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._AcceptorReceivedEvent_1 = []
        self._AcceptorReceivedEvent_2 = []
        self._AcceptorReceivedEvent_3 = []
        self._AcceptorReceivedEvent_5 = []
        self._AcceptorReceivedEvent_6 = []
        self._AcceptorReceivedEvent_7 = []
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_AcceptorReceivedEvent_0', PatternExpr_1038, sources=[PatternExpr_1044], destinations=None, timestamps=None, record_history=None, handlers=[self._Acceptor_handler_1037]), da.pat.EventPattern(da.pat.ReceivedEvent, '_AcceptorReceivedEvent_1', PatternExpr_1053, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_AcceptorReceivedEvent_2', PatternExpr_1075, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_AcceptorReceivedEvent_3', PatternExpr_1120, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_AcceptorReceivedEvent_4', PatternExpr_1161, sources=[PatternExpr_1172], destinations=None, timestamps=None, record_history=None, handlers=[self._Acceptor_handler_1160]), da.pat.EventPattern(da.pat.ReceivedEvent, '_AcceptorReceivedEvent_5', PatternExpr_1181, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_AcceptorReceivedEvent_6', PatternExpr_1202, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[]), da.pat.EventPattern(da.pat.ReceivedEvent, '_AcceptorReceivedEvent_7', PatternExpr_1275, sources=None, destinations=None, timestamps=None, record_history=True, handlers=[])])

    def setup(self, **rest_1614):
        super().setup(**rest_1614)
        pass

    def run(self):
        super()._label('_st_label_1272', block=False)
        _st_label_1272 = 0
        while (_st_label_1272 == 0):
            _st_label_1272 += 1
            if PatternExpr_1280.match_iter(self._AcceptorReceivedEvent_7, SELF_ID=self._id):
                _st_label_1272 += 1
            else:
                super()._label('_st_label_1272', block=True)
                _st_label_1272 -= 1
        self.output('terminating')

    def pmax(self, pvals):

        def UniversalOpExpr_1255(s, b):
            for (b2, _BoundPattern1260_, _) in pvals:
                if (_BoundPattern1260_ == s):
                    if (not (b2 <= b)):
                        return False
            return True
        return {(s, p) for (b, s, p) in pvals if UniversalOpExpr_1255(s=s, b=b)}

    def _Acceptor_handler_1037(self, leader):
        ballot = max((({b for (_, _, (_ConstantPattern1068_, b)) in self._AcceptorReceivedEvent_1 if (_ConstantPattern1068_ == '1a')} | {b for (_, _, (_ConstantPattern1092_, b, _, _)) in self._AcceptorReceivedEvent_2 if (_ConstantPattern1092_ == '2a')}) or {((- 1), (- 1))}))
        accepted = {(ballot, s, p) for (s, p) in self.pmax({(b, s, p) for (_, _, (_ConstantPattern1137_, b, s, p)) in self._AcceptorReceivedEvent_3 if (_ConstantPattern1137_ == '2a')})}
        self.send(('1b', ballot, accepted), leader)
    _Acceptor_handler_1037._labels = None
    _Acceptor_handler_1037._notlabels = None

    def _Acceptor_handler_1160(self, b, s, p, leader):
        ballot = max((({b for (_, _, (_ConstantPattern1195_, b)) in self._AcceptorReceivedEvent_5 if (_ConstantPattern1195_ == '1a')} | {b for (_, _, (_ConstantPattern1218_, b, _, _)) in self._AcceptorReceivedEvent_6 if (_ConstantPattern1218_ == '2a')}) or {((- 1), (- 1))}))
        self.send(('2b', ballot, s, p), leader)
    _Acceptor_handler_1160._labels = None
    _Acceptor_handler_1160._notlabels = None

class Client(da.DistProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_ClientReceivedEvent_0', PatternExpr_1383, sources=None, destinations=None, timestamps=None, record_history=None, handlers=[self._Client_handler_1382])])

    def setup(self, replicas, nops, **rest_1614):
        super().setup(replicas=replicas, nops=nops, **rest_1614)
        self._state.replicas = replicas
        self._state.nops = nops
        self._state.cid = 0
        self._state.results = dict()
        self._state.count = dict()

    def run(self):
        for i in range(self._state.nops):
            self.send(('request', (self._id, self._state.cid, random.randint(0, (NOPS - 1)))), self._state.replicas)
            super()._label('_st_label_1345', block=False)
            _st_label_1345 = 0
            while (_st_label_1345 == 0):
                _st_label_1345 += 1
                if (self._state.cid in self._state.results):
                    _st_label_1345 += 1
                else:
                    super()._label('_st_label_1345', block=True)
                    _st_label_1345 -= 1
            else:
                if (_st_label_1345 != 2):
                    continue
            if (_st_label_1345 != 2):
                break
            self.output('received result', self._state.cid, self._state.results[self._state.cid])
            self._state.cid += 1
        super()._label('_st_label_1359', block=False)
        cid = None

        def UniversalOpExpr_1360():
            nonlocal cid
            for self._state.cid in range(self._state.nops):
                if (not (self._state.count[self._state.cid] == len(self._state.replicas))):
                    return False
            return True
        _st_label_1359 = 0
        while (_st_label_1359 == 0):
            _st_label_1359 += 1
            if UniversalOpExpr_1360():
                _st_label_1359 += 1
            else:
                super()._label('_st_label_1359', block=True)
                _st_label_1359 -= 1
        self.output('terminating')
        self.send(('done',), self.nodeof(self._id))

    def _Client_handler_1382(self, cid, result):
        self.debug('### response', cid, result)
        if (not (cid in self._state.results)):
            self._state.results[cid] = result
        elif (not (self._state.results[cid] == result)):
            self.output('different result', cid, result, 'than', self._state.results[cid])
            raise RuntimeError('Desynced!!!!')
        self._state.count[cid] = (1 if (not (cid in self._state.count)) else (self._state.count[cid] + 1))
    _Client_handler_1382._labels = None
    _Client_handler_1382._notlabels = None

class Node_(da.NodeProcess):

    def __init__(self, procimpl, props):
        super().__init__(procimpl, props)
        self._Node_ReceivedEvent_0 = []
        self._events.extend([da.pat.EventPattern(da.pat.ReceivedEvent, '_Node_ReceivedEvent_0', PatternExpr_1587, sources=[PatternExpr_1592], destinations=None, timestamps=None, record_history=True, handlers=[])])

    def run(self):
        nacceptors = (int(sys.argv[1]) if (len(sys.argv) > 1) else 8)
        nreplicas = (int(sys.argv[2]) if (len(sys.argv) > 2) else 8)
        nleaders = (int(sys.argv[3]) if (len(sys.argv) > 3) else 2)
        nclients = (int(sys.argv[4]) if (len(sys.argv) > 4) else 5)
        nops = (int(sys.argv[5]) if (len(sys.argv) > 5) else 3)
        acceptors = self.new(Acceptor, (), num=nacceptors)
        replicas = self.new(Replica, num=nreplicas)
        leaders = self.new(Leader, (acceptors, replicas), num=nleaders)
        initial_state = []
        self._setup(replicas, (leaders, initial_state))
        clients = self.new(Client, (replicas, nops), num=nclients)
        self._start(acceptors)
        self._start((replicas | leaders))
        self._start(clients)
        super()._label('_st_label_1578', block=False)
        c = None

        def UniversalOpExpr_1579():
            nonlocal c
            for c in clients:
                if (not PatternExpr_1594.match_iter(self._Node_ReceivedEvent_0, _BoundPattern1600_=c)):
                    return False
            return True
        _st_label_1578 = 0
        while (_st_label_1578 == 0):
            _st_label_1578 += 1
            if UniversalOpExpr_1579():
                _st_label_1578 += 1
            else:
                super()._label('_st_label_1578', block=True)
                _st_label_1578 -= 1
        self.output('All clients done!')
        self.send(('done',), ((acceptors | replicas) | leaders))
