; LLVM IR from HarshCasper/NeoAlgo :: C/math (sum_of_divisors)

; 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: nounwind uwtable
define dso_local i64 @get_sum_of_divisors(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 0, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = sitofp i32 %6 to double
  %8 = call double @sqrt(double noundef %7) #3
  %9 = fptosi double %8 to i32
  store i32 %9, ptr %5, align 4, !tbaa !5
  store i64 1, ptr %4, align 8, !tbaa !9
  br label %10

10:                                               ; preds = %32, %1
  %11 = load i64, ptr %4, align 8, !tbaa !9
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = icmp slt i64 %11, %13
  br i1 %14, label %15, label %35

15:                                               ; preds = %10
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = load i64, ptr %4, align 8, !tbaa !9
  %19 = srem i64 %17, %18
  %20 = icmp eq i64 %19, 0
  br i1 %20, label %21, label %31

21:                                               ; preds = %15
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = load i64, ptr %3, align 8, !tbaa !9
  %24 = add nsw i64 %23, %22
  store i64 %24, ptr %3, align 8, !tbaa !9
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = load i64, ptr %4, align 8, !tbaa !9
  %28 = sdiv i64 %26, %27
  %29 = load i64, ptr %3, align 8, !tbaa !9
  %30 = add nsw i64 %29, %28
  store i64 %30, ptr %3, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %21, %15
  br label %32

32:                                               ; preds = %31
  %33 = load i64, ptr %4, align 8, !tbaa !9
  %34 = add nsw i64 %33, 1
  store i64 %34, ptr %4, align 8, !tbaa !9
  br label %10, !llvm.loop !11

35:                                               ; preds = %10
  %36 = load i32, ptr %2, align 4, !tbaa !5
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = srem i32 %36, %37
  %39 = icmp eq i32 %38, 0
  br i1 %39, label %40, label %63

40:                                               ; preds = %35
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = mul nsw i32 %41, %42
  %44 = load i32, ptr %2, align 4, !tbaa !5
  %45 = icmp eq i32 %43, %44
  br i1 %45, label %46, label %51

46:                                               ; preds = %40
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = load i64, ptr %3, align 8, !tbaa !9
  %50 = add nsw i64 %49, %48
  store i64 %50, ptr %3, align 8, !tbaa !9
  br label %62

51:                                               ; preds = %40
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = load i64, ptr %3, align 8, !tbaa !9
  %55 = add nsw i64 %54, %53
  store i64 %55, ptr %3, align 8, !tbaa !9
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = sdiv i32 %56, %57
  %59 = sext i32 %58 to i64
  %60 = load i64, ptr %3, align 8, !tbaa !9
  %61 = add nsw i64 %60, %59
  store i64 %61, ptr %3, align 8, !tbaa !9
  br label %62

62:                                               ; preds = %51, %46
  br label %63

63:                                               ; preds = %62, %35
  %64 = load i64, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %64
}

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

; Function Attrs: nounwind
declare double @sqrt(double noundef) #2

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

attributes #0 = { 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 "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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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: nofree nounwind memory(write) uwtable
define dso_local i64 @get_sum_of_divisors(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sitofp i32 %0 to double
  %3 = tail call double @sqrt(double noundef %2) #2
  %4 = fptosi double %3 to i32
  %5 = sext i32 %4 to i64
  %6 = icmp sgt i32 %4, 1
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %7 = sext i32 %0 to i64
  br label %8

8:                                                ; preds = %.lr.ph, %8
  %.028 = phi i64 [ 0, %.lr.ph ], [ %.1, %8 ]
  %.02427 = phi i64 [ 1, %.lr.ph ], [ %14, %8 ]
  %9 = srem i64 %7, %.02427
  %10 = sdiv i64 %7, %.02427
  %11 = icmp eq i64 %9, 0
  %12 = add nsw i64 %.028, %.02427
  %13 = add nsw i64 %12, %10
  %.1 = select i1 %11, i64 %13, i64 %.028
  %14 = add nuw nsw i64 %.02427, 1
  %exitcond.not = icmp eq i64 %14, %5
  br i1 %exitcond.not, label %._crit_edge, label %8, !llvm.loop !5

._crit_edge:                                      ; preds = %8, %1
  %.0.lcssa = phi i64 [ 0, %1 ], [ %.1, %8 ]
  %15 = srem i32 %0, %4
  %16 = sdiv i32 %0, %4
  %17 = icmp eq i32 %15, 0
  br i1 %17, label %18, label %25

18:                                               ; preds = %._crit_edge
  %19 = mul nsw i32 %4, %4
  %20 = icmp eq i32 %19, %0
  %21 = add nsw i64 %.0.lcssa, %5
  br i1 %20, label %25, label %22

22:                                               ; preds = %18
  %23 = sext i32 %16 to i64
  %24 = add nsw i64 %21, %23
  br label %25

25:                                               ; preds = %18, %22, %._crit_edge
  %.2 = phi i64 [ %24, %22 ], [ %.0.lcssa, %._crit_edge ], [ %21, %18 ]
  ret i64 %.2
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #1

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


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