; LLVM IR from AlgoGenesis/C :: Mathematical Algorithms/Divisor Algorithms (Program)

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

7:                                                ; preds = %42, %1
  %8 = load i32, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = mul nsw i32 %8, %9
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = icmp sle i32 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %7
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %45

14:                                               ; preds = %7
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 1, ptr %6, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %20, %14
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = srem i32 %16, %17
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %34

20:                                               ; preds = %15
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = sdiv i32 %22, %21
  store i32 %23, ptr %2, align 4, !tbaa !5
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sitofp i32 %26 to double
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = sitofp i32 %28 to double
  %30 = call double @pow(double noundef %27, double noundef %29) #3
  %31 = fptosi double %30 to i32
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = add nsw i32 %32, %31
  store i32 %33, ptr %6, align 4, !tbaa !5
  br label %15, !llvm.loop !9

34:                                               ; preds = %15
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = icmp sgt i32 %35, 0
  br i1 %36, label %37, label %41

37:                                               ; preds = %34
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = load i32, ptr %3, align 4, !tbaa !5
  %40 = mul nsw i32 %39, %38
  store i32 %40, ptr %3, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %37, %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %5, align 4, !tbaa !5
  br label %7, !llvm.loop !11

45:                                               ; preds = %13
  %46 = load i32, ptr %2, align 4, !tbaa !5
  %47 = icmp sgt i32 %46, 1
  br i1 %47, label %48, label %53

48:                                               ; preds = %45
  %49 = load i32, ptr %2, align 4, !tbaa !5
  %50 = add nsw i32 1, %49
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = mul nsw i32 %51, %50
  store i32 %52, ptr %3, align 4, !tbaa !5
  br label %53

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 "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 = 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: nofree nounwind memory(write) uwtable
define dso_local i32 @sum_divisors(i32 noundef %0) local_unnamed_addr #0 {
  %.not30 = icmp slt i32 %0, 4
  br i1 %.not30, label %._crit_edge34, label %.preheader

.preheader:                                       ; preds = %1, %._crit_edge
  %.01933 = phi i32 [ %17, %._crit_edge ], [ 2, %1 ]
  %.02132 = phi i32 [ %spec.select, %._crit_edge ], [ 1, %1 ]
  %.02231 = phi i32 [ %.123.lcssa, %._crit_edge ], [ %0, %1 ]
  %2 = srem i32 %.02231, %.01933
  %.not37 = icmp eq i32 %2, 0
  br i1 %.not37, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader
  %3 = uitofp nneg i32 %.01933 to double
  br label %7

._crit_edge34:                                    ; preds = %._crit_edge, %1
  %.022.lcssa = phi i32 [ %0, %1 ], [ %.123.lcssa, %._crit_edge ]
  %.021.lcssa = phi i32 [ 1, %1 ], [ %spec.select, %._crit_edge ]
  %4 = icmp sgt i32 %.022.lcssa, 1
  %5 = add nuw nsw i32 %.022.lcssa, 1
  %6 = select i1 %4, i32 %5, i32 1
  %.2 = mul nsw i32 %6, %.021.lcssa
  ret i32 %.2

7:                                                ; preds = %.lr.ph, %7
  %.027 = phi i32 [ 1, %.lr.ph ], [ %13, %7 ]
  %.02026 = phi i32 [ 0, %.lr.ph ], [ %9, %7 ]
  %.12325 = phi i32 [ %.02231, %.lr.ph ], [ %8, %7 ]
  %8 = sdiv i32 %.12325, %.01933
  %9 = add nuw nsw i32 %.02026, 1
  %10 = uitofp nneg i32 %9 to double
  %11 = tail call double @pow(double noundef %3, double noundef %10) #2
  %12 = fptosi double %11 to i32
  %13 = add nsw i32 %.027, %12
  %14 = srem i32 %8, %.01933
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %7, label %._crit_edge, !llvm.loop !5

._crit_edge:                                      ; preds = %7, %.preheader
  %16 = phi i32 [ 1, %.preheader ], [ %13, %7 ]
  %.123.lcssa = phi i32 [ %.02231, %.preheader ], [ %8, %7 ]
  %spec.select = mul nsw i32 %16, %.02132
  %17 = add nuw nsw i32 %.01933, 1
  %18 = mul nuw nsw i32 %17, %17
  %.not = icmp sgt i32 %18, %.123.lcssa
  br i1 %.not, label %._crit_edge34, label %.preheader, !llvm.loop !7
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @pow(double noundef, 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"}
!7 = distinct !{!7, !6}


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