; LLVM IR from JanaSabuj/cppdump :: Old/Math-NumberTheory/Sieve.ll (Z3Erav)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3Erav() #0 {
  %1 = alloca i32, align 4
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 1000007, ptr %1, align 4, !tbaa !5
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = add nsw i32 %7, 1
  %9 = zext i32 %8 to i64
  %10 = call ptr @llvm.stacksave.p0()
  store ptr %10, ptr %2, align 8
  %11 = alloca i8, i64 %9, align 16
  store i64 %9, ptr %3, align 8
  call void @llvm.memset.p0.i64(ptr align 16 %11, i8 1, i64 %9, i1 false)
  %12 = getelementptr inbounds i8, ptr %11, i64 0
  store i8 0, ptr %12, align 16, !tbaa !9
  %13 = getelementptr inbounds i8, ptr %11, i64 1
  store i8 0, ptr %13, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 2, ptr %4, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %48, %0
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = mul nsw i32 %15, %16
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = icmp sle i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %14
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %51

21:                                               ; preds = %14
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i8, ptr %11, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9, !range !11, !noundef !12
  %26 = trunc i8 %25 to i1
  %27 = zext i1 %26 to i32
  %28 = icmp eq i32 %27, 1
  br i1 %28, label %29, label %47

29:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %30 = load i32, ptr %4, align 4, !tbaa !5
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = mul nsw i32 %30, %31
  store i32 %32, ptr %6, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %42, %29
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = load i32, ptr %1, align 4, !tbaa !5
  %36 = icmp sle i32 %34, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %33
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %46

38:                                               ; preds = %33
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %11, i64 %40
  store i8 0, ptr %41, align 1, !tbaa !9
  br label %42

42:                                               ; preds = %38
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = add nsw i32 %44, %43
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %33, !llvm.loop !13

46:                                               ; preds = %37
  br label %47

47:                                               ; preds = %46, %21
  br label %48

48:                                               ; preds = %47
  %49 = load i32, ptr %4, align 4, !tbaa !5
  %50 = add nsw i32 %49, 1
  store i32 %50, ptr %4, align 4, !tbaa !5
  br label %14, !llvm.loop !15

51:                                               ; preds = %20
  %52 = load ptr, ptr %2, align 8
  call void @llvm.stackrestore.p0(ptr %52)
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  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
declare ptr @llvm.stacksave.p0() #2

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 = !{!"bool", !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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local void @_Z3Erav() local_unnamed_addr #0 {
  %1 = alloca [1000008 x i8], align 16
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1000008) %1, i8 1, i64 1000008, i1 false)
  %2 = getelementptr inbounds i8, ptr %1, i64 1
  store i8 0, ptr %2, align 1, !tbaa !5
  br label %4

3:                                                ; preds = %.loopexit
  ret void

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

.lr.ph.preheader:                                 ; preds = %4
  %10 = zext nneg i32 %5 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv17 = phi i64 [ %10, %.lr.ph.preheader ], [ %indvars.iv.next18, %.lr.ph ]
  %11 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv17
  store i8 0, ptr %11, align 1, !tbaa !5
  %indvars.iv.next18 = add nuw nsw i64 %indvars.iv17, %indvars.iv
  %12 = icmp ult i64 %indvars.iv.next18, 1000008
  br i1 %12, label %.lr.ph, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %4
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %13 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %exitcond.not = icmp eq i64 %indvars.iv, 1000
  br i1 %exitcond.not, label %3, label %14, !llvm.loop !13

14:                                               ; preds = %.loopexit
  %15 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.next
  %16 = load i8, ptr %15, align 1, !tbaa !5, !range !9, !noundef !10
  %17 = trunc nuw i8 %16 to i1
  %18 = icmp ult i64 %13, 1000008
  %or.cond.1 = and i1 %18, %17
  br i1 %or.cond.1, label %.lr.ph.1, label %.loopexit.1

.lr.ph.1:                                         ; preds = %14, %.lr.ph.1
  %indvars.iv17.1 = phi i64 [ %indvars.iv.next18.1, %.lr.ph.1 ], [ %13, %14 ]
  %19 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv17.1
  store i8 0, ptr %19, align 1, !tbaa !5
  %indvars.iv.next18.1 = add nuw nsw i64 %indvars.iv17.1, %indvars.iv.next
  %20 = icmp ult i64 %indvars.iv.next18.1, 1000008
  br i1 %20, label %.lr.ph.1, label %.loopexit.1, !llvm.loop !11

.loopexit.1:                                      ; preds = %.lr.ph.1, %14
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %21 = mul nuw nsw i64 %indvars.iv.next.1, %indvars.iv.next.1
  %22 = trunc nuw i64 %21 to i32
  br label %4
}

; 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 willreturn memory(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: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass