; LLVM IR from Dev-XYS/Algorithms :: Modular-Multiplicative-Inverse-Sieve(Factorial,Linear).ll (Z13fac_inv_sievev)

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

@n = external dso_local global i64, align 8
@fac_inv = external dso_local global [10000000 x i64], align 16
@p = external dso_local global i64, align 8

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_Z4qpowxx(i64 noundef, i64 noundef) #0

; 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 @_Z3facx(i64 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z13fac_inv_sievev() #0 {
  %1 = alloca i64, align 8
  %2 = load i64, ptr @n, align 8, !tbaa !5
  %3 = call noundef i64 @_Z3facx(i64 noundef %2)
  %4 = load i64, ptr @p, align 8, !tbaa !5
  %5 = sub nsw i64 %4, 2
  %6 = call noundef i64 @_Z4qpowxx(i64 noundef %3, i64 noundef %5)
  %7 = load i64, ptr @n, align 8, !tbaa !5
  %8 = getelementptr inbounds [10000000 x i64], ptr @fac_inv, i64 0, i64 %7
  store i64 %6, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %9 = load i64, ptr @n, align 8, !tbaa !5
  %10 = sub nsw i64 %9, 1
  store i64 %10, ptr %1, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %27, %0
  %12 = load i64, ptr %1, align 8, !tbaa !5
  %13 = icmp sge i64 %12, 1
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  br label %30

15:                                               ; preds = %11
  %16 = load i64, ptr %1, align 8, !tbaa !5
  %17 = add nsw i64 %16, 1
  %18 = getelementptr inbounds [10000000 x i64], ptr @fac_inv, i64 0, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = load i64, ptr %1, align 8, !tbaa !5
  %21 = add nsw i64 %20, 1
  %22 = mul nsw i64 %19, %21
  %23 = load i64, ptr @p, align 8, !tbaa !5
  %24 = srem i64 %22, %23
  %25 = load i64, ptr %1, align 8, !tbaa !5
  %26 = getelementptr inbounds [10000000 x i64], ptr @fac_inv, i64 0, i64 %25
  store i64 %24, ptr %26, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %15
  %28 = load i64, ptr %1, align 8, !tbaa !5
  %29 = add nsw i64 %28, -1
  store i64 %29, ptr %1, align 8, !tbaa !5
  br label %11, !llvm.loop !9

30:                                               ; preds = %14
  ret void
}

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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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"

@n = external dso_local local_unnamed_addr global i64, align 8
@fac_inv = external dso_local local_unnamed_addr global [10000000 x i64], align 16
@p = external dso_local local_unnamed_addr global i64, align 8

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z13fac_inv_sievev() local_unnamed_addr #0 {
  %1 = load i64, ptr @n, align 8, !tbaa !5
  %2 = tail call noundef i64 @_Z3facx(i64 noundef %1)
  %3 = load i64, ptr @p, align 8, !tbaa !5
  %4 = add nsw i64 %3, -2
  %5 = tail call noundef i64 @_Z4qpowxx(i64 noundef %2, i64 noundef %4)
  %6 = load i64, ptr @n, align 8, !tbaa !5
  %7 = getelementptr inbounds [10000000 x i64], ptr @fac_inv, i64 0, i64 %6
  store i64 %5, ptr %7, align 8, !tbaa !5
  %8 = icmp sgt i64 %6, 1
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0
  %9 = load i64, ptr @p, align 8, !tbaa !5
  %10 = and i64 %6, 1
  %lcmp.mod.not.not = icmp eq i64 %10, 0
  br i1 %lcmp.mod.not.not, label %.prol.loopexit.unr-lcssa, label %.prol.loopexit

.prol.loopexit.unr-lcssa:                         ; preds = %.lr.ph
  %.0.prol = add nsw i64 %6, -1
  %11 = mul nsw i64 %5, %6
  %12 = srem i64 %11, %9
  %13 = getelementptr inbounds [10000000 x i64], ptr @fac_inv, i64 0, i64 %.0.prol
  store i64 %12, ptr %13, align 8, !tbaa !5
  br label %.prol.loopexit

.prol.loopexit:                                   ; preds = %.prol.loopexit.unr-lcssa, %.lr.ph
  %.unr = phi i64 [ %5, %.lr.ph ], [ %12, %.prol.loopexit.unr-lcssa ]
  %.0.in5.unr = phi i64 [ %6, %.lr.ph ], [ %.0.prol, %.prol.loopexit.unr-lcssa ]
  %14 = icmp eq i64 %6, 2
  br i1 %14, label %._crit_edge, label %.lr.ph.new

._crit_edge:                                      ; preds = %.prol.loopexit, %.lr.ph.new, %0
  ret void

.lr.ph.new:                                       ; preds = %.prol.loopexit, %.lr.ph.new
  %15 = phi i64 [ %20, %.lr.ph.new ], [ %.unr, %.prol.loopexit ]
  %.0.in5 = phi i64 [ %.0.1, %.lr.ph.new ], [ %.0.in5.unr, %.prol.loopexit ]
  %.0 = add nsw i64 %.0.in5, -1
  %16 = mul nsw i64 %15, %.0.in5
  %17 = srem i64 %16, %9
  %18 = getelementptr inbounds [10000000 x i64], ptr @fac_inv, i64 0, i64 %.0
  store i64 %17, ptr %18, align 8, !tbaa !5
  %.0.1 = add nsw i64 %.0.in5, -2
  %19 = mul nsw i64 %17, %.0
  %20 = srem i64 %19, %9
  %21 = getelementptr inbounds [10000000 x i64], ptr @fac_inv, i64 0, i64 %.0.1
  store i64 %20, ptr %21, align 8, !tbaa !5
  %22 = icmp ugt i64 %.0, 2
  br i1 %22, label %.lr.ph.new, label %._crit_edge, !llvm.loop !9
}

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


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