; LLVM IR from BrunoLevy/learn-fpga :: LiteX/software/Doom/mc1-doom/src (m_cheat)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.cheatseq_t = type { ptr, ptr }

@firsttime = external hidden global i32, align 4
@cheat_xlate_table = external hidden global [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local i32 @cht_CheckCheat(ptr noundef %0, i8 noundef signext %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i8 %1, ptr %4, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !10
  %7 = load i32, ptr @firsttime, align 4, !tbaa !10
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %51

9:                                                ; preds = %2
  store i32 0, ptr @firsttime, align 4, !tbaa !10
  store i32 0, ptr %5, align 4, !tbaa !10
  br label %10

10:                                               ; preds = %47, %9
  %11 = load i32, ptr %5, align 4, !tbaa !10
  %12 = icmp slt i32 %11, 256
  br i1 %12, label %13, label %50

13:                                               ; preds = %10
  %14 = load i32, ptr %5, align 4, !tbaa !10
  %15 = and i32 %14, 1
  %16 = shl i32 %15, 7
  %17 = load i32, ptr %5, align 4, !tbaa !10
  %18 = and i32 %17, 2
  %19 = shl i32 %18, 5
  %20 = add nsw i32 %16, %19
  %21 = load i32, ptr %5, align 4, !tbaa !10
  %22 = and i32 %21, 4
  %23 = add nsw i32 %20, %22
  %24 = load i32, ptr %5, align 4, !tbaa !10
  %25 = and i32 %24, 8
  %26 = shl i32 %25, 1
  %27 = add nsw i32 %23, %26
  %28 = load i32, ptr %5, align 4, !tbaa !10
  %29 = and i32 %28, 16
  %30 = ashr i32 %29, 1
  %31 = add nsw i32 %27, %30
  %32 = load i32, ptr %5, align 4, !tbaa !10
  %33 = and i32 %32, 32
  %34 = add nsw i32 %31, %33
  %35 = load i32, ptr %5, align 4, !tbaa !10
  %36 = and i32 %35, 64
  %37 = ashr i32 %36, 5
  %38 = add nsw i32 %34, %37
  %39 = load i32, ptr %5, align 4, !tbaa !10
  %40 = and i32 %39, 128
  %41 = ashr i32 %40, 7
  %42 = add nsw i32 %38, %41
  %43 = trunc i32 %42 to i8
  %44 = load i32, ptr %5, align 4, !tbaa !10
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [256 x i8], ptr @cheat_xlate_table, i64 0, i64 %45
  store i8 %43, ptr %46, align 1, !tbaa !9
  br label %47

47:                                               ; preds = %13
  %48 = load i32, ptr %5, align 4, !tbaa !10
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %5, align 4, !tbaa !10
  br label %10, !llvm.loop !12

50:                                               ; preds = %10
  br label %51

51:                                               ; preds = %50, %2
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.cheatseq_t, ptr %52, i32 0, i32 1
  %54 = load ptr, ptr %53, align 8, !tbaa !14
  %55 = icmp ne ptr %54, null
  br i1 %55, label %62, label %56

56:                                               ; preds = %51
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.cheatseq_t, ptr %57, i32 0, i32 0
  %59 = load ptr, ptr %58, align 8, !tbaa !16
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.cheatseq_t, ptr %60, i32 0, i32 1
  store ptr %59, ptr %61, align 8, !tbaa !14
  br label %62

62:                                               ; preds = %56, %51
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.cheatseq_t, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !14
  %66 = load i8, ptr %65, align 1, !tbaa !9
  %67 = zext i8 %66 to i32
  %68 = icmp eq i32 %67, 0
  br i1 %68, label %69, label %75

69:                                               ; preds = %62
  %70 = load i8, ptr %4, align 1, !tbaa !9
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.cheatseq_t, ptr %71, i32 0, i32 1
  %73 = load ptr, ptr %72, align 8, !tbaa !14
  %74 = getelementptr inbounds i8, ptr %73, i32 1
  store ptr %74, ptr %72, align 8, !tbaa !14
  store i8 %70, ptr %73, align 1, !tbaa !9
  br label %99

75:                                               ; preds = %62
  %76 = load i8, ptr %4, align 1, !tbaa !9
  %77 = zext i8 %76 to i64
  %78 = getelementptr inbounds [256 x i8], ptr @cheat_xlate_table, i64 0, i64 %77
  %79 = load i8, ptr %78, align 1, !tbaa !9
  %80 = zext i8 %79 to i32
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.cheatseq_t, ptr %81, i32 0, i32 1
  %83 = load ptr, ptr %82, align 8, !tbaa !14
  %84 = load i8, ptr %83, align 1, !tbaa !9
  %85 = zext i8 %84 to i32
  %86 = icmp eq i32 %80, %85
  br i1 %86, label %87, label %92

87:                                               ; preds = %75
  %88 = load ptr, ptr %3, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.cheatseq_t, ptr %88, i32 0, i32 1
  %90 = load ptr, ptr %89, align 8, !tbaa !14
  %91 = getelementptr inbounds i8, ptr %90, i32 1
  store ptr %91, ptr %89, align 8, !tbaa !14
  br label %98

92:                                               ; preds = %75
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.cheatseq_t, ptr %93, i32 0, i32 0
  %95 = load ptr, ptr %94, align 8, !tbaa !16
  %96 = load ptr, ptr %3, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.cheatseq_t, ptr %96, i32 0, i32 1
  store ptr %95, ptr %97, align 8, !tbaa !14
  br label %98

98:                                               ; preds = %92, %87
  br label %99

99:                                               ; preds = %98, %69
  %100 = load ptr, ptr %3, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.cheatseq_t, ptr %100, i32 0, i32 1
  %102 = load ptr, ptr %101, align 8, !tbaa !14
  %103 = load i8, ptr %102, align 1, !tbaa !9
  %104 = zext i8 %103 to i32
  %105 = icmp eq i32 %104, 1
  br i1 %105, label %106, label %111

106:                                              ; preds = %99
  %107 = load ptr, ptr %3, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.cheatseq_t, ptr %107, i32 0, i32 1
  %109 = load ptr, ptr %108, align 8, !tbaa !14
  %110 = getelementptr inbounds i8, ptr %109, i32 1
  store ptr %110, ptr %108, align 8, !tbaa !14
  br label %125

111:                                              ; preds = %99
  %112 = load ptr, ptr %3, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.cheatseq_t, ptr %112, i32 0, i32 1
  %114 = load ptr, ptr %113, align 8, !tbaa !14
  %115 = load i8, ptr %114, align 1, !tbaa !9
  %116 = zext i8 %115 to i32
  %117 = icmp eq i32 %116, 255
  br i1 %117, label %118, label %124

118:                                              ; preds = %111
  %119 = load ptr, ptr %3, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.cheatseq_t, ptr %119, i32 0, i32 0
  %121 = load ptr, ptr %120, align 8, !tbaa !16
  %122 = load ptr, ptr %3, align 8, !tbaa !5
  %123 = getelementptr inbounds %struct.cheatseq_t, ptr %122, i32 0, i32 1
  store ptr %121, ptr %123, align 8, !tbaa !14
  store i32 1, ptr %6, align 4, !tbaa !10
  br label %124

124:                                              ; preds = %118, %111
  br label %125

125:                                              ; preds = %124, %106
  %126 = load i32, ptr %6, align 4, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %126
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 8}
!15 = !{!"", !6, i64 0, !6, i64 8}
!16 = !{!15, !6, i64 0}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@firsttime = external hidden local_unnamed_addr global i32, align 4
@cheat_xlate_table = external hidden local_unnamed_addr global [256 x i8], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 0, 2) i32 @cht_CheckCheat(ptr nocapture noundef %0, i8 noundef signext %1) local_unnamed_addr #0 {
  %3 = load i32, ptr @firsttime, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %.loopexit, label %vector.ph

