; LLVM IR from abhisheknaiidu/dsa :: gfg-DSA/prime-factorization.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"

@a = external dso_local global [1000001 x i64], align 16

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

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

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

6:                                                ; preds = %14, %0
  %7 = load i64, ptr %2, align 8, !tbaa !5
  %8 = load i64, ptr %1, align 8, !tbaa !5
  %9 = icmp sle i64 %7, %8
  br i1 %9, label %11, label %10

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

11:                                               ; preds = %6
  %12 = load i64, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds [1000001 x i64], ptr @a, i64 0, i64 %12
  store i64 -1, ptr %13, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %11
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = add nsw i64 %15, 1
  store i64 %16, ptr %2, align 8, !tbaa !5
  br label %6, !llvm.loop !9

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

18:                                               ; preds = %51, %17
  %19 = load i64, ptr %3, align 8, !tbaa !5
  %20 = load i64, ptr %1, align 8, !tbaa !5
  %21 = icmp sle i64 %19, %20
  br i1 %21, label %23, label %22

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

23:                                               ; preds = %18
  %24 = load i64, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds [1000001 x i64], ptr @a, i64 0, i64 %24
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = icmp eq i64 %26, -1
  br i1 %27, label %28, label %50

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %29 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %29, ptr %5, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %45, %28
  %31 = load i64, ptr %5, align 8, !tbaa !5
  %32 = load i64, ptr %1, align 8, !tbaa !5
  %33 = icmp sle i64 %31, %32
  br i1 %33, label %35, label %34

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

35:                                               ; preds = %30
  %36 = load i64, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds [1000001 x i64], ptr @a, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !5
  %39 = icmp eq i64 %38, -1
  br i1 %39, label %40, label %44

40:                                               ; preds = %35
  %41 = load i64, ptr %3, align 8, !tbaa !5
  %42 = load i64, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds [1000001 x i64], ptr @a, i64 0, i64 %42
  store i64 %41, ptr %43, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %40, %35
  br label %45

45:                                               ; preds = %44
  %46 = load i64, ptr %3, align 8, !tbaa !5
  %47 = load i64, ptr %5, align 8, !tbaa !5
  %48 = add nsw i64 %47, %46
  store i64 %48, ptr %5, align 8, !tbaa !5
  br label %30, !llvm.loop !11

49:                                               ; preds = %34
  br label %50

50:                                               ; preds = %49, %23
  br label %51

51:                                               ; preds = %50
  %52 = load i64, ptr %3, align 8, !tbaa !5
  %53 = add nsw i64 %52, 1
  store i64 %53, ptr %3, align 8, !tbaa !5
  br label %18, !llvm.loop !12

54:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret void
}

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


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

@a = external dso_local local_unnamed_addr global [1000001 x i64], 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 {
.preheader18.preheader:
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(8000000) getelementptr inbounds (i8, ptr @a, i64 8), i8 -1, i64 8000000, i1 false), !tbaa !5
  br label %.preheader18

0:                                                ; preds = %.loopexit
  ret void

.preheader18:                                     ; preds = %.preheader18.preheader, %.loopexit
  %.01621 = phi i64 [ %11, %.loopexit ], [ 2, %.preheader18.preheader ]
  %1 = getelementptr inbounds [1000001 x i64], ptr @a, i64 0, i64 %.01621
  %2 = load i64, ptr %1, align 8, !tbaa !5
  %3 = icmp eq i64 %2, -1
  br i1 %3, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader18, %8
  %.020 = phi i64 [ %9, %8 ], [ %.01621, %.preheader18 ]
  %4 = getelementptr inbounds [1000001 x i64], ptr @a, i64 0, i64 %.020
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp eq i64 %5, -1
  br i1 %6, label %7, label %8

7:                                                ; preds = %.lr.ph
  store i64 %.01621, ptr %4, align 8, !tbaa !5
  br label %8

8:                                                ; preds = %.lr.ph, %7
  %9 = add nuw nsw i64 %.020, %.01621
  %10 = icmp ult i64 %9, 1000001
  br i1 %10, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %8, %.preheader18
  %11 = add nuw nsw i64 %.01621, 1
  %exitcond.not = icmp eq i64 %11, 1000001
  br i1 %exitcond.not, label %0, label %.preheader18, !llvm.loop !11
}

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


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