; LLVM IR from manishbisht/Competitive-Programming :: Algorithms/Mathematics/Prime Number - Sieve of Eratosthenes.ll (Z5Sievex)

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5Sievex(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  store i8 0, ptr @prime, align 16, !tbaa !9
  store i8 0, ptr getelementptr inbounds ([1000005 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 %8

8:                                                ; preds = %15, %1
  %9 = load i64, ptr %3, align 8, !tbaa !5
  %10 = icmp slt i64 %9, 1000005
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %8
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %13
  store i8 1, ptr %14, align 1, !tbaa !9
  br label %15

15:                                               ; preds = %12
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = add nsw i64 %16, 1
  store i64 %17, ptr %3, align 8, !tbaa !5
  br label %8, !llvm.loop !11

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %19 = load i64, ptr %2, align 8, !tbaa !5
  %20 = call noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef %19)
  %21 = fptosi double %20 to i64
  store i64 %21, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 2, ptr %5, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %50, %18
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = load i64, ptr %4, align 8, !tbaa !5
  %25 = icmp sle i64 %23, %24
  br i1 %25, label %27, label %26

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

27:                                               ; preds = %22
  %28 = load i64, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !9, !range !13, !noundef !14
  %31 = trunc i8 %30 to i1
  br i1 %31, label %32, label %49

32:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %33 = load i64, ptr %5, align 8, !tbaa !5
  %34 = load i64, ptr %5, align 8, !tbaa !5
  %35 = mul nsw i64 %33, %34
  store i64 %35, ptr %7, align 8, !tbaa !5
  br label %36

36:                                               ; preds = %44, %32
  %37 = load i64, ptr %7, align 8, !tbaa !5
  %38 = load i64, ptr %2, align 8, !tbaa !5
  %39 = icmp sle i64 %37, %38
  br i1 %39, label %41, label %40

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

41:                                               ; preds = %36
  %42 = load i64, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %42
  store i8 0, ptr %43, align 1, !tbaa !9
  br label %44

44:                                               ; preds = %41
  %45 = load i64, ptr %5, align 8, !tbaa !5
  %46 = load i64, ptr %7, align 8, !tbaa !5
  %47 = add nsw i64 %46, %45
  store i64 %47, ptr %7, align 8, !tbaa !5
  br label %36, !llvm.loop !15

48:                                               ; preds = %40
  br label %49

49:                                               ; preds = %48, %27
  br label %50

50:                                               ; preds = %49
  %51 = load i64, ptr %5, align 8, !tbaa !5
  %52 = add nsw i64 %51, 1
  store i64 %52, ptr %5, align 8, !tbaa !5
  br label %22, !llvm.loop !16

53:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef) #2

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 = { inlinehint 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 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12}


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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5Sievex(i64 noundef %0) local_unnamed_addr #0 {
  store i8 0, ptr @prime, align 16, !tbaa !5
  store i8 0, ptr getelementptr inbounds (i8, ptr @prime, i64 1), align 1, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(1000003) getelementptr inbounds (i8, ptr @prime, i64 2), i8 1, i64 1000003, i1 false), !tbaa !5
  %2 = tail call noundef double @_ZSt4sqrtIxEN9__gnu_cxx11__enable_ifIXsr12__is_integerIT_EE7__valueEdE6__typeES2_(i64 noundef %0)
  %3 = fptosi double %2 to i64
  %.not21 = icmp slt i64 %3, 2
  br i1 %.not21, label %._crit_edge, label %.lr.ph24.preheader

.lr.ph24.preheader:                               ; preds = %1
  %4 = add nsw i64 %3, -1
  %xtraiter = and i64 %4, 1
  %5 = icmp eq i64 %3, 2
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph24.preheader.new

.lr.ph24.preheader.new:                           ; preds = %.lr.ph24.preheader
  %unroll_iter = and i64 %4, -2
  br label %.lr.ph24

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.loopexit.1, %.lr.ph24.preheader
  %.01522.unr = phi i64 [ 2, %.lr.ph24.preheader ], [ %25, %.loopexit.1 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph24.epil

.lr.ph24.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa
  %6 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %.01522.unr
  %7 = load i8, ptr %6, align 1, !tbaa !5, !range !9, !noundef !10
  %8 = trunc nuw i8 %7 to i1
  %9 = mul nuw nsw i64 %.01522.unr, %.01522.unr
  %.not1719.epil = icmp sle i64 %9, %0
  %or.cond.not.epil = select i1 %8, i1 %.not1719.epil, i1 false
  br i1 %or.cond.not.epil, label %.lr.ph.epil, label %._crit_edge

.lr.ph.epil:                                      ; preds = %.lr.ph24.epil, %.lr.ph.epil
  %.020.epil = phi i64 [ %11, %.lr.ph.epil ], [ %9, %.lr.ph24.epil ]
  %10 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %.020.epil
  store i8 0, ptr %10, align 1, !tbaa !5
  %11 = add nuw nsw i64 %.020.epil, %.01522.unr
  %.not17.epil = icmp sgt i64 %11, %0
  br i1 %.not17.epil, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !11

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %.lr.ph24.epil, %1
  ret void

.lr.ph24:                                         ; preds = %.loopexit.1, %.lr.ph24.preheader.new
  %.01522 = phi i64 [ 2, %.lr.ph24.preheader.new ], [ %25, %.loopexit.1 ]
  %niter = phi i64 [ 0, %.lr.ph24.preheader.new ], [ %niter.next.1, %.loopexit.1 ]
  %12 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %.01522
  %13 = load i8, ptr %12, align 2, !tbaa !5, !range !9, !noundef !10
  %14 = trunc nuw i8 %13 to i1
  %15 = mul nuw nsw i64 %.01522, %.01522
  %.not1719 = icmp sle i64 %15, %0
  %or.cond.not = select i1 %14, i1 %.not1719, i1 false
  br i1 %or.cond.not, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph24, %.lr.ph
  %.020 = phi i64 [ %17, %.lr.ph ], [ %15, %.lr.ph24 ]
  %16 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %.020
  store i8 0, ptr %16, align 2, !tbaa !5
  %17 = add nuw nsw i64 %.020, %.01522
  %.not17 = icmp sgt i64 %17, %0
  br i1 %.not17, label %.loopexit, label %.lr.ph, !llvm.loop !11

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph24
  %18 = or disjoint i64 %.01522, 1
  %19 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !5, !range !9, !noundef !10
  %21 = trunc nuw i8 %20 to i1
  %22 = mul nuw nsw i64 %18, %18
  %.not1719.1 = icmp sle i64 %22, %0
  %or.cond.not.1 = select i1 %21, i1 %.not1719.1, i1 false
  br i1 %or.cond.not.1, label %.lr.ph.1, label %.loopexit.1

.lr.ph.1:                                         ; preds = %.loopexit, %.lr.ph.1
  %.020.1 = phi i64 [ %24, %.lr.ph.1 ], [ %22, %.loopexit ]
  %23 = getelementptr inbounds [1000005 x i8], ptr @prime, i64 0, i64 %.020.1
  store i8 0, ptr %23, align 1, !tbaa !5
  %24 = add nuw nsw i64 %.020.1, %18
  %.not17.1 = icmp sgt i64 %24, %0
  br i1 %.not17.1, label %.loopexit.1, label %.lr.ph.1, !llvm.loop !11

.loopexit.1:                                      ; preds = %.lr.ph.1, %.loopexit
  %25 = add nuw i64 %.01522, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph24, !llvm.loop !13
}

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { inlinehint 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 #2 = { 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: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass