; LLVM IR from bhrigukansra/algorithms-in-C-Cplusplus-Java-Python-JavaScript :: Algorithms/Mathematics/C++/SieveOfEratosthenes.ll (Z5sievev)

; 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 [100000 x i8], align 16

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

3:                                                ; preds = %33, %0
  %4 = load i32, ptr %1, align 4, !tbaa !9
  %5 = load i32, ptr %1, align 4, !tbaa !9
  %6 = mul nsw i32 %4, %5
  %7 = icmp sle i32 %6, 100000
  br i1 %7, label %8, label %36

8:                                                ; preds = %3
  %9 = load i32, ptr %1, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [100000 x i8], ptr @prime, i64 0, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !5, !range !11, !noundef !12
  %13 = trunc i8 %12 to i1
  %14 = zext i1 %13 to i32
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %16, label %32

16:                                               ; preds = %8
  %17 = load i32, ptr %1, align 4, !tbaa !9
  %18 = load i32, ptr %1, align 4, !tbaa !9
  %19 = mul nsw i32 %17, %18
  store i32 %19, ptr %2, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %27, %16
  %21 = load i32, ptr %2, align 4, !tbaa !9
  %22 = icmp sle i32 %21, 100000
  br i1 %22, label %23, label %31

23:                                               ; preds = %20
  %24 = load i32, ptr %2, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [100000 x i8], ptr @prime, i64 0, i64 %25
  store i8 0, ptr %26, align 1, !tbaa !5
  br label %27

27:                                               ; preds = %23
  %28 = load i32, ptr %2, align 4, !tbaa !9
  %29 = load i32, ptr %1, align 4, !tbaa !9
  %30 = add nsw i32 %28, %29
  store i32 %30, ptr %2, align 4, !tbaa !9
  br label %20, !llvm.loop !13

31:                                               ; preds = %20
  br label %32

32:                                               ; preds = %31, %8
  br label %33

33:                                               ; preds = %32
  %34 = load i32, ptr %1, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %1, align 4, !tbaa !9
  br label %3, !llvm.loop !15

36:                                               ; preds = %3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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

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


; 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 [100000 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5sievev() local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(100000) @prime, i8 1, i64 100000, i1 false)
  store i8 0, ptr getelementptr inbounds (i8, ptr @prime, i64 1), align 1, !tbaa !5
  store i8 0, ptr @prime, align 16, !tbaa !5
  br label %1

1:                                                ; preds = %.loopexit.1, %0
  %indvars.iv = phi i64 [ 2, %0 ], [ %indvars.iv.next.1, %.loopexit.1 ]
  %2 = phi i32 [ 4, %0 ], [ %19, %.loopexit.1 ]
  %3 = getelementptr inbounds [100000 x i8], ptr @prime, i64 0, i64 %indvars.iv
  %4 = load i8, ptr %3, align 2, !tbaa !5, !range !9, !noundef !10
  %5 = trunc nuw i8 %4 to i1
  %6 = icmp ult i32 %2, 100001
  %or.cond = and i1 %6, %5
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %1
  %7 = zext nneg i32 %2 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv14 = phi i64 [ %7, %.lr.ph.preheader ], [ %indvars.iv.next15, %.lr.ph ]
  %8 = getelementptr inbounds [100000 x i8], ptr @prime, i64 0, i64 %indvars.iv14
  store i8 0, ptr %8, align 1, !tbaa !5
  %indvars.iv.next15 = add nuw nsw i64 %indvars.iv14, %indvars.iv
  %9 = icmp ult i64 %indvars.iv.next15, 100001
  br i1 %9, label %.lr.ph, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %1
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %10 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %exitcond.not = icmp eq i64 %indvars.iv, 316
  br i1 %exitcond.not, label %20, label %11, !llvm.loop !13

11:                                               ; preds = %.loopexit
  %12 = getelementptr inbounds [100000 x i8], ptr @prime, i64 0, i64 %indvars.iv.next
  %13 = load i8, ptr %12, align 1, !tbaa !5, !range !9, !noundef !10
  %14 = trunc nuw i8 %13 to i1
  %15 = icmp ult i64 %10, 100001
  %or.cond.1 = and i1 %15, %14
  br i1 %or.cond.1, label %.lr.ph.1, label %.loopexit.1

.lr.ph.1:                                         ; preds = %11, %.lr.ph.1
  %indvars.iv14.1 = phi i64 [ %indvars.iv.next15.1, %.lr.ph.1 ], [ %10, %11 ]
  %16 = getelementptr inbounds [100000 x i8], ptr @prime, i64 0, i64 %indvars.iv14.1
  store i8 0, ptr %16, align 1, !tbaa !5
  %indvars.iv.next15.1 = add nuw nsw i64 %indvars.iv14.1, %indvars.iv.next
  %17 = icmp ult i64 %indvars.iv.next15.1, 100001
  br i1 %17, label %.lr.ph.1, label %.loopexit.1, !llvm.loop !11

.loopexit.1:                                      ; preds = %.lr.ph.1, %11
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %18 = mul nuw nsw i64 %indvars.iv.next.1, %indvars.iv.next.1
  %19 = trunc nuw i64 %18 to i32
  br label %1

20:                                               ; preds = %.loopexit
  ret void
}

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


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