vector.ph:                                        ; preds = %2
  store i32 0, ptr @firsttime, align 4, !tbaa !5
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <16 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %vec.ind33 = phi <16 x i8> [ <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 8, i8 9, i8 10, i8 11, i8 12, i8 13, i8 14, i8 15>, %vector.ph ], [ %vec.ind.next34, %vector.body ]
  %vec.ind35 = phi <16 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>, %vector.ph ], [ %vec.ind.next36, %vector.body ]
  %4 = and <16 x i8> %vec.ind33, <i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3, i8 3>
  %5 = tail call <16 x i8> @llvm.bitreverse.v16i8(<16 x i8> %4)
  %6 = shl <16 x i32> %vec.ind35, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
  %7 = and <16 x i32> %6, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
  %8 = lshr <16 x i32> %vec.ind, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
  %9 = and <16 x i32> %8, <i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8>
  %10 = lshr <16 x i32> %vec.ind, <i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5>
  %11 = and <16 x i32> %10, <i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 2>
  %12 = lshr <16 x i32> %vec.ind, <i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7, i32 7>
  %13 = and <16 x i32> %vec.ind, <i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36, i32 36>
  %14 = add nuw nsw <16 x i32> %13, %12
  %15 = add nuw nsw <16 x i32> %14, %7
  %16 = add nuw nsw <16 x i32> %15, %9
  %17 = add nuw nsw <16 x i32> %16, %11
  %18 = trunc <16 x i32> %17 to <16 x i8>
  %19 = add <16 x i8> %5, %18
  %20 = getelementptr inbounds [256 x i8], ptr @cheat_xlate_table, i64 0, i64 %index
  store <16 x i8> %19, ptr %20, align 16, !tbaa !9
  %index.next = add nuw i64 %index, 16
  %vec.ind.next = add <16 x i32> %vec.ind, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
  %vec.ind.next34 = add <16 x i8> %vec.ind33, <i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16>
  %vec.ind.next36 = add <16 x i32> %vec.ind35, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
  %21 = icmp eq i64 %index.next, 256
  br i1 %21, label %.loopexit, label %vector.body, !llvm.loop !10

.loopexit:                                        ; preds = %vector.body, %2
  %22 = getelementptr inbounds i8, ptr %0, i64 8
  %23 = load ptr, ptr %22, align 8, !tbaa !14
  %.not30 = icmp eq ptr %23, null
  br i1 %.not30, label %24, label %26

24:                                               ; preds = %.loopexit
  %25 = load ptr, ptr %0, align 8, !tbaa !17
  store ptr %25, ptr %22, align 8, !tbaa !14
  br label %26

26:                                               ; preds = %24, %.loopexit
  %27 = phi ptr [ %25, %24 ], [ %23, %.loopexit ]
  %28 = load i8, ptr %27, align 1, !tbaa !9
  %29 = icmp eq i8 %28, 0
  br i1 %29, label %30, label %32

30:                                               ; preds = %26
  %31 = getelementptr inbounds i8, ptr %27, i64 1
  store ptr %31, ptr %22, align 8, !tbaa !14
  store i8 %1, ptr %27, align 1, !tbaa !9
  %.pre = load ptr, ptr %22, align 8, !tbaa !14
  br label %41

32:                                               ; preds = %26
  %33 = zext i8 %1 to i64
  %34 = getelementptr inbounds [256 x i8], ptr @cheat_xlate_table, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !9
  %36 = icmp eq i8 %35, %28
  br i1 %36, label %37, label %39

37:                                               ; preds = %32
  %38 = getelementptr inbounds i8, ptr %27, i64 1
  store ptr %38, ptr %22, align 8, !tbaa !14
  br label %41

39:                                               ; preds = %32
  %40 = load ptr, ptr %0, align 8, !tbaa !17
  store ptr %40, ptr %22, align 8, !tbaa !14
  br label %41

41:                                               ; preds = %37, %39, %30
  %42 = phi ptr [ %38, %37 ], [ %40, %39 ], [ %.pre, %30 ]
  %43 = load i8, ptr %42, align 1, !tbaa !9
  switch i8 %43, label %48 [
    i8 1, label %44
    i8 -1, label %46
  ]

44:                                               ; preds = %41
  %45 = getelementptr inbounds i8, ptr %42, i64 1
  br label %.sink.split

46:                                               ; preds = %41
  %47 = load ptr, ptr %0, align 8, !tbaa !17
  br label %.sink.split

.sink.split:                                      ; preds = %44, %46
  %.sink = phi ptr [ %47, %46 ], [ %45, %44 ]
  %.0.ph = phi i32 [ 1, %46 ], [ 0, %44 ]
  store ptr %.sink, ptr %22, align 8, !tbaa !14
  br label %48

48:                                               ; preds = %.sink.split, %41
  %.0 = phi i32 [ 0, %41 ], [ %.0.ph, %.sink.split ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <16 x i8> @llvm.bitreverse.v16i8(<16 x i8>) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11, !12, !13}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"llvm.loop.isvectorized", i32 1}
!13 = !{!"llvm.loop.unroll.runtime.disable"}
!14 = !{!15, !16, i64 8}
!15 = !{!"", !16, i64 0, !16, i64 8}
!16 = !{!"any pointer", !7, i64 0}
!17 = !{!15, !16, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalDCEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass