; LLVM IR from TheAlgorithms/C-Plus-Plus :: math/number_of_positive_divisors.ll (Z27number_of_positive_divisorsi)

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z27number_of_positive_divisorsi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 0
  br i1 %7, label %8, label %11

8:                                                ; preds = %1
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sub nsw i32 0, %9
  store i32 %10, ptr %2, align 4, !tbaa !5
  br label %11

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

12:                                               ; preds = %36, %11
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = load i32, ptr %4, align 4, !tbaa !5
  %15 = mul nsw i32 %13, %14
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = icmp sle i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %39

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

20:                                               ; preds = %25, %19
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = srem i32 %21, %22
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %31

25:                                               ; preds = %20
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %5, align 4, !tbaa !5
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sdiv i32 %29, %28
  store i32 %30, ptr %2, align 4, !tbaa !5
  br label %20, !llvm.loop !9

31:                                               ; preds = %20
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = mul nsw i32 %34, %33
  store i32 %35, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %36

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

39:                                               ; preds = %18
  %40 = load i32, ptr %2, align 4, !tbaa !5
  %41 = icmp sgt i32 %40, 1
  br i1 %41, label %42, label %45

42:                                               ; preds = %39
  %43 = load i32, ptr %3, align 4, !tbaa !5
  %44 = mul nsw i32 %43, 2
  store i32 %44, ptr %3, align 4, !tbaa !5
  br label %45

45:                                               ; preds = %42, %39
  %46 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %46
}

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i32 @_Z27number_of_positive_divisorsi(i32 noundef %0) local_unnamed_addr #0 {
  %spec.select = tail call i32 @llvm.abs.i32(i32 %0, i1 true)
  %.not26 = icmp ult i32 %spec.select, 4
  br i1 %.not26, label %._crit_edge30, label %.preheader

.preheader:                                       ; preds = %1, %._crit_edge
  %.01629 = phi i32 [ %12, %._crit_edge ], [ 2, %1 ]
  %.01728 = phi i32 [ %11, %._crit_edge ], [ 1, %1 ]
  %.11927 = phi i32 [ %.2.lcssa, %._crit_edge ], [ %spec.select, %1 ]
  %2 = srem i32 %.11927, %.01629
  %3 = icmp eq i32 %2, 0
  br i1 %3, label %.lr.ph, label %._crit_edge

._crit_edge30:                                    ; preds = %._crit_edge, %1
  %.119.lcssa = phi i32 [ %spec.select, %1 ], [ %.2.lcssa, %._crit_edge ]
  %.017.lcssa = phi i32 [ 1, %1 ], [ %11, %._crit_edge ]
  %4 = icmp sgt i32 %.119.lcssa, 1
  %5 = zext i1 %4 to i32
  %spec.select22 = shl nuw nsw i32 %.017.lcssa, %5
  ret i32 %spec.select22

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.024 = phi i32 [ %6, %.lr.ph ], [ 0, %.preheader ]
  %.223 = phi i32 [ %7, %.lr.ph ], [ %.11927, %.preheader ]
  %6 = add nuw nsw i32 %.024, 1
  %7 = sdiv i32 %.223, %.01629
  %8 = srem i32 %7, %.01629
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %.lr.ph, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.2.lcssa = phi i32 [ %.11927, %.preheader ], [ %7, %.lr.ph ]
  %.0.lcssa = phi i32 [ 0, %.preheader ], [ %6, %.lr.ph ]
  %10 = add nuw nsw i32 %.0.lcssa, 1
  %11 = mul nuw nsw i32 %10, %.01728
  %12 = add nuw nsw i32 %.01629, 1
  %13 = mul nuw nsw i32 %12, %12
  %.not = icmp sgt i32 %13, %.2.lcssa
  br i1 %.not, label %._crit_edge30, label %.preheader, !llvm.loop !7
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


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