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

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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@n = external dso_local global i32, align 4
@i = external dso_local global i32, align 4
@d = external dso_local global [50 x i64], align 16
@temp = external dso_local global [50 x i64], align 16
@j = external dso_local global i64, align 8
@en = external dso_local global [50 x i8], align 16
@m = external dso_local global [50 x i8], align 16
@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str.8 = external hidden unnamed_addr constant [28 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #0

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

6:                                                ; preds = %33, %0
  %7 = load i32, ptr @i, align 4, !tbaa !9
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [50 x i8], ptr @en, i64 0, i64 %8
  %10 = load i8, ptr %9, align 1, !tbaa !11
  %11 = sext i8 %10 to i32
  %12 = icmp ne i32 %11, -1
  br i1 %12, label %13, label %43

13:                                               ; preds = %6
  %14 = load i32, ptr @i, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [50 x i64], ptr @temp, i64 0, i64 %15
  %17 = load i64, ptr %16, align 8, !tbaa !5
  store i64 %17, ptr %2, align 8, !tbaa !5
  store i64 1, ptr %4, align 8, !tbaa !5
  store i64 0, ptr @j, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %30, %13
  %19 = load i64, ptr @j, align 8, !tbaa !5
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = icmp slt i64 %19, %20
  br i1 %21, label %22, label %33

22:                                               ; preds = %18
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = load i64, ptr %2, align 8, !tbaa !5
  %25 = mul nsw i64 %23, %24
  store i64 %25, ptr %4, align 8, !tbaa !5
  %26 = load i64, ptr %4, align 8, !tbaa !5
  %27 = load i32, ptr @n, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = srem i64 %26, %28
  store i64 %29, ptr %4, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %22
  %31 = load i64, ptr @j, align 8, !tbaa !5
  %32 = add nsw i64 %31, 1
  store i64 %32, ptr @j, align 8, !tbaa !5
  br label %18, !llvm.loop !12

33:                                               ; preds = %18
  %34 = load i64, ptr %4, align 8, !tbaa !5
  %35 = add nsw i64 %34, 96
  store i64 %35, ptr %1, align 8, !tbaa !5
  %36 = load i64, ptr %1, align 8, !tbaa !5
  %37 = trunc i64 %36 to i8
  %38 = load i32, ptr @i, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [50 x i8], ptr @m, i64 0, i64 %39
  store i8 %37, ptr %40, align 1, !tbaa !11
  %41 = load i32, ptr @i, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr @i, align 4, !tbaa !9
  br label %6, !llvm.loop !14

43:                                               ; preds = %6
  %44 = load i32, ptr @i, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [50 x i8], ptr @m, i64 0, i64 %45
  store i8 -1, ptr %46, align 1, !tbaa !11
  %47 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef @.str.8)
  store i32 0, ptr @i, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %61, %43
  %49 = load i32, ptr @i, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [50 x i8], ptr @m, i64 0, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !11
  %53 = sext i8 %52 to i32
  %54 = icmp ne i32 %53, -1
  br i1 %54, label %55, label %64

55:                                               ; preds = %48
  %56 = load i32, ptr @i, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [50 x i8], ptr @m, i64 0, i64 %57
  %59 = load i8, ptr %58, align 1, !tbaa !11
  %60 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i8 noundef signext %59)
  br label %61

61:                                               ; preds = %55
  %62 = load i32, ptr @i, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr @i, align 4, !tbaa !9
  br label %48, !llvm.loop !15

64:                                               ; preds = %48
  %65 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, 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) #2

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8), i8 noundef signext) #0

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #0

attributes #0 = { inlinehint 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 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}


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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@n = external dso_local local_unnamed_addr global i32, align 4
@i = external dso_local local_unnamed_addr global i32, align 4
@d = external dso_local local_unnamed_addr global [50 x i64], align 16
@temp = external dso_local local_unnamed_addr global [50 x i64], align 16
@j = external dso_local local_unnamed_addr global i64, align 8
@en = external dso_local local_unnamed_addr global [50 x i8], align 16
@m = external dso_local local_unnamed_addr global [50 x i8], align 16
@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str.8 = external hidden unnamed_addr constant [28 x i8], align 1

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7decryptv() local_unnamed_addr #1 {
  %1 = load i64, ptr @d, align 16, !tbaa !5
  %2 = load i8, ptr @en, align 16, !tbaa !9
  %.not13 = icmp eq i8 %2, -1
  br i1 %.not13, label %27, label %.lr.ph16

.lr.ph16:                                         ; preds = %0
  %3 = icmp sgt i64 %1, 0
  %4 = load i32, ptr @n, align 4
  %5 = sext i32 %4 to i64
  br i1 %3, label %.lr.ph.us.preheader, label %.lr.ph16.split

.lr.ph.us.preheader:                              ; preds = %.lr.ph16
  %xtraiter = and i64 %1, 3
  %6 = icmp ult i64 %1, 4
  %unroll_iter = and i64 %1, 9223372036854775804
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %indvars.iv31 = phi i64 [ %indvars.iv.next32, %._crit_edge.us ], [ 0, %.lr.ph.us.preheader ]
  %7 = getelementptr inbounds [50 x i64], ptr @temp, i64 0, i64 %indvars.iv31
  %8 = load i64, ptr %7, align 8, !tbaa !5
  br i1 %6, label %._crit_edge.us.unr-lcssa, label %.lr.ph.us.new

