(val _ = Construct
  (sqbkt ("Zreg"
           (sqbkt ("RAX" []) ("RCX" []) ("RDX" []) ("RBX" []) ("RSP" [])
                  ("RBP" []) ("RSI" []) ("RDI" []) ("zR8" []) ("zR9" [])
                  ("zR10" []) ("zR11" []) ("zR12" []) ("zR13" [])
                  ("zR14" []) ("zR15" [])))))


(val _ = Construct
  (sqbkt ("Zeflags"
           (sqbkt ("Z_CF" []) ("Z_PF" []) ("Z_AF" []) ("Z_ZF" [])
                  ("Z_SF" []) ("Z_OF" [])))))


(val _ = Construct
  (sqbkt ("Zsize"
           (sqbkt ("Z16" []) ("Z32" []) ("Z64" []) ("Z8" (sqbkt bTy))))))


(val _ = Construct
  (sqbkt ("Zbase"
           (sqbkt ("ZnoBase" []) ("ZregBase" (sqbkt CTy"Zreg"))
                  ("ZripBase" [])))))


(val _ = Construct
  (sqbkt ("Zrm"
           (sqbkt ("Zm"
                    (sqbkt (PTy (OTy (PTy (FTy 2) CTy"Zreg"))
                             (PTy CTy"Zbase" F64))))
                  ("Zr" (sqbkt CTy"Zreg"))))))


(val _ = Construct
  (sqbkt ("Zdest_src"
           (sqbkt ("Zr_rm" (sqbkt (PTy CTy"Zreg" CTy"Zrm")))
                  ("Zrm_i" (sqbkt (PTy CTy"Zrm" F64)))
                  ("Zrm_r" (sqbkt (PTy CTy"Zrm" CTy"Zreg")))))))


(val _ = Construct
  (sqbkt ("Zimm_rm" (sqbkt ("Zimm" (sqbkt F64)) ("Zrm" (sqbkt CTy"Zrm"))))))


(val _ = Construct
  (sqbkt ("Zmonop_name"
           (sqbkt ("Zdec" []) ("Zinc" []) ("Znot" []) ("Zneg" [])))))


(val _ = Construct
  (sqbkt ("Zbinop_name"
           (sqbkt ("Zadd" []) ("Zor" []) ("Zadc" []) ("Zsbb" [])
                  ("Zand" []) ("Zsub" []) ("Zxor" []) ("Zcmp" [])
                  ("Zrol" []) ("Zror" []) ("Zrcl" []) ("Zrcr" [])
                  ("Zshl" []) ("Zshr" []) ("Ztest" []) ("Zsar" [])))))


(val _ = Construct
  (sqbkt ("Zcond"
           (sqbkt ("Z_O" []) ("Z_NO" []) ("Z_B" []) ("Z_NB" []) ("Z_E" [])
                  ("Z_NE" []) ("Z_NA" []) ("Z_A" []) ("Z_S" [])
                  ("Z_NS" []) ("Z_P" []) ("Z_NP" []) ("Z_L" [])
                  ("Z_NL" []) ("Z_NG" []) ("Z_G" []) ("Z_ALWAYS" [])))))


(val _ = Construct
  (sqbkt ("Zea"
           (sqbkt ("Zea_i" (sqbkt (PTy CTy"Zsize" F64)))
                  ("Zea_m" (sqbkt (PTy CTy"Zsize" F64)))
                  ("Zea_r" (sqbkt (PTy CTy"Zsize" CTy"Zreg")))))))


(val _ = Construct
  (sqbkt ("instruction"
           (sqbkt ("Zbinop"
                    (sqbkt (PTy CTy"Zbinop_name"
                             (PTy CTy"Zsize" CTy"Zdest_src"))))
                  ("Zcall" (sqbkt CTy"Zimm_rm"))
                  ("Zcmpxchg"
                    (sqbkt (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))
                  ("Zcpuid" []) ("Zdiv" (sqbkt (PTy CTy"Zsize" CTy"Zrm")))
                  ("Zjcc" (sqbkt (PTy CTy"Zcond" F64)))
                  ("Zjmp" (sqbkt CTy"Zrm"))
                  ("Zlea" (sqbkt (PTy CTy"Zsize" CTy"Zdest_src")))
                  ("Zloop" (sqbkt (PTy CTy"Zcond" F64)))
                  ("Zmonop"
                    (sqbkt (PTy CTy"Zmonop_name"
                             (PTy CTy"Zsize" CTy"Zrm"))))
                  ("Zmov"
                    (sqbkt (PTy CTy"Zcond"
                             (PTy CTy"Zsize" CTy"Zdest_src"))))
                  ("Zmovzx"
                    (sqbkt (PTy CTy"Zsize"
                             (PTy CTy"Zdest_src" CTy"Zsize"))))
                  ("Zmul" (sqbkt (PTy CTy"Zsize" CTy"Zrm"))) ("Znop" [])
                  ("Zpop" (sqbkt CTy"Zrm")) ("Zpush" (sqbkt CTy"Zimm_rm"))
                  ("Zret" (sqbkt F64))
                  ("Zxadd"
                    (sqbkt (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))
                  ("Zxchg"
                    (sqbkt (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))))))


(val _ = Construct
  (sqbkt ("Zinst"
           (sqbkt ("Zdec_fail" (sqbkt sTy))
                  ("Zfull_inst"
                    (sqbkt (PTy (LTy F8) (PTy CTy"instruction" (LTy F8)))))))))


(val _ = Record ("REX" (sqbkt ("B" bTy) ("R" bTy) ("W" bTy) ("X" bTy))))


(val _ = Construct
  (sqbkt ("exception"
           (sqbkt ("BadFlagAccess" (sqbkt sTy))
                  ("BadMemAccess" (sqbkt F64)) ("FAIL" (sqbkt sTy))
                  ("NoException" [])))))


(val _ = Record
  ("state"
    (sqbkt ("EFLAGS" (ATy CTy"Zeflags" (OTy bTy)))
           ("ICACHE" (ATy F64 (OTy F8))) ("MEM" (ATy F64 (OTy F8)))
           ("REG" (ATy CTy"Zreg" F64)) ("RIP" F64)
           ("exception" CTy"exception"))))


(Def "raise'exception" (Var "e" CTy"exception")
  (Close qVar"state"
    (TP
      (sqbkt (LX VTy"a")
             (ITE
               (EQ (Dest "exception" CTy"exception" qVar"state")
                 (Const "NoException" CTy"exception"))
               (Rupd "exception"
                 (TP (sqbkt qVar"state" (Var "e" CTy"exception"))))
               qVar"state")))))


(Def "write'mem8" (TP (sqbkt (Var "b" F8) (Var "addr" F64)))
  (Close qVar"state"
    (ITE
      (Mop IsSome
        (Apply (Dest "MEM" (ATy F64 (OTy F8)) qVar"state")
          (Var "addr" F64)))
      (TP
        (sqbkt LU
               (Rupd "MEM"
                 (TP
                   (sqbkt qVar"state"
                          (Fupd
                            (Dest "MEM" (ATy F64 (OTy F8)) qVar"state")
                            (Var "addr" F64) (Mop Some (Var "b" F8))))))))
      (Apply
        (Call "raise'exception" (ATy qTy (PTy uTy qTy))
          (Call "BadMemAccess" CTy"exception" (Var "addr" F64)))
        qVar"state"))))


(Def "mem8" (Var "addr" F64)
  (Close qVar"state"
    (CS
      (Apply (Dest "MEM" (ATy F64 (OTy F8)) qVar"state") (Var "addr" F64))
      (sqbkt ((Mop Some (Var "b" F8))
               (TP (sqbkt (Var "b" F8) qVar"state")))
             ((LO F8)
               (Apply
                 (Call "raise'exception" (ATy qTy (PTy F8 qTy))
                   (Call "BadMemAccess" CTy"exception" (Var "addr" F64)))
                 qVar"state"))))))


(Def "write'mem16" (TP (sqbkt (Var "w" F16) (Var "addr" F64)))
  (Close qVar"state"
    (Apply
      (Call "write'mem8" (ATy qTy (PTy uTy qTy))
        (TP
          (sqbkt (EX (Var "w" F16) (LN 15) (LN 8) F8)
                 (Bop Add (Var "addr" F64) (LW 1 64)))))
      (Mop Snd
        (Apply
          (Call "write'mem8" (ATy qTy (PTy uTy qTy))
            (TP
              (sqbkt (EX (Var "w" F16) (LN 7) (LN 0) F8) (Var "addr" F64))))
          qVar"state")))))


(Def "mem16" (Var "addr" F64)
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v" F8) qVar"s"))
      (Apply
        (Call "mem8" (ATy qTy (PTy F8 qTy))
          (Bop Add (Var "addr" F64) (LW 1 64))) qVar"state")
      (Let (TP (sqbkt (Var "v0" F8) qVar"s"))
        (Apply (Call "mem8" (ATy qTy (PTy F8 qTy)) (Var "addr" F64))
          qVar"s")
        (TP (sqbkt CC(sqbkt (Var "v" F8) (Var "v0" F8)) qVar"s"))))))


(Def "write'mem32" (TP (sqbkt (Var "w" F32) (Var "addr" F64)))
  (Close qVar"state"
    (Apply
      (Call "write'mem16" (ATy qTy (PTy uTy qTy))
        (TP
          (sqbkt (EX (Var "w" F32) (LN 31) (LN 16) F16)
                 (Bop Add (Var "addr" F64) (LW 2 64)))))
      (Mop Snd
        (Apply
          (Call "write'mem16" (ATy qTy (PTy uTy qTy))
            (TP
              (sqbkt (EX (Var "w" F32) (LN 15) (LN 0) F16)
                     (Var "addr" F64)))) qVar"state")))))


(Def "mem32" (Var "addr" F64)
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v" F16) qVar"s"))
      (Apply
        (Call "mem16" (ATy qTy (PTy F16 qTy))
          (Bop Add (Var "addr" F64) (LW 2 64))) qVar"state")
      (Let (TP (sqbkt (Var "v0" F16) qVar"s"))
        (Apply (Call "mem16" (ATy qTy (PTy F16 qTy)) (Var "addr" F64))
          qVar"s")
        (TP (sqbkt CC(sqbkt (Var "v" F16) (Var "v0" F16)) qVar"s"))))))


(Def "write'mem64" (TP (sqbkt (Var "w" F64) (Var "addr" F64)))
  (Close qVar"state"
    (Apply
      (Call "write'mem32" (ATy qTy (PTy uTy qTy))
        (TP
          (sqbkt (EX (Var "w" F64) (LN 63) (LN 32) F32)
                 (Bop Add (Var "addr" F64) (LW 4 64)))))
      (Mop Snd
        (Apply
          (Call "write'mem32" (ATy qTy (PTy uTy qTy))
            (TP
              (sqbkt (EX (Var "w" F64) (LN 31) (LN 0) F32)
                     (Var "addr" F64)))) qVar"state")))))


(Def "mem64" (Var "addr" F64)
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v" F32) qVar"s"))
      (Apply
        (Call "mem32" (ATy qTy (PTy F32 qTy))
          (Bop Add (Var "addr" F64) (LW 4 64))) qVar"state")
      (Let (TP (sqbkt (Var "v0" F32) qVar"s"))
        (Apply (Call "mem32" (ATy qTy (PTy F32 qTy)) (Var "addr" F64))
          qVar"s")
        (TP (sqbkt CC(sqbkt (Var "v" F32) (Var "v0" F32)) qVar"s"))))))


(Def "Eflag" (Var "flag" CTy"Zeflags")
  (Close qVar"state"
    (CS
      (Apply (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy)) qVar"state")
        (Var "flag" CTy"Zeflags"))
      (sqbkt ((Mop Some bVar"b") (TP (sqbkt bVar"b" qVar"state")))
             ((LO bTy)
               (Apply
                 (Call "raise'exception" (ATy qTy (PTy bTy qTy))
                   (Call "BadFlagAccess" CTy"exception"
                     (Mop (Cast sTy) (Var "flag" CTy"Zeflags"))))
                 qVar"state"))))))


(Def "write'Eflag" (TP (sqbkt bVar"b" (Var "flag" CTy"Zeflags")))
  (Close qVar"state"
    (TP
      (sqbkt LU
             (Rupd "EFLAGS"
               (TP
                 (sqbkt qVar"state"
                        (Fupd
                          (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                            qVar"state") (Var "flag" CTy"Zeflags")
                          (Mop Some bVar"b")))))))))


(Def "FlagUnspecified" (Var "flag" CTy"Zeflags")
  (Close qVar"state"
    (TP
      (sqbkt LU
             (Rupd "EFLAGS"
               (TP
                 (sqbkt qVar"state"
                        (Fupd
                          (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                            qVar"state") (Var "flag" CTy"Zeflags")
                          (LO bTy)))))))))


(Def "CF" qVar"state"
  (Apply (Call "Eflag" (ATy qTy (PTy bTy qTy)) (LC "Z_CF" CTy"Zeflags"))
    qVar"state"))


(Def "write'CF" bVar"b"
  (Close qVar"state"
    (Apply
      (Call "write'Eflag" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt bVar"b" (LC "Z_CF" CTy"Zeflags")))) qVar"state")))


(Def "PF" qVar"state"
  (Apply (Call "Eflag" (ATy qTy (PTy bTy qTy)) (LC "Z_PF" CTy"Zeflags"))
    qVar"state"))


(Def "write'PF" bVar"b"
  (Close qVar"state"
    (Apply
      (Call "write'Eflag" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt bVar"b" (LC "Z_PF" CTy"Zeflags")))) qVar"state")))


(Def "write'AF" bVar"b"
  (Close qVar"state"
    (Apply
      (Call "write'Eflag" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt bVar"b" (LC "Z_AF" CTy"Zeflags")))) qVar"state")))


(Def "AF" qVar"state"
  (Apply (Call "Eflag" (ATy qTy (PTy bTy qTy)) (LC "Z_AF" CTy"Zeflags"))
    qVar"state"))


(Def "write'ZF" bVar"b"
  (Close qVar"state"
    (Apply
      (Call "write'Eflag" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt bVar"b" (LC "Z_ZF" CTy"Zeflags")))) qVar"state")))


(Def "ZF" qVar"state"
  (Apply (Call "Eflag" (ATy qTy (PTy bTy qTy)) (LC "Z_ZF" CTy"Zeflags"))
    qVar"state"))


(Def "write'SF" bVar"b"
  (Close qVar"state"
    (Apply
      (Call "write'Eflag" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt bVar"b" (LC "Z_SF" CTy"Zeflags")))) qVar"state")))


(Def "SF" qVar"state"
  (Apply (Call "Eflag" (ATy qTy (PTy bTy qTy)) (LC "Z_SF" CTy"Zeflags"))
    qVar"state"))


(Def "write'OF" bVar"b"
  (Close qVar"state"
    (Apply
      (Call "write'Eflag" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt bVar"b" (LC "Z_OF" CTy"Zeflags")))) qVar"state")))


(Def "OF" qVar"state"
  (Apply (Call "Eflag" (ATy qTy (PTy bTy qTy)) (LC "Z_OF" CTy"Zeflags"))
    qVar"state"))


(Def "ea_index" (Var "index" (OTy (PTy (FTy 2) CTy"Zreg")))
  (Close qVar"state"
    (CS (Var "index" (OTy (PTy (FTy 2) CTy"Zreg")))
      (sqbkt ((LO (PTy (FTy 2) CTy"Zreg"))
               (TP (sqbkt (LW 0 64) qVar"state")))
             ((Mop Some
                 (TP (sqbkt (Var "scale" (FTy 2)) (Var "idx" CTy"Zreg"))))
               (TP
                 (sqbkt (Bop Mul
                          (Bop Lsl (LW 1 64)
                            (Mop (Cast nTy) (Var "scale" (FTy 2))))
                          (Apply
                            (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                            (Var "idx" CTy"Zreg"))) qVar"state")))))))


(Def "ea_base" (Var "base" CTy"Zbase")
  (Close qVar"state"
    (CS (Var "base" CTy"Zbase")
      (sqbkt ((Const "ZnoBase" CTy"Zbase")
               (TP (sqbkt (LW 0 64) qVar"state")))
             ((Const "ZripBase" CTy"Zbase")
               (TP (sqbkt (Dest "RIP" F64 qVar"state") qVar"state")))
             ((Call "ZregBase" CTy"Zbase" (Var "b" CTy"Zreg"))
               (TP
                 (sqbkt (Apply
                          (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                          (Var "b" CTy"Zreg")) qVar"state")))))))


(Def "ea_Zrm" (TP (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm")))
  (Close qVar"state"
    (CS (Var "rm" CTy"Zrm")
      (sqbkt ((Call "Zr" CTy"Zrm" (Var "r" CTy"Zreg"))
               (TP
                 (sqbkt (Call "Zea_r" CTy"Zea"
                          (TP
                            (sqbkt (Var "size" CTy"Zsize")
                                   (Var "r" CTy"Zreg")))) qVar"state")))
             ((Call "Zm" CTy"Zrm"
                 (TP
                   (sqbkt (Var "index" (OTy (PTy (FTy 2) CTy"Zreg")))
                          (Var "base" CTy"Zbase") (Var "displacement" F64))))
               (TP
                 (sqbkt (Call "Zea_m" CTy"Zea"
                          (TP
                            (sqbkt (Var "size" CTy"Zsize")
                                   (Bop Add
                                     (Bop Add
                                       (Mop Fst
                                         (Apply
                                           (Call "ea_index"
                                             (ATy qTy (PTy F64 qTy))
                                             (Var "index"
                                               (OTy
                                                 (PTy (FTy 2) CTy"Zreg"))))
                                           qVar"state"))
                                       (Mop Fst
                                         (Apply
                                           (Call "ea_base"
                                             (ATy qTy (PTy F64 qTy))
                                             (Var "base" CTy"Zbase"))
                                           qVar"state")))
                                     (Var "displacement" F64)))))
                        qVar"state")))))))


(Def "ea_Zdest"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "ds" CTy"Zdest_src")))
  (Close qVar"state"
    (CS (Var "ds" CTy"Zdest_src")
      (sqbkt ((Call "Zrm_i" CTy"Zdest_src"
                 (TP (sqbkt (Var "rm" CTy"Zrm") (AVar F64))))
               (TP
                 (sqbkt (Mop Fst
                          (Apply
                            (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
                              (TP
                                (sqbkt (Var "size" CTy"Zsize")
                                       (Var "rm" CTy"Zrm")))) qVar"state"))
                        qVar"state")))
             ((Call "Zrm_r" CTy"Zdest_src"
                 (TP (sqbkt (Var "rm" CTy"Zrm") (AVar CTy"Zreg"))))
               (TP
                 (sqbkt (Mop Fst
                          (Apply
                            (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
                              (TP
                                (sqbkt (Var "size" CTy"Zsize")
                                       (Var "rm" CTy"Zrm")))) qVar"state"))
                        qVar"state")))
             ((Call "Zr_rm" CTy"Zdest_src"
                 (TP (sqbkt (Var "r" CTy"Zreg") (AVar CTy"Zrm"))))
               (TP
                 (sqbkt (Call "Zea_r" CTy"Zea"
                          (TP
                            (sqbkt (Var "size" CTy"Zsize")
                                   (Var "r" CTy"Zreg")))) qVar"state")))))))


(Def "ea_Zsrc"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "ds" CTy"Zdest_src")))
  (Close qVar"state"
    (CS (Var "ds" CTy"Zdest_src")
      (sqbkt ((Call "Zrm_i" CTy"Zdest_src"
                 (TP (sqbkt (AVar CTy"Zrm") (Var "i" F64))))
               (TP
                 (sqbkt (Call "Zea_i" CTy"Zea"
                          (TP
                            (sqbkt (Var "size" CTy"Zsize") (Var "i" F64))))
                        qVar"state")))
             ((Call "Zrm_r" CTy"Zdest_src"
                 (TP (sqbkt (AVar CTy"Zrm") (Var "r" CTy"Zreg"))))
               (TP
                 (sqbkt (Call "Zea_r" CTy"Zea"
                          (TP
                            (sqbkt (Var "size" CTy"Zsize")
                                   (Var "r" CTy"Zreg")))) qVar"state")))
             ((Call "Zr_rm" CTy"Zdest_src"
                 (TP (sqbkt (AVar CTy"Zreg") (Var "rm" CTy"Zrm"))))
               (TP
                 (sqbkt (Mop Fst
                          (Apply
                            (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
                              (TP
                                (sqbkt (Var "size" CTy"Zsize")
                                       (Var "rm" CTy"Zrm")))) qVar"state"))
                        qVar"state")))))))


(Def "ea_Zimm_rm"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "imm_rm" CTy"Zimm_rm")))
  (Close qVar"state"
    (CS (Var "imm_rm" CTy"Zimm_rm")
      (sqbkt ((Call "Zrm" CTy"Zimm_rm" (Var "rm" CTy"Zrm"))
               (TP
                 (sqbkt (Mop Fst
                          (Apply
                            (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
                              (TP
                                (sqbkt (Var "size" CTy"Zsize")
                                       (Var "rm" CTy"Zrm")))) qVar"state"))
                        qVar"state")))
             ((Call "Zimm" CTy"Zimm_rm" (Var "imm" F64))
               (TP
                 (sqbkt (Call "Zea_i" CTy"Zea"
                          (TP
                            (sqbkt (Var "size" CTy"Zsize") (Var "imm" F64))))
                        qVar"state")))))))


(Def "restrictSize" (TP (sqbkt (Var "size" CTy"Zsize") (Var "imm" F64)))
  (CS (Var "size" CTy"Zsize")
    (sqbkt ((Call "Z8" CTy"Zsize" (AVar bTy))
             (Bop BAnd (Var "imm" F64) (LW 255 64)))
           ((Const "Z16" CTy"Zsize")
             (Bop BAnd (Var "imm" F64) (LW 65535 64)))
           ((Const "Z32" CTy"Zsize")
             (Bop BAnd (Var "imm" F64) (LW 4294967295 64)))
           ((Const "Z64" CTy"Zsize") (Var "imm" F64)))))


(Def "EA" (Var "ea" CTy"Zea")
  (Close qVar"state"
    (CS (Var "ea" CTy"Zea")
      (sqbkt ((Call "Zea_i" CTy"Zea" (Var "i" (PTy CTy"Zsize" F64)))
               (TP
                 (sqbkt (Call "restrictSize" F64
                          (Var "i" (PTy CTy"Zsize" F64))) qVar"state")))
             ((Call "Zea_r" CTy"Zea"
                 (TP
                   (sqbkt (Call "Z8" CTy"Zsize" bVar"have_rex")
                          (Var "r" CTy"Zreg"))))
               (TP
                 (sqbkt (ITE
                          (Bop Or bVar"have_rex"
                            (Mop Not
                              (Bop In (Var "r" CTy"Zreg")
                                (SL(sqbkt (LC "RSP" CTy"Zreg")
                                          (LC "RBP" CTy"Zreg")
                                          (LC "RSI" CTy"Zreg")
                                          (LC "RDI" CTy"Zreg"))))))
                          (Bop BAnd
                            (Apply
                              (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                              (Var "r" CTy"Zreg")) (LW 255 64))
                          (Bop BAnd
                            (Bop Lsr
                              (Apply
                                (Dest "REG" (ATy CTy"Zreg" F64)
                                  qVar"state")
                                (Mop (Cast CTy"Zreg")
                                  (Bop Sub
                                    (Mop (Cast nTy) (Var "r" CTy"Zreg"))
                                    (LN 4)))) (LN 8)) (LW 255 64)))
                        qVar"state")))
             ((Call "Zea_r" CTy"Zea"
                 (TP (sqbkt (Var "s" CTy"Zsize") (Var "r" CTy"Zreg"))))
               (TP
                 (sqbkt (Call "restrictSize" F64
                          (TP
                            (sqbkt (Var "s" CTy"Zsize")
                                   (Apply
                                     (Dest "REG" (ATy CTy"Zreg" F64)
                                       qVar"state") (Var "r" CTy"Zreg")))))
                        qVar"state")))
             ((Call "Zea_m" CTy"Zea"
                 (TP
                   (sqbkt (Call "Z8" CTy"Zsize" (AVar bTy)) (Var "a" F64))))
               (Let (TP (sqbkt (Var "v" F8) qVar"s"))
                 (Apply (Call "mem8" (ATy qTy (PTy F8 qTy)) (Var "a" F64))
                   qVar"state")
                 (TP (sqbkt (Mop (Cast F64) (Var "v" F8)) qVar"s"))))
             ((Call "Zea_m" CTy"Zea"
                 (TP (sqbkt (Const "Z16" CTy"Zsize") (Var "a" F64))))
               (Let (TP (sqbkt (Var "v" F16) qVar"s"))
                 (Apply
                   (Call "mem16" (ATy qTy (PTy F16 qTy)) (Var "a" F64))
                   qVar"state")
                 (TP (sqbkt (Mop (Cast F64) (Var "v" F16)) qVar"s"))))
             ((Call "Zea_m" CTy"Zea"
                 (TP (sqbkt (Const "Z32" CTy"Zsize") (Var "a" F64))))
               (Let (TP (sqbkt (Var "v" F32) qVar"s"))
                 (Apply
                   (Call "mem32" (ATy qTy (PTy F32 qTy)) (Var "a" F64))
                   qVar"state")
                 (TP (sqbkt (Mop (Cast F64) (Var "v" F32)) qVar"s"))))
             ((Call "Zea_m" CTy"Zea"
                 (TP (sqbkt (Const "Z64" CTy"Zsize") (Var "a" F64))))
               (Apply (Call "mem64" (ATy qTy (PTy F64 qTy)) (Var "a" F64))
                 qVar"state"))))))


(Def "write'EA" (TP (sqbkt (Var "w" F64) (Var "ea" CTy"Zea")))
  (Close qVar"state"
    (CS (Var "ea" CTy"Zea")
      (sqbkt ((Call "Zea_i" CTy"Zea" (Var "i" (PTy CTy"Zsize" F64)))
               (Apply
                 (Call "raise'exception" (ATy qTy (PTy uTy qTy))
                   (Call "FAIL" CTy"exception" (LS"write to constant")))
                 qVar"state"))
             ((Call "Zea_r" CTy"Zea"
                 (TP
                   (sqbkt (Call "Z8" CTy"Zsize" bVar"have_rex")
                          (Var "r" CTy"Zreg"))))
               (ITE
                 (Bop Or bVar"have_rex"
                   (Mop Not
                     (Bop In (Var "r" CTy"Zreg")
                       (SL(sqbkt (LC "RSP" CTy"Zreg") (LC "RBP" CTy"Zreg")
                                 (LC "RSI" CTy"Zreg") (LC "RDI" CTy"Zreg"))))))
                 (Let (Var "v" (ATy CTy"Zreg" F64))
                   (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                   (TP
                     (sqbkt LU
                            (Rupd "REG"
                              (TP
                                (sqbkt qVar"state"
                                       (Fupd (Var "v" (ATy CTy"Zreg" F64))
                                         (Var "r" CTy"Zreg")
                                         (BFI (LN 7) (LN 0)
                                           (EX (Var "w" F64) (LN 7) (LN 0)
                                             F8)
                                           (Apply
                                             (Var "v" (ATy CTy"Zreg" F64))
                                             (Var "r" CTy"Zreg"))))))))))
                 (Let (Var "v" (ATy CTy"Zreg" F64))
                   (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                   (Let (Var "x" CTy"Zreg")
                     (Mop (Cast CTy"Zreg")
                       (Bop Sub (Mop (Cast nTy) (Var "r" CTy"Zreg"))
                         (LN 4)))
                     (TP
                       (sqbkt LU
                              (Rupd "REG"
                                (TP
                                  (sqbkt qVar"state"
                                         (Fupd
                                           (Var "v" (ATy CTy"Zreg" F64))
                                           (Var "x" CTy"Zreg")
                                           (BFI (LN 15) (LN 8)
                                             (EX (Var "w" F64) (LN 7)
                                               (LN 0) F8)
                                             (Apply
                                               (Var "v"
                                                 (ATy CTy"Zreg" F64))
                                               (Var "x" CTy"Zreg")))))))))))))
             ((Call "Zea_r" CTy"Zea"
                 (TP (sqbkt (Const "Z16" CTy"Zsize") (Var "r" CTy"Zreg"))))
               (Let (Var "v" (ATy CTy"Zreg" F64))
                 (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                 (TP
                   (sqbkt LU
                          (Rupd "REG"
                            (TP
                              (sqbkt qVar"state"
                                     (Fupd (Var "v" (ATy CTy"Zreg" F64))
                                       (Var "r" CTy"Zreg")
                                       (BFI (LN 15) (LN 0)
                                         (EX (Var "w" F64) (LN 15) (LN 0)
                                           F16)
                                         (Apply
                                           (Var "v" (ATy CTy"Zreg" F64))
                                           (Var "r" CTy"Zreg")))))))))))
             ((Call "Zea_r" CTy"Zea"
                 (TP (sqbkt (Const "Z32" CTy"Zsize") (Var "r" CTy"Zreg"))))
               (TP
                 (sqbkt LU
                        (Rupd "REG"
                          (TP
                            (sqbkt qVar"state"
                                   (Fupd
                                     (Dest "REG" (ATy CTy"Zreg" F64)
                                       qVar"state") (Var "r" CTy"Zreg")
                                     (Mop (Cast F64)
                                       (EX (Var "w" F64) (LN 31) (LN 0)
                                         F32)))))))))
             ((Call "Zea_r" CTy"Zea"
                 (TP (sqbkt (Const "Z64" CTy"Zsize") (Var "r" CTy"Zreg"))))
               (TP
                 (sqbkt LU
                        (Rupd "REG"
                          (TP
                            (sqbkt qVar"state"
                                   (Fupd
                                     (Dest "REG" (ATy CTy"Zreg" F64)
                                       qVar"state") (Var "r" CTy"Zreg")
                                     (Var "w" F64))))))))
             ((Call "Zea_m" CTy"Zea"
                 (TP
                   (sqbkt (Call "Z8" CTy"Zsize" (AVar bTy)) (Var "a" F64))))
               (Apply
                 (Call "write'mem8" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (EX (Var "w" F64) (LN 7) (LN 0) F8)
                            (Var "a" F64)))) qVar"state"))
             ((Call "Zea_m" CTy"Zea"
                 (TP (sqbkt (Const "Z16" CTy"Zsize") (Var "a" F64))))
               (Apply
                 (Call "write'mem16" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (EX (Var "w" F64) (LN 15) (LN 0) F16)
                            (Var "a" F64)))) qVar"state"))
             ((Call "Zea_m" CTy"Zea"
                 (TP (sqbkt (Const "Z32" CTy"Zsize") (Var "a" F64))))
               (Apply
                 (Call "write'mem32" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (EX (Var "w" F64) (LN 31) (LN 0) F32)
                            (Var "a" F64)))) qVar"state"))
             ((Call "Zea_m" CTy"Zea"
                 (TP (sqbkt (Const "Z64" CTy"Zsize") (Var "a" F64))))
               (Apply
                 (Call "write'mem64" (ATy qTy (PTy uTy qTy))
                   (TP (sqbkt (Var "w" F64) (Var "a" F64)))) qVar"state"))))))


(Def "read_dest_src_ea" (Var "sd" (PTy CTy"Zsize" CTy"Zdest_src"))
  (Close qVar"state"
    (Let (Var "v" CTy"Zea")
      (Mop Fst
        (Apply
          (Call "ea_Zdest" (ATy qTy (PTy CTy"Zea" qTy))
            (Var "sd" (PTy CTy"Zsize" CTy"Zdest_src"))) qVar"state"))
      (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
        (Apply (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "v" CTy"Zea"))
          qVar"state")
        (Let (TP (sqbkt (Var "v0" (PTy F64 F64)) qVar"s"))
          (Let (TP (sqbkt (Var "v1" F64) qVar"s"))
            (Apply
              (Call "EA" (ATy qTy (PTy F64 qTy))
                (Mop Fst
                  (Apply
                    (Call "ea_Zsrc" (ATy qTy (PTy CTy"Zea" qTy))
                      (Var "sd" (PTy CTy"Zsize" CTy"Zdest_src"))) qVar"s")))
              qVar"s")
            (TP
              (sqbkt (TP (sqbkt (Var "v0" F64) (Var "v1" F64))) qVar"s")))
          (TP
            (sqbkt (TP
                     (sqbkt (Var "v" CTy"Zea") (Var "v0" (PTy F64 F64))))
                   qVar"s")))))))


(Def "call_dest_from_ea" (Var "ea" CTy"Zea")
  (Close qVar"state"
    (CS (Var "ea" CTy"Zea")
      (sqbkt ((Call "Zea_i" CTy"Zea"
                 (TP (sqbkt (AVar CTy"Zsize") (Var "i" F64))))
               (TP
                 (sqbkt (Bop Add (Dest "RIP" F64 qVar"state")
                          (Var "i" F64)) qVar"state")))
             ((Call "Zea_r" CTy"Zea"
                 (TP (sqbkt (AVar CTy"Zsize") (Var "r" CTy"Zreg"))))
               (TP
                 (sqbkt (Apply
                          (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                          (Var "r" CTy"Zreg")) qVar"state")))
             ((Call "Zea_m" CTy"Zea"
                 (TP (sqbkt (AVar CTy"Zsize") (Var "a" F64))))
               (Apply (Call "mem64" (ATy qTy (PTy F64 qTy)) (Var "a" F64))
                 qVar"state"))))))


(Def "get_ea_address" (Var "ea" CTy"Zea")
  (CS (Var "ea" CTy"Zea")
    (sqbkt ((Call "Zea_i" CTy"Zea"
               (TP (sqbkt (AVar CTy"Zsize") (Var "i" F64)))) (LW 0 64))
           ((Call "Zea_r" CTy"Zea"
               (TP (sqbkt (AVar CTy"Zsize") (Var "r" CTy"Zreg"))))
             (LW 0 64))
           ((Call "Zea_m" CTy"Zea"
               (TP (sqbkt (AVar CTy"Zsize") (Var "a" F64))))
             (Var "a" F64)))))


(Def "jump_to_ea" (Var "ea" CTy"Zea")
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v" F64) qVar"s"))
      (Apply
        (Call "call_dest_from_ea" (ATy qTy (PTy F64 qTy))
          (Var "ea" CTy"Zea")) qVar"state")
      (TP (sqbkt LU (Rupd "RIP" (TP (sqbkt qVar"s" (Var "v" F64)))))))))


(Def "ByteParity" (Var "b" F8)
  (EQ
    (Bop Mod
      (Bop Add
        (Bop Add
          (Bop Add
            (Bop Add
              (Bop Add
                (Bop Add
                  (Bop Add (Mop (Cast nTy) (Bop Bit (Var "b" F8) (LN 7)))
                    (Mop (Cast nTy) (Bop Bit (Var "b" F8) (LN 6))))
                  (Mop (Cast nTy) (Bop Bit (Var "b" F8) (LN 5))))
                (Mop (Cast nTy) (Bop Bit (Var "b" F8) (LN 4))))
              (Mop (Cast nTy) (Bop Bit (Var "b" F8) (LN 3))))
            (Mop (Cast nTy) (Bop Bit (Var "b" F8) (LN 2))))
          (Mop (Cast nTy) (Bop Bit (Var "b" F8) (LN 1))))
        (Mop (Cast nTy) (Bop Bit (Var "b" F8) (LN 0)))) (LN 2)) (LN 0)))


(Def "Zsize_width" (Var "size" CTy"Zsize")
  (CS (Var "size" CTy"Zsize")
    (sqbkt ((Call "Z8" CTy"Zsize" (AVar bTy)) (LN 8))
           ((Const "Z16" CTy"Zsize") (LN 16))
           ((Const "Z32" CTy"Zsize") (LN 32))
           ((Const "Z64" CTy"Zsize") (LN 64)))))


(Def "word_size_msb" (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64)))
  (Bop Bit (Var "w" F64)
    (Bop Sub (Call "Zsize_width" nTy (Var "size" CTy"Zsize")) (LN 1))))


(Def "write_PF" (Var "w" F64)
  (Close qVar"state"
    (Apply
      (Call "write'PF" (ATy qTy (PTy uTy qTy))
        (Call "ByteParity" bTy (EX (Var "w" F64) (LN 7) (LN 0) F8)))
      qVar"state")))


(Def "write_SF" (Var "s_w" (PTy CTy"Zsize" F64))
  (Close qVar"state"
    (Apply
      (Call "write'SF" (ATy qTy (PTy uTy qTy))
        (Call "word_size_msb" bTy (Var "s_w" (PTy CTy"Zsize" F64))))
      qVar"state")))


(Def "write_ZF" (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64)))
  (Close qVar"state"
    (Apply
      (Call "write'ZF" (ATy qTy (PTy uTy qTy))
        (CS (Var "size" CTy"Zsize")
          (sqbkt ((Call "Z8" CTy"Zsize" (AVar bTy))
                   (EQ (Mop (Cast F8) (Var "w" F64)) (LW 0 8)))
                 ((Const "Z16" CTy"Zsize")
                   (EQ (Mop (Cast F16) (Var "w" F64)) (LW 0 16)))
                 ((Const "Z32" CTy"Zsize")
                   (EQ (Mop (Cast F32) (Var "w" F64)) (LW 0 32)))
                 ((Const "Z64" CTy"Zsize") (EQ (Var "w" F64) (LW 0 64))))))
      qVar"state")))


(Def "write_logical_eflags"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64)))
  (Close qVar"state"
    (Apply
      (Call "FlagUnspecified" (ATy qTy (PTy uTy qTy))
        (LC "Z_AF" CTy"Zeflags"))
      (Mop Snd
        (Apply
          (Call "write_ZF" (ATy qTy (PTy uTy qTy))
            (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64))))
          (Mop Snd
            (Apply
              (Call "write_SF" (ATy qTy (PTy uTy qTy))
                (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64))))
              (Mop Snd
                (Apply
                  (Call "write_PF" (ATy qTy (PTy uTy qTy)) (Var "w" F64))
                  (Mop Snd
                    (Apply (Call "write'OF" (ATy qTy (PTy uTy qTy)) LF)
                      (Mop Snd
                        (Apply
                          (Call "write'CF" (ATy qTy (PTy uTy qTy)) LF)
                          qVar"state")))))))))))))


(Def "write_arith_eflags_except_CF_OF"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64)))
  (Close qVar"state"
    (Apply
      (Call "FlagUnspecified" (ATy qTy (PTy uTy qTy))
        (LC "Z_AF" CTy"Zeflags"))
      (Mop Snd
        (Apply
          (Call "write_ZF" (ATy qTy (PTy uTy qTy))
            (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64))))
          (Mop Snd
            (Apply
              (Call "write_SF" (ATy qTy (PTy uTy qTy))
                (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64))))
              (Mop Snd
                (Apply
                  (Call "write_PF" (ATy qTy (PTy uTy qTy)) (Var "w" F64))
                  qVar"state")))))))))


(Def "write_arith_eflags"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "r" (PTy F64 (PTy bTy bTy)))))
  (Close qVar"state"
    (Let (TP (sqbkt (Var "w" F64) bVar"c" bVar"x"))
      (Var "r" (PTy F64 (PTy bTy bTy)))
      (Apply
        (Call "write_arith_eflags_except_CF_OF" (ATy qTy (PTy uTy qTy))
          (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64))))
        (Mop Snd
          (Apply (Call "write'OF" (ATy qTy (PTy uTy qTy)) bVar"x")
            (Mop Snd
              (Apply (Call "write'CF" (ATy qTy (PTy uTy qTy)) bVar"c")
                qVar"state"))))))))


(Def "erase_eflags" qVar"state"
  (TP
    (sqbkt LU
           (Rupd "EFLAGS"
             (TP (sqbkt qVar"state" (Mop (K1 CTy"Zeflags") (LO bTy))))))))


(Def "value_width" (Var "s" CTy"Zsize")
  (Bop Exp (LN 2) (Call "Zsize_width" nTy (Var "s" CTy"Zsize"))))


(Def "word_signed_overflow_add"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "a" F64) (Var "b" F64)))
  (Bop And
    (EQ
      (Call "word_size_msb" bTy
        (TP (sqbkt (Var "size" CTy"Zsize") (Var "a" F64))))
      (Call "word_size_msb" bTy
        (TP (sqbkt (Var "size" CTy"Zsize") (Var "b" F64)))))
    (Mop Not
      (EQ
        (Call "word_size_msb" bTy
          (TP
            (sqbkt (Var "size" CTy"Zsize")
                   (Bop Add (Var "a" F64) (Var "b" F64)))))
        (Call "word_size_msb" bTy
          (TP (sqbkt (Var "size" CTy"Zsize") (Var "a" F64))))))))


(Def "word_signed_overflow_sub"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "a" F64) (Var "b" F64)))
  (Bop And
    (Mop Not
      (EQ
        (Call "word_size_msb" bTy
          (TP (sqbkt (Var "size" CTy"Zsize") (Var "a" F64))))
        (Call "word_size_msb" bTy
          (TP (sqbkt (Var "size" CTy"Zsize") (Var "b" F64))))))
    (Mop Not
      (EQ
        (Call "word_size_msb" bTy
          (TP
            (sqbkt (Var "size" CTy"Zsize")
                   (Bop Sub (Var "a" F64) (Var "b" F64)))))
        (Call "word_size_msb" bTy
          (TP (sqbkt (Var "size" CTy"Zsize") (Var "a" F64))))))))


(Def "add_with_carry_out"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "x" F64) (Var "y" F64)))
  (TP
    (sqbkt (Bop Add (Var "x" F64) (Var "y" F64))
           (Bop Le (Call "value_width" nTy (Var "size" CTy"Zsize"))
             (Bop Add (Mop (Cast nTy) (Var "x" F64))
               (Mop (Cast nTy) (Var "y" F64))))
           (Call "word_signed_overflow_add" bTy
             (TP
               (sqbkt (Var "size" CTy"Zsize") (Var "x" F64) (Var "y" F64)))))))


(Def "sub_with_borrow"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "x" F64) (Var "y" F64)))
  (TP
    (sqbkt (Bop Sub (Var "x" F64) (Var "y" F64))
           (Bop Ult (Var "x" F64) (Var "y" F64))
           (Call "word_signed_overflow_sub" bTy
             (TP
               (sqbkt (Var "size" CTy"Zsize") (Var "x" F64) (Var "y" F64)))))))


(Def "write_arith_result"
  (TP
    (sqbkt (Var "size" CTy"Zsize") (Var "r" (PTy F64 (PTy bTy bTy)))
           (Var "ea" CTy"Zea")))
  (Close qVar"state"
    (Apply
      (Call "write'EA" (ATy qTy (PTy uTy qTy))
        (TP
          (sqbkt (Mop Fst (Var "r" (PTy F64 (PTy bTy bTy))))
                 (Var "ea" CTy"Zea"))))
      (Mop Snd
        (Apply
          (Call "write_arith_eflags" (ATy qTy (PTy uTy qTy))
            (TP
              (sqbkt (Var "size" CTy"Zsize")
                     (Var "r" (PTy F64 (PTy bTy bTy)))))) qVar"state")))))


(Def "write_arith_result_no_CF_OF"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64) (Var "ea" CTy"Zea")))
  (Close qVar"state"
    (Apply
      (Call "write'EA" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt (Var "w" F64) (Var "ea" CTy"Zea"))))
      (Mop Snd
        (Apply
          (Call "write_arith_eflags_except_CF_OF" (ATy qTy (PTy uTy qTy))
            (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64))))
          qVar"state")))))


(Def "write_logical_result"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64) (Var "ea" CTy"Zea")))
  (Close qVar"state"
    (Apply
      (Call "write'EA" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt (Var "w" F64) (Var "ea" CTy"Zea"))))
      (Mop Snd
        (Apply
          (Call "write_logical_eflags" (ATy qTy (PTy uTy qTy))
            (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64))))
          qVar"state")))))


(Def "write_result_erase_eflags"
  (TP (sqbkt (Var "w" F64) (Var "ea" CTy"Zea")))
  (Close qVar"state"
    (Apply
      (Call "write'EA" (ATy qTy (PTy uTy qTy))
        (TP (sqbkt (Var "w" F64) (Var "ea" CTy"Zea"))))
      (Mop Snd
        (Apply (Const "erase_eflags" (ATy qTy (PTy uTy qTy))) qVar"state")))))


(Def "maskShift" (TP (sqbkt (Var "size" CTy"Zsize") (Var "w" F64)))
  (ITE (EQ (Var "size" CTy"Zsize") (Const "Z64" CTy"Zsize"))
    (Mop (Cast nTy) (EX (Var "w" F64) (LN 5) (LN 0) (FTy 6)))
    (Mop (Cast nTy) (EX (Var "w" F64) (LN 4) (LN 0) (FTy 5)))))


