noncomputable def powMod (a b n : Nat) : Nat :=
  aux (b + 1) (a.mod n) b 1
where
  aux : Nat → ((a b c : Nat) → Nat) :=
    Nat.rec (fun _ _ _ => 0)
      (fun _ r a b c =>
        (b.beq 0).rec
          (((b.mod 2).beq 0).rec
            (r ((a.mul a).mod n) (b.div 2) ((a.mul c).mod n))
            (r ((a.mul a).mod n) (b.div 2) c))
          (c.mod n))

def x : Nat := 127780414391497973212171930170926986757577048484820926201064729783485263494817422495127775983679039078116803697168137524940219819335799478153348592755198599590903607242050230924443865709697486743641039970666450337071378658828331722728467720393963808366917988956767802913905167890490075236068196363700359481304279948916896583006686025357237170212018946813663108217900835975808683160984117514866915965161953626338070145596982334808959718966160701183250747572515090867613655044807172211728519357721287835503689517292364425608325467094686443862517374850243698013720305871319056887431952190952721719757200172695537054790570648290887720009455171821568413052107356003828041937567129362866696549587422369864562815134637684140271767482353107080370450890024342225936273158281477009232714640818424893445193089479459814572594522258577931514012256573162006292678354475638319009668319255772179069845291474717503333030909793536116894869761453687330048252587304656806182949368202671739705463406846852567720022377005763291104588535681445561286808586673846016527511475331939430139687698419185010117348285933672139833826832898565919546377321517928825162277951756632134321102813522053716838646284289

set_option maxRecDepth 20000

/--
error: Tactic `decide` failed. The elaborator is able to reduce the `Decidable` instance, but the kernel fails with:

  (kernel) deep recursion detected
-/
#guard_msgs in
example : powMod 2 (x - 1) x = 1 := by decide +kernel

/-- error: (kernel) deep recursion detected -/
#guard_msgs in
example : powMod 2 (x - 1) x = 1 := by decide

/-- error: (kernel) deep recursion detected -/
#guard_msgs in
example : powMod 2 (x - 1) x = 1 := by rfl

set_option debug.skipKernelTC true

#guard_msgs in
example : powMod 2 (x - 1) x = 1 := by decide

#guard_msgs in
example : powMod 2 (x - 1) x = 1 := by rfl
