;; Fixture app for the cgen single-binary build test (jolt-a7ds).
;; count-point is a numeric leaf -> compiled to native C and statically linked;
;; run/-main stay bytecode and call into it. -main prints a deterministic total.
(ns cgapp)

(defn count-point [cr ci cap]
  (loop [i 0 zr 0.0 zi 0.0]
    (if (or (>= i cap) (> (+ (* zr zr) (* zi zi)) 4.0))
      i
      (recur (inc i)
             (+ (- (* zr zr) (* zi zi)) cr)
             (+ (* 2.0 (* zr zi)) ci)))))

(defn run [n]
  (let [cap 200
        nd (* 1.0 n)]
    (loop [y 0 acc 0]
      (if (< y n)
        (let [ci (- (/ (* 2.0 y) nd) 1.0)
              row (loop [x 0 a 0]
                    (if (< x n)
                      (let [cr (- (/ (* 2.0 x) nd) 1.5)]
                        (recur (inc x) (+ a (count-point cr ci cap))))
                      a))]
          (recur (inc y) (+ acc row)))
        acc))))

(defn -main [& args]
  (let [n (if (seq args) (Integer/parseInt (first args)) 200)]
    (println (run n))))
