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

@numberOfDivisors = 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
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 1, ptr getelementptr inbounds ([100005 x i32], ptr @numberOfDivisors, i64 0, i64 1), align 4, !tbaa !5
  store i32 1, ptr @numberOfDivisors, 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 %6

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

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

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

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

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

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

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %27 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %27, ptr %5, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %33, %26
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = load i32, ptr %1, align 4, !tbaa !5
  %31 = srem i32 %29, %30
  %32 = icmp eq i32 %31, 0
  br i1 %32, label %33, label %39

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

39:                                               ; preds = %28
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [100005 x i32], ptr @numberOfDivisors, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %53

45:                                               ; preds = %39
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = add nsw i32 %46, 1
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [100005 x i32], ptr @numberOfDivisors, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = mul nsw i32 %51, %47
  store i32 %52, ptr %50, align 4, !tbaa !5
  br label %59

53:                                               ; preds = %39
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = add nsw i32 %54, 1
  %56 = load i32, ptr %3, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [100005 x i32], ptr @numberOfDivisors, i64 0, i64 %57
  store i32 %55, ptr %58, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %53, %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %60

60:                                               ; preds = %59
  %61 = load i32, ptr %1, align 4, !tbaa !5
  %62 = load i32, ptr %3, align 4, !tbaa !5
  %63 = add nsw i32 %62, %61
  store i32 %63, ptr %3, align 4, !tbaa !5
  br label %21, !llvm.loop !11

64:                                               ; preds = %25
  br label %65

65:                                               ; preds = %64, %13
  br label %66

66:                                               ; preds = %65
  %67 = load i32, ptr %1, align 4, !tbaa !5
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %1, align 4, !tbaa !5
  br label %6, !llvm.loop !12

69:                                               ; preds = %12
  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}
!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"

@numberOfDivisors = 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 @numberOfDivisors, i64 4), align 4, !tbaa !5
  store i32 1, ptr @numberOfDivisors, align 16, !tbaa !5
  %1 = load i32, ptr @n, align 4, !tbaa !5
  %.not29 = icmp slt i32 %1, 4
  br i1 %.not29, label %._crit_edge34, label %.lr.ph33.preheader

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

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

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

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

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %indvars.iv35 = phi i64 [ %indvars.iv, %.preheader.preheader ], [ %indvars.iv.next36, %._crit_edge ]
  %7 = trunc nuw nsw i64 %indvars.iv35 to i32
  %8 = urem i32 %7, %6
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.027 = phi i32 [ %11, %.lr.ph ], [ %7, %.preheader ]
  %.01926 = phi i32 [ %10, %.lr.ph ], [ 0, %.preheader ]
  %10 = add nuw nsw i32 %.01926, 1
  %11 = sdiv i32 %.027, %6
  %12 = srem i32 %11, %6
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %.lr.ph, label %._crit_edge, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.019.lcssa = phi i32 [ 0, %.preheader ], [ %10, %.lr.ph ]
  %14 = getelementptr inbounds [100005 x i32], ptr @numberOfDivisors, i64 0, i64 %indvars.iv35
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = add nuw nsw i32 %.019.lcssa, 1
  %17 = tail call i32 @llvm.umax.i32(i32 %15, i32 1)
  %storemerge = mul nsw i32 %17, %16
  store i32 %storemerge, ptr %14, align 4, !tbaa !5
  %indvars.iv.next36 = add nuw nsw i64 %indvars.iv35, %indvars.iv
  %18 = trunc nuw i64 %indvars.iv.next36 to i32
  %19 = icmp sgt i32 %1, %18
  br i1 %19, label %.preheader, label %.loopexit, !llvm.loop !11

.loopexit:                                        ; preds = %._crit_edge, %.lr.ph33
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %20 = mul nuw nsw i64 %indvars.iv.next, %indvars.iv.next
  %21 = trunc nuw i64 %20 to i32
  %.not = icmp slt i32 %1, %21
  br i1 %.not, label %._crit_edge34, label %.lr.ph33, !llvm.loop !12
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #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 nosync nounwind speculatable willreturn memory(none) }

!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}
!12 = distinct !{!12, !10}


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