(Def "ROL"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "x" F64) (Var "y" F64)))
  (CS (Var "size" CTy"Zsize")
    (sqbkt ((Call "Z8" CTy"Zsize" (AVar bTy))
             (Mop (Cast F64)
               (Bop Rol (EX (Var "x" F64) (LN 7) (LN 0) F8)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z16" CTy"Zsize")
             (Mop (Cast F64)
               (Bop Rol (EX (Var "x" F64) (LN 15) (LN 0) F16)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z32" CTy"Zsize")
             (Mop (Cast F64)
               (Bop Rol (EX (Var "x" F64) (LN 31) (LN 0) F32)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z64" CTy"Zsize")
             (Bop Rol (Var "x" F64)
               (Mop (Cast nTy) (EX (Var "y" F64) (LN 5) (LN 0) (FTy 6))))))))


(Def "ROR"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "x" F64) (Var "y" F64)))
  (CS (Var "size" CTy"Zsize")
    (sqbkt ((Call "Z8" CTy"Zsize" (AVar bTy))
             (Mop (Cast F64)
               (Bop Ror (EX (Var "x" F64) (LN 7) (LN 0) F8)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z16" CTy"Zsize")
             (Mop (Cast F64)
               (Bop Ror (EX (Var "x" F64) (LN 15) (LN 0) F16)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z32" CTy"Zsize")
             (Mop (Cast F64)
               (Bop Ror (EX (Var "x" F64) (LN 31) (LN 0) F32)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z64" CTy"Zsize")
             (Bop Ror (Var "x" F64)
               (Mop (Cast nTy) (EX (Var "y" F64) (LN 5) (LN 0) (FTy 6))))))))


(Def "SAR"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "x" F64) (Var "y" F64)))
  (CS (Var "size" CTy"Zsize")
    (sqbkt ((Call "Z8" CTy"Zsize" (AVar bTy))
             (Mop (Cast F64)
               (Bop Asr (EX (Var "x" F64) (LN 7) (LN 0) F8)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z16" CTy"Zsize")
             (Mop (Cast F64)
               (Bop Asr (EX (Var "x" F64) (LN 15) (LN 0) F16)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z32" CTy"Zsize")
             (Mop (Cast F64)
               (Bop Asr (EX (Var "x" F64) (LN 31) (LN 0) F32)
                 (Mop (Cast nTy) (EX (Var "y" F64) (LN 4) (LN 0) (FTy 5))))))
           ((Const "Z64" CTy"Zsize")
             (Bop Asr (Var "x" F64)
               (Mop (Cast nTy) (EX (Var "y" F64) (LN 5) (LN 0) (FTy 6))))))))


(Def "write_binop"
  (TP
    (sqbkt (Var "s" CTy"Zsize") (Var "bop" CTy"Zbinop_name") (Var "x" F64)
           (Var "y" F64) (Var "ea" CTy"Zea")))
  (Close qVar"state"
    (CS (Var "bop" CTy"Zbinop_name")
      (sqbkt ((LC "Zadd" CTy"Zbinop_name")
               (Apply
                 (Call "write_arith_result" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Call "add_with_carry_out"
                              (PTy F64 (PTy bTy bTy))
                              (TP
                                (sqbkt (Var "s" CTy"Zsize") (Var "x" F64)
                                       (Var "y" F64))))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zsub" CTy"Zbinop_name")
               (Apply
                 (Call "write_arith_result" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Call "sub_with_borrow"
                              (PTy F64 (PTy bTy bTy))
                              (TP
                                (sqbkt (Var "s" CTy"Zsize") (Var "x" F64)
                                       (Var "y" F64))))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zcmp" CTy"Zbinop_name")
               (Apply
                 (Call "write_arith_eflags" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Call "sub_with_borrow"
                              (PTy F64 (PTy bTy bTy))
                              (TP
                                (sqbkt (Var "s" CTy"Zsize") (Var "x" F64)
                                       (Var "y" F64))))))) qVar"state"))
             ((LC "Ztest" CTy"Zbinop_name")
               (Apply
                 (Call "write_logical_eflags" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Bop BAnd (Var "x" F64) (Var "y" F64)))))
                 qVar"state"))
             ((LC "Zand" CTy"Zbinop_name")
               (Apply
                 (Call "write_logical_result" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Bop BAnd (Var "x" F64) (Var "y" F64))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zxor" CTy"Zbinop_name")
               (Apply
                 (Call "write_logical_result" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Bop BXor (Var "x" F64) (Var "y" F64))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zor" CTy"Zbinop_name")
               (Apply
                 (Call "write_logical_result" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Bop BOr (Var "x" F64) (Var "y" F64))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zrol" CTy"Zbinop_name")
               (Apply
                 (Call "write_result_erase_eflags" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Call "ROL" F64
                              (TP
                                (sqbkt (Var "s" CTy"Zsize") (Var "x" F64)
                                       (Var "y" F64))))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zror" CTy"Zbinop_name")
               (Apply
                 (Call "write_result_erase_eflags" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Call "ROR" F64
                              (TP
                                (sqbkt (Var "s" CTy"Zsize") (Var "x" F64)
                                       (Var "y" F64))))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zsar" CTy"Zbinop_name")
               (Apply
                 (Call "write_result_erase_eflags" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Call "SAR" F64
                              (TP
                                (sqbkt (Var "s" CTy"Zsize") (Var "x" F64)
                                       (Var "y" F64))))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zshl" CTy"Zbinop_name")
               (Apply
                 (Call "write_result_erase_eflags" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Bop Lsl (Var "x" F64)
                              (Call "maskShift" nTy
                                (TP
                                  (sqbkt (Var "s" CTy"Zsize")
                                         (Var "y" F64)))))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zshr" CTy"Zbinop_name")
               (Apply
                 (Call "write_result_erase_eflags" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Bop Lsr (Var "x" F64)
                              (Call "maskShift" nTy
                                (TP
                                  (sqbkt (Var "s" CTy"Zsize")
                                         (Var "y" F64)))))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zadc" CTy"Zbinop_name")
               (Let (TP (sqbkt bVar"v" qVar"s0"))
                 (Apply (Const "CF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (Apply
                   (Call "write_arith_result_no_CF_OF"
                     (ATy qTy (PTy uTy qTy))
                     (TP
                       (sqbkt (Var "s" CTy"Zsize")
                              (Bop Add
                                (Bop Add (Var "x" F64) (Var "y" F64))
                                (Mop (Cast F64) bVar"v"))
                              (Var "ea" CTy"Zea"))))
                   (Mop Snd
                     (Apply
                       (Call "FlagUnspecified" (ATy qTy (PTy uTy qTy))
                         (LC "Z_OF" CTy"Zeflags"))
                       (Mop Snd
                         (Apply
                           (Call "write'CF" (ATy qTy (PTy uTy qTy))
                             (Bop Le
                               (Call "value_width" nTy
                                 (Var "s" CTy"Zsize"))
                               (Bop Add
                                 (Bop Add (Mop (Cast nTy) (Var "x" F64))
                                   (Mop (Cast nTy) (Var "y" F64)))
                                 (Mop (Cast nTy) bVar"v")))) qVar"s0")))))))
             ((LC "Zsbb" CTy"Zbinop_name")
               (Let (TP (sqbkt bVar"v" qVar"s0"))
                 (Apply (Const "CF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (Apply
                   (Call "write_arith_result_no_CF_OF"
                     (ATy qTy (PTy uTy qTy))
                     (TP
                       (sqbkt (Var "s" CTy"Zsize")
                              (Bop Sub (Var "x" F64)
                                (Bop Add (Var "y" F64)
                                  (Mop (Cast F64) bVar"v")))
                              (Var "ea" CTy"Zea"))))
                   (Mop Snd
                     (Apply
                       (Call "FlagUnspecified" (ATy qTy (PTy uTy qTy))
                         (LC "Z_OF" CTy"Zeflags"))
                       (Mop Snd
                         (Apply
                           (Call "write'CF" (ATy qTy (PTy uTy qTy))
                             (Bop Lt (Mop (Cast nTy) (Var "x" F64))
                               (Bop Add (Mop (Cast nTy) (Var "y" F64))
                                 (Mop (Cast nTy) bVar"v")))) qVar"s0")))))))
             ((AVar CTy"Zbinop_name")
               (Apply
                 (Call "raise'exception" (ATy qTy (PTy uTy qTy))
                   (Call "FAIL" CTy"exception"
                     (CC(sqbkt LS"Binary op not implemented: "
                               (Mop (Cast sTy)
                                 (Var "bop" CTy"Zbinop_name"))))))
                 qVar"state"))))))


(Def "write_monop"
  (TP
    (sqbkt (Var "s" CTy"Zsize") (Var "mop" CTy"Zmonop_name") (Var "x" F64)
           (Var "ea" CTy"Zea")))
  (Close qVar"state"
    (CS (Var "mop" CTy"Zmonop_name")
      (sqbkt ((LC "Znot" CTy"Zmonop_name")
               (Apply
                 (Call "write'EA" (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Mop BNot (Var "x" F64)) (Var "ea" CTy"Zea"))))
                 qVar"state"))
             ((LC "Zdec" CTy"Zmonop_name")
               (Apply
                 (Call "write_arith_result_no_CF_OF"
                   (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Bop Sub (Var "x" F64) (LW 1 64))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zinc" CTy"Zmonop_name")
               (Apply
                 (Call "write_arith_result_no_CF_OF"
                   (ATy qTy (PTy uTy qTy))
                   (TP
                     (sqbkt (Var "s" CTy"Zsize")
                            (Bop Add (Var "x" F64) (LW 1 64))
                            (Var "ea" CTy"Zea")))) qVar"state"))
             ((LC "Zneg" CTy"Zmonop_name")
               (Apply
                 (Call "FlagUnspecified" (ATy qTy (PTy uTy qTy))
                   (LC "Z_CF" CTy"Zeflags"))
                 (Mop Snd
                   (Apply
                     (Call "write_arith_result_no_CF_OF"
                       (ATy qTy (PTy uTy qTy))
                       (TP
                         (sqbkt (Var "s" CTy"Zsize")
                                (Mop Neg (Var "x" F64))
                                (Var "ea" CTy"Zea")))) qVar"state"))))))))


(Def "read_cond" (Var "c" CTy"Zcond")
  (Close qVar"state"
    (CS (Var "c" CTy"Zcond")
      (sqbkt ((LC "Z_O" CTy"Zcond")
               (Apply (Const "OF" (ATy qTy (PTy bTy qTy))) qVar"state"))
             ((LC "Z_NO" CTy"Zcond")
               (Let (TP (sqbkt bVar"v" qVar"s"))
                 (Apply (Const "OF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (TP (sqbkt (Mop Not bVar"v") qVar"s"))))
             ((LC "Z_B" CTy"Zcond")
               (Apply (Const "CF" (ATy qTy (PTy bTy qTy))) qVar"state"))
             ((LC "Z_NB" CTy"Zcond")
               (Let (TP (sqbkt bVar"v" qVar"s"))
                 (Apply (Const "CF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (TP (sqbkt (Mop Not bVar"v") qVar"s"))))
             ((LC "Z_E" CTy"Zcond")
               (Apply (Const "ZF" (ATy qTy (PTy bTy qTy))) qVar"state"))
             ((LC "Z_NE" CTy"Zcond")
               (Let (TP (sqbkt bVar"v" qVar"s"))
                 (Apply (Const "ZF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (TP (sqbkt (Mop Not bVar"v") qVar"s"))))
             ((LC "Z_A" CTy"Zcond")
               (CS
                 (TP
                   (sqbkt (Apply
                            (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                              qVar"state") (LC "Z_CF" CTy"Zeflags"))
                          (Apply
                            (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                              qVar"state") (LC "Z_ZF" CTy"Zeflags"))))
                 (sqbkt ((TP (sqbkt (Mop Some LF) (Mop Some LF)))
                          (TP (sqbkt LT qVar"state")))
                        ((TP (sqbkt (Mop Some LT) (AVar (OTy bTy))))
                          (TP (sqbkt LF qVar"state")))
                        ((TP (sqbkt (AVar (OTy bTy)) (Mop Some LT)))
                          (TP (sqbkt LF qVar"state")))
                        ((AVar (PTy (OTy bTy) (OTy bTy)))
                          (Apply
                            (Call "raise'exception"
                              (ATy qTy (PTy bTy qTy))
                              (Call "BadFlagAccess" CTy"exception"
                                (CC(sqbkt LS"read_cond: "
                                          (Mop (Cast sTy)
                                            (Var "c" CTy"Zcond"))))))
                            qVar"state")))))
             ((LC "Z_NA" CTy"Zcond")
               (CS
                 (TP
                   (sqbkt (Apply
                            (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                              qVar"state") (LC "Z_CF" CTy"Zeflags"))
                          (Apply
                            (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                              qVar"state") (LC "Z_ZF" CTy"Zeflags"))))
                 (sqbkt ((TP (sqbkt (Mop Some LT) (AVar (OTy bTy))))
                          (TP (sqbkt LT qVar"state")))
                        ((TP (sqbkt (AVar (OTy bTy)) (Mop Some LT)))
                          (TP (sqbkt LT qVar"state")))
                        ((TP (sqbkt (Mop Some LF) (Mop Some LF)))
                          (TP (sqbkt LF qVar"state")))
                        ((AVar (PTy (OTy bTy) (OTy bTy)))
                          (Apply
                            (Call "raise'exception"
                              (ATy qTy (PTy bTy qTy))
                              (Call "BadFlagAccess" CTy"exception"
                                (CC(sqbkt LS"read_cond: "
                                          (Mop (Cast sTy)
                                            (Var "c" CTy"Zcond"))))))
                            qVar"state")))))
             ((LC "Z_S" CTy"Zcond")
               (Apply (Const "SF" (ATy qTy (PTy bTy qTy))) qVar"state"))
             ((LC "Z_NS" CTy"Zcond")
               (Let (TP (sqbkt bVar"v" qVar"s"))
                 (Apply (Const "SF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (TP (sqbkt (Mop Not bVar"v") qVar"s"))))
             ((LC "Z_P" CTy"Zcond")
               (Apply (Const "PF" (ATy qTy (PTy bTy qTy))) qVar"state"))
             ((LC "Z_NP" CTy"Zcond")
               (Let (TP (sqbkt bVar"v" qVar"s"))
                 (Apply (Const "PF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (TP (sqbkt (Mop Not bVar"v") qVar"s"))))
             ((LC "Z_L" CTy"Zcond")
               (Let (TP (sqbkt bVar"v" qVar"s"))
                 (Apply (Const "SF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (Let (TP (sqbkt bVar"v" qVar"s"))
                   (Let (TP (sqbkt bVar"v0" qVar"s"))
                     (Apply (Const "OF" (ATy qTy (PTy bTy qTy))) qVar"s")
                     (TP (sqbkt (EQ bVar"v" bVar"v0") qVar"s")))
                   (TP (sqbkt (Mop Not bVar"v") qVar"s")))))
             ((LC "Z_NL" CTy"Zcond")
               (Let (TP (sqbkt bVar"v" qVar"s"))
                 (Apply (Const "SF" (ATy qTy (PTy bTy qTy))) qVar"state")
                 (Let (TP (sqbkt bVar"v0" qVar"s"))
                   (Apply (Const "OF" (ATy qTy (PTy bTy qTy))) qVar"s")
                   (TP (sqbkt (EQ bVar"v" bVar"v0") qVar"s")))))
             ((LC "Z_G" CTy"Zcond")
               (CS
                 (TP
                   (sqbkt (Apply
                            (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                              qVar"state") (LC "Z_SF" CTy"Zeflags"))
                          (Apply
                            (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                              qVar"state") (LC "Z_OF" CTy"Zeflags"))))
                 (sqbkt ((TP
                            (sqbkt (Mop Some bVar"a") (Mop Some bVar"b")))
                          (Let (TP (sqbkt bVar"v" qVar"s"))
                            (Apply (Const "ZF" (ATy qTy (PTy bTy qTy)))
                              qVar"state")
                            (TP
                              (sqbkt (Bop And (EQ bVar"a" bVar"b")
                                       (Mop Not bVar"v")) qVar"s"))))
                        ((AVar (PTy (OTy bTy) (OTy bTy)))
                          (CS
                            (Apply
                              (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                                qVar"state") (LC "Z_ZF" CTy"Zeflags"))
                            (sqbkt ((Mop Some LT)
                                     (TP (sqbkt LF qVar"state")))
                                   ((AVar (OTy bTy))
                                     (Apply
                                       (Call "raise'exception"
                                         (ATy qTy (PTy bTy qTy))
                                         (Call "BadFlagAccess"
                                           CTy"exception"
                                           (CC(sqbkt LS"read_cond: "
                                                     (Mop (Cast sTy)
                                                       (Var "c"
                                                         CTy"Zcond"))))))
                                       qVar"state"))))))))
             ((LC "Z_NG" CTy"Zcond")
               (CS
                 (TP
                   (sqbkt (Apply
                            (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                              qVar"state") (LC "Z_SF" CTy"Zeflags"))
                          (Apply
                            (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                              qVar"state") (LC "Z_OF" CTy"Zeflags"))))
                 (sqbkt ((TP
                            (sqbkt (Mop Some bVar"a") (Mop Some bVar"b")))
                          (Let (TP (sqbkt bVar"v" qVar"s"))
                            (Apply (Const "ZF" (ATy qTy (PTy bTy qTy)))
                              qVar"state")
                            (TP
                              (sqbkt (Bop Or
                                       (Mop Not (EQ bVar"a" bVar"b"))
                                       bVar"v") qVar"s"))))
                        ((AVar (PTy (OTy bTy) (OTy bTy)))
                          (CS
                            (Apply
                              (Dest "EFLAGS" (ATy CTy"Zeflags" (OTy bTy))
                                qVar"state") (LC "Z_ZF" CTy"Zeflags"))
                            (sqbkt ((Mop Some LT)
                                     (TP (sqbkt LT qVar"state")))
                                   ((AVar (OTy bTy))
                                     (Apply
                                       (Call "raise'exception"
                                         (ATy qTy (PTy bTy qTy))
                                         (Call "BadFlagAccess"
                                           CTy"exception"
                                           (CC(sqbkt LS"read_cond: "
                                                     (Mop (Cast sTy)
                                                       (Var "c"
                                                         CTy"Zcond"))))))
                                       qVar"state"))))))))
             ((LC "Z_ALWAYS" CTy"Zcond") (TP (sqbkt LT qVar"state")))))))


(Def "x64_pop_aux" qVar"state"
  (Let (Var "v" F64)
    (Apply (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
      (LC "RSP" CTy"Zreg"))
    (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
      (Apply (Call "mem64" (ATy qTy (PTy F64 qTy)) (Var "v" F64))
        qVar"state")
      (TP
        (sqbkt (Var "v0" F64)
               (Rupd "REG"
                 (TP
                   (sqbkt qVar"s"
                          (Fupd (Dest "REG" (ATy CTy"Zreg" F64) qVar"s")
                            (LC "RSP" CTy"Zreg")
                            (Bop Add (Var "v" F64) (LW 8 64)))))))))))


(Def "x64_pop" (Var "rm" CTy"Zrm")
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
      (Apply (Const "x64_pop_aux" (ATy qTy (PTy F64 qTy))) qVar"state")
      (Apply
        (Call "write'EA" (ATy qTy (PTy uTy qTy))
          (TP
            (sqbkt (Var "v0" F64)
                   (Mop Fst
                     (Apply
                       (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
                         (TP
                           (sqbkt (Const "Z64" CTy"Zsize")
                                  (Var "rm" CTy"Zrm")))) qVar"state")))))
        qVar"s"))))


(Def "x64_pop_rip" qVar"state"
  (Let (TP (sqbkt (Var "v" F64) qVar"s"))
    (Apply (Const "x64_pop_aux" (ATy qTy (PTy F64 qTy))) qVar"state")
    (TP (sqbkt LU (Rupd "RIP" (TP (sqbkt qVar"s" (Var "v" F64))))))))


(Def "x64_push_aux" (Var "w" F64)
  (Close qVar"state"
    (Let (Var "v" F64)
      (Bop Sub
        (Apply (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
          (LC "RSP" CTy"Zreg")) (LW 8 64))
      (Apply
        (Call "write'mem64" (ATy qTy (PTy uTy qTy))
          (TP (sqbkt (Var "w" F64) (Var "v" F64))))
        (Rupd "REG"
          (TP
            (sqbkt qVar"state"
                   (Fupd (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                     (LC "RSP" CTy"Zreg") (Var "v" F64)))))))))


(Def "x64_push" (Var "imm_rm" CTy"Zimm_rm")
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v" F64) qVar"s"))
      (Apply
        (Call "EA" (ATy qTy (PTy F64 qTy))
          (Mop Fst
            (Apply
              (Call "ea_Zimm_rm" (ATy qTy (PTy CTy"Zea" qTy))
                (TP
                  (sqbkt (Const "Z64" CTy"Zsize")
                         (Var "imm_rm" CTy"Zimm_rm")))) qVar"state")))
        qVar"state")
      (Apply (Call "x64_push_aux" (ATy qTy (PTy uTy qTy)) (Var "v" F64))
        qVar"s"))))


(Def "x64_push_rip" qVar"state"
  (Apply
    (Call "x64_push_aux" (ATy qTy (PTy uTy qTy))
      (Dest "RIP" F64 qVar"state")) qVar"state"))


(Def "x64_drop" (Var "imm" F64)
  (Close qVar"state"
    (Let qVar"s"
      (ITE (Mop Not (EQ (EX (Var "imm" F64) (LN 7) (LN 0) F8) (LW 0 8)))
        (Mop Snd
          (Apply
            (Call "raise'exception" (ATy qTy (PTy uTy qTy))
              (Call "FAIL" CTy"exception" (LS"x64_drop"))) qVar"state"))
        qVar"state")
      (TP
        (sqbkt LU
               (Rupd "REG"
                 (TP
                   (sqbkt qVar"s"
                          (Fupd (Dest "REG" (ATy CTy"Zreg" F64) qVar"s")
                            (LC "RSP" CTy"Zreg")
                            (Bop Add
                              (Apply
                                (Dest "REG" (ATy CTy"Zreg" F64) qVar"s")
                                (LC "RSP" CTy"Zreg")) (Var "imm" F64)))))))))))


(Def "dfn'Zbinop"
  (TP
    (sqbkt (Var "bop" CTy"Zbinop_name") (Var "size" CTy"Zsize")
           (Var "dst_src" CTy"Zdest_src")))
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v" (PTy CTy"Zea" (PTy F64 F64))) qVar"s"))
      (Apply
        (Call "read_dest_src_ea"
          (ATy qTy (PTy (PTy CTy"Zea" (PTy F64 F64)) qTy))
          (TP
            (sqbkt (Var "size" CTy"Zsize") (Var "dst_src" CTy"Zdest_src"))))
        qVar"state")
      (Let
        (TP
          (sqbkt (Var "ea" CTy"Zea") (Var "val_dst" F64)
                 (Var "val_src" F64)))
        (Var "v" (PTy CTy"Zea" (PTy F64 F64)))
        (Apply
          (Call "write_binop" (ATy qTy (PTy uTy qTy))
            (TP
              (sqbkt (Var "size" CTy"Zsize") (Var "bop" CTy"Zbinop_name")
                     (Var "val_dst" F64) (Var "val_src" F64)
                     (Var "ea" CTy"Zea")))) qVar"s")))))


(Def "dfn'Zcall" (Var "imm_rm" CTy"Zimm_rm")
  (Close qVar"state"
    (Let qVar"s"
      (Mop Snd
        (Apply (Const "x64_push_rip" (ATy qTy (PTy uTy qTy))) qVar"state"))
      (Apply
        (Call "jump_to_ea" (ATy qTy (PTy uTy qTy))
          (Mop Fst
            (Apply
              (Call "ea_Zimm_rm" (ATy qTy (PTy CTy"Zea" qTy))
                (TP
                  (sqbkt (Const "Z64" CTy"Zsize")
                         (Var "imm_rm" CTy"Zimm_rm")))) qVar"s")))
        qVar"s"))))


(Def "dfn'Zcmpxchg"
  (TP
    (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm") (Var "r" CTy"Zreg")))
  (Close qVar"state"
    (Let (Var "ea_src" CTy"Zea")
      (Call "Zea_r" CTy"Zea"
        (TP (sqbkt (Var "size" CTy"Zsize") (Var "r" CTy"Zreg"))))
      (Let (Var "v" CTy"Zea")
        (Mop Fst
          (Apply
            (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
              (TP (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm"))))
            qVar"state"))
        (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
          (Apply (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "v" CTy"Zea"))
            qVar"state")
          (Let (TP (sqbkt (Var "v1" F64) qVar"s"))
            (Apply
              (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "ea_src" CTy"Zea"))
              qVar"s")
            (Let qVar"s"
              (Mop Snd
                (Apply
                  (Call "write_binop" (ATy qTy (PTy uTy qTy))
                    (TP
                      (sqbkt (Var "size" CTy"Zsize")
                             (LC "Zcmp" CTy"Zbinop_name") (Var "v1" F64)
                             (Var "v0" F64) (Var "ea_src" CTy"Zea"))))
                  qVar"s"))
              (ITE (EQ (Var "v1" F64) (Var "v0" F64))
                (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
                  (Apply
                    (Call "EA" (ATy qTy (PTy F64 qTy))
                      (Var "ea_src" CTy"Zea")) qVar"s")
                  (Apply
                    (Call "write'EA" (ATy qTy (PTy uTy qTy))
                      (TP (sqbkt (Var "v0" F64) (Var "v" CTy"Zea"))))
                    qVar"s"))
                (Apply
                  (Call "write'EA" (ATy qTy (PTy uTy qTy))
                    (TP
                      (sqbkt (Var "v0" F64)
                             (Call "Zea_r" CTy"Zea"
                               (TP
                                 (sqbkt (Var "size" CTy"Zsize")
                                        (LC "RAX" CTy"Zreg")))))))
                  qVar"s")))))))))


(Def "dfn'Zcpuid" qVar"state"
  (Let qVar"s"
    (Rupd "ICACHE"
      (TP (sqbkt qVar"state" (Dest "MEM" (ATy F64 (OTy F8)) qVar"state"))))
    (Let qVar"s"
      (Rupd "REG"
        (TP
          (sqbkt qVar"s"
                 (Fupd (Dest "REG" (ATy CTy"Zreg" F64) qVar"s")
                   (LC "RAX" CTy"Zreg") (LX F64)))))
      (Let qVar"s"
        (Rupd "REG"
          (TP
            (sqbkt qVar"s"
                   (Fupd (Dest "REG" (ATy CTy"Zreg" F64) qVar"s")
                     (LC "RBX" CTy"Zreg") (LX F64)))))
        (Let qVar"s"
          (Rupd "REG"
            (TP
              (sqbkt qVar"s"
                     (Fupd (Dest "REG" (ATy CTy"Zreg" F64) qVar"s")
                       (LC "RCX" CTy"Zreg") (LX F64)))))
          (TP
            (sqbkt LU
                   (Rupd "REG"
                     (TP
                       (sqbkt qVar"s"
                              (Fupd
                                (Dest "REG" (ATy CTy"Zreg" F64) qVar"s")
                                (LC "RDX" CTy"Zreg") (LX F64))))))))))))


(Def "dfn'Zdiv" (TP (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm")))
  (Close qVar"state"
    (Let nVar"w" (Call "value_width" nTy (Var "size" CTy"Zsize"))
      (Let (Var "ea_eax" CTy"Zea")
        (Call "Zea_r" CTy"Zea"
          (TP (sqbkt (Var "size" CTy"Zsize") (LC "RAX" CTy"Zreg"))))
        (Let (Var "ea_edx" CTy"Zea")
          (Call "Zea_r" CTy"Zea"
            (TP (sqbkt (Var "size" CTy"Zsize") (LC "RAX" CTy"Zreg"))))
          (Let (TP (sqbkt (Var "v" F64) qVar"s"))
            (Apply
              (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "ea_eax" CTy"Zea"))
              qVar"state")
            (Let (TP (sqbkt nVar"v" qVar"s"))
              (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
                (Apply
                  (Call "EA" (ATy qTy (PTy F64 qTy))
                    (Var "ea_edx" CTy"Zea")) qVar"s")
                (TP
                  (sqbkt (Bop Add
                           (Bop Mul (Mop (Cast nTy) (Var "v" F64))
                             nVar"w") (Mop (Cast nTy) (Var "v0" F64)))
                         qVar"s")))
              (Let (TP (sqbkt nVar"v0" qVar"s"))
                (Let (TP (sqbkt (Var "v" F64) qVar"s"))
                  (Apply
                    (Call "EA" (ATy qTy (PTy F64 qTy))
                      (Mop Fst
                        (Apply
                          (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
                            (TP
                              (sqbkt (Var "size" CTy"Zsize")
                                     (Var "rm" CTy"Zrm")))) qVar"s")))
                    qVar"s")
                  (TP (sqbkt (Mop (Cast nTy) (Var "v" F64)) qVar"s")))
                (Let nVar"q" (Bop Div nVar"v" nVar"v0")
                  (Apply (Const "erase_eflags" (ATy qTy (PTy uTy qTy)))
                    (Mop Snd
                      (Apply
                        (Call "write'EA" (ATy qTy (PTy uTy qTy))
                          (TP
                            (sqbkt (Mop (Cast F64)
                                     (Bop Mod nVar"v" nVar"v0"))
                                   (Var "ea_edx" CTy"Zea"))))
                        (Mop Snd
                          (Apply
                            (Call "write'EA" (ATy qTy (PTy uTy qTy))
                              (TP
                                (sqbkt (Mop (Cast F64) nVar"q")
                                       (Var "ea_eax" CTy"Zea"))))
                            (ITE
                              (Bop Or (EQ nVar"v0" (LN 0))
                                (Bop Le nVar"w" nVar"q"))
                              (Mop Snd
                                (Apply
                                  (Call "raise'exception"
                                    (ATy qTy (PTy uTy qTy))
                                    (Call "FAIL" CTy"exception"
                                      (LS"division"))) qVar"s")) qVar"s")))))))))))))))


(Def "dfn'Zjcc" (TP (sqbkt (Var "cond" CTy"Zcond") (Var "imm" F64)))
  (Close qVar"state"
    (Let (TP (sqbkt bVar"v" qVar"s"))
      (Apply
        (Call "read_cond" (ATy qTy (PTy bTy qTy)) (Var "cond" CTy"Zcond"))
        qVar"state")
      (TP
        (sqbkt LU
               (ITE bVar"v"
                 (Rupd "RIP"
                   (TP
                     (sqbkt qVar"s"
                            (Bop Add (Dest "RIP" F64 qVar"s")
                              (Var "imm" F64))))) qVar"s"))))))


(Def "dfn'Zjmp" (Var "rm" CTy"Zrm")
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v" F64) qVar"s"))
      (Apply
        (Call "EA" (ATy qTy (PTy F64 qTy))
          (Mop Fst
            (Apply
              (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
                (TP (sqbkt (Const "Z64" CTy"Zsize") (Var "rm" CTy"Zrm"))))
              qVar"state"))) qVar"state")
      (TP (sqbkt LU (Rupd "RIP" (TP (sqbkt qVar"s" (Var "v" F64)))))))))


(Def "dfn'Zlea"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "dst_src" CTy"Zdest_src")))
  (Close qVar"state"
    (Apply
      (Call "write'EA" (ATy qTy (PTy uTy qTy))
        (TP
          (sqbkt (Call "get_ea_address" F64
                   (Mop Fst
                     (Apply
                       (Call "ea_Zsrc" (ATy qTy (PTy CTy"Zea" qTy))
                         (TP
                           (sqbkt (Var "size" CTy"Zsize")
                                  (Var "dst_src" CTy"Zdest_src"))))
                       qVar"state")))
                 (Mop Fst
                   (Apply
                     (Call "ea_Zdest" (ATy qTy (PTy CTy"Zea" qTy))
                       (TP
                         (sqbkt (Var "size" CTy"Zsize")
                                (Var "dst_src" CTy"Zdest_src"))))
                     qVar"state"))))) qVar"state")))


(Def "dfn'Zloop" (TP (sqbkt (Var "cond" CTy"Zcond") (Var "imm" F64)))
  (Close qVar"state"
    (Let (Var "v" F64)
      (Bop Sub
        (Apply (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
          (LC "RCX" CTy"Zreg")) (LW 1 64))
      (Let (TP (sqbkt bVar"v0" qVar"s"))
        (Apply
          (Call "read_cond" (ATy qTy (PTy bTy qTy))
            (Var "cond" CTy"Zcond"))
          (Rupd "REG"
            (TP
              (sqbkt qVar"state"
                     (Fupd (Dest "REG" (ATy CTy"Zreg" F64) qVar"state")
                       (LC "RCX" CTy"Zreg") (Var "v" F64))))))
        (TP
          (sqbkt LU
                 (ITE
                   (Bop And (Mop Not (EQ (Var "v" F64) (LW 0 64)))
                     bVar"v0")
                   (Rupd "RIP"
                     (TP
                       (sqbkt qVar"s"
                              (Bop Add (Dest "RIP" F64 qVar"s")
                                (Var "imm" F64))))) qVar"s")))))))


(Def "dfn'Zmonop"
  (TP
    (sqbkt (Var "mop" CTy"Zmonop_name") (Var "size" CTy"Zsize")
           (Var "rm" CTy"Zrm")))
  (Close qVar"state"
    (Let (Var "v" CTy"Zea")
      (Mop Fst
        (Apply
          (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
            (TP (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm"))))
          qVar"state"))
      (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
        (Apply (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "v" CTy"Zea"))
          qVar"state")
        (Apply
          (Call "write_monop" (ATy qTy (PTy uTy qTy))
            (TP
              (sqbkt (Var "size" CTy"Zsize") (Var "mop" CTy"Zmonop_name")
                     (Var "v0" F64) (Var "v" CTy"Zea")))) qVar"s")))))


(Def "dfn'Zmov"
  (TP
    (sqbkt (Var "cond" CTy"Zcond") (Var "size" CTy"Zsize")
           (Var "dst_src" CTy"Zdest_src")))
  (Close qVar"state"
    (Let (TP (sqbkt bVar"v" qVar"s"))
      (Apply
        (Call "read_cond" (ATy qTy (PTy bTy qTy)) (Var "cond" CTy"Zcond"))
        qVar"state")
      (ITE bVar"v"
        (Let (TP (sqbkt (Var "v0" F64) qVar"s0"))
          (Apply
            (Call "EA" (ATy qTy (PTy F64 qTy))
              (Mop Fst
                (Apply
                  (Call "ea_Zsrc" (ATy qTy (PTy CTy"Zea" qTy))
                    (TP
                      (sqbkt (Var "size" CTy"Zsize")
                             (Var "dst_src" CTy"Zdest_src")))) qVar"s")))
            qVar"s")
          (Apply
            (Call "write'EA" (ATy qTy (PTy uTy qTy))
              (TP
                (sqbkt (Var "v0" F64)
                       (Mop Fst
                         (Apply
                           (Call "ea_Zdest" (ATy qTy (PTy CTy"Zea" qTy))
                             (TP
                               (sqbkt (Var "size" CTy"Zsize")
                                      (Var "dst_src" CTy"Zdest_src"))))
                           qVar"s"))))) qVar"s0"))
        (TP (sqbkt LU qVar"s"))))))


(Def "dfn'Zmovzx"
  (TP
    (sqbkt (Var "size1" CTy"Zsize") (Var "dst_src" CTy"Zdest_src")
           (Var "size2" CTy"Zsize")))
  (Close qVar"state"
    (Let (TP (sqbkt (Var "v" (PTy F64 CTy"Zea")) qVar"s"))
      (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
        (Apply
          (Call "EA" (ATy qTy (PTy F64 qTy))
            (Mop Fst
              (Apply
                (Call "ea_Zsrc" (ATy qTy (PTy CTy"Zea" qTy))
                  (TP
                    (sqbkt (Var "size1" CTy"Zsize")
                           (Var "dst_src" CTy"Zdest_src")))) qVar"state")))
          qVar"state")
        (TP
          (sqbkt (TP
                   (sqbkt (Var "v0" F64)
                          (Mop Fst
                            (Apply
                              (Call "ea_Zdest"
                                (ATy qTy (PTy CTy"Zea" qTy))
                                (TP
                                  (sqbkt (Var "size2" CTy"Zsize")
                                         (Var "dst_src" CTy"Zdest_src"))))
                              qVar"state")))) qVar"s")))
      (Apply
        (Call "write'EA" (ATy qTy (PTy uTy qTy))
          (Var "v" (PTy F64 CTy"Zea"))) qVar"s"))))


(Def "dfn'Zmul" (TP (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm")))
  (Close qVar"state"
    (Let (Var "ea_eax" CTy"Zea")
      (Call "Zea_r" CTy"Zea"
        (TP (sqbkt (Var "size" CTy"Zsize") (LC "RAX" CTy"Zreg"))))
      (Let (TP (sqbkt (Var "v" F64) qVar"s"))
        (Apply (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "ea_eax" CTy"Zea"))
          qVar"state")
        (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
          (Apply
            (Call "EA" (ATy qTy (PTy F64 qTy))
              (Mop Fst
                (Apply
                  (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
                    (TP
                      (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm"))))
                  qVar"s"))) qVar"s")
          (Apply (Const "erase_eflags" (ATy qTy (PTy uTy qTy)))
            (CS (Var "size" CTy"Zsize")
              (sqbkt ((Call "Z8" CTy"Zsize" (AVar bTy))
                       (Mop Snd
                         (Apply
                           (Call "write'EA" (ATy qTy (PTy uTy qTy))
                             (TP
                               (sqbkt (Bop Mul (Var "v" F64)
                                        (Var "v0" F64))
                                      (Call "Zea_r" CTy"Zea"
                                        (TP
                                          (sqbkt (Const "Z16" CTy"Zsize")
                                                 (LC "RAX" CTy"Zreg")))))))
                           qVar"s")))
                     ((AVar CTy"Zsize")
                       (Mop Snd
                         (Apply
                           (Call "write'EA" (ATy qTy (PTy uTy qTy))
                             (TP
                               (sqbkt (Mop (Cast F64)
                                        (Bop Div
                                          (Bop Mul
                                            (Mop (Cast nTy) (Var "v" F64))
                                            (Mop (Cast nTy)
                                              (Var "v0" F64)))
                                          (Call "value_width" nTy
                                            (Var "size" CTy"Zsize"))))
                                      (Call "Zea_r" CTy"Zea"
                                        (TP
                                          (sqbkt (Var "size" CTy"Zsize")
                                                 (LC "RDX" CTy"Zreg")))))))
                           (Mop Snd
                             (Apply
                               (Call "write'EA" (ATy qTy (PTy uTy qTy))
                                 (TP
                                   (sqbkt (Bop Mul (Var "v" F64)
                                            (Var "v0" F64))
                                          (Var "ea_eax" CTy"Zea"))))
                               qVar"s")))))))))))))


(Def0 "dfn'Znop" LU)


(Def "dfn'Zpop" (Var "rm" CTy"Zrm")
  (Close qVar"state"
    (Apply (Call "x64_pop" (ATy qTy (PTy uTy qTy)) (Var "rm" CTy"Zrm"))
      qVar"state")))


(Def "dfn'Zpush" (Var "imm_rm" CTy"Zimm_rm")
  (Close qVar"state"
    (Apply
      (Call "x64_push" (ATy qTy (PTy uTy qTy))
        (Var "imm_rm" CTy"Zimm_rm")) qVar"state")))


(Def "dfn'Zret" (Var "imm" F64)
  (Close qVar"state"
    (Apply (Call "x64_drop" (ATy qTy (PTy uTy qTy)) (Var "imm" F64))
      (Mop Snd
        (Apply (Const "x64_pop_rip" (ATy qTy (PTy uTy qTy))) qVar"state")))))


(Def "dfn'Zxadd"
  (TP
    (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm") (Var "r" CTy"Zreg")))
  (Close qVar"state"
    (Let (Var "ea_src" CTy"Zea")
      (Call "Zea_r" CTy"Zea"
        (TP (sqbkt (Var "size" CTy"Zsize") (Var "r" CTy"Zreg"))))
      (Let (Var "v" CTy"Zea")
        (Mop Fst
          (Apply
            (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
              (TP (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm"))))
            qVar"state"))
        (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
          (Apply
            (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "ea_src" CTy"Zea"))
            qVar"state")
          (Let (TP (sqbkt (Var "v1" F64) qVar"s"))
            (Apply (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "v" CTy"Zea"))
              qVar"s")
            (Apply
              (Call "write_binop" (ATy qTy (PTy uTy qTy))
                (TP
                  (sqbkt (Var "size" CTy"Zsize")
                         (LC "Zadd" CTy"Zbinop_name") (Var "v0" F64)
                         (Var "v1" F64) (Var "v" CTy"Zea"))))
              (Mop Snd
                (Apply
                  (Call "write'EA" (ATy qTy (PTy uTy qTy))
                    (TP (sqbkt (Var "v1" F64) (Var "ea_src" CTy"Zea"))))
                  qVar"s")))))))))


(Def "dfn'Zxchg"
  (TP
    (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm") (Var "r" CTy"Zreg")))
  (Close qVar"state"
    (Let (Var "ea_src" CTy"Zea")
      (Call "Zea_r" CTy"Zea"
        (TP (sqbkt (Var "size" CTy"Zsize") (Var "r" CTy"Zreg"))))
      (Let (Var "v" CTy"Zea")
        (Mop Fst
          (Apply
            (Call "ea_Zrm" (ATy qTy (PTy CTy"Zea" qTy))
              (TP (sqbkt (Var "size" CTy"Zsize") (Var "rm" CTy"Zrm"))))
            qVar"state"))
        (Let (TP (sqbkt (Var "v0" F64) qVar"s"))
          (Apply
            (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "ea_src" CTy"Zea"))
            qVar"state")
          (Let (TP (sqbkt (Var "v1" F64) qVar"s"))
            (Apply (Call "EA" (ATy qTy (PTy F64 qTy)) (Var "v" CTy"Zea"))
              qVar"s")
            (Apply
              (Call "write'EA" (ATy qTy (PTy uTy qTy))
                (TP (sqbkt (Var "v0" F64) (Var "v" CTy"Zea"))))
              (Mop Snd
                (Apply
                  (Call "write'EA" (ATy qTy (PTy uTy qTy))
                    (TP (sqbkt (Var "v1" F64) (Var "ea_src" CTy"Zea"))))
                  qVar"s")))))))))


(Def "Run" (Var "v0" CTy"instruction")
  (Close qVar"state"
    (CS (Var "v0" CTy"instruction")
      (sqbkt ((Const "Zcpuid" CTy"instruction")
               (Apply (Const "dfn'Zcpuid" (ATy qTy (PTy uTy qTy)))
                 qVar"state"))
             ((Const "Znop" CTy"instruction")
               (TP (sqbkt (Const "dfn'Znop" uTy) qVar"state")))
             ((Call "Zbinop" CTy"instruction"
                 (Var "v1"
                   (PTy CTy"Zbinop_name" (PTy CTy"Zsize" CTy"Zdest_src"))))
               (Apply
                 (Call "dfn'Zbinop" (ATy qTy (PTy uTy qTy))
                   (Var "v1"
                     (PTy CTy"Zbinop_name"
                       (PTy CTy"Zsize" CTy"Zdest_src")))) qVar"state"))
             ((Call "Zcall" CTy"instruction" (Var "v2" CTy"Zimm_rm"))
               (Apply
                 (Call "dfn'Zcall" (ATy qTy (PTy uTy qTy))
                   (Var "v2" CTy"Zimm_rm")) qVar"state"))
             ((Call "Zcmpxchg" CTy"instruction"
                 (Var "v3" (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))
               (Apply
                 (Call "dfn'Zcmpxchg" (ATy qTy (PTy uTy qTy))
                   (Var "v3" (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))
                 qVar"state"))
             ((Call "Zdiv" CTy"instruction"
                 (Var "v4" (PTy CTy"Zsize" CTy"Zrm")))
               (Apply
                 (Call "dfn'Zdiv" (ATy qTy (PTy uTy qTy))
                   (Var "v4" (PTy CTy"Zsize" CTy"Zrm"))) qVar"state"))
             ((Call "Zjcc" CTy"instruction"
                 (Var "v5" (PTy CTy"Zcond" F64)))
               (Apply
                 (Call "dfn'Zjcc" (ATy qTy (PTy uTy qTy))
                   (Var "v5" (PTy CTy"Zcond" F64))) qVar"state"))
             ((Call "Zjmp" CTy"instruction" (Var "v6" CTy"Zrm"))
               (Apply
                 (Call "dfn'Zjmp" (ATy qTy (PTy uTy qTy))
                   (Var "v6" CTy"Zrm")) qVar"state"))
             ((Call "Zlea" CTy"instruction"
                 (Var "v7" (PTy CTy"Zsize" CTy"Zdest_src")))
               (Apply
                 (Call "dfn'Zlea" (ATy qTy (PTy uTy qTy))
                   (Var "v7" (PTy CTy"Zsize" CTy"Zdest_src")))
                 qVar"state"))
             ((Call "Zloop" CTy"instruction"
                 (Var "v8" (PTy CTy"Zcond" F64)))
               (Apply
                 (Call "dfn'Zloop" (ATy qTy (PTy uTy qTy))
                   (Var "v8" (PTy CTy"Zcond" F64))) qVar"state"))
             ((Call "Zmonop" CTy"instruction"
                 (Var "v9"
                   (PTy CTy"Zmonop_name" (PTy CTy"Zsize" CTy"Zrm"))))
               (Apply
                 (Call "dfn'Zmonop" (ATy qTy (PTy uTy qTy))
                   (Var "v9"
                     (PTy CTy"Zmonop_name" (PTy CTy"Zsize" CTy"Zrm"))))
                 qVar"state"))
             ((Call "Zmov" CTy"instruction"
                 (Var "v10"
                   (PTy CTy"Zcond" (PTy CTy"Zsize" CTy"Zdest_src"))))
               (Apply
                 (Call "dfn'Zmov" (ATy qTy (PTy uTy qTy))
                   (Var "v10"
                     (PTy CTy"Zcond" (PTy CTy"Zsize" CTy"Zdest_src"))))
                 qVar"state"))
             ((Call "Zmovzx" CTy"instruction"
                 (Var "v11"
                   (PTy CTy"Zsize" (PTy CTy"Zdest_src" CTy"Zsize"))))
               (Apply
                 (Call "dfn'Zmovzx" (ATy qTy (PTy uTy qTy))
                   (Var "v11"
                     (PTy CTy"Zsize" (PTy CTy"Zdest_src" CTy"Zsize"))))
                 qVar"state"))
             ((Call "Zmul" CTy"instruction"
                 (Var "v12" (PTy CTy"Zsize" CTy"Zrm")))
               (Apply
                 (Call "dfn'Zmul" (ATy qTy (PTy uTy qTy))
                   (Var "v12" (PTy CTy"Zsize" CTy"Zrm"))) qVar"state"))
             ((Call "Zpop" CTy"instruction" (Var "v13" CTy"Zrm"))
               (Apply
                 (Call "dfn'Zpop" (ATy qTy (PTy uTy qTy))
                   (Var "v13" CTy"Zrm")) qVar"state"))
             ((Call "Zpush" CTy"instruction" (Var "v14" CTy"Zimm_rm"))
               (Apply
                 (Call "dfn'Zpush" (ATy qTy (PTy uTy qTy))
                   (Var "v14" CTy"Zimm_rm")) qVar"state"))
             ((Call "Zret" CTy"instruction" (Var "v15" F64))
               (Apply
                 (Call "dfn'Zret" (ATy qTy (PTy uTy qTy)) (Var "v15" F64))
                 qVar"state"))
             ((Call "Zxadd" CTy"instruction"
                 (Var "v16" (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))
               (Apply
                 (Call "dfn'Zxadd" (ATy qTy (PTy uTy qTy))
                   (Var "v16" (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))
                 qVar"state"))
             ((Call "Zxchg" CTy"instruction"
                 (Var "v17" (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))
               (Apply
                 (Call "dfn'Zxchg" (ATy qTy (PTy uTy qTy))
                   (Var "v17" (PTy CTy"Zsize" (PTy CTy"Zrm" CTy"Zreg"))))
                 qVar"state"))))))


(Def "immediate8" (Var "strm" (LTy F8))
  (CS (Var "strm" (LTy F8))
    (sqbkt ((LLC (sqbkt (Var "b" F8)) (Var "t" (LTy F8)))
             (TP (sqbkt (Mop (SE F64) (Var "b" F8)) (Var "t" (LTy F8)))))
           ((AVar (LTy F8)) (LX (PTy F64 (LTy F8)))))))


(Def "immediate16" (Var "strm" (LTy F8))
  (CS (Var "strm" (LTy F8))
    (sqbkt ((LLC (sqbkt (Var "b1" F8) (Var "b2" F8)) (Var "t" (LTy F8)))
             (TP
               (sqbkt (Mop (SE F64)
                        (CC(sqbkt (Var "b2" F8) (Var "b1" F8))))
                      (Var "t" (LTy F8)))))
           ((AVar (LTy F8)) (LX (PTy F64 (LTy F8)))))))


(Def "immediate32" (Var "strm" (LTy F8))
  (CS (Var "strm" (LTy F8))
    (sqbkt ((LLC
               (sqbkt (Var "b1" F8) (Var "b2" F8) (Var "b3" F8)
                      (Var "b4" F8)) (Var "t" (LTy F8)))
             (TP
               (sqbkt (Mop (SE F64)
                        (CC(sqbkt (Var "b4" F8) (Var "b3" F8)
                                  (Var "b2" F8) (Var "b1" F8))))
                      (Var "t" (LTy F8)))))
           ((AVar (LTy F8)) (LX (PTy F64 (LTy F8)))))))


(Def "immediate64" (Var "strm" (LTy F8))
  (CS (Var "strm" (LTy F8))
    (sqbkt ((LLC
               (sqbkt (Var "b1" F8) (Var "b2" F8) (Var "b3" F8)
                      (Var "b4" F8) (Var "b5" F8) (Var "b6" F8)
                      (Var "b7" F8) (Var "b8" F8)) (Var "t" (LTy F8)))
             (TP
               (sqbkt CC(sqbkt (Var "b8" F8) (Var "b7" F8) (Var "b6" F8)
                               (Var "b5" F8) (Var "b4" F8) (Var "b3" F8)
                               (Var "b2" F8) (Var "b1" F8))
                      (Var "t" (LTy F8)))))
           ((AVar (LTy F8)) (LX (PTy F64 (LTy F8)))))))


(Def "immediate"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "strm" (LTy F8))))
  (CS (Var "size" CTy"Zsize")
    (sqbkt ((Call "Z8" CTy"Zsize" (AVar bTy))
             (Call "immediate8" (PTy F64 (LTy F8)) (Var "strm" (LTy F8))))
           ((Const "Z16" CTy"Zsize")
             (Call "immediate16" (PTy F64 (LTy F8)) (Var "strm" (LTy F8))))
           ((AVar CTy"Zsize")
             (Call "immediate32" (PTy F64 (LTy F8)) (Var "strm" (LTy F8)))))))


(Def "full_immediate"
  (TP (sqbkt (Var "size" CTy"Zsize") (Var "strm" (LTy F8))))
  (ITE (EQ (Var "size" CTy"Zsize") (Const "Z64" CTy"Zsize"))
    (Call "immediate64" (PTy F64 (LTy F8)) (Var "strm" (LTy F8)))
    (Call "immediate" (PTy F64 (LTy F8))
      (TP (sqbkt (Var "size" CTy"Zsize") (Var "strm" (LTy F8)))))))


(Def "rec'REX" (Var "x" F4)
  (Rec CTy"REX"
    (sqbkt (Bop Bit (Var "x" F4) (LN 0)) (Bop Bit (Var "x" F4) (LN 2))
           (Bop Bit (Var "x" F4) (LN 3)) (Bop Bit (Var "x" F4) (LN 1)))))


(Def "reg'REX" (Var "x" CTy"REX")
  (CS (Var "x" CTy"REX")
    (sqbkt ((Rec CTy"REX" (sqbkt bVar"B" bVar"R" bVar"W" bVar"X"))
             (Bop Mdfy
               (Close (TP (sqbkt nVar"i" (AVar bTy)))
                 (ITB
                   (sqbkt ((EQ nVar"i" (LN 3)) bVar"W")
                          ((EQ nVar"i" (LN 2)) bVar"R")
                          ((EQ nVar"i" (LN 1)) bVar"X")) bVar"B"))
               (LW 0 4))))))


(Def "write'rec'REX" (TP (sqbkt (AVar F4) (Var "x" CTy"REX")))
  (Call "reg'REX" F4 (Var "x" CTy"REX")))


(Def "write'reg'REX" (TP (sqbkt (AVar CTy"REX") (Var "x" F4)))
  (Call "rec'REX" CTy"REX" (Var "x" F4)))


(Def "RexReg" (TP (sqbkt bVar"b" (Var "r" (FTy 3))))
  (Mop (Cast CTy"Zreg")
    (CC(sqbkt (Mop (Cast F1) bVar"b") (Var "r" (FTy 3))))))


(Def "readDisplacement"
  (TP (sqbkt (Var "Mod" (FTy 2)) (Var "strm" (LTy F8))))
  (ITB
    (sqbkt ((EQ (Var "Mod" (FTy 2)) (LW 1 2))
             (Call "immediate8" (PTy F64 (LTy F8)) (Var "strm" (LTy F8))))
           ((EQ (Var "Mod" (FTy 2)) (LW 2 2))
             (Call "immediate32" (PTy F64 (LTy F8)) (Var "strm" (LTy F8)))))
    (TP (sqbkt (LW 0 64) (Var "strm" (LTy F8))))))


(Def "readSibDisplacement"
  (TP (sqbkt (Var "Mod" (FTy 2)) (Var "strm" (LTy F8))))
  (ITB
    (sqbkt ((EQ (Var "Mod" (FTy 2)) (LW 0 2))
             (TP (sqbkt (LW 0 64) (Var "strm" (LTy F8)))))
           ((EQ (Var "Mod" (FTy 2)) (LW 1 2))
             (Call "immediate8" (PTy F64 (LTy F8)) (Var "strm" (LTy F8)))))
    (Call "immediate32" (PTy F64 (LTy F8)) (Var "strm" (LTy F8)))))


(Def "readSIB"
  (TP
    (sqbkt (Var "REX" CTy"REX") (Var "Mod" (FTy 2)) (Var "strm" (LTy F8))))
  (CS (Var "strm" (LTy F8))
    (sqbkt ((LLC (sqbkt (Var "v#0" F8)) (Var "v#1" (LTy F8)))
             (Let (Var "base" CTy"Zreg")
               (Call "RexReg" CTy"Zreg"
                 (TP
                   (sqbkt (Dest "B" bTy (Var "REX" CTy"REX"))
                          (EX (Var "v#0" F8) (LN 2) (LN 0) (FTy 3)))))
               (Let (Var "index" CTy"Zreg")
                 (Call "RexReg" CTy"Zreg"
                   (TP
                     (sqbkt (Dest "X" bTy (Var "REX" CTy"REX"))
                            (EX (Var "v#0" F8) (LN 5) (LN 3) (FTy 3)))))
                 (Let (Var "scaled_index" (OTy (PTy (FTy 2) CTy"Zreg")))
                   (ITE (EQ (Var "index" CTy"Zreg") (LC "RSP" CTy"Zreg"))
                     (LO (PTy (FTy 2) CTy"Zreg"))
                     (Mop Some
                       (TP
                         (sqbkt (EX (Var "v#0" F8) (LN 7) (LN 6) (FTy 2))
                                (Var "index" CTy"Zreg")))))
                   (ITE (EQ (Var "base" CTy"Zreg") (LC "RBP" CTy"Zreg"))
                     (Let
                       (TP
                         (sqbkt (Var "displacement" F64)
                                (Var "strm2" (LTy F8))))
                       (Call "readSibDisplacement" (PTy F64 (LTy F8))
                         (TP
                           (sqbkt (Var "Mod" (FTy 2)) (Var "v#1" (LTy F8)))))
                       (TP
                         (sqbkt (Call "Zm" CTy"Zrm"
                                  (TP
                                    (sqbkt (Var "scaled_index"
                                             (OTy (PTy (FTy 2) CTy"Zreg")))
                                           (ITE
                                             (EQ (Var "Mod" (FTy 2))
                                               (LW 0 2))
                                             (Const "ZnoBase" CTy"Zbase")
                                             (Call "ZregBase" CTy"Zbase"
                                               (Var "base" CTy"Zreg")))
                                           (Var "displacement" F64))))
                                (Var "strm2" (LTy F8)))))
                     (Let
                       (TP
                         (sqbkt (Var "displacement" F64)
                                (Var "strm2" (LTy F8))))
                       (Call "readDisplacement" (PTy F64 (LTy F8))
                         (TP
                           (sqbkt (Var "Mod" (FTy 2)) (Var "v#1" (LTy F8)))))
                       (TP
                         (sqbkt (Call "Zm" CTy"Zrm"
                                  (TP
                                    (sqbkt (Var "scaled_index"
                                             (OTy (PTy (FTy 2) CTy"Zreg")))
                                           (Call "ZregBase" CTy"Zbase"
                                             (Var "base" CTy"Zreg"))
                                           (Var "displacement" F64))))
                                (Var "strm2" (LTy F8))))))))))
           ((AVar (LTy F8))
             (TP (sqbkt (LX CTy"Zrm") (Var "strm" (LTy F8))))))))


(Def "readModRM" (TP (sqbkt (Var "REX" CTy"REX") (Var "strm" (LTy F8))))
  (CS (Var "strm" (LTy F8))
    (sqbkt ((LLC (sqbkt (Var "v#0" F8)) (Var "v#1" (LTy F8)))
             (CS (TP (sqbkt (BL 8 (Var "v#0" F8)) (Var "v#1" (LTy F8))))
               (sqbkt ((TP
                          (sqbkt (TP
                                   (sqbkt LF LF (AVar bTy) (AVar bTy)
                                          (AVar bTy) LT LF LT))
                                 (Var "strm1" (LTy F8))))
                        (Let
                          (TP
                            (sqbkt (Var "displacement" F64)
                                   (Var "strm2" (LTy F8))))
                          (Call "immediate32" (PTy F64 (LTy F8))
                            (Var "strm1" (LTy F8)))
                          (TP
                            (sqbkt (Call "RexReg" CTy"Zreg"
                                     (TP
                                       (sqbkt (Dest "R" bTy
                                                (Var "REX" CTy"REX"))
                                              (EX (Var "v#0" F8) (LN 5)
                                                (LN 3) (FTy 3)))))
                                   (Call "Zm" CTy"Zrm"
                                     (TP
                                       (sqbkt (LO (PTy (FTy 2) CTy"Zreg"))
                                              (Const "ZripBase"
                                                CTy"Zbase")
                                              (Var "displacement" F64))))
                                   (Var "strm2" (LTy F8))))))
                      ((TP
                          (sqbkt (TP
                                   (sqbkt LT LT (AVar bTy) (AVar bTy)
                                          (AVar bTy) (AVar bTy) (AVar bTy)
                                          (AVar bTy)))
                                 (Var "strm1" (LTy F8))))
                        (TP
                          (sqbkt (Call "RexReg" CTy"Zreg"
                                   (TP
                                     (sqbkt (Dest "R" bTy
                                              (Var "REX" CTy"REX"))
                                            (EX (Var "v#0" F8) (LN 5)
                                              (LN 3) (FTy 3)))))
                                 (Call "Zr" CTy"Zrm"
                                   (Call "RexReg" CTy"Zreg"
                                     (TP
                                       (sqbkt (Dest "B" bTy
                                                (Var "REX" CTy"REX"))
                                              (EX (Var "v#0" F8) (LN 2)
                                                (LN 0) (FTy 3))))))
                                 (Var "strm1" (LTy F8)))))
                      ((TP
                          (sqbkt (TP
                                   (sqbkt (AVar bTy) (AVar bTy) (AVar bTy)
                                          (AVar bTy) (AVar bTy) LT LF LF))
                                 (Var "strm1" (LTy F8))))
                        (Let
                          (TP
                            (sqbkt (Var "sib" CTy"Zrm")
                                   (Var "strm2" (LTy F8))))
                          (Call "readSIB" (PTy CTy"Zrm" (LTy F8))
                            (TP
                              (sqbkt (Var "REX" CTy"REX")
                                     (EX (Var "v#0" F8) (LN 7) (LN 6)
                                       (FTy 2)) (Var "strm1" (LTy F8)))))
                          (TP
                            (sqbkt (Call "RexReg" CTy"Zreg"
                                     (TP
                                       (sqbkt (Dest "R" bTy
                                                (Var "REX" CTy"REX"))
                                              (EX (Var "v#0" F8) (LN 5)
                                                (LN 3) (FTy 3)))))
                                   (Var "sib" CTy"Zrm")
                                   (Var "strm2" (LTy F8))))))
                      ((TP
                          (sqbkt (TP
                                   (sqbkt (AVar bTy) (AVar bTy) (AVar bTy)
                                          (AVar bTy) (AVar bTy) (AVar bTy)
                                          (AVar bTy) (AVar bTy)))
                                 (Var "strm1" (LTy F8))))
                        (Let
                          (TP
                            (sqbkt (Var "displacement" F64)
                                   (Var "strm2" (LTy F8))))
                          (Call "readDisplacement" (PTy F64 (LTy F8))
                            (TP
                              (sqbkt (EX (Var "v#0" F8) (LN 7) (LN 6)
                                       (FTy 2)) (Var "strm1" (LTy F8)))))
                          (TP
                            (sqbkt (Call "RexReg" CTy"Zreg"
                                     (TP
                                       (sqbkt (Dest "R" bTy
                                                (Var "REX" CTy"REX"))
                                              (EX (Var "v#0" F8) (LN 5)
                                                (LN 3) (FTy 3)))))
                                   (Call "Zm" CTy"Zrm"
                                     (TP
                                       (sqbkt (LO (PTy (FTy 2) CTy"Zreg"))
                                              (Call "ZregBase" CTy"Zbase"
                                                (Call "RexReg" CTy"Zreg"
                                                  (TP
                                                    (sqbkt (Dest "B" bTy
                                                             (Var "REX"
                                                               CTy"REX"))
                                                           (EX
                                                             (Var "v#0"
                                                               F8) (LN 2)
                                                             (LN 0)
                                                             (FTy 3))))))
                                              (Var "displacement" F64))))
                                   (Var "strm2" (LTy F8)))))))))
           ((AVar (LTy F8))
             (TP
               (sqbkt (LX CTy"Zreg") (LX CTy"Zrm") (Var "strm" (LTy F8))))))))


(Def "readOpcodeModRM"
  (TP (sqbkt (Var "REX" CTy"REX") (Var "strm" (LTy F8))))
  (Let
    (TP
      (sqbkt (Var "opcode" CTy"Zreg") (Var "rm" CTy"Zrm")
             (Var "strm1" (LTy F8))))
    (Call "readModRM" (PTy CTy"Zreg" (PTy CTy"Zrm" (LTy F8)))
      (TP (sqbkt (Var "REX" CTy"REX") (Var "strm" (LTy F8)))))
    (TP
      (sqbkt (Mop (Cast (FTy 3))
               (Bop Mod (Mop (Cast nTy) (Var "opcode" CTy"Zreg")) (LN 8)))
             (Var "rm" CTy"Zrm") (Var "strm1" (LTy F8))))))


(Def "prefixGroup" (Var "b" F8)
  (CS (Var "b" F8)
    (sqbkt ((LW 240 8) (LN 1)) ((LW 242 8) (LN 1)) ((LW 243 8) (LN 1))
           ((LW 38 8) (LN 2)) ((LW 46 8) (LN 2)) ((LW 54 8) (LN 2))
           ((LW 62 8) (LN 2)) ((LW 100 8) (LN 2)) ((LW 101 8) (LN 2))
           ((LW 102 8) (LN 3)) ((LW 103 8) (LN 4))
           ((AVar F8)
             (ITE (EQ (EX (Var "b" F8) (LN 7) (LN 4) F4) (LW 4 4)) (LN 5)
               (LN 0))))))


(Def "readPrefix"
  (TP
    (sqbkt (Var "s" (STy nTy)) (Var "p" (LTy F8)) (Var "strm" (LTy F8))))
  (CS (Var "strm" (LTy F8))
    (sqbkt ((LLC (sqbkt (Var "h" F8)) (Var "strm1" (LTy F8)))
             (Let nVar"group" (Call "prefixGroup" nTy (Var "h" F8))
               (ITB
                 (sqbkt ((EQ nVar"group" (LN 0))
                          (Mop Some
                            (TP
                              (sqbkt (Var "p" (LTy F8)) LF
                                     (Call "rec'REX" CTy"REX" (LW 0 4))
                                     (Var "strm" (LTy F8))))))
                        ((EQ nVar"group" (LN 5))
                          (Mop Some
                            (TP
                              (sqbkt (Var "p" (LTy F8)) LT
                                     (Call "rec'REX" CTy"REX"
                                       (EX (Var "h" F8) (LN 3) (LN 0) F4))
                                     (Var "strm1" (LTy F8))))))
                        ((Bop In nVar"group" (Var "s" (STy nTy)))
                          (LO
                            (PTy (LTy F8)
                              (PTy bTy (PTy CTy"REX" (LTy F8)))))))
                 (Call "readPrefix"
                   (OTy (PTy (LTy F8) (PTy bTy (PTy CTy"REX" (LTy F8)))))
                   (TP
                     (sqbkt (Bop Insert nVar"group" (Var "s" (STy nTy)))
                            (LLC (sqbkt (Var "h" F8)) (Var "p" (LTy F8)))
                            (Var "strm1" (LTy F8))))))))
           ((LNL F8)
             (Mop Some
               (TP
                 (sqbkt (Var "p" (LTy F8)) LF (LX CTy"REX")
                        (Var "strm" (LTy F8)))))))))


(Def "readPrefixes" (Var "strm" (LTy F8))
  (Call "readPrefix"
    (OTy (PTy (LTy F8) (PTy bTy (PTy CTy"REX" (LTy F8)))))
    (TP (sqbkt (LE nTy) (LNL F8) (Var "strm" (LTy F8))))))


(Def "OpSize"
  (TP (sqbkt bVar"have_rex" bVar"w" (Var "v" F1) bVar"override"))
  (ITB
    (sqbkt ((EQ (Var "v" F1) (LW 0 1))
             (Call "Z8" CTy"Zsize" bVar"have_rex"))
           (bVar"w" (Const "Z64" CTy"Zsize"))
           (bVar"override" (Const "Z16" CTy"Zsize")))
    (Const "Z32" CTy"Zsize")))


(Def "isZm" (Var "rm" CTy"Zrm")
  (CS (Var "rm" CTy"Zrm")
    (sqbkt ((Call "Zm" CTy"Zrm"
               (AVar
                 (PTy (OTy (PTy (FTy 2) CTy"Zreg")) (PTy CTy"Zbase" F64))))
             LT) ((AVar CTy"Zrm") LF))))


(Def "x64_decode" (Var "strm" (LTy F8))
  (CS
    (Call "readPrefixes"
      (OTy (PTy (LTy F8) (PTy bTy (PTy CTy"REX" (LTy F8)))))
      (Var "strm" (LTy F8)))
    (sqbkt ((LO (PTy (LTy F8) (PTy bTy (PTy CTy"REX" (LTy F8)))))
             (Call "Zdec_fail" CTy"Zinst" (LS"Bad prefix")))
           ((Mop Some
               (TP
                 (sqbkt (Var "p" (LTy F8)) bVar"have_rex"
                        (Var "REX" CTy"REX") (Var "strm1" (LTy F8)))))
             (Let bVar"op_size_override"
               (Bop In (LW 102 8) (Mop SofL (Var "p" (LTy F8))))
               (ITE
                 (Bop And (Dest "W" bTy (Var "REX" CTy"REX"))
                   bVar"op_size_override")
                 (Call "Zdec_fail" CTy"Zinst"
                   (LS"REX.W together with override prefix"))
                 (CS (Var "strm1" (LTy F8))
                   (sqbkt ((LLC (sqbkt (Var "v#0" F8))
                              (Var "v#1" (LTy F8)))
                            (CS
                              (TP
                                (sqbkt (BL 8 (Var "v#0" F8))
                                       (Var "v#1" (LTy F8))))
                              (sqbkt ((TP
                                         (sqbkt (TP
                                                  (sqbkt LF LF (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy) LF
                                                         (AVar bTy)
                                                         (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "reg" CTy"Zreg")
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readModRM"
                                           (PTy CTy"Zreg"
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zbinop"
                                                      CTy"instruction"
                                                      (TP
                                                        (sqbkt (Mop
                                                                 (Cast
                                                                   CTy"Zbinop_name")
                                                                 (EX
                                                                   (Var
                                                                     "v#0"
                                                                     F8)
                                                                   (LN 5)
                                                                   (LN 3)
                                                                   (FTy 3)))
                                                               (Call
                                                                 "OpSize"
                                                                 CTy"Zsize"
                                                                 (TP
                                                                   (sqbkt bVar"have_rex"
                                                                          (Dest
                                                                            "W"
                                                                            bTy
                                                                            (Var
                                                                              "REX"
                                                                              CTy"REX"))
                                                                          (EX
                                                                            (Var
                                                                              "v#0"
                                                                              F8)
                                                                            (LN
                                                                              0)
                                                                            (LN
                                                                              0)
                                                                            F1)
                                                                          bVar"op_size_override")))
                                                               (ITE
                                                                 (EQ
                                                                   (EX
                                                                     (Var
                                                                       "v#0"
                                                                       F8)
                                                                     (LN 1)
                                                                     (LN 1)
                                                                     F1)
                                                                   (LW 0
                                                                     1))
                                                                 (Call
                                                                   "Zrm_r"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Var
                                                                              "rm"
                                                                              CTy"Zrm")
                                                                            (Var
                                                                              "reg"
                                                                              CTy"Zreg"))))
                                                                 (Call
                                                                   "Zr_rm"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Var
                                                                              "reg"
                                                                              CTy"Zreg")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm"))))))))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LF LF (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy) LT LF
                                                         (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "size" CTy"Zsize")
                                         (Call "OpSize" CTy"Zsize"
                                           (TP
                                             (sqbkt bVar"have_rex"
                                                    (Dest "W" bTy
                                                      (Var "REX" CTy"REX"))
                                                    (EX (Var "v#0" F8)
                                                      (LN 0) (LN 0) F1)
                                                    bVar"op_size_override")))
                                         (Let
                                           (TP
                                             (sqbkt (Var "imm" F64)
                                                    (Var "strm3" (LTy F8))))
                                           (Call "immediate"
                                             (PTy F64 (LTy F8))
                                             (TP
                                               (sqbkt (Var "size"
                                                        CTy"Zsize")
                                                      (Var "strm2"
                                                        (LTy F8)))))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zbinop"
                                                        CTy"instruction"
                                                        (TP
                                                          (sqbkt (Mop
                                                                   (Cast
                                                                     CTy"Zbinop_name")
                                                                   (EX
                                                                     (Var
                                                                       "v#0"
                                                                       F8)
                                                                     (LN 5)
                                                                     (LN 3)
                                                                     (FTy
                                                                       3)))
                                                                 (Var
                                                                   "size"
                                                                   CTy"Zsize")
                                                                 (Call
                                                                   "Zrm_i"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Call
                                                                              "Zr"
                                                                              CTy"Zrm"
                                                                              (LC
                                                                                "RAX"
                                                                                CTy"Zreg"))
                                                                            (Var
                                                                              "imm"
                                                                              F64)))))))
                                                      (Var "strm3"
                                                        (LTy F8))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LF LT LF LT
                                                         (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "reg" CTy"Zrm")
                                         (Call "Zr" CTy"Zrm"
                                           (Mop (Cast CTy"Zreg")
                                             (CC(sqbkt (Mop (Cast F1)
                                                         (Dest "B" bTy
                                                           (Var "REX"
                                                             CTy"REX")))
                                                       (EX (Var "v#0" F8)
                                                         (LN 2) (LN 0)
                                                         (FTy 3))))))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (ITE
                                                      (EQ
                                                        (EX (Var "v#0" F8)
                                                          (LN 3) (LN 3)
                                                          F1) (LW 0 1))
                                                      (Call "Zpush"
                                                        CTy"instruction"
                                                        (Call "Zrm"
                                                          CTy"Zimm_rm"
                                                          (Var "reg"
                                                            CTy"Zrm")))
                                                      (Call "Zpop"
                                                        CTy"instruction"
                                                        (Var "reg"
                                                          CTy"Zrm")))
                                                    (Var "strm2" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LF LT LT LF LT LF
                                                         (AVar bTy) LF))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "imm" F64)
                                                  (Var "strm3" (LTy F8))))
                                         (ITE
                                           (EQ
                                             (EX (Var "v#0" F8) (LN 1)
                                               (LN 1) F1) (LW 1 1))
                                           (Call "immediate8"
                                             (PTy F64 (LTy F8))
                                             (Var "strm2" (LTy F8)))
                                           (Call "immediate32"
                                             (PTy F64 (LTy F8))
                                             (Var "strm2" (LTy F8))))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zpush"
                                                      CTy"instruction"
                                                      (Call "Zimm"
                                                        CTy"Zimm_rm"
                                                        (Var "imm" F64)))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LF LT LT LT
                                                         (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "imm" F64)
                                                  (Var "strm3" (LTy F8))))
                                         (Call "immediate8"
                                           (PTy F64 (LTy F8))
                                           (Var "strm2" (LTy F8)))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zjcc"
                                                      CTy"instruction"
                                                      (TP
                                                        (sqbkt (Mop
                                                                 (Cast
                                                                   CTy"Zcond")
                                                                 (EX
                                                                   (Var
                                                                     "v#0"
                                                                     F8)
                                                                   (LN 3)
                                                                   (LN 0)
                                                                   F4))
                                                               (Var "imm"
                                                                 F64))))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LF LF LF LF
                                                         LF (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "size" CTy"Zsize")
                                         (Call "OpSize" CTy"Zsize"
                                           (TP
                                             (sqbkt bVar"have_rex"
                                                    (Dest "W" bTy
                                                      (Var "REX" CTy"REX"))
                                                    (EX (Var "v#0" F8)
                                                      (LN 0) (LN 0) F1)
                                                    bVar"op_size_override")))
                                         (Let
                                           (TP
                                             (sqbkt (Var "opcode" (FTy 3))
                                                    (Var "rm" CTy"Zrm")
                                                    (Var "strm3" (LTy F8))))
                                           (Call "readOpcodeModRM"
                                             (PTy (FTy 3)
                                               (PTy CTy"Zrm" (LTy F8)))
                                             (TP
                                               (sqbkt (Var "REX" CTy"REX")
                                                      (Var "strm2"
                                                        (LTy F8)))))
                                           (Let
                                             (TP
                                               (sqbkt (Var "imm" F64)
                                                      (Var "strm4"
                                                        (LTy F8))))
                                             (Call "immediate"
                                               (PTy F64 (LTy F8))
                                               (TP
                                                 (sqbkt (Var "size"
                                                          CTy"Zsize")
                                                        (Var "strm3"
                                                          (LTy F8)))))
                                             (Call "Zfull_inst" CTy"Zinst"
                                               (TP
                                                 (sqbkt (Var "p" (LTy F8))
                                                        (Call "Zbinop"
                                                          CTy"instruction"
                                                          (TP
                                                            (sqbkt (Mop
                                                                     (Cast
                                                                       CTy"Zbinop_name")
                                                                     (Var
                                                                       "opcode"
                                                                       (FTy
                                                                         3)))
                                                                   (Var
                                                                     "size"
                                                                     CTy"Zsize")
                                                                   (Call
                                                                     "Zrm_i"
                                                                     CTy"Zdest_src"
                                                                     (TP
                                                                       (sqbkt (Var
                                                                                "rm"
                                                                                CTy"Zrm")
                                                                              (Var
                                                                                "imm"
                                                                                F64)))))))
                                                        (Var "strm4"
                                                          (LTy F8)))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LF LF LF LF
                                                         LT LT))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "opcode" (FTy 3))
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readOpcodeModRM"
                                           (PTy (FTy 3)
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (Let
                                           (TP
                                             (sqbkt (Var "imm" F64)
                                                    (Var "strm4" (LTy F8))))
                                           (Call "immediate8"
                                             (PTy F64 (LTy F8))
                                             (Var "strm3" (LTy F8)))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zbinop"
                                                        CTy"instruction"
                                                        (TP
                                                          (sqbkt (Mop
                                                                   (Cast
                                                                     CTy"Zbinop_name")
                                                                   (Var
                                                                     "opcode"
                                                                     (FTy
                                                                       3)))
                                                                 (Call
                                                                   "OpSize"
                                                                   CTy"Zsize"
                                                                   (TP
                                                                     (sqbkt LF
                                                                            (Dest
                                                                              "W"
                                                                              bTy
                                                                              (Var
                                                                                "REX"
                                                                                CTy"REX"))
                                                                            (LW
                                                                              1
                                                                              1)
                                                                            bVar"op_size_override")))
                                                                 (Call
                                                                   "Zrm_i"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Var
                                                                              "rm"
                                                                              CTy"Zrm")
                                                                            (Var
                                                                              "imm"
                                                                              F64)))))))
                                                      (Var "strm4"
                                                        (LTy F8))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LF LF LF LT
                                                         LF (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "reg" CTy"Zreg")
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readModRM"
                                           (PTy CTy"Zreg"
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zbinop"
                                                      CTy"instruction"
                                                      (TP
                                                        (sqbkt (LC "Ztest"
                                                                 CTy"Zbinop_name")
                                                               (Call
                                                                 "OpSize"
                                                                 CTy"Zsize"
                                                                 (TP
                                                                   (sqbkt bVar"have_rex"
                                                                          (Dest
                                                                            "W"
                                                                            bTy
                                                                            (Var
                                                                              "REX"
                                                                              CTy"REX"))
                                                                          (EX
                                                                            (Var
                                                                              "v#0"
                                                                              F8)
                                                                            (LN
                                                                              0)
                                                                            (LN
                                                                              0)
                                                                            F1)
                                                                          bVar"op_size_override")))
                                                               (Call
                                                                 "Zrm_r"
                                                                 CTy"Zdest_src"
                                                                 (TP
                                                                   (sqbkt (Var
                                                                            "rm"
                                                                            CTy"Zrm")
                                                                          (Var
                                                                            "reg"
                                                                            CTy"Zreg")))))))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LF LF LF LT
                                                         LT (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "reg" CTy"Zreg")
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readModRM"
                                           (PTy CTy"Zreg"
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zxchg"
                                                      CTy"instruction"
                                                      (TP
                                                        (sqbkt (Call
                                                                 "OpSize"
                                                                 CTy"Zsize"
                                                                 (TP
                                                                   (sqbkt bVar"have_rex"
                                                                          (Dest
                                                                            "W"
                                                                            bTy
                                                                            (Var
                                                                              "REX"
                                                                              CTy"REX"))
                                                                          (EX
                                                                            (Var
                                                                              "v#0"
                                                                              F8)
                                                                            (LN
                                                                              0)
                                                                            (LN
                                                                              0)
                                                                            F1)
                                                                          bVar"op_size_override")))
                                                               (Var "rm"
                                                                 CTy"Zrm")
                                                               (Var "reg"
                                                                 CTy"Zreg"))))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LF LF LT LF
                                                         (AVar bTy)
                                                         (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "reg" CTy"Zreg")
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readModRM"
                                           (PTy CTy"Zreg"
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zmov"
                                                      CTy"instruction"
                                                      (TP
                                                        (sqbkt (LC
                                                                 "Z_ALWAYS"
                                                                 CTy"Zcond")
                                                               (Call
                                                                 "OpSize"
                                                                 CTy"Zsize"
                                                                 (TP
                                                                   (sqbkt bVar"have_rex"
                                                                          (Dest
                                                                            "W"
                                                                            bTy
                                                                            (Var
                                                                              "REX"
                                                                              CTy"REX"))
                                                                          (EX
                                                                            (Var
                                                                              "v#0"
                                                                              F8)
                                                                            (LN
                                                                              0)
                                                                            (LN
                                                                              0)
                                                                            F1)
                                                                          bVar"op_size_override")))
                                                               (ITE
                                                                 (EQ
                                                                   (EX
                                                                     (Var
                                                                       "v#0"
                                                                       F8)
                                                                     (LN 1)
                                                                     (LN 1)
                                                                     F1)
                                                                   (LW 0
                                                                     1))
                                                                 (Call
                                                                   "Zrm_r"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Var
                                                                              "rm"
                                                                              CTy"Zrm")
                                                                            (Var
                                                                              "reg"
                                                                              CTy"Zreg"))))
                                                                 (Call
                                                                   "Zr_rm"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Var
                                                                              "reg"
                                                                              CTy"Zreg")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm"))))))))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LF LF LT LT
                                                         LF LT))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "reg" CTy"Zreg")
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readModRM"
                                           (PTy CTy"Zreg"
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (ITE
                                           (Call "isZm" bTy
                                             (Var "rm" CTy"Zrm"))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zlea"
                                                        CTy"instruction"
                                                        (TP
                                                          (sqbkt (Call
                                                                   "OpSize"
                                                                   CTy"Zsize"
                                                                   (TP
                                                                     (sqbkt LT
                                                                            (Dest
                                                                              "W"
                                                                              bTy
                                                                              (Var
                                                                                "REX"
                                                                                CTy"REX"))
                                                                            (LW
                                                                              1
                                                                              1)
                                                                            bVar"op_size_override")))
                                                                 (Call
                                                                   "Zr_rm"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Var
                                                                              "reg"
                                                                              CTy"Zreg")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm")))))))
                                                      (Var "strm3"
                                                        (LTy F8)))))
                                           (Call "Zdec_fail" CTy"Zinst"
                                             (LS
                                               "LEA with register argument")))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LF LF LT LT
                                                         LT LT))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "opcode" (FTy 3))
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readOpcodeModRM"
                                           (PTy (FTy 3)
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (ITE
                                           (EQ (Var "opcode" (FTy 3))
                                             (LW 0 3))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zpop"
                                                        CTy"instruction"
                                                        (Var "rm"
                                                          CTy"Zrm"))
                                                      (Var "strm3"
                                                        (LTy F8)))))
                                           (Call "Zdec_fail" CTy"Zinst"
                                             (LS
                                               "Unsupported opcode: Group 1a")))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LF LT LF
                                                         (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "reg" CTy"Zreg")
                                         (Call "RexReg" CTy"Zreg"
                                           (TP
                                             (sqbkt (Dest "B" bTy
                                                      (Var "REX" CTy"REX"))
                                                    (EX (Var "v#0" F8)
                                                      (LN 2) (LN 0)
                                                      (FTy 3)))))
                                         (ITE
                                           (EQ (Var "reg" CTy"Zreg")
                                             (LC "RAX" CTy"Zreg"))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Const "Znop"
                                                        CTy"instruction")
                                                      (Var "strm2"
                                                        (LTy F8)))))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zxchg"
                                                        CTy"instruction"
                                                        (TP
                                                          (sqbkt (Call
                                                                   "OpSize"
                                                                   CTy"Zsize"
                                                                   (TP
                                                                     (sqbkt LT
                                                                            (Dest
                                                                              "W"
                                                                              bTy
                                                                              (Var
                                                                                "REX"
                                                                                CTy"REX"))
                                                                            (LW
                                                                              1
                                                                              1)
                                                                            bVar"op_size_override")))
                                                                 (Call
                                                                   "Zr"
                                                                   CTy"Zrm"
                                                                   (LC
                                                                     "RAX"
                                                                     CTy"Zreg"))
                                                                 (Var
                                                                   "reg"
                                                                   CTy"Zreg"))))
                                                      (Var "strm2"
                                                        (LTy F8))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LT LF LT LF
                                                         LF (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "size" CTy"Zsize")
                                         (Call "OpSize" CTy"Zsize"
                                           (TP
                                             (sqbkt LT
                                                    (Dest "W" bTy
                                                      (Var "REX" CTy"REX"))
                                                    (EX (Var "v#0" F8)
                                                      (LN 0) (LN 0) F1)
                                                    bVar"op_size_override")))
                                         (Let
                                           (TP
                                             (sqbkt (Var "imm" F64)
                                                    (Var "strm3" (LTy F8))))
                                           (Call "immediate"
                                             (PTy F64 (LTy F8))
                                             (TP
                                               (sqbkt (Var "size"
                                                        CTy"Zsize")
                                                      (Var "strm2"
                                                        (LTy F8)))))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zbinop"
                                                        CTy"instruction"
                                                        (TP
                                                          (sqbkt (LC
                                                                   "Ztest"
                                                                   CTy"Zbinop_name")
                                                                 (Var
                                                                   "size"
                                                                   CTy"Zsize")
                                                                 (Call
                                                                   "Zrm_i"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Call
                                                                              "Zr"
                                                                              CTy"Zrm"
                                                                              (LC
                                                                                "RAX"
                                                                                CTy"Zreg"))
                                                                            (Var
                                                                              "imm"
                                                                              F64)))))))
                                                      (Var "strm3"
                                                        (LTy F8))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LF LT LT
                                                         (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy)
                                                         (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "size" CTy"Zsize")
                                         (Call "OpSize" CTy"Zsize"
                                           (TP
                                             (sqbkt bVar"have_rex"
                                                    (Dest "W" bTy
                                                      (Var "REX" CTy"REX"))
                                                    (EX (Var "v#0" F8)
                                                      (LN 3) (LN 3) F1)
                                                    bVar"op_size_override")))
                                         (Let
                                           (TP
                                             (sqbkt (Var "imm" F64)
                                                    (Var "strm3" (LTy F8))))
                                           (Call "full_immediate"
                                             (PTy F64 (LTy F8))
                                             (TP
                                               (sqbkt (Var "size"
                                                        CTy"Zsize")
                                                      (Var "strm2"
                                                        (LTy F8)))))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zmov"
                                                        CTy"instruction"
                                                        (TP
                                                          (sqbkt (LC
                                                                   "Z_ALWAYS"
                                                                   CTy"Zcond")
                                                                 (Var
                                                                   "size"
                                                                   CTy"Zsize")
                                                                 (Call
                                                                   "Zrm_i"
                                                                   CTy"Zdest_src"
                                                                   (TP
                                                                     (sqbkt (Call
                                                                              "Zr"
                                                                              CTy"Zrm"
                                                                              (Mop
                                                                                (Cast
                                                                                  CTy"Zreg")
                                                                                (CC(sqbkt (Mop
                                                                                            (Cast
                                                                                              F1)
                                                                                            (Dest
                                                                                              "B"
                                                                                              bTy
                                                                                              (Var
                                                                                                "REX"
                                                                                                CTy"REX")))
                                                                                          (EX
                                                                                            (Var
                                                                                              "v#0"
                                                                                              F8)
                                                                                            (LN
                                                                                              2)
                                                                                            (LN
                                                                                              0)
                                                                                            (FTy
                                                                                              3))))))
                                                                            (Var
                                                                              "imm"
                                                                              F64)))))))
                                                      (Var "strm3"
                                                        (LTy F8))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LF LF LF LF
                                                         LF (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "opcode" (FTy 3))
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readOpcodeModRM"
                                           (PTy (FTy 3)
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (Let
                                           (TP
                                             (sqbkt (Var "imm" F64)
                                                    (Var "strm4" (LTy F8))))
                                           (Call "immediate8"
                                             (PTy F64 (LTy F8))
                                             (Var "strm3" (LTy F8)))
                                           (ITE
                                             (EQ (Var "opcode" (FTy 3))
                                               (LW 6 3))
                                             (Call "Zdec_fail" CTy"Zinst"
                                               (LS
                                                 "Unsupported opcode: Shift Group 2"))
                                             (Call "Zfull_inst" CTy"Zinst"
                                               (TP
                                                 (sqbkt (Var "p" (LTy F8))
                                                        (Call "Zbinop"
                                                          CTy"instruction"
                                                          (TP
                                                            (sqbkt (Mop
                                                                     (Cast
                                                                       CTy"Zbinop_name")
                                                                     (Bop
                                                                       Add
                                                                       (Mop
                                                                         (Cast
                                                                           nTy)
                                                                         (Var
                                                                           "opcode"
                                                                           (FTy
                                                                             3)))
                                                                       (LN
                                                                         8)))
                                                                   (Call
                                                                     "OpSize"
                                                                     CTy"Zsize"
                                                                     (TP
                                                                       (sqbkt bVar"have_rex"
                                                                              (Dest
                                                                                "W"
                                                                                bTy
                                                                                (Var
                                                                                  "REX"
                                                                                  CTy"REX"))
                                                                              (EX
                                                                                (Var
                                                                                  "v#0"
                                                                                  F8)
                                                                                (LN
                                                                                  0)
                                                                                (LN
                                                                                  0)
                                                                                F1)
                                                                              bVar"op_size_override")))
                                                                   (Call
                                                                     "Zrm_i"
                                                                     CTy"Zdest_src"
                                                                     (TP
                                                                       (sqbkt (Var
                                                                                "rm"
                                                                                CTy"Zrm")
                                                                              (Var
                                                                                "imm"
                                                                                F64)))))))
                                                        (Var "strm4"
                                                          (LTy F8)))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LF LF LF LF
                                                         LT (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (ITE
                                         (EQ
                                           (EX (Var "v#0" F8) (LN 0)
                                             (LN 0) F1) (LW 0 1))
                                         (Let
                                           (TP
                                             (sqbkt (Var "imm" F64)
                                                    (Var "strm3" (LTy F8))))
                                           (Call "immediate16"
                                             (PTy F64 (LTy F8))
                                             (Var "strm2" (LTy F8)))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zret"
                                                        CTy"instruction"
                                                        (Var "imm" F64))
                                                      (Var "strm3"
                                                        (LTy F8))))))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zret"
                                                      CTy"instruction"
                                                      (LW 0 64))
                                                    (Var "strm2" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LF LF LF LT
                                                         LT (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "size" CTy"Zsize")
                                         (Call "OpSize" CTy"Zsize"
                                           (TP
                                             (sqbkt bVar"have_rex"
                                                    (Dest "W" bTy
                                                      (Var "REX" CTy"REX"))
                                                    (EX (Var "v#0" F8)
                                                      (LN 0) (LN 0) F1)
                                                    bVar"op_size_override")))
                                         (Let
                                           (TP
                                             (sqbkt (Var "opcode" (FTy 3))
                                                    (Var "rm" CTy"Zrm")
                                                    (Var "strm3" (LTy F8))))
                                           (Call "readOpcodeModRM"
                                             (PTy (FTy 3)
                                               (PTy CTy"Zrm" (LTy F8)))
                                             (TP
                                               (sqbkt (Var "REX" CTy"REX")
                                                      (Var "strm2"
                                                        (LTy F8)))))
                                           (Let
                                             (TP
                                               (sqbkt (Var "imm" F64)
                                                      (Var "strm4"
                                                        (LTy F8))))
                                             (Call "immediate"
                                               (PTy F64 (LTy F8))
                                               (TP
                                                 (sqbkt (Var "size"
                                                          CTy"Zsize")
                                                        (Var "strm3"
                                                          (LTy F8)))))
                                             (ITE
                                               (EQ (Var "opcode" (FTy 3))
                                                 (LW 0 3))
                                               (Call "Zfull_inst"
                                                 CTy"Zinst"
                                                 (TP
                                                   (sqbkt (Var "p"
                                                            (LTy F8))
                                                          (Call "Zmov"
                                                            CTy"instruction"
                                                            (TP
                                                              (sqbkt (LC
                                                                       "Z_ALWAYS"
                                                                       CTy"Zcond")
                                                                     (Var
                                                                       "size"
                                                                       CTy"Zsize")
                                                                     (Call
                                                                       "Zrm_i"
                                                                       CTy"Zdest_src"
                                                                       (TP
                                                                         (sqbkt (Var
                                                                                  "rm"
                                                                                  CTy"Zrm")
                                                                                (Var
                                                                                  "imm"
                                                                                  F64)))))))
                                                          (Var "strm4"
                                                            (LTy F8)))))
                                               (Call "Zdec_fail"
                                                 CTy"Zinst"
                                                 (LS
                                                   "Unsupported opcode: Group 11")))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LF LT LF LF
                                                         (AVar bTy)
                                                         (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "opcode" (FTy 3))
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readOpcodeModRM"
                                           (PTy (FTy 3)
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (ITE
                                           (EQ (Var "opcode" (FTy 3))
                                             (LW 6 3))
                                           (Call "Zdec_fail" CTy"Zinst"
                                             (LS
                                               "Unsupported opcode: Shift Group 2"))
                                           (Call "Zfull_inst" CTy"Zinst"
                                             (TP
                                               (sqbkt (Var "p" (LTy F8))
                                                      (Call "Zbinop"
                                                        CTy"instruction"
                                                        (TP
                                                          (sqbkt (Mop
                                                                   (Cast
                                                                     CTy"Zbinop_name")
                                                                   (Bop
                                                                     Add
                                                                     (Mop
                                                                       (Cast
                                                                         nTy)
                                                                       (Var
                                                                         "opcode"
                                                                         (FTy
                                                                           3)))
                                                                     (LN 8)))
                                                                 (Call
                                                                   "OpSize"
                                                                   CTy"Zsize"
                                                                   (TP
                                                                     (sqbkt bVar"have_rex"
                                                                            (Dest
                                                                              "W"
                                                                              bTy
                                                                              (Var
                                                                                "REX"
                                                                                CTy"REX"))
                                                                            (EX
                                                                              (Var
                                                                                "v#0"
                                                                                F8)
                                                                              (LN
                                                                                0)
                                                                              (LN
                                                                                0)
                                                                              F1)
                                                                            bVar"op_size_override")))
                                                                 (ITE
                                                                   (EQ
                                                                     (EX
                                                                       (Var
                                                                         "v#0"
                                                                         F8)
                                                                       (LN
                                                                         1)
                                                                       (LN
                                                                         1)
                                                                       F1)
                                                                     (LW 0
                                                                       1))
                                                                   (Call
                                                                     "Zrm_i"
                                                                     CTy"Zdest_src"
                                                                     (TP
                                                                       (sqbkt (Var
                                                                                "rm"
                                                                                CTy"Zrm")
                                                                              (LW
                                                                                1
                                                                                64))))
                                                                   (Call
                                                                     "Zrm_r"
                                                                     CTy"Zdest_src"
                                                                     (TP
                                                                       (sqbkt (Var
                                                                                "rm"
                                                                                CTy"Zrm")
                                                                              (LC
                                                                                "RCX"
                                                                                CTy"Zreg"))))))))
                                                      (Var "strm3"
                                                        (LTy F8))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LT LF LF LF
                                                         LF (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "imm" F64)
                                                  (Var "strm3" (LTy F8))))
                                         (Call "immediate8"
                                           (PTy F64 (LTy F8))
                                           (Var "strm2" (LTy F8)))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zloop"
                                                      CTy"instruction"
                                                      (TP
                                                        (sqbkt (ITE
                                                                 (EQ
                                                                   (EX
                                                                     (Var
                                                                       "v#0"
                                                                       F8)
                                                                     (LN 0)
                                                                     (LN 0)
                                                                     F1)
                                                                   (LW 0
                                                                     1))
                                                                 (LC
                                                                   "Z_NE"
                                                                   CTy"Zcond")
                                                                 (LC "Z_E"
                                                                   CTy"Zcond"))
                                                               (Var "imm"
                                                                 F64))))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LT LF LF LF
                                                         LT LF))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "imm" F64)
                                                  (Var "strm3" (LTy F8))))
                                         (Call "immediate8"
                                           (PTy F64 (LTy F8))
                                           (Var "strm2" (LTy F8)))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zloop"
                                                      CTy"instruction"
                                                      (TP
                                                        (sqbkt (LC
                                                                 "Z_ALWAYS"
                                                                 CTy"Zcond")
                                                               (Var "imm"
                                                                 F64))))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LT LF LT LF
                                                         LF LF))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "imm" F64)
                                                  (Var "strm3" (LTy F8))))
                                         (Call "immediate32"
                                           (PTy F64 (LTy F8))
                                           (Var "strm2" (LTy F8)))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zcall"
                                                      CTy"instruction"
                                                      (Call "Zimm"
                                                        CTy"Zimm_rm"
                                                        (Var "imm" F64)))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LT LF LT LF
                                                         (AVar bTy) LT))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "imm" F64)
                                                  (Var "strm3" (LTy F8))))
                                         (ITE
                                           (EQ
                                             (EX (Var "v#0" F8) (LN 1)
                                               (LN 1) F1) (LW 0 1))
                                           (Call "immediate32"
                                             (PTy F64 (LTy F8))
                                             (Var "strm2" (LTy F8)))
                                           (Call "immediate8"
                                             (PTy F64 (LTy F8))
                                             (Var "strm2" (LTy F8))))
                                         (Call "Zfull_inst" CTy"Zinst"
                                           (TP
                                             (sqbkt (Var "p" (LTy F8))
                                                    (Call "Zjcc"
                                                      CTy"instruction"
                                                      (TP
                                                        (sqbkt (LC
                                                                 "Z_ALWAYS"
                                                                 CTy"Zcond")
                                                               (Var "imm"
                                                                 F64))))
                                                    (Var "strm3" (LTy F8)))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LT LT LF LT
                                                         LT (AVar bTy)))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "size" CTy"Zsize")
                                         (Call "OpSize" CTy"Zsize"
                                           (TP
                                             (sqbkt bVar"have_rex"
                                                    (Dest "W" bTy
                                                      (Var "REX" CTy"REX"))
                                                    (EX (Var "v#0" F8)
                                                      (LN 0) (LN 0) F1)
                                                    bVar"op_size_override")))
                                         (Let
                                           (TP
                                             (sqbkt (Var "opcode" (FTy 3))
                                                    (Var "rm" CTy"Zrm")
                                                    (Var "strm3" (LTy F8))))
                                           (Call "readOpcodeModRM"
                                             (PTy (FTy 3)
                                               (PTy CTy"Zrm" (LTy F8)))
                                             (TP
                                               (sqbkt (Var "REX" CTy"REX")
                                                      (Var "strm2"
                                                        (LTy F8)))))
                                           (CS (Var "opcode" (FTy 3))
                                             (sqbkt ((LW 0 3)
                                                      (Let
                                                        (TP
                                                          (sqbkt (Var
                                                                   "imm"
                                                                   F64)
                                                                 (Var
                                                                   "strm4"
                                                                   (LTy
                                                                     F8))))
                                                        (Call "immediate"
                                                          (PTy F64
                                                            (LTy F8))
                                                          (TP
                                                            (sqbkt (Var
                                                                     "size"
                                                                     CTy"Zsize")
                                                                   (Var
                                                                     "strm3"
                                                                     (LTy
                                                                       F8)))))
                                                        (Call "Zfull_inst"
                                                          CTy"Zinst"
                                                          (TP
                                                            (sqbkt (Var
                                                                     "p"
                                                                     (LTy
                                                                       F8))
                                                                   (Call
                                                                     "Zbinop"
                                                                     CTy"instruction"
                                                                     (TP
                                                                       (sqbkt (LC
                                                                                "Ztest"
                                                                                CTy"Zbinop_name")
                                                                              (Var
                                                                                "size"
                                                                                CTy"Zsize")
                                                                              (Call
                                                                                "Zrm_i"
                                                                                CTy"Zdest_src"
                                                                                (TP
                                                                                  (sqbkt (Var
                                                                                           "rm"
                                                                                           CTy"Zrm")
                                                                                         (Var
                                                                                           "imm"
                                                                                           F64)))))))
                                                                   (Var
                                                                     "strm4"
                                                                     (LTy
                                                                       F8)))))))
                                                    ((LW 2 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zmonop"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (LC
                                                                              "Znot"
                                                                              CTy"Zmonop_name")
                                                                            (Var
                                                                              "size"
                                                                              CTy"Zsize")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm"))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((LW 3 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zmonop"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (LC
                                                                              "Zneg"
                                                                              CTy"Zmonop_name")
                                                                            (Var
                                                                              "size"
                                                                              CTy"Zsize")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm"))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((LW 4 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zmul"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (Var
                                                                              "size"
                                                                              CTy"Zsize")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm"))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((LW 6 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zdiv"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (Var
                                                                              "size"
                                                                              CTy"Zsize")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm"))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((AVar (FTy 3))
                                                      (Call "Zdec_fail"
                                                        CTy"Zinst"
                                                        (LS
                                                          "Unsupported opcode: Unary Group 3"))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LT LT LT LT
                                                         LT LF))
                                                (Var "strm2" (LTy F8))))
                                       (Let
                                         (TP
                                           (sqbkt (Var "opcode" (FTy 3))
                                                  (Var "rm" CTy"Zrm")
                                                  (Var "strm3" (LTy F8))))
                                         (Call "readOpcodeModRM"
                                           (PTy (FTy 3)
                                             (PTy CTy"Zrm" (LTy F8)))
                                           (TP
                                             (sqbkt (Var "REX" CTy"REX")
                                                    (Var "strm2" (LTy F8)))))
                                         (ITB
                                           (sqbkt ((EQ
                                                      (Var "opcode"
                                                        (FTy 3)) (LW 0 3))
                                                    (Call "Zfull_inst"
                                                      CTy"Zinst"
                                                      (TP
                                                        (sqbkt (Var "p"
                                                                 (LTy F8))
                                                               (Call
                                                                 "Zmonop"
                                                                 CTy"instruction"
                                                                 (TP
                                                                   (sqbkt (LC
                                                                            "Zinc"
                                                                            CTy"Zmonop_name")
                                                                          (Call
                                                                            "Z8"
                                                                            CTy"Zsize"
                                                                            bVar"have_rex")
                                                                          (Var
                                                                            "rm"
                                                                            CTy"Zrm"))))
                                                               (Var
                                                                 "strm3"
                                                                 (LTy F8))))))
                                                  ((EQ
                                                      (Var "opcode"
                                                        (FTy 3)) (LW 1 3))
                                                    (Call "Zfull_inst"
                                                      CTy"Zinst"
                                                      (TP
                                                        (sqbkt (Var "p"
                                                                 (LTy F8))
                                                               (Call
                                                                 "Zmonop"
                                                                 CTy"instruction"
                                                                 (TP
                                                                   (sqbkt (LC
                                                                            "Zdec"
                                                                            CTy"Zmonop_name")
                                                                          (Call
                                                                            "Z8"
                                                                            CTy"Zsize"
                                                                            bVar"have_rex")
                                                                          (Var
                                                                            "rm"
                                                                            CTy"Zrm"))))
                                                               (Var
                                                                 "strm3"
                                                                 (LTy F8)))))))
                                           (Call "Zdec_fail" CTy"Zinst"
                                             (LS
                                               "Unsupported opcode: INC/DEC Group 4")))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LT LT LT LT LT LT
                                                         LT LT))
                                                (Var "strm2" (LTy F8))))
                                       (Let (Var "size" CTy"Zsize")
                                         (Call "OpSize" CTy"Zsize"
                                           (TP
                                             (sqbkt bVar"have_rex"
                                                    (Dest "W" bTy
                                                      (Var "REX" CTy"REX"))
                                                    (LW 1 1)
                                                    bVar"op_size_override")))
                                         (Let
                                           (TP
                                             (sqbkt (Var "opcode" (FTy 3))
                                                    (Var "rm" CTy"Zrm")
                                                    (Var "strm3" (LTy F8))))
                                           (Call "readOpcodeModRM"
                                             (PTy (FTy 3)
                                               (PTy CTy"Zrm" (LTy F8)))
                                             (TP
                                               (sqbkt (Var "REX" CTy"REX")
                                                      (Var "strm2"
                                                        (LTy F8)))))
                                           (CS (Var "opcode" (FTy 3))
                                             (sqbkt ((LW 0 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zmonop"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (LC
                                                                              "Zinc"
                                                                              CTy"Zmonop_name")
                                                                            (Var
                                                                              "size"
                                                                              CTy"Zsize")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm"))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((LW 1 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zmonop"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (LC
                                                                              "Zdec"
                                                                              CTy"Zmonop_name")
                                                                            (Var
                                                                              "size"
                                                                              CTy"Zsize")
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm"))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((LW 2 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zcall"
                                                                   CTy"instruction"
                                                                   (Call
                                                                     "Zrm"
                                                                     CTy"Zimm_rm"
                                                                     (Var
                                                                       "rm"
                                                                       CTy"Zrm")))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((LW 4 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zjmp"
                                                                   CTy"instruction"
                                                                   (Var
                                                                     "rm"
                                                                     CTy"Zrm"))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((LW 6 3)
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zpush"
                                                                   CTy"instruction"
                                                                   (Call
                                                                     "Zrm"
                                                                     CTy"Zimm_rm"
                                                                     (Var
                                                                       "rm"
                                                                       CTy"Zrm")))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))
                                                    ((AVar (FTy 3))
                                                      (Call "Zdec_fail"
                                                        CTy"Zinst"
                                                        (LS
                                                          "Unsupported opcode: INC/DEC Group 5"))))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LF LF LF LF LT LT
                                                         LT LT))
                                                (LLC
                                                  (sqbkt (LW 56 8)
                                                         (Var "opc" F8))
                                                  (AVar (LTy F8)))))
                                       (Call "Zdec_fail" CTy"Zinst"
                                         (CC(sqbkt LS
                                                    "Unsupported opcode: 0F 38 "
                                                   (Mop (Cast sTy)
                                                     (Var "opc" F8))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LF LF LF LF LT LT
                                                         LT LT))
                                                (LLC
                                                  (sqbkt (LW 58 8)
                                                         (Var "opc" F8))
                                                  (AVar (LTy F8)))))
                                       (Call "Zdec_fail" CTy"Zinst"
                                         (CC(sqbkt LS
                                                    "Unsupported opcode: 0F 3A "
                                                   (Mop (Cast sTy)
                                                     (Var "opc" F8))))))
                                     ((TP
                                         (sqbkt (TP
                                                  (sqbkt LF LF LF LF LT LT
                                                         LT LT))
                                                (LLC
                                                  (sqbkt (Var "opc" F8))
                                                  (Var "strm2" (LTy F8)))))
                                       (Let
                                         (TP
                                           (sqbkt bVar"b'7" bVar"b'6"
                                                  bVar"b'5" bVar"b'4"
                                                  bVar"b'3" bVar"b'2"
                                                  bVar"b'1" bVar"b'0"))
                                         (BL 8 (Var "opc" F8))
                                         (ITB
                                           (sqbkt ((Bop And
                                                      (Mop Not bVar"b'7")
                                                      (Bop And bVar"b'6"
                                                        (Bop And
                                                          (Mop Not
                                                            bVar"b'5")
                                                          (Mop Not
                                                            bVar"b'4"))))
                                                    (Let
                                                      (TP
                                                        (sqbkt (Var "reg"
                                                                 CTy"Zreg")
                                                               (Var "rm"
                                                                 CTy"Zrm")
                                                               (Var
                                                                 "strm3"
                                                                 (LTy F8))))
                                                      (Call "readModRM"
                                                        (PTy CTy"Zreg"
                                                          (PTy CTy"Zrm"
                                                            (LTy F8)))
                                                        (TP
                                                          (sqbkt (Var
                                                                   "REX"
                                                                   CTy"REX")
                                                                 (Var
                                                                   "strm2"
                                                                   (LTy
                                                                     F8)))))
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zmov"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (Mop
                                                                              (Cast
                                                                                CTy"Zcond")
                                                                              (EX
                                                                                (Var
                                                                                  "opc"
                                                                                  F8)
                                                                                (LN
                                                                                  3)
                                                                                (LN
                                                                                  0)
                                                                                F4))
                                                                            (Call
                                                                              "OpSize"
                                                                              CTy"Zsize"
                                                                              (TP
                                                                                (sqbkt LT
                                                                                       (Dest
                                                                                         "W"
                                                                                         bTy
                                                                                         (Var
                                                                                           "REX"
                                                                                           CTy"REX"))
                                                                                       (LW
                                                                                         1
                                                                                         1)
                                                                                       bVar"op_size_override")))
                                                                            (Call
                                                                              "Zr_rm"
                                                                              CTy"Zdest_src"
                                                                              (TP
                                                                                (sqbkt (Var
                                                                                         "reg"
                                                                                         CTy"Zreg")
                                                                                       (Var
                                                                                         "rm"
                                                                                         CTy"Zrm")))))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8)))))))
                                                  ((Bop And bVar"b'7"
                                                      (Bop And
                                                        (Mop Not
                                                          bVar"b'6")
                                                        (Bop And
                                                          (Mop Not
                                                            bVar"b'5")
                                                          (Mop Not
                                                            bVar"b'4"))))
                                                    (Let
                                                      (TP
                                                        (sqbkt (Var "imm"
                                                                 F64)
                                                               (Var
                                                                 "strm3"
                                                                 (LTy F8))))
                                                      (Call "immediate32"
                                                        (PTy F64 (LTy F8))
                                                        (Var "strm2"
                                                          (LTy F8)))
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zjcc"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (Mop
                                                                              (Cast
                                                                                CTy"Zcond")
                                                                              (EX
                                                                                (Var
                                                                                  "opc"
                                                                                  F8)
                                                                                (LN
                                                                                  3)
                                                                                (LN
                                                                                  0)
                                                                                F4))
                                                                            (Var
                                                                              "imm"
                                                                              F64))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8)))))))
                                                  ((Bop And bVar"b'7"
                                                      (Bop And
                                                        (Mop Not
                                                          bVar"b'6")
                                                        (Bop And bVar"b'5"
                                                          (Bop And
                                                            (Mop Not
                                                              bVar"b'4")
                                                            (Bop And
                                                              (Mop Not
                                                                bVar"b'3")
                                                              (Bop And
                                                                (Mop Not
                                                                  bVar"b'2")
                                                                (Bop And
                                                                  bVar"b'1"
                                                                  (Mop Not
                                                                    bVar"b'0"))))))))
                                                    (Call "Zfull_inst"
                                                      CTy"Zinst"
                                                      (TP
                                                        (sqbkt (Var "p"
                                                                 (LTy F8))
                                                               (Const
                                                                 "Zcpuid"
                                                                 CTy"instruction")
                                                               (Var
                                                                 "strm2"
                                                                 (LTy F8))))))
                                                  ((Bop And bVar"b'7"
                                                      (Bop And
                                                        (Mop Not
                                                          bVar"b'6")
                                                        (Bop And bVar"b'5"
                                                          (Bop And
                                                            bVar"b'4"
                                                            (Bop And
                                                              (Mop Not
                                                                bVar"b'3")
                                                              (Bop And
                                                                (Mop Not
                                                                  bVar"b'2")
                                                                (Mop Not
                                                                  bVar"b'1")))))))
                                                    (Let
                                                      (TP
                                                        (sqbkt (Var "reg"
                                                                 CTy"Zreg")
                                                               (Var "rm"
                                                                 CTy"Zrm")
                                                               (Var
                                                                 "strm3"
                                                                 (LTy F8))))
                                                      (Call "readModRM"
                                                        (PTy CTy"Zreg"
                                                          (PTy CTy"Zrm"
                                                            (LTy F8)))
                                                        (TP
                                                          (sqbkt (Var
                                                                   "REX"
                                                                   CTy"REX")
                                                                 (Var
                                                                   "strm2"
                                                                   (LTy
                                                                     F8)))))
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zcmpxchg"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (Call
                                                                              "OpSize"
                                                                              CTy"Zsize"
                                                                              (TP
                                                                                (sqbkt bVar"have_rex"
                                                                                       (Dest
                                                                                         "W"
                                                                                         bTy
                                                                                         (Var
                                                                                           "REX"
                                                                                           CTy"REX"))
                                                                                       (EX
                                                                                         (Var
                                                                                           "opc"
                                                                                           F8)
                                                                                         (LN
                                                                                           0)
                                                                                         (LN
                                                                                           0)
                                                                                         F1)
                                                                                       bVar"op_size_override")))
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm")
                                                                            (Var
                                                                              "reg"
                                                                              CTy"Zreg"))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8)))))))
                                                  ((Bop And bVar"b'7"
                                                      (Bop And bVar"b'6"
                                                        (Bop And
                                                          (Mop Not
                                                            bVar"b'5")
                                                          (Bop And
                                                            (Mop Not
                                                              bVar"b'4")
                                                            (Bop And
                                                              (Mop Not
                                                                bVar"b'3")
                                                              (Bop And
                                                                (Mop Not
                                                                  bVar"b'2")
                                                                (Mop Not
                                                                  bVar"b'1")))))))
                                                    (Let
                                                      (TP
                                                        (sqbkt (Var "reg"
                                                                 CTy"Zreg")
                                                               (Var "rm"
                                                                 CTy"Zrm")
                                                               (Var
                                                                 "strm3"
                                                                 (LTy F8))))
                                                      (Call "readModRM"
                                                        (PTy CTy"Zreg"
                                                          (PTy CTy"Zrm"
                                                            (LTy F8)))
                                                        (TP
                                                          (sqbkt (Var
                                                                   "REX"
                                                                   CTy"REX")
                                                                 (Var
                                                                   "strm2"
                                                                   (LTy
                                                                     F8)))))
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zxadd"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (Call
                                                                              "OpSize"
                                                                              CTy"Zsize"
                                                                              (TP
                                                                                (sqbkt bVar"have_rex"
                                                                                       (Dest
                                                                                         "W"
                                                                                         bTy
                                                                                         (Var
                                                                                           "REX"
                                                                                           CTy"REX"))
                                                                                       (EX
                                                                                         (Var
                                                                                           "opc"
                                                                                           F8)
                                                                                         (LN
                                                                                           0)
                                                                                         (LN
                                                                                           0)
                                                                                         F1)
                                                                                       bVar"op_size_override")))
                                                                            (Var
                                                                              "rm"
                                                                              CTy"Zrm")
                                                                            (Var
                                                                              "reg"
                                                                              CTy"Zreg"))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8)))))))
                                                  ((Bop And bVar"b'7"
                                                      (Bop And
                                                        (Mop Not
                                                          bVar"b'6")
                                                        (Bop And bVar"b'5"
                                                          (Bop And
                                                            bVar"b'4"
                                                            (Bop And
                                                              (Mop Not
                                                                bVar"b'3")
                                                              (Bop And
                                                                bVar"b'2"
                                                                bVar"b'1"))))))
                                                    (Let
                                                      (TP
                                                        (sqbkt (Var "reg"
                                                                 CTy"Zreg")
                                                               (Var "rm"
                                                                 CTy"Zrm")
                                                               (Var
                                                                 "strm3"
                                                                 (LTy F8))))
                                                      (Call "readModRM"
                                                        (PTy CTy"Zreg"
                                                          (PTy CTy"Zrm"
                                                            (LTy F8)))
                                                        (TP
                                                          (sqbkt (Var
                                                                   "REX"
                                                                   CTy"REX")
                                                                 (Var
                                                                   "strm2"
                                                                   (LTy
                                                                     F8)))))
                                                      (Call "Zfull_inst"
                                                        CTy"Zinst"
                                                        (TP
                                                          (sqbkt (Var "p"
                                                                   (LTy
                                                                     F8))
                                                                 (Call
                                                                   "Zmovzx"
                                                                   CTy"instruction"
                                                                   (TP
                                                                     (sqbkt (Call
                                                                              "OpSize"
                                                                              CTy"Zsize"
                                                                              (TP
                                                                                (sqbkt bVar"have_rex"
                                                                                       (Dest
                                                                                         "W"
                                                                                         bTy
                                                                                         (Var
                                                                                           "REX"
                                                                                           CTy"REX"))
                                                                                       (LW
                                                                                         1
                                                                                         1)
                                                                                       bVar"op_size_override")))
                                                                            (Call
                                                                              "Zr_rm"
                                                                              CTy"Zdest_src"
                                                                              (TP
                                                                                (sqbkt (Var
                                                                                         "reg"
                                                                                         CTy"Zreg")
                                                                                       (Var
                                                                                         "rm"
                                                                                         CTy"Zrm"))))
                                                                            (ITE
                                                                              (EQ
                                                                                (EX
                                                                                  (Var
                                                                                    "opc"
                                                                                    F8)
                                                                                  (LN
                                                                                    0)
                                                                                  (LN
                                                                                    0)
                                                                                  F1)
                                                                                (LW
                                                                                  1
                                                                                  1))
                                                                              (Const
                                                                                "Z16"
                                                                                CTy"Zsize")
                                                                              (Call
                                                                                "Z8"
                                                                                CTy"Zsize"
                                                                                bVar"have_rex")))))
                                                                 (Var
                                                                   "strm3"
                                                                   (LTy
                                                                     F8))))))))
                                           (Call "Zdec_fail" CTy"Zinst"
                                             (CC(sqbkt LS
                                                        "Unsupported opcode: 0F "
                                                       (Mop (Cast sTy)
                                                         (Var "opc" F8))))))))
                                     ((TP
                                         (sqbkt (AVar
                                                  (PTy bTy
                                                    (PTy bTy
                                                      (PTy bTy
                                                        (PTy bTy
                                                          (PTy bTy
                                                            (PTy bTy
                                                              (PTy bTy
                                                                bTy))))))))
                                                (AVar (LTy F8))))
                                       (Call "Zdec_fail" CTy"Zinst"
                                         (CC(sqbkt LS
                                                    "Unsupported opcode: "
                                                   (Mop (Cast sTy)
                                                     (Var "v#0" F8)))))))))
                          ((LNL F8)
                            (Call "Zdec_fail" CTy"Zinst" (LS"No opcode")))))))))))


