(declare (block) (standard-bindings) (extended-bindings))
(define std/net/socket/kqueue-server#kqueue-socket-server
  (lambda ()
    (letrec* ((_fdtab433_ (make-table 'test: eq?))
              (_kq434_ (std/os/kqueue#kqueue))
              (_maxevts435_ '8192)
              (_evts436_ (std/os/kqueue#make-kevents _maxevts435_))
              (_ev-in437_
               (let ()
                 (declare (not safe))
                 (##fxior std/os/kqueue#EVFILT_READ
                          std/os/kqueue#EV_EOF
                          std/os/kqueue#EV_ERROR)))
              (_ev-out438_
               (let ()
                 (declare (not safe))
                 (##fxior std/os/kqueue#EVFILT_WRITE
                          std/os/kqueue#EV_EOF
                          std/os/kqueue#EV_ERROR)))
              (_do-kevent439_
               (lambda ()
                 (let ((_count661_
                        (std/os/kqueue#kqueue-poll
                         _kq434_
                         _evts436_
                         _maxevts435_)))
                   (if (let () (declare (not safe)) (##fxpositive? _count661_))
                       (let _lp663_ ((_k665_ '0))
                         (if (let ()
                               (declare (not safe))
                               (##fx< _k665_ _count661_))
                             (let ((_fd667_ (std/os/kqueue#kevent-ident
                                             _evts436_
                                             _k665_))
                                   (_ready668_
                                    (std/os/kqueue#kevent-flags
                                     _evts436_
                                     _k665_)))
                               (let* ((_g669677_
                                       (table-ref _fdtab433_ _fd667_))
                                      (_E671681_
                                       (lambda ()
                                         (error '"No clause matching"
                                                _g669677_)))
                                      (_K672687_
                                       (lambda (_io-out684_ _io-in685_)
                                         (if (let ((__tmp5764
                                                    (let ()
                                                      (declare (not safe))
                                                      (##fxand _ready668_
                                                               _ev-in437_))))
                                               (declare (not safe))
                                               (##fxzero? __tmp5764))
                                             '#!void
                                             (if _io-in685_
                                                 (std/net/socket/basic-server#io-state-signal-ready!
                                                  _io-in685_
                                                  'ready)
                                                 '#!void))
                                         (if (let ((__tmp5765
                                                    (let ()
                                                      (declare (not safe))
                                                      (##fxand _ready668_
                                                               _ev-out438_))))
                                               (declare (not safe))
                                               (##fxzero? __tmp5765))
                                             '#!void
                                             (if _io-out684_
                                                 (std/net/socket/basic-server#io-state-signal-ready!
                                                  _io-out684_
                                                  'ready)
                                                 '#!void))
                                         (_lp663_ (let ()
                                                    (declare (not safe))
                                                    (##fx+ _k665_ '1))))))
                                 (if (let ()
                                       (declare (not safe))
                                       (##structure-direct-instance-of?
                                        _g669677_
                                        'std/net/socket/base#!socket-state::t))
                                     (let* ((_e673690_
                                             (let ()
                                               (declare (not safe))
                                               (##vector-ref _g669677_ '1)))
                                            (_e674693_
                                             (let ()
                                               (declare (not safe))
                                               (##vector-ref _g669677_ '2)))
                                            (_io-in696_ _e674693_)
                                            (_e675698_
                                             (let ()
                                               (declare (not safe))
                                               (##vector-ref _g669677_ '3)))
                                            (_io-out701_ _e675698_))
                                       (_K672687_ _io-out701_ _io-in696_))
                                     (_E671681_))))
                             '#!void))
                       '#!void))))
              (_add-socket440_
               (lambda (_sock623_)
                 (let* ((_self625_ (current-thread))
                        (_fd627_ (std/os/fd#fd-e _sock623_))
                        (_io-in629_
                         (if (std/os/fd#fd-io-in _sock623_)
                             (let ((__obj5762
                                    (make-object
                                     std/net/socket/base#!io-state::t
                                     '3)))
                               (std/net/socket/basic-server#!io-state:::init!
                                __obj5762)
                               __obj5762)
                             '#f))
                        (_wait-in634_
                         (if _io-in629_
                             (lambda (_ssock631_ _timeo632_)
                               (std/net/socket/basic-server#io-state-wait-io!
                                _io-in629_
                                _timeo632_
                                'input))
                             '#f))
                        (_io-out636_
                         (if (std/os/fd#fd-io-out _sock623_)
                             (let ((__obj5763
                                    (make-object
                                     std/net/socket/base#!io-state::t
                                     '3)))
                               (std/net/socket/basic-server#!io-state:::init!
                                __obj5763)
                               __obj5763)
                             '#f))
                        (_wait-out641_
                         (if _io-out636_
                             (lambda (_ssock638_ _timeo639_)
                               (std/net/socket/basic-server#io-state-wait-io!
                                _io-out636_
                                _timeo639_
                                'output))
                             '#f))
                        (_close647_
                         (lambda (_ssock643_ _dir644_ _shutdown645_)
                           (std/actor/message#send-message__0
                            _self625_
                            (let ((__tmp5766
                                   (let ()
                                     (declare (not safe))
                                     (##structure
                                      std/net/socket/base#socket-server.close::t
                                      _ssock643_
                                      _dir644_
                                      _shutdown645_))))
                              (declare (not safe))
                              (##structure
                               std/actor/proto#!event::t
                               __tmp5766)))))
                        (_ssock649_
                         (let ()
                           (declare (not safe))
                           (##structure
                            std/net/socket/base#!socket::t
                            _sock623_
                            _wait-in634_
                            _wait-out641_
                            _close647_)))
                        (_state651_
                         (let ()
                           (declare (not safe))
                           (##structure
                            std/net/socket/base#!socket-state::t
                            _sock623_
                            _io-in629_
                            _io-out636_)))
                        (_nchanges653_ '0))
                   (if _io-in629_
                       (begin
                         (std/os/kqueue#kevent-set!__%
                          '#f
                          (std/os/fd#fd-e _sock623_)
                          std/os/kqueue#EVFILT_READ
                          (let ()
                            (declare (not safe))
                            (##fxior std/os/kqueue#EV_ADD
                                     std/os/kqueue#EV_CLEAR))
                          std/os/kqueue#NOTE_LOWAT
                          '1
                          absent-value
                          _evts436_
                          _nchanges653_)
                         (set! _nchanges653_ (+ _nchanges653_ '1)))
                       '#!void)
                   (if _io-out636_
                       (begin
                         (std/os/kqueue#kevent-set!__%
                          '#f
                          (std/os/fd#fd-e _sock623_)
                          std/os/kqueue#EVFILT_WRITE
                          (let ()
                            (declare (not safe))
                            (##fxior std/os/kqueue#EV_ADD
                                     std/os/kqueue#EV_CLEAR))
                          std/os/kqueue#NOTE_LOWAT
                          '1
                          absent-value
                          _evts436_
                          _nchanges653_)
                         (set! _nchanges653_ (+ _nchanges653_ '1)))
                       '#!void)
                   (std/os/kqueue#kevent
                    _kq434_
                    _evts436_
                    _nchanges653_
                    '#f
                    '0)
                   (make-will
                    _ssock649_
                    (lambda (_g655657_) (_close647_ _g655657_ 'inout '#f)))
                   (table-set! _fdtab433_ _fd627_ _state651_)
                   _ssock649_)))
              (_close-socket441_
               (lambda (_ssock528_ _dir529_ _shutdown530_)
                 (letrec ((_close-io-in!532_
                           (lambda (_io-in620_ _sock621_)
                             (std/net/socket/basic-server#io-state-close-in!
                              _io-in620_
                              _sock621_
                              _shutdown530_)))
                          (_close-io-out!533_
                           (lambda (_io-out617_ _sock618_)
                             (std/net/socket/basic-server#io-state-close-out!
                              _io-out617_
                              _sock618_
                              _shutdown530_))))
                   (let* ((_ssock534542_ _ssock528_)
                          (_E536546_
                           (lambda ()
                             (error '"No clause matching" _ssock534542_)))
                          (_K537599_
                           (lambda (_wait-out549_ _wait-in550_ _sock551_)
                             (if (or _wait-in550_ _wait-out549_)
                                 (let* ((_state556_
                                         (table-ref
                                          _fdtab433_
                                          (std/os/fd#fd-e _sock551_)
                                          '#f))
                                        (_state557566_ _state556_)
                                        (_else559574_ (lambda () '#!void))
                                        (_K561583_
                                         (lambda (_io-out577_ _io-in578_)
                                           (let ((_$e580_ _dir529_))
                                             (if (eq? 'in _$e580_)
                                                 (begin
                                                   (if _io-out577_
                                                       (std/os/kqueue#kqueue-kevent-disable
                                                        _kq434_
                                                        _sock551_
                                                        std/os/kqueue#EVFILT_READ)
                                                       (begin
                                                         (std/os/kqueue#kqueue-kevent-del
                                                          _kq434_
                                                          _sock551_
                                                          std/os/kqueue#EVFILT_READ)
                                                         (table-set!
                                                          _fdtab433_
                                                          (std/os/fd#fd-e
                                                           _sock551_))))
                                                   (##direct-structure-set!
                                                    _ssock528_
                                                    '#f
                                                    '2
                                                    std/net/socket/base#!socket::t
                                                    '#f)
                                                   (##direct-structure-set!
                                                    _state556_
                                                    '#f
                                                    '2
                                                    std/net/socket/base#!socket-state::t
                                                    '#f)
                                                   (_close-io-in!532_
                                                    _io-in578_
                                                    _sock551_)
                                                   (if _io-out577_
                                                       '#!void
                                                       (close-port _sock551_)))
                                                 (if (eq? 'out _$e580_)
                                                     (begin
                                                       (if _io-in578_
                                                           (std/os/kqueue#kqueue-kevent-disable
                                                            _kq434_
                                                            _sock551_
                                                            std/os/kqueue#EVFILT_WRITE)
                                                           (begin
                                                             (std/os/kqueue#kqueue-kevent-del
                                                              _kq434_
                                                              _sock551_
                                                              std/os/kqueue#EVFILT_WRITE)
                                                             (table-set!
                                                              _fdtab433_
                                                              (std/os/fd#fd-e
                                                               _sock551_))))
                                                       (##direct-structure-set!
                                                        _ssock528_
                                                        '#f
                                                        '3
                                                        std/net/socket/base#!socket::t
                                                        '#f)
                                                       (##direct-structure-set!
                                                        _state556_
                                                        '#f
                                                        '3
                                                        std/net/socket/base#!socket-state::t
                                                        '#f)
                                                       (_close-io-out!533_
                                                        _io-out577_
                                                        _sock551_)
                                                       (if _io-in578_
                                                           '#!void
                                                           (close-port
                                                            _sock551_)))
                                                     (if (eq? 'inout _$e580_)
                                                         (begin
                                                           (std/os/kqueue#kevent-set!__%
                                                            '#f
                                                            (std/os/fd#fd-e
                                                             _sock551_)
                                                            std/os/kqueue#EVFILT_READ
                                                            std/os/kqueue#EV_DELETE
                                                            absent-value
                                                            absent-value
                                                            absent-value
                                                            _evts436_
                                                            '0)
                                                           (std/os/kqueue#kevent-set!__%
                                                            '#f
                                                            (std/os/fd#fd-e
                                                             _sock551_)
                                                            std/os/kqueue#EVFILT_WRITE
                                                            std/os/kqueue#EV_DELETE
                                                            absent-value
                                                            absent-value
                                                            absent-value
                                                            _evts436_
                                                            '1)
                                                           (std/os/kqueue#kevent
                                                            _kq434_
                                                            _evts436_
                                                            '2
                                                            '#f
                                                            '0)
                                                           (table-set!
                                                            _fdtab433_
                                                            (std/os/fd#fd-e
                                                             _sock551_))
                                                           (if _io-in578_
                                                               (begin
;;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                         (##direct-structure-set!
                          _ssock528_
                          '#f
                          '2
                          std/net/socket/base#!socket::t
                          '#f)
                         (##direct-structure-set!
                          _state556_
                          '#f
                          '2
                          std/net/socket/base#!socket-state::t
                          '#f)
                         (_close-io-in!532_ _io-in578_ _sock551_))
                       '#!void)
                   (if _io-out577_
                       (begin
                         (##direct-structure-set!
                          _ssock528_
                          '#f
                          '3
                          std/net/socket/base#!socket::t
                          '#f)
                         (##direct-structure-set!
                          _state556_
                          '#f
                          '3
                          std/net/socket/base#!socket-state::t
                          '#f)
                         (_close-io-out!533_ _io-out577_ _sock551_))
                       '#!void)
                   (close-port _sock551_))
                 (error '"Bad direction" _dir529_))))))))
;;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
                                   (if (let ()
                                         (declare (not safe))
                                         (##structure-direct-instance-of?
                                          _state557566_
                                          'std/net/socket/base#!socket-state::t))
                                       (let* ((_e562586_
                                               (let ()
                                                 (declare (not safe))
                                                 (##vector-ref
                                                  _state557566_
                                                  '1)))
                                              (_e563589_
                                               (let ()
                                                 (declare (not safe))
                                                 (##vector-ref
                                                  _state557566_
                                                  '2)))
                                              (_io-in592_ _e563589_)
                                              (_e564594_
                                               (let ()
                                                 (declare (not safe))
                                                 (##vector-ref
                                                  _state557566_
                                                  '3)))
                                              (_io-out597_ _e564594_))
                                         (_K561583_ _io-out597_ _io-in592_))
                                       '#!void))
                                 '#!void))))
                     (if (let ()
                           (declare (not safe))
                           (##structure-direct-instance-of?
                            _ssock534542_
                            'std/net/socket/base#!socket::t))
                         (let* ((_e538602_
                                 (let ()
                                   (declare (not safe))
                                   (##vector-ref _ssock534542_ '1)))
                                (_sock605_ _e538602_)
                                (_e539607_
                                 (let ()
                                   (declare (not safe))
                                   (##vector-ref _ssock534542_ '2)))
                                (_wait-in610_ _e539607_)
                                (_e540612_
                                 (let ()
                                   (declare (not safe))
                                   (##vector-ref _ssock534542_ '3)))
                                (_wait-out615_ _e540612_))
                           (_K537599_ _wait-out615_ _wait-in610_ _sock605_))
                         (_E536546_))))))
              (_shutdown!442_
               (lambda ()
                 (close-port _kq434_)
                 (let ((_iterable502_ (std/iter#in-hash-values _fdtab433_))
                       (_iter-do504_
                        (lambda (_val445_)
                          (let* ((_val447452_ _val445_)
                                 (_E449456_
                                  (lambda ()
                                    (error '"No clause matching" _val447452_)))
                                 (_K450497_
                                  (lambda (_state459_)
                                    (let* ((_state460468_ _state459_)
                                           (_E462472_
                                            (lambda ()
                                              (error '"No clause matching"
                                                     _state460468_)))
                                           (_K463479_
                                            (lambda (_io-out475_
                                                     _io-in476_
                                                     _sock477_)
                                              (if _io-in476_
                                                  (std/net/socket/basic-server#io-state-close-in!
                                                   _io-in476_
                                                   _sock477_
                                                   '#f)
                                                  '#!void)
                                              (if _io-out475_
                                                  (std/net/socket/basic-server#io-state-close-out!
                                                   _io-out475_
                                                   _sock477_
                                                   '#f)
                                                  '#!void)
                                              (close-port _sock477_))))
                                      (if (let ()
                                            (declare (not safe))
                                            (##structure-direct-instance-of?
                                             _state460468_
                                             'std/net/socket/base#!socket-state::t))
                                          (let* ((_e464482_
                                                  (let ()
                                                    (declare (not safe))
                                                    (##vector-ref
                                                     _state460468_
                                                     '1)))
                                                 (_sock485_ _e464482_)
                                                 (_e465487_
                                                  (let ()
                                                    (declare (not safe))
                                                    (##vector-ref
                                                     _state460468_
                                                     '2)))
                                                 (_io-in490_ _e465487_)
                                                 (_e466492_
                                                  (let ()
                                                    (declare (not safe))
                                                    (##vector-ref
                                                     _state460468_
                                                     '3)))
                                                 (_io-out495_ _e466492_))
                                            (_K463479_
                                             _io-out495_
                                             _io-in490_
                                             _sock485_))
                                          (_E462472_)))))
                                 (_state500_ _val447452_))
                            (_K450497_ _state500_)))))
                   (if (pair? _iterable502_)
                       (for-each _iter-do504_ _iterable502_)
                       (if (null? _iterable502_)
                           '#!void
                           (let* ((_it507_ (std/iter#iter _iterable502_))
                                  (_next!510_
                                   (let ()
                                     (declare (not safe))
                                     (##unchecked-structure-ref
                                      _it507_
                                      '2
                                      std/iter#iterator::t
                                      '#f))))
                             (declare (not safe))
                             (begin
                               (let _lp515_ ()
                                 (let ((_val519_ (_next!510_ _it507_)))
                                   (if (eq? std/iter#iter-end _val519_)
                                       '#!void
                                       (begin
                                         (_iter-do504_ _val519_)
                                         (_lp515_)))))
                               (let ((_$e521_ (##unchecked-structure-ref
                                               _it507_
                                               '3
                                               std/iter#iterator::t
                                               '#f)))
                                 (if _$e521_
                                     ((lambda (_g523525_) (_g523525_ _it507_))
                                      _$e521_)
                                     '#!void))
                               '#!void)))))
                 (set! _fdtab433_ '#f)
                 (set! _kq434_ '#f))))
      (std/net/socket/basic-server#server-loop
       (std/os/fd#fd-io-in _kq434_)
       _do-kevent439_
       _add-socket440_
       _close-socket441_
       _shutdown!442_))))
