(declare (block) (standard-bindings) (extended-bindings))
(begin
  (define std/event#wait__%
    (lambda (_sel1602_ _timeo1603_)
      (if (std/event#maybe-timeout? _timeo1603_)
          (let ((_wait-e1605_ (std/event#selector-wait-e _sel1602_)))
            (if (_wait-e1605_ _sel1602_ _timeo1603_) _sel1602_ '#f))
          (error '"Bad argument; expected timeout or #f" _timeo1603_))))
  (define std/event#wait__0
    (lambda (_sel1610_)
      (let ((_timeo1612_ '#f)) (std/event#wait__% _sel1610_ _timeo1612_))))
  (define std/event#wait
    (lambda _g5317_
      (let ((_g5316_ (let () (declare (not safe)) (##length _g5317_))))
        (cond ((let () (declare (not safe)) (##fx= _g5316_ 1))
               (apply std/event#wait__0 _g5317_))
              ((let () (declare (not safe)) (##fx= _g5316_ 2))
               (apply std/event#wait__% _g5317_))
              (else
               (##raise-wrong-number-of-arguments-exception
                std/event#wait
                _g5317_))))))
  (define std/event#select__%
    (lambda (_sels1548_ _timeo1549_)
      (let* ((_sels15501560_ _sels1548_)
             (_else15531568_
              (lambda ()
                (if (std/event#maybe-timeout? _timeo1549_)
                    (std/event#do-select _sels1548_ _timeo1549_)
                    (error '"Bad argument; expected timeout or #f"
                           _timeo1549_)))))
        (let ((_K15581588_ (lambda () (error '"empty selection set")))
              (_K15551574_
               (lambda (_sel1572_) (std/event#wait__% _sel1572_ _timeo1549_))))
          (let ((_try-match15521584_
                 (lambda ()
                   (if (let () (declare (not safe)) (##pair? _sels15501560_))
                       (let ((_tl15571579_
                              (let ()
                                (declare (not safe))
                                (##cdr _sels15501560_)))
                             (_hd15561577_
                              (let ()
                                (declare (not safe))
                                (##car _sels15501560_))))
                         (if (let ()
                               (declare (not safe))
                               (##null? _tl15571579_))
                             (let ((_sel1582_ _hd15561577_))
                               (_K15551574_ _sel1582_))
                             (_else15531568_)))
                       (_else15531568_)))))
            (if (let () (declare (not safe)) (##null? _sels15501560_))
                (_K15581588_)
                (_try-match15521584_)))))))
  (define std/event#select__0
    (lambda (_sels1594_)
      (let ((_timeo1596_ '#f)) (std/event#select__% _sels1594_ _timeo1596_))))
  (define std/event#select
    (lambda _g5319_
      (let ((_g5318_ (let () (declare (not safe)) (##length _g5319_))))
        (cond ((let () (declare (not safe)) (##fx= _g5318_ 1))
               (apply std/event#select__0 _g5319_))
              ((let () (declare (not safe)) (##fx= _g5318_ 2))
               (apply std/event#select__% _g5319_))
              (else
               (##raise-wrong-number-of-arguments-exception
                std/event#select
                _g5319_))))))
  (define std/event#selection::t
    (make-struct-type
     'std/event#selection::t
     '#f
     '3
     'selection
     '((final: . #t))
     '#f
     '(e mx cv)))
  (define std/event#selection? (make-struct-predicate std/event#selection::t))
  (define std/event#make-selection
    (lambda _$args1544_
      (apply make-struct-instance std/event#selection::t _$args1544_)))
  (define std/event#selection-e
    (make-struct-field-accessor std/event#selection::t '0))
  (define std/event#selection-mx
    (make-struct-field-accessor std/event#selection::t '1))
  (define std/event#selection-cv
    (make-struct-field-accessor std/event#selection::t '2))
  (define std/event#selection-e-set!
    (make-struct-field-mutator std/event#selection::t '0))
  (define std/event#selection-mx-set!
    (make-struct-field-mutator std/event#selection::t '1))
  (define std/event#selection-cv-set!
    (make-struct-field-mutator std/event#selection::t '2))
  (define std/event#selector::t
    (make-struct-type
     'std/event#selector::t
     '#f
     '3
     'selector
     '((final: . #t))
     '#f
     '(sel wait abort)))
  (define std/event#selector? (make-struct-predicate std/event#selector::t))
  (define std/event#make-selector
    (lambda _$args1541_
      (apply make-struct-instance std/event#selector::t _$args1541_)))
  (define std/event#selector-sel
    (make-struct-field-accessor std/event#selector::t '0))
  (define std/event#selector-wait
    (make-struct-field-accessor std/event#selector::t '1))
  (define std/event#selector-abort
    (make-struct-field-accessor std/event#selector::t '2))
  (define std/event#selector-sel-set!
    (make-struct-field-mutator std/event#selector::t '0))
  (define std/event#selector-wait-set!
    (make-struct-field-mutator std/event#selector::t '1))
  (define std/event#selector-abort-set!
    (make-struct-field-mutator std/event#selector::t '2))
  (define std/event#do-select
    (lambda (_sels1510_ _timeo1511_)
      (let* ((_xsels1513_ (map std/event#wrap-selector _sels1510_))
             (_mx1515_ (make-mutex 'select))
             (_cv1517_ (make-condition-variable 'select))
             (_selection1519_
              (let ()
                (declare (not safe))
                (##structure std/event#selection::t '#f _mx1515_ _cv1517_)))
             (_threads1523_
              (map (lambda (_xsel1521_)
                     (gerbil/gambit/threads#spawn/name
                      'select
                      std/event#select1
                      _selection1519_
                      _xsel1521_))
                   _xsels1513_))
             (_abs-timeo1525_
              (if _timeo1511_
                  (std/event#timeout->abs-timeout _timeo1511_)
                  absent-obj))
             (_finalize!1530_
              (lambda ()
                (for-each thread-terminate! _threads1523_)
                (for-each
                 (lambda (_xsel1528_)
                   ((##direct-structure-ref
                     _xsel1528_
                     '3
                     std/event#selector::t
                     '#f)
                    (##direct-structure-ref
                     _xsel1528_
                     '1
                     std/event#selector::t
                     '#f)))
                 _xsels1513_))))
        (let _lp1533_ ()
          (mutex-lock! _mx1515_)
          (let ((_$e1536_
                 (##direct-structure-ref
                  _selection1519_
                  '1
                  std/event#selection::t
                  '#f)))
            (if _$e1536_
                ((lambda (_xsel1539_)
                   (mutex-unlock! _mx1515_)
                   (_finalize!1530_)
                   (##direct-structure-ref
                    _xsel1539_
                    '1
                    std/event#selector::t
                    '#f))
                 _$e1536_)
                (if (mutex-unlock! _mx1515_ _cv1517_ _abs-timeo1525_)
                    (_lp1533_)
                    (begin (_finalize!1530_) '#f))))))))
  (define std/event#select1
    (lambda (_selection1507_ _xsel1508_)
      (if ((##direct-structure-ref _xsel1508_ '2 std/event#selector::t '#f)
           (##direct-structure-ref _xsel1508_ '1 std/event#selector::t '#f)
           '#f)
          (begin
            (mutex-lock!
             (##direct-structure-ref
              _selection1507_
              '2
              std/event#selection::t
              '#f))
            (if (##direct-structure-ref
                 _selection1507_
                 '1
                 std/event#selection::t
                 '#f)
                '#!void
                (begin
                  (##direct-structure-set!
                   _selection1507_
                   _xsel1508_
                   '1
                   std/event#selection::t
                   '#f)
                  (condition-variable-signal!
                   (##direct-structure-ref
                    _selection1507_
                    '3
                    std/event#selection::t
                    '#f))))
            (mutex-unlock!
             (##direct-structure-ref
              _selection1507_
              '2
              std/event#selection::t
              '#f)))
          '#!void)))
  (define std/event#wrap-selector
    (lambda (_sel1505_)
      (if (thread? _sel1505_)
          (let ()
            (declare (not safe))
            (##structure
             std/event#selector::t
             _sel1505_
             std/event#selector-wait-thread
             void))
          (if (and (pair? _sel1505_)
                   (mutex? (car _sel1505_))
                   (condition-variable? (cdr _sel1505_)))
              (if (eq? (current-thread)
                       (macro-mutex-btq-owner (car _sel1505_)))
                  (let ()
                    (declare (not safe))
                    (##structure
                     std/event#selector::t
                     _sel1505_
                     std/event#selector-wait-condvar
                     std/event#selector-abort-condvar))
                  (error '"Illegal selector; mutex must be owned by current thread"
                         _sel1505_))
              (if (std/event#io-condition-variable? _sel1505_)
                  (let ()
                    (declare (not safe))
                    (##structure
                     std/event#selector::t
                     _sel1505_
                     std/event#selector-wait-io-condvar
                     void))
                  (error '"Illegal selector" _sel1505_))))))
  (define std/event#selector-wait-e
    (lambda (_sel1503_)
      (if (thread? _sel1503_)
          std/event#selector-wait-thread
          (if (and (pair? _sel1503_)
                   (mutex? (car _sel1503_))
                   (condition-variable? (cdr _sel1503_)))
              (if (eq? (current-thread)
                       (macro-mutex-btq-owner (car _sel1503_)))
                  std/event#selector-wait-condvar
                  (error '"Illegal selector; mutex must be owned by current thread"
                         _sel1503_))
              (if (std/event#io-condition-variable? _sel1503_)
                  std/event#selector-wait-io-condvar
                  (error '"Illegal selector" _sel1503_))))))
  (define std/event#selector-wait-thread
    (lambda (_sel1477_ _timeo1478_)
      (with-exception-catcher
       (lambda (_g14791481_)
         (if ((lambda (_$obj1484_) (uncaught-exception? _$obj1484_))
              _g14791481_)
             (let ((_e1487_ _g14791481_)) '#t)
             (if ((lambda (_$obj1489_)
                    (terminated-thread-exception? _$obj1489_))
                  _g14791481_)
                 (let ((_e1492_ _g14791481_)) '#t)
                 (if ((lambda (_$obj1494_)
                        (join-timeout-exception? _$obj1494_))
                      _g14791481_)
                     (let ((_e1497_ _g14791481_)) '#f)
                     (raise _g14791481_)))))
       (lambda ()
         (thread-join!
          _sel1477_
          (let ((_$e1500_ _timeo1478_)) (if _$e1500_ _$e1500_ absent-obj)))
         '#t))))
  (define std/event#selector-wait-condvar
    (lambda (_sel1443_ _timeo1444_)
      (let* ((_sel14451452_ _sel1443_)
             (_E14471456_
              (lambda () (error '"No clause matching" _sel14451452_)))
             (_K14481465_
              (lambda (_cv1459_ _mx1460_)
                (mutex-unlock!
                 _mx1460_
                 _cv1459_
                 (let ((_$e1462_ _timeo1444_))
                   (if _$e1462_ _$e1462_ absent-obj))))))
        (if (let () (declare (not safe)) (##pair? _sel14451452_))
            (let ((_hd14491468_
                   (let () (declare (not safe)) (##car _sel14451452_)))
                  (_tl14501470_
                   (let () (declare (not safe)) (##cdr _sel14451452_))))
              (let* ((_mx1473_ _hd14491468_) (_cv1475_ _tl14501470_))
                (_K14481465_ _cv1475_ _mx1473_)))
            (_E14471456_)))))
  (define std/event#selector-abort-condvar
    (lambda (_sel1413_)
      (let* ((_sel14141421_ _sel1413_)
             (_E14161425_
              (lambda () (error '"No clause matching" _sel14141421_)))
             (_K14171431_
              (lambda (_cv1428_ _mx1429_)
                (if (eq? (current-thread) (macro-mutex-btq-owner _mx1429_))
                    (mutex-unlock! _mx1429_)
                    '#!void))))
        (if (let () (declare (not safe)) (##pair? _sel14141421_))
            (let ((_hd14181434_
                   (let () (declare (not safe)) (##car _sel14141421_)))
                  (_tl14191436_
                   (let () (declare (not safe)) (##cdr _sel14141421_))))
              (let* ((_mx1439_ _hd14181434_) (_cv1441_ _tl14191436_))
                (_K14171431_ _cv1441_ _mx1439_)))
            (_E14161425_)))))
  (define std/event#selector-wait-io-condvar
    (lambda (_sel1410_ _timeo1411_)
      (let ((__tmp5320
             (if _timeo1411_
                 (time->seconds (std/event#timeout->abs-timeout _timeo1411_))
                 '#t)))
        (declare (not safe))
        (##wait-for-io! _sel1410_ __tmp5320))))
  (define std/event#maybe-timeout?
    (lambda (_obj1402_)
      (let ((_$e1404_ (not _obj1402_)))
        (if _$e1404_
            _$e1404_
            (let ((_$e1407_ (real? _obj1402_)))
              (if _$e1407_ _$e1407_ (time? _obj1402_)))))))
  (define std/event#timeout->abs-timeout
    (lambda (_timeo1400_)
      (if (time? _timeo1400_)
          _timeo1400_
          (seconds->time
           (+ (let () (declare (not safe)) (##current-time-point))
              _timeo1400_)))))
  (define std/event#io-condition-variable?
    (lambda (_obj1398_)
      (if (condition-variable? _obj1398_)
          (let ((__tmp5321 (macro-condvar-name _obj1398_)))
            (declare (not safe))
            (##foreign? __tmp5321))
          '#f)))
  (define std/event#event::t
    (make-struct-type
     'std/event#event::t
     '#f
     '4
     'event
     '((final: . #t))
     ':init!
     '(e poll ready abort)))
  (define std/event#event? (make-struct-predicate std/event#event::t))
  (define std/event#make-event
    (lambda _$args1395_
      (apply make-struct-instance std/event#event::t _$args1395_)))
  (define std/event#event-e (make-struct-field-accessor std/event#event::t '0))
  (define std/event#event-poll
    (make-struct-field-accessor std/event#event::t '1))
  (define std/event#event-ready
    (make-struct-field-accessor std/event#event::t '2))
  (define std/event#event-abort
    (make-struct-field-accessor std/event#event::t '3))
  (define std/event#event-e-set!
    (make-struct-field-mutator std/event#event::t '0))
  (define std/event#event-poll-set!
    (make-struct-field-mutator std/event#event::t '1))
  (define std/event#event-ready-set!
    (make-struct-field-mutator std/event#event::t '2))
  (define std/event#event-abort-set!
    (make-struct-field-mutator std/event#event::t '3))
  (define std/event#event-handler::t
    (make-struct-type
     'std/event#event-handler::t
     '#f
     '2
     'event-handler
     '((final: . #t))
     '#f
     '(e K)))
  (define std/event#event-handler?
    (make-struct-predicate std/event#event-handler::t))
  (define std/event#make-event-handler
    (lambda _$args1392_
      (apply make-struct-instance std/event#event-handler::t _$args1392_)))
  (define std/event#event-handler-e
    (make-struct-field-accessor std/event#event-handler::t '0))
  (define std/event#event-handler-K
    (make-struct-field-accessor std/event#event-handler::t '1))
  (define std/event#event-handler-e-set!
    (make-struct-field-mutator std/event#event-handler::t '0))
  (define std/event#event-handler-K-set!
    (make-struct-field-mutator std/event#event-handler::t '1))
  (define std/event#event-set::t
    (make-struct-type
     'std/event#event-set::t
     '#f
     '1
     'event-set
     '((final: . #t))
     '#f
     '(e)))
  (define std/event#event-set? (make-struct-predicate std/event#event-set::t))
  (define std/event#make-event-set
    (lambda _$args1389_
      (apply make-struct-instance std/event#event-set::t _$args1389_)))
  (define std/event#event-set-e
    (make-struct-field-accessor std/event#event-set::t '0))
  (define std/event#event-set-e-set!
    (make-struct-field-mutator std/event#event-set::t '0))
  (define std/event#event:::init!__%
    (lambda (_self1367_ _e1368_ _poll1369_ _ready1370_ _abort1371_)
      (if (let ((__tmp5322
                 (let () (declare (not safe)) (##vector-length _self1367_))))
            (declare (not safe))
            (##fx< '4 __tmp5322))
          (begin
            (let () (declare (not safe)) (##vector-set! _self1367_ '1 _e1368_))
            (let ()
              (declare (not safe))
              (##vector-set! _self1367_ '2 _poll1369_))
            (let ()
              (declare (not safe))
              (##vector-set! _self1367_ '3 _ready1370_))
            (let ()
              (declare (not safe))
              (##vector-set! _self1367_ '4 _abort1371_)))
          (error '"struct-instance-init!: too many arguments for struct"
                 _self1367_))))
  (define std/event#event:::init!__0
    (lambda (_self1376_ _e1377_ _poll1378_ _ready1379_)
      (let ((_abort1381_ void))
        (if (let ((__tmp5323
                   (let () (declare (not safe)) (##vector-length _self1376_))))
              (declare (not safe))
              (##fx< '4 __tmp5323))
            (begin
              (let ()
                (declare (not safe))
                (##vector-set! _self1376_ '1 _e1377_))
              (let ()
                (declare (not safe))
                (##vector-set! _self1376_ '2 _poll1378_))
              (let ()
                (declare (not safe))
                (##vector-set! _self1376_ '3 _ready1379_))
              (let ()
                (declare (not safe))
                (##vector-set! _self1376_ '4 _abort1381_)))
            (error '"struct-instance-init!: too many arguments for struct"
                   _self1376_)))))
  (define std/event#event:::init!
    (lambda _g5325_
      (let ((_g5324_ (let () (declare (not safe)) (##length _g5325_))))
        (cond ((let () (declare (not safe)) (##fx= _g5324_ 4))
               (apply std/event#event:::init!__0 _g5325_))
              ((let () (declare (not safe)) (##fx= _g5324_ 5))
               (apply (lambda (_self1383_
                               _e1384_
                               _poll1385_
                               _ready1386_
                               _abort1387_)
                        (if (let ((__tmp5326
                                   (let ()
                                     (declare (not safe))
                                     (##vector-length _self1383_))))
                              (declare (not safe))
                              (##fx< '4 __tmp5326))
                            (begin
                              (let ()
                                (declare (not safe))
                                (##vector-set! _self1383_ '1 _e1384_))
                              (let ()
                                (declare (not safe))
                                (##vector-set! _self1383_ '2 _poll1385_))
                              (let ()
                                (declare (not safe))
                                (##vector-set! _self1383_ '3 _ready1386_))
                              (let ()
                                (declare (not safe))
                                (##vector-set! _self1383_ '4 _abort1387_)))
                            (error '"struct-instance-init!: too many arguments for struct"
                                   _self1383_)))
                      _g5325_))
              (else
               (##raise-wrong-number-of-arguments-exception
                std/event#event:::init!
                _g5325_))))))
  (bind-method! std/event#event::t ':init! std/event#event:::init! '#f)
  (define std/event#sync-object?
    (lambda (_obj1236_)
      (let ((_$e1238_
             (let ()
               (declare (not safe))
               (##structure-direct-instance-of?
                _obj1236_
                'std/event#event::t))))
        (if _$e1238_
            _$e1238_
            (let ((_$e1241_
                   (let ()
                     (declare (not safe))
                     (##structure-direct-instance-of?
                      _obj1236_
                      'std/event#event-handler::t))))
              (if _$e1241_
                  _$e1241_
                  (let ()
                    (declare (not safe))
                    (##structure-direct-instance-of?
                     _obj1236_
                     'std/event#event-set::t))))))))
  (define std/event#never-evt
    (let ((__obj5305 (make-object std/event#event::t '4)))
      (std/event#event:::init!__0 __obj5305 '#f false void)
      __obj5305))
  (define std/event#always-evt
    (let ((__obj5306 (make-object std/event#event::t '4)))
      (std/event#event:::init!__0 __obj5306 '#f true values)
      __obj5306))
  (define std/event#wrap-evt
    (lambda (_obj1231_)
      (if (std/event#sync-object? _obj1231_)
          _obj1231_
          (if (std/event#sync-selector? _obj1231_)
              (let ((__obj5307 (make-object std/event#event::t '4)))
                (if (let ((__tmp5327
                           (let ()
                             (declare (not safe))
                             (##vector-length __obj5307))))
                      (declare (not safe))
                      (##fx< '4 __tmp5327))
                    (begin
                      (let ()
                        (declare (not safe))
                        (##vector-set! __obj5307 '1 _obj1231_))
                      (let ()
                        (declare (not safe))
                        (##vector-set! __obj5307 '2 std/event#event-e))
                      (let ()
                        (declare (not safe))
                        (##vector-set! __obj5307 '3 std/event#event-e))
                      (let ()
                        (declare (not safe))
                        (##vector-set!
                         __obj5307
                         '4
                         std/event#event-abort-sync-selector)))
                    (error '"struct-instance-init!: too many arguments for struct"
                           __obj5307))
                __obj5307)
              (if (or (time? _obj1231_) (real? _obj1231_))
                  (let ((__obj5308 (make-object std/event#event::t '4)))
                    (std/event#event:::init!__0
                     __obj5308
                     (std/event#timeout->abs-timeout _obj1231_)
                     std/event#event-e
                     false)
                    __obj5308)
                  (if (input-port? _obj1231_)
                      (std/event#input-port-evt _obj1231_)
                      (std/event#wrap-evt
                       (let ((__method5309 (method-ref _obj1231_ ':event)))
                         (if __method5309
                             (__method5309 _obj1231_)
                             (error '"Missing method"
                                    _obj1231_
                                    ':event))))))))))
  (define std/event#handle-evt
    (lambda (_obj1222_ _K1223_)
      (let ((_evt1225_ (std/event#wrap-evt _obj1222_)))
        (if (let ()
              (declare (not safe))
              (##structure-direct-instance-of?
               _evt1225_
               'std/event#event-set::t))
            (let ((__tmp5328
                   (map (lambda (_g12261228_)
                          (let ()
                            (declare (not safe))
                            (##structure
                             std/event#event-handler::t
                             _g12261228_
                             _K1223_)))
                        (##direct-structure-ref
                         _evt1225_
                         '1
                         std/event#event-set::t
                         '#f))))
              (declare (not safe))
              (##structure std/event#event-set::t __tmp5328))
            (let ()
              (declare (not safe))
              (##structure std/event#event-handler::t _evt1225_ _K1223_))))))
  (define std/event#choice-evt
    (lambda _args1180_
      (let _lp1182_ ((_rest1184_ _args1180_) (_evts1185_ '()))
        (let* ((_rest11861194_ _rest1184_)
               (_else11881202_
                (lambda ()
                  (let ()
                    (declare (not safe))
                    (##structure std/event#event-set::t _evts1185_))))
               (_K11901210_
                (lambda (_rest1205_ _obj1206_)
                  (let ((_evt1208_ (std/event#wrap-evt _obj1206_)))
                    (if (let ()
                          (declare (not safe))
                          (##structure-direct-instance-of?
                           _evt1208_
                           'std/event#event-set::t))
                        (if (null? _evts1185_)
                            (_lp1182_
                             _rest1205_
                             (##direct-structure-ref
                              _evt1208_
                              '1
                              std/event#event-set::t
                              '#f))
                            (_lp1182_
                             _rest1205_
                             (foldl1 cons
                                     _evts1185_
                                     (##direct-structure-ref
                                      _evt1208_
                                      '1
                                      std/event#event-set::t
                                      '#f))))
                        (_lp1182_ _rest1205_ (cons _evt1208_ _evts1185_)))))))
          (if (let () (declare (not safe)) (##pair? _rest11861194_))
              (let ((_hd11911213_
                     (let () (declare (not safe)) (##car _rest11861194_)))
                    (_tl11921215_
                     (let () (declare (not safe)) (##cdr _rest11861194_))))
                (let* ((_obj1218_ _hd11911213_) (_rest1220_ _tl11921215_))
                  (_K11901210_ _rest1220_ _obj1218_)))
              (_else11881202_))))))
  (define std/event#sync
    (lambda _args1016_
      (letrec* ((_mutexes1018_ (make-table 'test: eq?))
                (_loop1019_
                 (lambda (_rest1136_
                          _sels1137_
                          _sel-evts1138_
                          _timeo1139_
                          _timeo-evt1140_)
                   (let* ((_rest11411149_ _rest1136_)
                          (_else11431159_
                           (lambda ()
                             (if (not (null? _sels1137_))
                                 (let ((_rz1157_
                                        (std/event#select__%
                                         _sels1137_
                                         _timeo1139_)))
                                   (if _rz1157_
                                       (_dispatch-selector1023_
                                        _rz1157_
                                        _sels1137_
                                        _sel-evts1138_)
                                       (_dispatch-evt1022_ _timeo-evt1140_)))
                                 (if _timeo1139_
                                     (begin
                                       (thread-sleep! _timeo1139_)
                                       (_dispatch-evt1022_ _timeo-evt1140_))
                                     (thread-sleep! '+inf.0)))))
                          (_K11451168_
                           (lambda (_rest1162_ _evt1163_)
                             (if (or (let ()
                                       (declare (not safe))
                                       (##structure-direct-instance-of?
                                        _evt1163_
                                        'std/event#event::t))
                                     (let ()
                                       (declare (not safe))
                                       (##structure-direct-instance-of?
                                        _evt1163_
                                        'std/event#event-handler::t)))
                                 (_poll1020_
                                  _evt1163_
                                  _rest1162_
                                  _sels1137_
                                  _sel-evts1138_
                                  _timeo1139_
                                  _timeo-evt1140_)
                                 (_loop1019_
                                  (foldl1 cons
                                          _rest1162_
                                          (std/misc/shuffle#shuffle
                                           (##direct-structure-ref
                                            _evt1163_
                                            '1
                                            std/event#event-set::t
                                            '#f)))
                                  _sels1137_
                                  _sel-evts1138_
                                  _timeo1139_
                                  _timeo-evt1140_)))))
                     (if (let () (declare (not safe)) (##pair? _rest11411149_))
                         (let ((_hd11461171_
                                (let ()
                                  (declare (not safe))
                                  (##car _rest11411149_)))
                               (_tl11471173_
                                (let ()
                                  (declare (not safe))
                                  (##cdr _rest11411149_))))
                           (let* ((_evt1176_ _hd11461171_)
                                  (_rest1178_ _tl11471173_))
                             (_K11451168_ _rest1178_ _evt1176_)))
                         (_else11431159_)))))
                (_poll1020_
                 (lambda (_evt1126_
                          _rest1127_
                          _sels1128_
                          _sel-evts1129_
                          _timeo1130_
                          _timeo-evt1131_)
                   (let ((_rz1133_ (_poll-evt1021_ _evt1126_)))
                     (if (not _rz1133_)
                         (_loop1019_
                          _rest1127_
                          _sels1128_
                          _sel-evts1129_
                          _timeo1130_
                          _timeo-evt1131_)
                         (if (eq? _rz1133_ '#t)
                             (begin
                               (_abort-selectors!1026_ _rest1127_ _sels1128_)
                               (_dispatch-evt1022_ _evt1126_))
                             (if (time? _rz1133_)
                                 (if _timeo1130_
                                     (if (< (time->seconds _rz1133_)
                                            (time->seconds _timeo1130_))
                                         (_loop1019_
                                          _rest1127_
                                          _sels1128_
                                          _sel-evts1129_
                                          _rz1133_
                                          _evt1126_)
                                         (_loop1019_
                                          _rest1127_
                                          _sels1128_
                                          _sel-evts1129_
                                          _timeo1130_
                                          _timeo-evt1131_))
                                     (_loop1019_
                                      _rest1127_
                                      _sels1128_
                                      _sel-evts1129_
                                      _rz1133_
                                      _evt1126_))
                                 (if (std/event#sync-selector? _rz1133_)
                                     (begin
                                       (_check-mutex!1024_
                                        _rz1133_
                                        _rest1127_
                                        _sels1128_)
                                       (_loop1019_
                                        _rest1127_
                                        (cons _rz1133_ _sels1128_)
                                        (cons _evt1126_ _sel-evts1129_)
                                        _timeo1130_
                                        _timeo-evt1131_))
                                     (_abort!1025_
                                      _rest1127_
                                      _sels1128_
                                      (lambda ()
                                        (error '"Bad synchronizer"
                                               _evt1126_
                                               _rz1133_))))))))))
                (_poll-evt1021_
                 (lambda (_evt1120_)
                   (let _lp1122_ ((_evt1124_ _evt1120_))
                     (if (let ()
                           (declare (not safe))
                           (##structure-direct-instance-of?
                            _evt1124_
                            'std/event#event::t))
                         ((##direct-structure-ref
                           _evt1124_
                           '2
                           std/event#event::t
                           '#f)
                          _evt1124_)
                         (_lp1122_
                          (##direct-structure-ref
                           _evt1124_
                           '1
                           std/event#event-handler::t
                           '#f))))))
                (_dispatch-evt1022_
                 (lambda (_evt1113_)
                   (let _recur1115_ ((_evt1117_ _evt1113_))
                     (if (let ()
                           (declare (not safe))
                           (##structure-direct-instance-of?
                            _evt1117_
                            'std/event#event::t))
                         ((##direct-structure-ref
                           _evt1117_
                           '3
                           std/event#event::t
                           '#f)
                          _evt1117_)
                         (call-with-values
                          (lambda ()
                            (_recur1115_
                             (##direct-structure-ref
                              _evt1117_
                              '1
                              std/event#event-handler::t
                              '#f)))
                          (##direct-structure-ref
                           _evt1117_
                           '2
                           std/event#event-handler::t
                           '#f))))))
                (_dispatch-selector1023_
                 (lambda (_rz1048_ _sels1049_ _sel-evts1050_)
                   (let _lp1052_ ((_rest-sels1054_ _sels1049_)
                                  (_rest-evts1055_ _sel-evts1050_))
                     (let* ((_rest-sels10561063_ _rest-sels1054_)
                            (_E10581067_
                             (lambda ()
                               (error '"No clause matching"
                                      _rest-sels10561063_)))
                            (_K10591101_
                             (lambda (_rest-sels1070_ _sel1071_)
                               (let* ((_rest-evts10721079_ _rest-evts1055_)
                                      (_E10741083_
                                       (lambda ()
                                         (error '"No clause matching"
                                                _rest-evts10721079_)))
                                      (_K10751089_
                                       (lambda (_rest-evts1086_ _evt1087_)
                                         (if (eq? _sel1071_ _rz1048_)
                                             (_dispatch-evt1022_ _evt1087_)
                                             (_lp1052_
                                              _rest-sels1070_
                                              _rest-evts1086_)))))
                                 (if (let ()
                                       (declare (not safe))
                                       (##pair? _rest-evts10721079_))
                                     (let ((_hd10761092_
                                            (let ()
                                              (declare (not safe))
                                              (##car _rest-evts10721079_)))
                                           (_tl10771094_
                                            (let ()
                                              (declare (not safe))
                                              (##cdr _rest-evts10721079_))))
                                       (let* ((_evt1097_ _hd10761092_)
                                              (_rest-evts1099_ _tl10771094_))
                                         (_K10751089_
                                          _rest-evts1099_
                                          _evt1097_)))
                                     (_E10741083_))))))
                       (if (let ()
                             (declare (not safe))
                             (##pair? _rest-sels10561063_))
                           (let ((_hd10601104_
                                  (let ()
                                    (declare (not safe))
                                    (##car _rest-sels10561063_)))
                                 (_tl10611106_
                                  (let ()
                                    (declare (not safe))
                                    (##cdr _rest-sels10561063_))))
                             (let* ((_sel1109_ _hd10601104_)
                                    (_rest-sels1111_ _tl10611106_))
                               (_K10591101_ _rest-sels1111_ _sel1109_)))
                           (_E10581067_))))))
                (_check-mutex!1024_
                 (lambda (_rz1041_ _rest1042_ _sels1043_)
                   (if (pair? _rz1041_)
                       (let ((_mx1045_ (car _rz1041_)))
                         (if (table-ref _mutexes1018_ _mx1045_ '#f)
                             (_abort!1025_
                              _rest1042_
                              _sels1043_
                              (lambda ()
                                (error '"Duplicate mutex synchronizer"
                                       _rz1041_)))
                             (table-set! _mutexes1018_ _mx1045_ '#t)))
                       '#!void)))
                (_abort!1025_
                 (lambda (_rest1037_ _sels1038_ _E1039_)
                   (_abort-selectors!1026_ _rest1037_ _sels1038_)
                   (_E1039_)))
                (_abort-selectors!1026_
                 (lambda (_rest1034_ _sels1035_)
                   (for-each _abort-selector!1027_ _sels1035_)
                   (for-each _abort-evt!1028_ _rest1034_)))
                (_abort-selector!1027_
                 (lambda (_sel1032_)
                   (if (pair? _sel1032_)
                       (mutex-unlock! (car _sel1032_))
                       '#!void)))
                (_abort-evt!1028_
                 (lambda (_evt1030_)
                   (if (let ()
                         (declare (not safe))
                         (##structure-direct-instance-of?
                          _evt1030_
                          'std/event#event::t))
                       ((##direct-structure-ref
                         _evt1030_
                         '4
                         std/event#event::t
                         '#f)
                        _evt1030_)
                       (if (let ()
                             (declare (not safe))
                             (##structure-direct-instance-of?
                              _evt1030_
                              'std/event#event-handler::t))
                           (_abort-evt!1028_
                            (##direct-structure-ref
                             _evt1030_
                             '1
                             std/event#event-handler::t
                             '#f))
                           (for-each
                            _abort-evt!1028_
                            (##direct-structure-ref
                             _evt1030_
                             '1
                             std/event#event-set::t
                             '#f)))))))
        (_loop1019_
         (std/misc/shuffle#shuffle (map std/event#wrap-evt _args1016_))
         '()
         '()
         '#f
         '#f))))
  (define std/event#sync-selector?
    (lambda (_obj1008_)
      (let ((_$e1010_ (thread? _obj1008_)))
        (if _$e1010_
            _$e1010_
            (let ((_$e1013_
                   (if (pair? _obj1008_)
                       (if (mutex? (car _obj1008_))
                           (condition-variable? (cdr _obj1008_))
                           '#f)
                       '#f)))
              (if _$e1013_
                  _$e1013_
                  (std/event#io-condition-variable? _obj1008_)))))))
  (define std/event#event-abort-sync-selector
    (lambda (_evt1004_)
      (let ((_sel1006_
             (##direct-structure-ref _evt1004_ '1 std/event#event::t '#f)))
        (if (pair? _sel1006_) (mutex-unlock! (car _sel1006_)) '#!void))))
  (define std/event#input-port-evt
    (lambda (_port1002_)
      (if (macro-u8vector-port? _port1002_)
          (std/event#make-u8vector-port-evt _port1002_)
          (if (macro-string-port? _port1002_)
              (std/event#make-string-port-evt _port1002_)
              (if (macro-vector-port? _port1002_)
                  (std/event#make-vector-port-evt _port1002_)
                  (if (macro-device-port? _port1002_)
                      (std/event#make-device-port-evt _port1002_)
                      (if (macro-tcp-server-port? _port1002_)
                          (std/event#make-tcp-server-port-evt _port1002_)
                          (if (macro-raw-device-port? _port1002_)
                              (std/event#make-raw-device-port-evt _port1002_)
                              (error '"can't wrap event around unknown port type"
                                     _port1002_)))))))))
  (define std/event#make-u8vector-port-evt
    (lambda (_port987_)
      (letrec ((_rbuf-check989_
                (lambda (_port1000_)
                  (let ((__tmp5330 (macro-byte-port-rlo _port1000_))
                        (__tmp5329 (macro-byte-port-rhi _port1000_)))
                    (declare (not safe))
                    (##fx< __tmp5330 __tmp5329))))
               (_rbuf-fill990_
                (lambda (_port998_) (macro-byte-port-rbuf-fill _port998_)))
               (_condvar-e991_
                (lambda (_port996_) (macro-u8vector-port-rcondvar _port996_)))
               (_poll992_
                (lambda (_evt994_)
                  (std/event#user-port-poll
                   (##direct-structure-ref _evt994_ '1 std/event#event::t '#f)
                   _rbuf-check989_
                   _rbuf-fill990_
                   _condvar-e991_))))
        (let ((__obj5310 (make-object std/event#event::t '4)))
          (std/event#event:::init!__0
           __obj5310
           _port987_
           _poll992_
           std/event#event-e)
          __obj5310))))
  (define std/event#make-string-port-evt
    (lambda (_port972_)
      (letrec ((_rbuf-check974_
                (lambda (_port985_)
                  (let ((__tmp5332 (macro-character-port-rlo _port985_))
                        (__tmp5331 (macro-character-port-rhi _port985_)))
                    (declare (not safe))
                    (##fx< __tmp5332 __tmp5331))))
               (_rbuf-fill975_
                (lambda (_port983_)
                  (macro-character-port-rbuf-fill _port983_)))
               (_condvar-e976_
                (lambda (_port981_) (macro-string-port-rcondvar _port981_)))
               (_poll977_
                (lambda (_evt979_)
                  (std/event#user-port-poll
                   (##direct-structure-ref _evt979_ '1 std/event#event::t '#f)
                   _rbuf-check974_
                   _rbuf-fill975_
                   _condvar-e976_))))
        (let ((__obj5311 (make-object std/event#event::t '4)))
          (std/event#event:::init!__0
           __obj5311
           _port972_
           _poll977_
           std/event#event-e)
          __obj5311))))
  (define std/event#make-vector-port-evt
    (lambda (_port957_)
      (letrec ((_rbuf-check959_
                (lambda (_port970_)
                  (let ((__tmp5334 (macro-vector-port-rlo _port970_))
                        (__tmp5333 (macro-vector-port-rhi _port970_)))
                    (declare (not safe))
                    (##fx< __tmp5334 __tmp5333))))
               (_rbuf-fill960_
                (lambda (_port968_) (macro-vector-port-rbuf-fill _port968_)))
               (_condvar-e961_
                (lambda (_port966_) (macro-vector-port-rcondvar _port966_)))
               (_poll962_
                (lambda (_evt964_)
                  (std/event#user-port-poll
                   (##direct-structure-ref _evt964_ '1 std/event#event::t '#f)
                   _rbuf-check959_
                   _rbuf-fill960_
                   _condvar-e961_))))
        (let ((__obj5312 (make-object std/event#event::t '4)))
          (std/event#event:::init!__0
           __obj5312
           _port957_
           _poll962_
           std/event#event-e)
          __obj5312))))
  (define std/event#user-port-poll
    (lambda (_port945_ _rbuf-check946_ _rbuf-fill947_ _condvar-e948_)
      (letrec ((_fill!950_
                (lambda (_port955_)
                  (std/event#input-port-rbuf-fill!
                   _port955_
                   (_rbuf-fill947_ _port955_)))))
        (let ((_mx952_ (macro-port-mutex _port945_)))
          (mutex-lock! _mx952_)
          (if (_rbuf-check946_ _port945_)
              (begin (mutex-unlock! _mx952_) '#t)
              (if (_fill!950_ _port945_)
                  (begin (mutex-unlock! _mx952_) '#t)
                  (cons _mx952_ (_condvar-e948_ _port945_))))))))
  (define std/event#input-port-rbuf-fill!
    (lambda (_port937_ _fill938_)
      (let _lp940_ ()
        (let ((_r943_ (_fill938_ _port937_ '1 '#f)))
          (if (eq? _r943_ ##err-code-EINTR)
              (_lp940_)
              (if (eq? _r943_ ##err-code-EAGAIN) '#f '#t))))))
  (define std/event#make-device-port-evt
    (lambda (_port920_)
      (letrec ((_fill!922_
                (lambda (_port935_)
                  (std/event#input-port-rbuf-fill!
                   _port935_
                   (macro-byte-port-rbuf-fill _port935_))))
               (_poll923_
                (lambda (_evt925_)
                  (let* ((_port927_
                          (##direct-structure-ref
                           _evt925_
                           '1
                           std/event#event::t
                           '#f))
                         (_mx929_ (macro-port-mutex _port927_)))
                    (if (or (let ((__tmp5336 (macro-byte-port-rlo _port927_))
                                  (__tmp5335 (macro-byte-port-rhi _port927_)))
                              (declare (not safe))
                              (##fx< __tmp5336 __tmp5335))
                            (let ((__tmp5338
                                   (macro-character-port-rlo _port927_))
                                  (__tmp5337
                                   (macro-character-port-rhi _port927_)))
                              (declare (not safe))
                              (##fx< __tmp5338 __tmp5337)))
                        (begin (mutex-unlock! _mx929_) '#t)
                        (if (_fill!922_ _port927_)
                            (begin (mutex-unlock! _mx929_) '#t)
                            (begin
                              (mutex-unlock! _mx929_)
                              (macro-device-port-rdevice-condvar
                               _port927_))))))))
        (let ((__obj5313 (make-object std/event#event::t '4)))
          (std/event#event:::init!__0
           __obj5313
           _port920_
           _poll923_
           std/event#event-e)
          __obj5313))))
  (define std/event#make-tcp-server-port-evt
    (lambda (_port912_)
      (letrec ((_poll914_
                (lambda (_evt916_)
                  (let ((_port918_
                         (##direct-structure-ref
                          _evt916_
                          '1
                          std/event#event::t
                          '#f)))
                    (macro-tcp-server-port-rdevice-condvar _port918_)))))
        (let ((__obj5314 (make-object std/event#event::t '4)))
          (std/event#event:::init!__0
           __obj5314
           _port912_
           _poll914_
           std/event#event-e)
          __obj5314))))
  (define std/event#make-raw-device-port-evt
    (lambda (_port904_)
      (letrec ((_poll906_
                (lambda (_evt908_)
                  (let ((_port910_
                         (##direct-structure-ref
                          _evt908_
                          '1
                          std/event#event::t
                          '#f)))
                    (macro-raw-device-port-rdevice-condvar _port910_)))))
        (let ((__obj5315 (make-object std/event#event::t '4)))
          (std/event#event:::init!__0
           __obj5315
           _port904_
           _poll906_
           std/event#event-e)
          __obj5315)))))
