; LLVM IR from TheAlgorithms/C-Plus-Plus :: math/primes_up_to_billion.ll (Z5Sievel)

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

@prime = external dso_local global [100000000 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5Sievel(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 16 @prime, i8 49, i64 100000000, i1 false)
  store i8 48, ptr @prime, align 16, !tbaa !9
  store i8 48, ptr getelementptr inbounds ([100000000 x i8], ptr @prime, i64 0, i64 1), align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 2, ptr %3, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %37, %1
  %7 = load i64, ptr %3, align 8, !tbaa !5
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = mul nsw i64 %7, %8
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = icmp sle i64 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %6
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %40

13:                                               ; preds = %6
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds [100000000 x i8], ptr @prime, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !9
  %17 = sext i8 %16 to i32
  %18 = icmp eq i32 %17, 49
  br i1 %18, label %19, label %36

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = load i64, ptr %3, align 8, !tbaa !5
  %22 = mul nsw i64 %20, %21
  store i64 %22, ptr %5, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %31, %19
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = load i64, ptr %2, align 8, !tbaa !5
  %26 = icmp sle i64 %24, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %23
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %35

28:                                               ; preds = %23
  %29 = load i64, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds [100000000 x i8], ptr @prime, i64 0, i64 %29
  store i8 48, ptr %30, align 1, !tbaa !9
  br label %31

31:                                               ; preds = %28
  %32 = load i64, ptr %3, align 8, !tbaa !5
  %33 = load i64, ptr %5, align 8, !tbaa !5
  %34 = add nsw i64 %33, %32
  store i64 %34, ptr %5, align 8, !tbaa !5
  br label %23, !llvm.loop !10

35:                                               ; preds = %27
  br label %36

36:                                               ; preds = %35, %13
  br label %37

37:                                               ; preds = %36
  %38 = load i64, ptr %3, align 8, !tbaa !5
  %39 = add nsw i64 %38, 1
  store i64 %39, ptr %3, align 8, !tbaa !5
  br label %6, !llvm.loop !12

40:                                               ; preds = %12
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; 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

attributes #0 = { 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 #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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

@prime = external dso_local local_unnamed_addr global [100000000 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5Sievel(i64 noundef %0) local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(100000000) @prime, i8 49, i64 100000000, i1 false)
  store i8 48, ptr @prime, align 16, !tbaa !5
  store i8 48, ptr getelementptr inbounds (i8, ptr @prime, i64 1), align 1, !tbaa !5
  %.not18 = icmp slt i64 %0, 4
  br i1 %.not18, label %._crit_edge, label %.lr.ph20

._crit_edge:                                      ; preds = %.loopexit, %1
  ret void

.lr.ph20:                                         ; preds = %1, %.loopexit
  %2 = phi i64 [ %9, %.loopexit ], [ 4, %1 ]
  %.01219 = phi i64 [ %8, %.loopexit ], [ 2, %1 ]
  %3 = getelementptr inbounds [100000000 x i8], ptr @prime, i64 0, i64 %.01219
  %4 = load i8, ptr %3, align 1, !tbaa !5
  %5 = icmp ne i8 %4, 49
  %.not1516 = icmp sgt i64 %2, %0
  %or.cond = or i1 %5, %.not1516
  br i1 %or.cond, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph20, %.lr.ph
  %.017 = phi i64 [ %7, %.lr.ph ], [ %2, %.lr.ph20 ]
  %6 = getelementptr inbounds [100000000 x i8], ptr @prime, i64 0, i64 %.017
  store i8 48, ptr %6, align 1, !tbaa !5
  %7 = add nuw nsw i64 %.017, %.01219
  %.not15 = icmp sgt i64 %7, %0
  br i1 %.not15, label %.loopexit, label %.lr.ph, !llvm.loop !8

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph20
  %8 = add nuw nsw i64 %.01219, 1
  %9 = mul nuw nsw i64 %8, %8
  %.not = icmp sgt i64 %9, %0
  br i1 %.not, label %._crit_edge, label %.lr.ph20, !llvm.loop !10
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

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


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