.lr.ph.us.new:                                    ; preds = %.lr.ph.us, %.lr.ph.us.new
  %.011.us = phi i64 [ %16, %.lr.ph.us.new ], [ 1, %.lr.ph.us ]
  %niter = phi i64 [ %niter.next.3, %.lr.ph.us.new ], [ 0, %.lr.ph.us ]
  %9 = mul nsw i64 %.011.us, %8
  %10 = srem i64 %9, %5
  %11 = mul nsw i64 %10, %8
  %12 = srem i64 %11, %5
  %13 = mul nsw i64 %12, %8
  %14 = srem i64 %13, %5
  %15 = mul nsw i64 %14, %8
  %16 = srem i64 %15, %5
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.us.unr-lcssa, label %.lr.ph.us.new, !llvm.loop !10

._crit_edge.us.unr-lcssa:                         ; preds = %.lr.ph.us.new, %.lr.ph.us
  %.lcssa38.ph = phi i64 [ poison, %.lr.ph.us ], [ %16, %.lr.ph.us.new ]
  %.011.us.unr = phi i64 [ 1, %.lr.ph.us ], [ %16, %.lr.ph.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa, %.epil.preheader
  %.011.us.epil = phi i64 [ %18, %.epil.preheader ], [ %.011.us.unr, %._crit_edge.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.unr-lcssa ]
  %17 = mul nsw i64 %.011.us.epil, %8
  %18 = srem i64 %17, %5
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.us, label %.epil.preheader, !llvm.loop !12

._crit_edge.us:                                   ; preds = %.epil.preheader, %._crit_edge.us.unr-lcssa
  %.lcssa38 = phi i64 [ %.lcssa38.ph, %._crit_edge.us.unr-lcssa ], [ %18, %.epil.preheader ]
  %19 = trunc i64 %.lcssa38 to i8
  %20 = add i8 %19, 96
  %21 = getelementptr inbounds [50 x i8], ptr @m, i64 0, i64 %indvars.iv31
  store i8 %20, ptr %21, align 1, !tbaa !9
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %22 = getelementptr inbounds [50 x i8], ptr @en, i64 0, i64 %indvars.iv.next32
  %23 = load i8, ptr %22, align 1, !tbaa !9
  %.not.us = icmp eq i8 %23, -1
  br i1 %.not.us, label %._crit_edge17, label %.lr.ph.us, !llvm.loop !14

.lr.ph16.split:                                   ; preds = %.lr.ph16, %.lr.ph16.split
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph16.split ], [ 0, %.lr.ph16 ]
  %24 = getelementptr inbounds [50 x i8], ptr @m, i64 0, i64 %indvars.iv
  store i8 97, ptr %24, align 1, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %25 = getelementptr inbounds [50 x i8], ptr @en, i64 0, i64 %indvars.iv.next
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %.not = icmp eq i8 %26, -1
  br i1 %.not, label %._crit_edge17, label %.lr.ph16.split, !llvm.loop !14

._crit_edge17:                                    ; preds = %.lr.ph16.split, %._crit_edge.us
  %.us-phi = phi i64 [ %1, %._crit_edge.us ], [ 0, %.lr.ph16.split ]
  %.us-phi21 = phi i64 [ %indvars.iv.next32, %._crit_edge.us ], [ %indvars.iv.next, %.lr.ph16.split ]
  %.us-phi20 = trunc i64 %.us-phi21 to i32
  store i64 %.us-phi, ptr @j, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %._crit_edge17, %0
  %storemerge.lcssa = phi i32 [ %.us-phi20, %._crit_edge17 ], [ 0, %0 ]
  %.lcssa = phi i64 [ %.us-phi21, %._crit_edge17 ], [ 0, %0 ]
  store i32 %storemerge.lcssa, ptr @i, align 4, !tbaa !15
  %28 = getelementptr inbounds [50 x i8], ptr @m, i64 0, i64 %.lcssa
  store i8 -1, ptr %28, align 1, !tbaa !9
  %29 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef nonnull @.str.8)
  store i32 0, ptr @i, align 4, !tbaa !15
  %30 = load i8, ptr @m, align 16, !tbaa !9
  %.not822 = icmp eq i8 %30, -1
  br i1 %.not822, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %27, %.lr.ph
  %31 = phi i8 [ %37, %.lr.ph ], [ %30, %27 ]
  %32 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i8 noundef signext %31)
  %33 = load i32, ptr @i, align 4, !tbaa !15
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr @i, align 4, !tbaa !15
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [50 x i8], ptr @m, i64 0, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !9
  %.not8 = icmp eq i8 %37, -1
  br i1 %.not8, label %._crit_edge, label %.lr.ph, !llvm.loop !17

._crit_edge:                                      ; preds = %.lr.ph, %27
  %38 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, ptr noundef nonnull @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c(ptr noundef nonnull align 8 dereferenceable(8), i8 noundef signext) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEPFRSoS_E(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(ptr noundef nonnull align 8 dereferenceable(8)) #0

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass