; LLVM IR from jgaeddert/liquid-dsp :: src/math/src (modular_arithmetic)

; 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 @liquid_totient(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) #2
  %7 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %7, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %8 = load i32, ptr %2, align 4, !tbaa !5
  store i32 %8, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  br label %9

9:                                                ; preds = %41, %1
  store i32 2, ptr %6, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %37, %9
  %11 = load i32, ptr %6, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = icmp ule i32 %11, %12
  br i1 %13, label %14, label %40

14:                                               ; preds = %10
  %15 = load i32, ptr %4, align 4, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = urem i32 %15, %16
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %36

19:                                               ; preds = %14
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = udiv i32 %21, %20
  store i32 %22, ptr %4, align 4, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = load i32, ptr %6, align 4, !tbaa !5
  %25 = icmp ne i32 %23, %24
  br i1 %25, label %26, label %34

26:                                               ; preds = %19
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = sub i32 %27, 1
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = mul i32 %29, %28
  store i32 %30, ptr %3, align 4, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = load i32, ptr %3, align 4, !tbaa !5
  %33 = udiv i32 %32, %31
  store i32 %33, ptr %3, align 4, !tbaa !5
  br label %34

34:                                               ; preds = %26, %19
  %35 = load i32, ptr %6, align 4, !tbaa !5
  store i32 %35, ptr %5, align 4, !tbaa !5
  br label %40

36:                                               ; preds = %14
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = add i32 %38, 1
  store i32 %39, ptr %6, align 4, !tbaa !5
  br label %10, !llvm.loop !9

40:                                               ; preds = %34, %10
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %4, align 4, !tbaa !5
  %43 = icmp ugt i32 %42, 1
  br i1 %43, label %9, label %44, !llvm.loop !11

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

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 }

!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 norecurse nosync nounwind memory(none) uwtable
define dso_local i32 @liquid_totient(i32 noundef %0) local_unnamed_addr #0 {
  br label %2

2:                                                ; preds = %.loopexit, %1
  %.021 = phi i32 [ %0, %1 ], [ %.2, %.loopexit ]
  %.019 = phi i32 [ %0, %1 ], [ %.120, %.loopexit ]
  %.018 = phi i32 [ 0, %1 ], [ %.1, %.loopexit ]
  %.not25 = icmp ult i32 %.019, 2
  br i1 %.not25, label %.loopexit.thread, label %.lr.ph

.lr.ph:                                           ; preds = %2, %11
  %.026 = phi i32 [ %12, %11 ], [ 2, %2 ]
  %3 = urem i32 %.019, %.026
  %4 = udiv i32 %.019, %.026
  %5 = icmp eq i32 %3, 0
  br i1 %5, label %6, label %11

6:                                                ; preds = %.lr.ph
  %.not23 = icmp eq i32 %.018, %.026
  br i1 %.not23, label %.loopexit, label %7

7:                                                ; preds = %6
  %8 = add i32 %.026, -1
  %9 = mul i32 %8, %.021
  %10 = udiv i32 %9, %.026
  br label %.loopexit

11:                                               ; preds = %.lr.ph
  %12 = add i32 %.026, 1
  %.not = icmp ugt i32 %12, %.019
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !5

.loopexit:                                        ; preds = %11, %6, %7
  %.2 = phi i32 [ %10, %7 ], [ %.021, %6 ], [ %.021, %11 ]
  %.120 = phi i32 [ %4, %7 ], [ %4, %6 ], [ %.019, %11 ]
  %.1 = phi i32 [ %.026, %7 ], [ %.018, %6 ], [ %.018, %11 ]
  %13 = icmp ugt i32 %.120, 1
  br i1 %13, label %2, label %.loopexit.thread, !llvm.loop !7

.loopexit.thread:                                 ; preds = %2, %.loopexit
  %.231 = phi i32 [ %.2, %.loopexit ], [ %.021, %2 ]
  ret i32 %.231
}

attributes #0 = { nofree norecurse nosync nounwind 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" }

!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: DivRemPairsPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass