; LLVM IR from Muhammad-Magdi/problem-solving :: Number Theory/Sieve_Prime_Factors.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"

@greatestPF = external dso_local global [100005 x i32], align 16
@n = external dso_local global i32, align 4

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

4:                                                ; preds = %35, %0
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = load i32, ptr %1, align 4, !tbaa !5
  %7 = mul nsw i32 %5, %6
  %8 = load i32, ptr @n, align 4, !tbaa !5
  %9 = icmp sle i32 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %4
  store i32 2, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %38

11:                                               ; preds = %4
  %12 = load i32, ptr %1, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [100005 x i32], ptr @greatestPF, i64 0, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %34, label %17

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

19:                                               ; preds = %29, %17
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = load i32, ptr @n, align 4, !tbaa !5
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %24, label %23

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

24:                                               ; preds = %19
  %25 = load i32, ptr %1, align 4, !tbaa !5
  %26 = load i32, ptr %3, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100005 x i32], ptr @greatestPF, i64 0, i64 %27
  store i32 %25, ptr %28, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %24
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = add nsw i32 %31, %30
  store i32 %32, ptr %3, align 4, !tbaa !5
  br label %19, !llvm.loop !9

33:                                               ; preds = %23
  br label %34

34:                                               ; preds = %33, %11
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %1, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %1, align 4, !tbaa !5
  br label %4, !llvm.loop !11

38:                                               ; preds = %10
  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

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 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !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"

@greatestPF = external dso_local local_unnamed_addr global [100005 x i32], align 16
@n = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5sievev() local_unnamed_addr #0 {
  store i32 1, ptr getelementptr inbounds (i8, ptr @greatestPF, i64 4), align 4, !tbaa !5
  store i32 1, ptr @greatestPF, align 16, !tbaa !5
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %.not13 = icmp slt i32 %1, 4
  br i1 %.not13, label %._crit_edge, label %.lr.ph16.preheader

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

._crit_edge:                                      ; preds = %.loopexit, %0
  ret void

.lr.ph16:                                         ; preds = %.lr.ph16.preheader, %.loopexit
  %indvars.iv = phi i64 [ 2, %.lr.ph16.preheader ], [ %indvars.iv.next, %.loopexit ]
  %3 = getelementptr inbounds [100005 x i32], ptr @greatestPF, i64 0, i64 %indvars.iv
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %.not11 = icmp eq i32 %4, 0
  %5 = icmp ult i64 %indvars.iv, %2
  %or.cond = and i1 %.not11, %5
  br i1 %or.cond, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %.lr.ph16
  %6 = trunc nuw nsw i64 %indvars.iv to i32
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv17 = phi i64 [ %indvars.iv, %.lr.ph.preheader ], [ %indvars.iv.next18, %.lr.ph ]
  %7 = getelementptr inbounds [100005 x i32], ptr @greatestPF, i64 0, i64 %indvars.iv17
  store i32 %6, ptr %7, align 4, !tbaa !5
  %indvars.iv.next18 = add nuw nsw i64 %indvars.iv17, %indvars.iv
  %8 = trunc nuw i64 %indvars.iv.next18 to i32
  %9 = icmp sgt i32 %1, %8
  br i1 %9, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph16
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %10 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %11 = trunc nuw i64 %10 to i32
  %.not = icmp slt i32 %1, %11
  br i1 %.not, label %._crit_edge, label %.lr.ph16, !llvm.loop !11
}

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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