; LLVM IR from ShahjalalShohag/code-library :: Number Theory/Sieve.ll (Z8sieve_v0v)

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

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@primes = external dso_local global %"class.std::vector", align 8
@is_prime = external dso_local global [1000009 x i8], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z8sieve_v0v() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 2, ptr %1, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %14, %0
  %7 = load i32, ptr %1, align 4, !tbaa !5
  %8 = icmp slt i32 %7, 1000009
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %17

10:                                               ; preds = %6
  %11 = load i32, ptr %1, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [1000009 x i8], ptr @is_prime, i64 0, i64 %12
  store i8 1, ptr %13, align 1, !tbaa !9
  br label %14

14:                                               ; preds = %10
  %15 = load i32, ptr %1, align 4, !tbaa !5
  %16 = add nsw i32 %15, 1
  store i32 %16, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !11

17:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  store i32 2, ptr %2, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %48, %17
  %19 = load i32, ptr %2, align 4, !tbaa !5
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = mul nsw i32 %19, %20
  %22 = icmp slt i32 %21, 1000009
  br i1 %22, label %24, label %23

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

24:                                               ; preds = %18
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [1000009 x i8], ptr @is_prime, i64 0, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !9, !range !13, !noundef !14
  %29 = trunc i8 %28 to i1
  br i1 %29, label %30, label %47

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

34:                                               ; preds = %42, %30
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = icmp slt i32 %35, 1000009
  br i1 %36, label %38, label %37

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

38:                                               ; preds = %34
  %39 = load i32, ptr %4, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [1000009 x i8], ptr @is_prime, i64 0, i64 %40
  store i8 0, ptr %41, align 1, !tbaa !9
  br label %42

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

46:                                               ; preds = %37
  br label %47

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

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

51:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 2, ptr %5, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %64, %51
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = icmp slt i32 %53, 1000009
  br i1 %54, label %56, label %55

55:                                               ; preds = %52
  store i32 11, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %67

56:                                               ; preds = %52
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [1000009 x i8], ptr @is_prime, i64 0, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !9, !range !13, !noundef !14
  %61 = trunc i8 %60 to i1
  br i1 %61, label %62, label %63

62:                                               ; preds = %56
  call void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24) @primes, ptr noundef nonnull align 4 dereferenceable(4) %5)
  br label %63

63:                                               ; preds = %62, %56
  br label %64

64:                                               ; preds = %63
  %65 = load i32, ptr %5, align 4, !tbaa !5
  %66 = add nsw i32 %65, 1
  store i32 %66, ptr %5, align 4, !tbaa !5
  br label %52, !llvm.loop !17

67:                                               ; preds = %55
  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: mustprogress uwtable
declare dso_local void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 4 dereferenceable(4)) #0 align 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 = { 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12}
!17 = distinct !{!17, !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"

%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl" = type { %"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" }
%"struct.std::_Vector_base<int, std::allocator<int>>::_Vector_impl_data" = type { ptr, ptr, ptr }

@primes = external dso_local global %"class.std::vector", align 8
@is_prime = external dso_local local_unnamed_addr global [1000009 x i8], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z8sieve_v0v() local_unnamed_addr #0 {
.preheader17.preheader:
  %0 = alloca i32, align 4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 2 dereferenceable(1000007) getelementptr inbounds (i8, ptr @is_prime, i64 2), i8 1, i64 1000007, i1 false), !tbaa !5
  br label %.preheader17

1:                                                ; preds = %.loopexit
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %0) #3
  store i32 2, ptr %0, align 4, !tbaa !9
  br label %20

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

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv23 = phi i64 [ %7, %.lr.ph.preheader ], [ %indvars.iv.next24, %.lr.ph ]
  %8 = getelementptr inbounds [1000009 x i8], ptr @is_prime, i64 0, i64 %indvars.iv23
  store i8 0, ptr %8, align 1, !tbaa !5
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, %indvars.iv
  %9 = icmp ult i64 %indvars.iv.next24, 1000009
  br i1 %9, label %.lr.ph, label %.loopexit, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph, %.preheader17
  %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, 1000
  br i1 %exitcond.not, label %1, label %.preheader17.1, !llvm.loop !15

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

.lr.ph.1:                                         ; preds = %.preheader17.1, %.lr.ph.1
  %indvars.iv23.1 = phi i64 [ %indvars.iv.next24.1, %.lr.ph.1 ], [ %10, %.preheader17.1 ]
  %15 = getelementptr inbounds [1000009 x i8], ptr @is_prime, i64 0, i64 %indvars.iv23.1
  store i8 0, ptr %15, align 1, !tbaa !5
  %indvars.iv.next24.1 = add nuw nsw i64 %indvars.iv23.1, %indvars.iv.next
  %16 = icmp ult i64 %indvars.iv.next24.1, 1000009
  br i1 %16, label %.lr.ph.1, label %.loopexit.1, !llvm.loop !13

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

19:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %0) #3
  ret void

20:                                               ; preds = %1, %26
  %storemerge21 = phi i32 [ 2, %1 ], [ %28, %26 ]
  %21 = sext i32 %storemerge21 to i64
  %22 = getelementptr inbounds [1000009 x i8], ptr @is_prime, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !5, !range !11, !noundef !12
  %24 = trunc nuw i8 %23 to i1
  br i1 %24, label %25, label %26

25:                                               ; preds = %20
  call void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24) @primes, ptr noundef nonnull align 4 dereferenceable(4) %0)
  %.pre = load i32, ptr %0, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %20, %25
  %27 = phi i32 [ %storemerge21, %20 ], [ %.pre, %25 ]
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %0, align 4, !tbaa !9
  %29 = icmp slt i32 %27, 1000008
  br i1 %29, label %20, label %19, !llvm.loop !16
}

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

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt6vectorIiSaIiEE9push_backERKi(ptr noundef nonnull align 8 dereferenceable(24), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #0 align 2

; 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 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 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}
!16 = distinct !{!16, !14}


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