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

@.str = external hidden unnamed_addr constant [88 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [58 x i8], align 1

; 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

declare i32 @liquid_error_fl(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @liquid_unique_factor(i32 noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %12 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %12, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %62, %3
  store i32 2, ptr %8, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %52, %13
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = load i32, ptr %9, align 4, !tbaa !5
  %17 = icmp ule i32 %15, %16
  br i1 %17, label %18, label %55

18:                                               ; preds = %14
  %19 = load i32, ptr %9, align 4, !tbaa !5
  %20 = load i32, ptr %8, align 4, !tbaa !5
  %21 = urem i32 %19, %20
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %51

23:                                               ; preds = %18
  %24 = load i32, ptr %8, align 4, !tbaa !5
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = load i32, ptr %10, align 4, !tbaa !5
  %27 = zext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %25, i64 %27
  store i32 %24, ptr %28, align 4, !tbaa !5
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = load i32, ptr %9, align 4, !tbaa !5
  %31 = udiv i32 %30, %29
  store i32 %31, ptr %9, align 4, !tbaa !5
  %32 = load i32, ptr %10, align 4, !tbaa !5
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %23
  %35 = load i32, ptr %10, align 4, !tbaa !5
  %36 = add i32 %35, 1
  store i32 %36, ptr %10, align 4, !tbaa !5
  br label %50

37:                                               ; preds = %23
  %38 = load ptr, ptr %6, align 8, !tbaa !9
  %39 = load i32, ptr %10, align 4, !tbaa !5
  %40 = sub i32 %39, 1
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %38, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !5
  %45 = icmp ne i32 %43, %44
  br i1 %45, label %46, label %49

46:                                               ; preds = %37
  %47 = load i32, ptr %10, align 4, !tbaa !5
  %48 = add i32 %47, 1
  store i32 %48, ptr %10, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %46, %37
  br label %50

50:                                               ; preds = %49, %34
  br label %55

51:                                               ; preds = %18
  br label %52

52:                                               ; preds = %51
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = add i32 %53, 1
  store i32 %54, ptr %8, align 4, !tbaa !5
  br label %14, !llvm.loop !11

55:                                               ; preds = %50, %14
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %9, align 4, !tbaa !5
  %58 = icmp ugt i32 %57, 1
  br i1 %58, label %59, label %62

59:                                               ; preds = %56
  %60 = load i32, ptr %10, align 4, !tbaa !5
  %61 = icmp ult i32 %60, 40
  br label %62

62:                                               ; preds = %59, %56
  %63 = phi i1 [ false, %56 ], [ %61, %59 ]
  br i1 %63, label %13, label %64, !llvm.loop !13

64:                                               ; preds = %62
  %65 = load i32, ptr %9, align 4, !tbaa !5
  %66 = icmp ugt i32 %65, 1
  br i1 %66, label %67, label %73

67:                                               ; preds = %64
  %68 = load i32, ptr %10, align 4, !tbaa !5
  %69 = icmp eq i32 %68, 40
  br i1 %69, label %70, label %73

70:                                               ; preds = %67
  %71 = load i32, ptr %5, align 4, !tbaa !5
  %72 = call i32 (i32, ptr, i32, ptr, ...) @liquid_error_fl(i32 noundef 3, ptr noundef @.str, i32 noundef 109, ptr noundef @.str.2, i32 noundef %71, i32 noundef 40)
  store i32 %72, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %76

73:                                               ; preds = %67, %64
  %74 = load i32, ptr %10, align 4, !tbaa !5
  %75 = load ptr, ptr %7, align 8, !tbaa !9
  store i32 %74, ptr %75, align 4, !tbaa !5
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %76

76:                                               ; preds = %73, %70
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %77 = load i32, ptr %4, align 4
  ret i32 %77
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 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 #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 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@.str = external hidden unnamed_addr constant [88 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [58 x i8], align 1

declare i32 @liquid_error_fl(i32 noundef, ptr noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @liquid_unique_factor(i32 noundef %0, ptr nocapture noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #1 {
  br label %4

4:                                                ; preds = %.loopexit, %3
  %.025 = phi i32 [ %0, %3 ], [ %.126, %.loopexit ]
  %.0 = phi i32 [ 0, %3 ], [ %.1, %.loopexit ]
  %.not31 = icmp ult i32 %.025, 2
  br i1 %.not31, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %4, %18
  %.02732 = phi i32 [ %19, %18 ], [ 2, %4 ]
  %5 = urem i32 %.025, %.02732
  %6 = udiv i32 %.025, %.02732
  %7 = icmp eq i32 %5, 0
  br i1 %7, label %8, label %18

8:                                                ; preds = %.lr.ph
  %9 = zext i32 %.0 to i64
  %10 = getelementptr inbounds i32, ptr %1, i64 %9
  store i32 %.02732, ptr %10, align 4, !tbaa !5
  %11 = icmp eq i32 %.0, 0
  br i1 %11, label %.loopexit, label %12

12:                                               ; preds = %8
  %13 = add i32 %.0, -1
  %14 = zext i32 %13 to i64
  %15 = getelementptr inbounds i32, ptr %1, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %.not29 = icmp ne i32 %16, %.02732
  %17 = zext i1 %.not29 to i32
  %spec.select = add i32 %.0, %17
  br label %.loopexit

18:                                               ; preds = %.lr.ph
  %19 = add i32 %.02732, 1
  %.not = icmp ugt i32 %19, %.025
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !9

.loopexit:                                        ; preds = %18, %12, %8
  %.126 = phi i32 [ %6, %8 ], [ %6, %12 ], [ %.025, %18 ]
  %.1 = phi i32 [ 1, %8 ], [ %spec.select, %12 ], [ %.0, %18 ]
  %20 = icmp ugt i32 %.126, 1
  %21 = icmp ult i32 %.1, 40
  %22 = select i1 %20, i1 %21, i1 false
  br i1 %22, label %4, label %23, !llvm.loop !11

23:                                               ; preds = %.loopexit
  %24 = icmp eq i32 %.1, 40
  %or.cond = select i1 %20, i1 %24, i1 false
  br i1 %or.cond, label %25, label %.thread

25:                                               ; preds = %23
  %26 = tail call i32 (i32, ptr, i32, ptr, ...) @liquid_error_fl(i32 noundef 3, ptr noundef nonnull @.str, i32 noundef 109, ptr noundef nonnull @.str.2, i32 noundef %0, i32 noundef 40) #2
  br label %27

.thread:                                          ; preds = %4, %23
  %.13639 = phi i32 [ %.1, %23 ], [ %.0, %4 ]
  store i32 %.13639, ptr %2, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %.thread, %25
  %.028 = phi i32 [ %26, %25 ], [ 0, %.thread ]
  ret i32 %.028
}

attributes #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 = { 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}


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