;; OptimizerV3 in Push3 — 8-input / 4-output redesign
;;
;; Inputs on DYADIC stack (slot 0 on top, slot 7 at bottom):
;;   [0] percentageStaked      (0 to 1e18, where 1e18 = 100%)
;;   [1] averageTaxRate        (0 to 1e18, normalized from Stake contract)
;;   [2] vwapX96               (future; 0 if unavailable)
;;   [3] currentTick           (future; 0 if unavailable)
;;   [4] recentVolume          (future; 0 if unavailable)
;;   [5] timeSinceLastRecenter (future; 0 if unavailable)
;;   [6] movingAveragePrice    (future; 0 if unavailable)
;;   [7] reserved              0
;;
;; Outputs on DYADIC stack at termination (top to bottom):
;;   top: capitalInefficiency (0..1e18)
;;        anchorShare         (0..1e18)
;;        anchorWidth         (tick units)
;;   bot: discoveryDepth      (0..1e18)
;;
;; Logic: binary bear/bull from slots 0 and 1
;;   stakedPct = percentageStaked * 100 / 1e18   (0-100)
;;   if stakedPct <= 91 → BEAR
;;   deltaS = 100 - stakedPct
;;   effIdx = _taxRateToEffectiveIndex(averageTaxRate)  (0-29, with +1 shift at >=14)
;;   penalty = deltaS^3 * effIdx / 20
;;   if penalty < 50 → BULL, else → BEAR
;;
;; BULL params: CI=0, AS=1e18,   AW=20,  DD=1e18
;; BEAR params: CI=0, AS=0.3e18, AW=100, DD=0.3e18

