; LLVM IR from mbcse/Security_Hacking_Scripts :: Encryption_Algorithms/RSA/enc_dec_RSA.ll (Z14encryption_keyv)

; 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"

@x = external dso_local global i32, align 4
@y = external dso_local global i32, align 4
@t = external dso_local global i32, align 4
@i = external dso_local global i32, align 4
@flag = external dso_local global i32, align 4
@e = external dso_local global [50 x i64], align 16
@d = external dso_local global [50 x i64], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z5primel(i64 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14encryption_keyv() #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  store i32 2, ptr @i, align 4, !tbaa !5
  br label %2

2:                                                ; preds = %54, %0
  %3 = load i32, ptr @i, align 4, !tbaa !5
  %4 = load i32, ptr @t, align 4, !tbaa !5
  %5 = icmp slt i32 %3, %4
  br i1 %5, label %6, label %57

6:                                                ; preds = %2
  %7 = load i32, ptr @t, align 4, !tbaa !5
  %8 = load i32, ptr @i, align 4, !tbaa !5
  %9 = srem i32 %7, %8
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %6
  br label %54

12:                                               ; preds = %6
  %13 = load i32, ptr @i, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = call noundef i32 @_Z5primel(i64 noundef %14)
  store i32 %15, ptr @flag, align 4, !tbaa !5
  %16 = load i32, ptr @flag, align 4, !tbaa !5
  %17 = icmp eq i32 %16, 1
  br i1 %17, label %18, label %53

18:                                               ; preds = %12
  %19 = load i32, ptr @i, align 4, !tbaa !5
  %20 = load i32, ptr @x, align 4, !tbaa !5
  %21 = icmp ne i32 %19, %20
  br i1 %21, label %22, label %53

22:                                               ; preds = %18
  %23 = load i32, ptr @i, align 4, !tbaa !5
  %24 = load i32, ptr @y, align 4, !tbaa !5
  %25 = icmp ne i32 %23, %24
  br i1 %25, label %26, label %53

26:                                               ; preds = %22
  %27 = load i32, ptr @i, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = load i32, ptr %1, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [50 x i64], ptr @e, i64 0, i64 %30
  store i64 %28, ptr %31, align 8, !tbaa !9
  %32 = load i32, ptr %1, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [50 x i64], ptr @e, i64 0, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = call noundef i64 @_Z2cdl(i64 noundef %35)
  %37 = trunc i64 %36 to i32
  store i32 %37, ptr @flag, align 4, !tbaa !5
  %38 = load i32, ptr @flag, align 4, !tbaa !5
  %39 = icmp sgt i32 %38, 0
  br i1 %39, label %40, label %48

40:                                               ; preds = %26
  %41 = load i32, ptr @flag, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = load i32, ptr %1, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [50 x i64], ptr @d, i64 0, i64 %44
  store i64 %42, ptr %45, align 8, !tbaa !9
  %46 = load i32, ptr %1, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %1, align 4, !tbaa !5
  br label %48

48:                                               ; preds = %40, %26
  %49 = load i32, ptr %1, align 4, !tbaa !5
  %50 = icmp eq i32 %49, 99
  br i1 %50, label %51, label %52

51:                                               ; preds = %48
  br label %57

52:                                               ; preds = %48
  br label %53

53:                                               ; preds = %52, %22, %18, %12
  br label %54

54:                                               ; preds = %53, %11
  %55 = load i32, ptr @i, align 4, !tbaa !5
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr @i, align 4, !tbaa !5
  br label %2, !llvm.loop !11

57:                                               ; preds = %51, %2
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

; 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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z2cdl(i64 noundef) #2

attributes #0 = { mustprogress 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 = { mustprogress 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 #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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"

@x = external dso_local local_unnamed_addr global i32, align 4
@y = external dso_local local_unnamed_addr global i32, align 4
@t = external dso_local local_unnamed_addr global i32, align 4
@i = external dso_local local_unnamed_addr global i32, align 4
@flag = external dso_local local_unnamed_addr global i32, align 4
@e = external dso_local local_unnamed_addr global [50 x i64], align 16
@d = external dso_local local_unnamed_addr global [50 x i64], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z5primel(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z14encryption_keyv() local_unnamed_addr #0 {
  store i32 2, ptr @i, align 4, !tbaa !5
  %1 = load i32, ptr @t, align 4, !tbaa !5
  %2 = icmp sgt i32 %1, 2
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %26
  %3 = phi i32 [ %29, %26 ], [ %1, %0 ]
  %.011 = phi i32 [ %.2, %26 ], [ 0, %0 ]
  %storemerge10 = phi i32 [ %28, %26 ], [ 2, %0 ]
  %4 = srem i32 %3, %storemerge10
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %26, label %6

6:                                                ; preds = %.lr.ph
  %7 = sext i32 %storemerge10 to i64
  %8 = tail call noundef i32 @_Z5primel(i64 noundef %7)
  store i32 %8, ptr @flag, align 4, !tbaa !5
  %9 = icmp eq i32 %8, 1
  %.pre14 = load i32, ptr @i, align 4, !tbaa !5
  br i1 %9, label %10, label %26

10:                                               ; preds = %6
  %11 = load i32, ptr @x, align 4, !tbaa !5
  %.not = icmp eq i32 %.pre14, %11
  %12 = load i32, ptr @y, align 4
  %.not9 = icmp eq i32 %.pre14, %12
  %or.cond = select i1 %.not, i1 true, i1 %.not9
  br i1 %or.cond, label %26, label %13

13:                                               ; preds = %10
  %14 = sext i32 %.pre14 to i64
  %15 = sext i32 %.011 to i64
  %16 = getelementptr inbounds [50 x i64], ptr @e, i64 0, i64 %15
  store i64 %14, ptr %16, align 8, !tbaa !9
  %17 = tail call noundef i64 @_Z2cdl(i64 noundef %14)
  %18 = trunc i64 %17 to i32
  store i32 %18, ptr @flag, align 4, !tbaa !5
  %19 = icmp sgt i32 %18, 0
  br i1 %19, label %20, label %24

20:                                               ; preds = %13
  %21 = and i64 %17, 2147483647
  %22 = getelementptr inbounds [50 x i64], ptr @d, i64 0, i64 %15
  store i64 %21, ptr %22, align 8, !tbaa !9
  %23 = add nsw i32 %.011, 1
  br label %24

24:                                               ; preds = %20, %13
  %.1 = phi i32 [ %23, %20 ], [ %.011, %13 ]
  %25 = icmp eq i32 %.1, 99
  br i1 %25, label %._crit_edge, label %._crit_edge13

._crit_edge13:                                    ; preds = %24
  %.pre = load i32, ptr @i, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %._crit_edge13, %6, %10, %.lr.ph
  %27 = phi i32 [ %storemerge10, %.lr.ph ], [ %.pre, %._crit_edge13 ], [ %.pre14, %10 ], [ %.pre14, %6 ]
  %.2 = phi i32 [ %.011, %.lr.ph ], [ %.1, %._crit_edge13 ], [ %.011, %10 ], [ %.011, %6 ]
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr @i, align 4, !tbaa !5
  %29 = load i32, ptr @t, align 4, !tbaa !5
  %30 = icmp slt i32 %28, %29
  br i1 %30, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %26, %24, %0
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z2cdl(i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { mustprogress 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" }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass