(declare (block) (standard-bindings) (extended-bindings))
(begin
  (define gerbil/tools/gxprof#main
    (lambda _args490_
      (let* ((_args491499_ _args490_)
             (_else493507_
              (lambda () (displayln '"Usage: gxprof exe-module-path arg ...")))
             (_K495520_
              (lambda (_args510_ _mod511_)
                (_gx#load-expander!)
                (let* ((_ctx513_
                        (gx#import-module__%
                         (gerbil/tools/gxprof#module-path _mod511_)
                         '#f
                         '#t))
                       (_main-id515_
                        (gerbil/tools/gxprof#find-runtime-symbol
                         _ctx513_
                         'main))
                       (_main-fn517_ (eval _main-id515_)))
                  (gerbil/tools/gxprof#profile _main-fn517_ _args510_)))))
        (if (let () (declare (not safe)) (##pair? _args491499_))
            (let ((_hd496523_
                   (let () (declare (not safe)) (##car _args491499_)))
                  (_tl497525_
                   (let () (declare (not safe)) (##cdr _args491499_))))
              (let* ((_mod528_ _hd496523_) (_args530_ _tl497525_))
                (_K495520_ _args530_ _mod528_)))
            (_else493507_)))))
  (define gerbil/tools/gxprof#module-path
    (lambda (_str488_)
      (if (and (> (string-length _str488_) '0)
               (eq? (string-ref _str488_ '0) '#\:))
          (string->symbol _str488_)
          _str488_)))
  (define gerbil/tools/gxprof#find-export-binding
    (lambda (_ctx438_ _id439_)
      (let ((_$e485_ (find (lambda (_e440442_)
                             (let* ((_g444454_ _e440442_)
                                    (_else446462_ (lambda () '#f))
                                    (_K448466_ (lambda () '#t)))
                               (if (let ()
                                     (declare (not safe))
                                     (##structure-direct-instance-of?
                                      _g444454_
                                      'gx#module-export::t))
                                   (let* ((_e449469_
                                           (let ()
                                             (declare (not safe))
                                             (##vector-ref _g444454_ '1)))
                                          (_e450472_
                                           (let ()
                                             (declare (not safe))
                                             (##vector-ref _g444454_ '2)))
                                          (_e451475_
                                           (let ()
                                             (declare (not safe))
                                             (##vector-ref _g444454_ '3))))
                                     (if (let ()
                                           (declare (not safe))
                                           (##eq? _e451475_ '0))
                                         (let ((_e452478_
                                                (let ()
                                                  (declare (not safe))
                                                  (##vector-ref
                                                   _g444454_
                                                   '4))))
                                           (if ((lambda (_g480482_)
                                                  (eq? _g480482_ _id439_))
                                                _e452478_)
                                               (_K448466_)
                                               (_else446462_)))
                                         (_else446462_)))
                                   (_else446462_))))
                           (##structure-ref
                            _ctx438_
                            '9
                            gx#module-context::t
                            '#f))))
        (if _$e485_ (gx#core-resolve-module-export _$e485_) '#f))))
  (define gerbil/tools/gxprof#find-runtime-symbol
    (lambda (_ctx430_ _id431_)
      (let ((_$e433_ (gerbil/tools/gxprof#find-export-binding
                      _ctx430_
                      _id431_)))
        (if _$e433_
            ((lambda (_bind436_)
               (if (let ()
                     (declare (not safe))
                     (##structure-instance-of?
                      _bind436_
                      'gx#runtime-binding::t))
                   '#!void
                   (error '"export is not a runtime binding" _id431_))
               (##structure-ref _bind436_ '1 gx#binding::t '#f))
             _$e433_)
            (error '"module does not export symbol"
                   (##structure-ref _ctx430_ '1 gx#expander-context::t '#f)
                   _id431_)))))
  (define gerbil/tools/gxprof#profile
    (lambda (_fun420_ _args421_)
      (with-unwind-protect
       (lambda ()
         (dynamic-wind
          gerbil/tools/gxprof#profile-start!
          (lambda () (apply _fun420_ _args421_))
          gerbil/tools/gxprof#profile-end!))
       (lambda ()
         (call-with-output-file
          '"gxprof.out"
          (lambda (_g425427_)
            (gerbil/tools/gxprof#write-samples
             gerbil/tools/gxprof#samples
             _g425427_)))))))
  (define gerbil/tools/gxprof#samples '())
  (define gerbil/tools/gxprof#profile-start!
    (lambda ()
      (let ((__tmp4719 (exact->inexact '1/1000)))
        (declare (not safe))
        (##set-heartbeat-interval! __tmp4719))
      (let ()
        (declare (not safe))
        (##interrupt-vector-set! '2 gerbil/tools/gxprof#profile-heartbeat!))))
  (define gerbil/tools/gxprof#profile-end!
    (lambda ()
      (let ((__tmp4720 (exact->inexact '1/100)))
        (declare (not safe))
        (##set-heartbeat-interval! __tmp4720))
      (let ()
        (declare (not safe))
        (##interrupt-vector-set! '2 ##thread-heartbeat!))))
  (define gerbil/tools/gxprof#profile-heartbeat!
    (lambda ()
      (let ((__tmp4721
             (lambda (_cont416_)
               (let () (declare (not safe)) (##thread-heartbeat!))
               (set! gerbil/tools/gxprof#samples
                     (cons (gerbil/tools/gxprof#continuation->sample _cont416_)
                           gerbil/tools/gxprof#samples)))))
        (declare (not safe))
        (##continuation-capture __tmp4721))))
  (define gerbil/tools/gxprof#continuation->sample
    (lambda (_cont406_)
      (let _lp408_ ((_cont410_
                     (let ()
                       (declare (not safe))
                       (##continuation-next-frame _cont406_ '#t)))
                    (_bt411_ '()))
        (if _cont410_
            (let ((_trace413_
                   (let ()
                     (declare (not safe))
                     (##continuation-creator _cont410_))))
              (_lp408_ (let ()
                         (declare (not safe))
                         (##continuation-next-frame _cont410_ '#t))
                       (cons _trace413_ _bt411_)))
            (reverse _bt411_)))))
  (define gerbil/tools/gxprof#write-samples
    (lambda (_samples389_ _port390_)
      (letrec* ((_traces392_ (make-table 'test: eq?))
                (_trace->name393_
                 (lambda (_trace399_)
                   (let ((_$e401_ (table-ref _traces392_ _trace399_ '#f)))
                     (if _$e401_
                         (values _$e401_)
                         (let ((_name404_
                                (let ()
                                  (declare (not safe))
                                  (##procedure-name _trace399_))))
                           (table-set! _traces392_ _trace399_ _name404_)
                           _name404_))))))
        (let ((_samples/names397_
               (map (lambda (_bt395_) (map _trace->name393_ _bt395_))
                    _samples389_)))
          (write _samples/names397_ _port390_))))))
