(declare (block) (standard-bindings) (extended-bindings))
(begin
  (define gerbil/tools/gxprof#main
    (lambda _args177_
      (let* ((_args178186_ _args177_)
             (_else180194_
              (lambda () (displayln '"Usage: gxprof exe-module-path arg ...")))
             (_K182207_
              (lambda (_args197_ _mod198_)
                (_gx#load-expander!)
                (let* ((_ctx200_
                        (gx#import-module__%
                         (gerbil/tools/gxprof#module-path _mod198_)
                         '#f
                         '#t))
                       (_main-id202_
                        (gerbil/tools/gxprof#find-runtime-symbol
                         _ctx200_
                         'main))
                       (_main-fn204_ (eval _main-id202_)))
                  (gerbil/tools/gxprof#profile _main-fn204_ _args197_)))))
        (if (##pair? _args178186_)
            (let ((_hd183210_ (##car _args178186_))
                  (_tl184212_ (##cdr _args178186_)))
              (let* ((_mod215_ _hd183210_) (_args217_ _tl184212_))
                (_K182207_ _args217_ _mod215_)))
            (_else180194_)))))
  (define gerbil/tools/gxprof#module-path
    (lambda (_str175_)
      (if (and (> (string-length _str175_) '0)
               (eq? (string-ref _str175_ '0) '#\:))
          (string->symbol _str175_)
          _str175_)))
  (define gerbil/tools/gxprof#find-export-binding
    (lambda (_ctx125_ _id126_)
      (let ((_$e172_ (find (lambda (_e127129_)
                             (let* ((_g131141_ _e127129_)
                                    (_else133149_ (lambda () '#f))
                                    (_K135153_ (lambda () '#t)))
                               (if (##structure-direct-instance-of?
                                    _g131141_
                                    'gx#module-export::t)
                                   (let* ((_e136156_
                                           (##vector-ref _g131141_ '1))
                                          (_e137159_
                                           (##vector-ref _g131141_ '2))
                                          (_e138162_
                                           (##vector-ref _g131141_ '3)))
                                     (if (##eq? _e138162_ '0)
                                         (let ((_e139165_
                                                (##vector-ref _g131141_ '4)))
                                           (if ((lambda (_g167169_)
                                                  (eq? _g167169_ _id126_))
                                                _e139165_)
                                               (_K135153_)
                                               (_else133149_)))
                                         (_else133149_)))
                                   (_else133149_))))
                           (##structure-ref
                            _ctx125_
                            '9
                            gx#module-context::t
                            '#f))))
        (if _$e172_ (gx#core-resolve-module-export _$e172_) '#f))))
  (define gerbil/tools/gxprof#find-runtime-symbol
    (lambda (_ctx117_ _id118_)
      (let ((_$e120_ (gerbil/tools/gxprof#find-export-binding
                      _ctx117_
                      _id118_)))
        (if _$e120_
            ((lambda (_bind123_)
               (if (##structure-instance-of? _bind123_ 'gx#runtime-binding::t)
                   '#!void
                   (error '"export is not a runtime binding" _id118_))
               (##structure-ref _bind123_ '1 gx#binding::t '#f))
             _$e120_)
            (error '"module does not export symbol"
                   (##structure-ref _ctx117_ '1 gx#expander-context::t '#f)
                   _id118_)))))
  (define gerbil/tools/gxprof#profile
    (lambda (_fun107_ _args108_)
      (with-unwind-protect
       (lambda ()
         (dynamic-wind
          gerbil/tools/gxprof#profile-start!
          (lambda () (apply _fun107_ _args108_))
          gerbil/tools/gxprof#profile-end!))
       (lambda ()
         (call-with-output-file
          '"gxprof.out"
          (lambda (_g112114_)
            (write gerbil/tools/gxprof#samples _g112114_)))))))
  (define gerbil/tools/gxprof#samples '())
  (define gerbil/tools/gxprof#profile-start!
    (lambda ()
      (##set-heartbeat-interval! (exact->inexact '1/1000))
      (##interrupt-vector-set! '2 gerbil/tools/gxprof#profile-heartbeat!)))
  (define gerbil/tools/gxprof#profile-end!
    (lambda ()
      (##set-heartbeat-interval! (exact->inexact '1/100))
      (##interrupt-vector-set! '2 ##thread-heartbeat!)))
  (define gerbil/tools/gxprof#profile-heartbeat!
    (lambda ()
      (##continuation-capture
       (lambda (_cont103_)
         (##thread-heartbeat!)
         (set! gerbil/tools/gxprof#samples
               (cons (gerbil/tools/gxprof#continuation->sample _cont103_)
                     gerbil/tools/gxprof#samples))))))
  (define gerbil/tools/gxprof#continuation->sample
    (lambda (_cont93_)
      (let _lp95_ ((_cont97_ (##continuation-next-frame _cont93_ '#t))
                   (_bt98_ '()))
        (if _cont97_
            (let ((_trace100_
                   (##procedure-name (##continuation-creator _cont97_))))
              (_lp95_ (##continuation-next-frame _cont97_ '#t)
                      (cons _trace100_ _bt98_)))
            (reverse _bt98_))))))
