 (defn list-code
   ([] (with-loaded list-code))
   ([loaded]
-    (let [program (:program loaded)]
+    (let [{program :program cfr :cfr} loaded]
       (-> program
           get-cfg
           cfg-to-stmt-map
-          (show-stmt-map program)))))
+          (show-stmt-map program cfr)))))
 
 (defn show-state
   ([state-num] (with-loaded show-state state-num))


(defn- edge-flatten
  [edge]
  (let [[from to] edge
        from_lbl  (.getLabel from)
        to_lbl    (.getLabel to)]
    {from_lbl [to_lbl] to_lbl []}))

(defn cfg-to-stmt-map
  [cfg]

  [jumps]
  (if (empty? jumps)
    ""
    (str "-> [" (string/join "," jumps) "]")))

(defn show-abstract-state
  [cfr location]
  (str
   (-> cfr
    .getReachedStates
    (.where location))))

(defn show-stmt-map
  [stmt-map program cfr]
  (->>
   stmt-map
   (into (sorted-map))
   (reduce remove-right-after [])
   reverse
   (map
    (fn [[location jumps]]
      (let [lbl (.getLabel location)]
        (if (zero? (.getIndex lbl))
          (let [address (.getAddress lbl)
                instruction (.getInstruction program address)]
            (-> program
                (.getInstructionString address instruction)
                println)))
        (println
         (str
          lbl
          (string/join
           " "
           [
            (.getStatement program lbl)
            (show-jumps jumps)
            (show-abstract-state cfr location)]))))))
   dorun)
  nil)