(Def "x64_fetch" qVar"state"
  (Let (TP (sqbkt (Var "r" (LTy F8)) (Var "s1" (PTy (LTy F8) qTy))))
    (Let (Var "s" (PTy (LTy F8) qTy))
      (Mop Snd
        (Apply
          (For
            (TP
              (sqbkt LN 19 LN 0
                     (Close nVar"i"
                       (Close (Var "state" (PTy (LTy F8) qTy))
                         (TP
                           (sqbkt LU
                                  (LLC
                                    (sqbkt (Mop ValOf
                                             (Apply
                                               (Dest "MEM"
                                                 (ATy F64 (OTy F8))
                                                 (Mop Snd
                                                   (Var "state"
                                                     (PTy (LTy F8) qTy))))
                                               (Bop Add
                                                 (Dest "RIP" F64
                                                   (Mop Snd
                                                     (Var "state"
                                                       (PTy (LTy F8) qTy))))
                                                 (Mop (Cast F64) nVar"i")))))
                                    (Mop Fst
                                      (Var "state" (PTy (LTy F8) qTy))))
                                  (Mop Snd
                                    (Var "state" (PTy (LTy F8) qTy))))))))))
          (TP (sqbkt (LNL F8) qVar"state"))))
      (TP
        (sqbkt (Mop Fst (Var "s" (PTy (LTy F8) qTy)))
               (Var "s" (PTy (LTy F8) qTy)))))
    (TP
      (sqbkt (Var "r" (LTy F8)) (Mop Snd (Var "s1" (PTy (LTy F8) qTy)))))))


(Def "checkIcache" nVar"n"
  (Close qVar"state"
    (Apply
      (For
        (TP
          (sqbkt LN 0 (Bop Sub nVar"n" (LN 1))
                 (Close nVar"i"
                   (Close qVar"state"
                     (Let (Var "v" F64)
                       (Bop Add (Dest "RIP" F64 qVar"state")
                         (Mop (Cast F64) nVar"i"))
                       (ITE
                         (Mop Not
                           (EQ
                             (Apply
                               (Dest "MEM" (ATy F64 (OTy F8)) qVar"state")
                               (Var "v" F64))
                             (Apply
                               (Dest "ICACHE" (ATy F64 (OTy F8))
                                 qVar"state") (Var "v" F64))))
                         (Apply
                           (Call "raise'exception" (ATy qTy (PTy uTy qTy))
                             (Call "FAIL" CTy"exception"
                               (LS"icache miss"))) qVar"state")
                         (TP (sqbkt LU qVar"state"))))))))) qVar"state")))


(Def "x64_next" qVar"state"
  (CS
    (Call "x64_decode" CTy"Zinst"
      (Mop Fst
        (Apply (Const "x64_fetch" (ATy qTy (PTy (LTy F8) qTy)))
          qVar"state")))
    (sqbkt ((Call "Zfull_inst" CTy"Zinst"
               (TP
                 (sqbkt (AVar (LTy F8)) (Var "i" CTy"instruction")
                        (Var "strm1" (LTy F8)))))
             (Let nVar"len"
               (Bop Sub (LN 20) (Mop Length (Var "strm1" (LTy F8))))
               (Let qVar"s"
                 (Mop Snd
                   (Apply
                     (Call "checkIcache" (ATy qTy (PTy uTy qTy))
                       nVar"len") qVar"state"))
                 (Apply
                   (Call "Run" (ATy qTy (PTy uTy qTy))
                     (Var "i" CTy"instruction"))
                   (Rupd "RIP"
                     (TP
                       (sqbkt qVar"s"
                              (Bop Add (Dest "RIP" F64 qVar"s")
                                (Mop (Cast F64) nVar"len")))))))))
           ((Call "Zdec_fail" CTy"Zinst" sVar"s0")
             (Apply
               (Call "raise'exception" (ATy qTy (PTy uTy qTy))
                 (Call "FAIL" CTy"exception" sVar"s0")) qVar"state")))))