(
  ;; Step 1: Bind slot 0 (top) to PERCENTAGESTAKED, slot 1 to TAXRATE.
  PERCENTAGESTAKED DYADIC.DEFINE
  ;; Stack: [slot7(bot), slot6, slot5, slot4, slot3, slot2, slot1(top)]
  TAXRATE DYADIC.DEFINE
  ;; Stack: [slot7(bot), slot6, slot5, slot4, slot3, slot2(top)]

  ;; Step 2: Discard unused inputs (slots 2-7) — 6 pops.
  DYADIC.POP
  DYADIC.POP
  DYADIC.POP
  DYADIC.POP
  DYADIC.POP
  DYADIC.POP
  ;; Stack: []

  ;; Step 3: Compute stakedPct = percentageStaked * 100 / 1e18  (integer 0..100)
  PERCENTAGESTAKED
  100 DYADIC.*
  1000000000000000000 DYADIC./
  ;; Stack: [stakedPct]
  STAKED DYADIC.DEFINE
  ;; Stack: []

  ;; Step 4: Main conditional — stakedPct > 91?
  STAKED 91 DYADIC.>
  ;; bool_stack: [stakedPct > 91]

  EXEC.IF

  ;; TRUE branch: stakedPct > 91 — compute penalty
  (
    ;; deltaS = 100 - stakedPct
    100 STAKED DYADIC.-
    ;; Stack: [deltaS]
    DELTAS DYADIC.DEFINE
    ;; Stack: []

    ;; Compute raw tax index via 30-way threshold lookup.
    ;; Each level: if TAXRATE <= threshold then push index, else go deeper.
    TAXRATE 206185567010309 DYADIC.<=
    EXEC.IF
      0
      ( TAXRATE 412371134020618 DYADIC.<=
        EXEC.IF
          1
          ( TAXRATE 618556701030927 DYADIC.<=
            EXEC.IF
              2
              ( TAXRATE 1030927835051546 DYADIC.<=
                EXEC.IF
                  3
                  ( TAXRATE 1546391752577319 DYADIC.<=
                    EXEC.IF
                      4
                      ( TAXRATE 2164948453608247 DYADIC.<=
                        EXEC.IF
                          5
                          ( TAXRATE 2783505154639175 DYADIC.<=
                            EXEC.IF
                              6
                              ( TAXRATE 3608247422680412 DYADIC.<=
                                EXEC.IF
                                  7
                                  ( TAXRATE 4639175257731958 DYADIC.<=
                                    EXEC.IF
                                      8
                                      ( TAXRATE 5670103092783505 DYADIC.<=
                                        EXEC.IF
                                          9
                                          ( TAXRATE 7216494845360824 DYADIC.<=
                                            EXEC.IF
                                              10
                                              ( TAXRATE 9278350515463917 DYADIC.<=
                                                EXEC.IF
                                                  11
                                                  ( TAXRATE 11855670103092783 DYADIC.<=
                                                    EXEC.IF
                                                      12
                                                      ( TAXRATE 15979381443298969 DYADIC.<=
                                                        EXEC.IF
                                                          13
                                                          ( TAXRATE 22164948453608247 DYADIC.<=
                                                            EXEC.IF
                                                              14
                                                              ( TAXRATE 29381443298969072 DYADIC.<=
                                                                EXEC.IF
                                                                  15
                                                                  ( TAXRATE 38144329896907216 DYADIC.<=
                                                                    EXEC.IF
                                                                      16
                                                                      ( TAXRATE 49484536082474226 DYADIC.<=
                                                                        EXEC.IF
                                                                          17
                                                                          ( TAXRATE 63917525773195876 DYADIC.<=
                                                                            EXEC.IF
                                                                              18
                                                                              ( TAXRATE 83505154639175257 DYADIC.<=
                                                                                EXEC.IF
                                                                                  19
                                                                                  ( TAXRATE 109278350515463917 DYADIC.<=
                                                                                    EXEC.IF
                                                                                      20
                                                                                      ( TAXRATE 144329896907216494 DYADIC.<=
                                                                                        EXEC.IF
                                                                                          21
                                                                                          ( TAXRATE 185567010309278350 DYADIC.<=
                                                                                            EXEC.IF
                                                                                              22
                                                                                              ( TAXRATE 237113402061855670 DYADIC.<=
                                                                                                EXEC.IF
                                                                                                  23
                                                                                                  ( TAXRATE 309278350515463917 DYADIC.<=
                                                                                                    EXEC.IF
                                                                                                      24
                                                                                                      ( TAXRATE 402061855670103092 DYADIC.<=
                                                                                                        EXEC.IF
                                                                                                          25
                                                                                                          ( TAXRATE 520618556701030927 DYADIC.<=
                                                                                                            EXEC.IF
                                                                                                              26
                                                                                                              ( TAXRATE 680412371134020618 DYADIC.<=
                                                                                                                EXEC.IF
                                                                                                                  27
                                                                                                                  ( TAXRATE 886597938144329896 DYADIC.<=
                                                                                                                    EXEC.IF
                                                                                                                      28
                                                                                                                      29
                                                                                                                  )
                                                                                                              )
                                                                                                          )
                                                                                                      )
                                                                                                  )
                                                                                              )
                                                                                          )
                                                                                      )
                                                                                  )
                                                                              )
                                                                          )
                                                                      )
                                                                  )
                                                              )
                                                          )
                                                      )
                                                  )
                                              )
                                          )
                                      )
                                  )
                              )
                          )
                      )
                  )
              )
          )
      )
    ;; Stack: [raw_idx (0-29)]

    ;; Apply effIdx shift: if raw_idx >= 14, effIdx = min(raw_idx + 1, 29)
    DYADIC.DUP 14 DYADIC.>=
    EXEC.IF
      (
        1 DYADIC.+
        DYADIC.DUP 29 DYADIC.>
        EXEC.IF
          ( DYADIC.POP 29 )
          ( )
      )
      ( )
    ;; Stack: [effIdx (0-29)]

    EFFIDX DYADIC.DEFINE
    ;; Stack: []

    ;; Compute penalty = deltaS^3 * effIdx / 20
    DELTAS DELTAS DYADIC.*
    DELTAS DYADIC.*
    EFFIDX DYADIC.*
    20 DYADIC./
    ;; Stack: [penalty]

    ;; penalty < 50 → BULL; else → BEAR
    50 DYADIC.<
    ;; bool_stack: [penalty < 50]

    EXEC.IF
    ;; BULL: push 4 outputs bottom-first (discoveryDepth at bottom, ci at top)
    (
      1000000000000000000
      20
      1000000000000000000
      0
    )
    ;; BEAR
    (
      300000000000000000
      100
      300000000000000000
      0
    )
  )

  ;; FALSE branch: stakedPct <= 91 — always bear
  (
    300000000000000000
    100
    300000000000000000
    0
  )
)
