; LLVM IR from tfhe/spqlios-arithmetic :: spqlios/q120 (q120_ntt)

; 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 @modq_pow(i32 noundef %0, i64 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %10 = load i64, ptr %5, align 8, !tbaa !9
  %11 = load i32, ptr %6, align 4, !tbaa !5
  %12 = sub i32 %11, 1
  %13 = zext i32 %12 to i64
  %14 = srem i64 %10, %13
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = add nsw i64 %14, %16
  %18 = sub nsw i64 %17, 1
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = sub i32 %19, 1
  %21 = zext i32 %20 to i64
  %22 = srem i64 %18, %21
  store i64 %22, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = zext i32 %23 to i64
  store i64 %24, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 1, ptr %9, align 8, !tbaa !9
  br label %25

25:                                               ; preds = %39, %3
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = icmp ne i64 %26, 0
  br i1 %27, label %28, label %48

28:                                               ; preds = %25
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = and i64 %29, 1
  %31 = icmp ne i64 %30, 0
  br i1 %31, label %32, label %39

32:                                               ; preds = %28
  %33 = load i64, ptr %9, align 8, !tbaa !9
  %34 = load i64, ptr %8, align 8, !tbaa !9
  %35 = mul i64 %33, %34
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = zext i32 %36 to i64
  %38 = urem i64 %35, %37
  store i64 %38, ptr %9, align 8, !tbaa !9
  br label %39

39:                                               ; preds = %32, %28
  %40 = load i64, ptr %8, align 8, !tbaa !9
  %41 = load i64, ptr %8, align 8, !tbaa !9
  %42 = mul i64 %40, %41
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = zext i32 %43 to i64
  %45 = urem i64 %42, %44
  store i64 %45, ptr %8, align 8, !tbaa !9
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = lshr i64 %46, 1
  store i64 %47, ptr %7, align 8, !tbaa !9
  br label %25, !llvm.loop !11

48:                                               ; preds = %25
  %49 = load i64, ptr %9, align 8, !tbaa !9
  %50 = trunc i64 %49 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i32 %50
}

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 = !{!10, !10, i64 0}
!10 = !{!"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 norecurse nosync nounwind memory(none) uwtable
define dso_local range(i32 0, -1) i32 @modq_pow(i32 noundef %0, i64 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = add i32 %2, -1
  %5 = zext i32 %4 to i64
  %6 = srem i64 %1, %5
  %7 = zext i32 %2 to i64
  %8 = add nsw i64 %7, -1
  %9 = add nsw i64 %8, %6
  %10 = srem i64 %9, %5
  %.not18 = icmp eq i64 %10, 0
  br i1 %.not18, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %11 = zext i32 %0 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %16
  %.021 = phi i64 [ %.1, %16 ], [ 1, %.lr.ph.preheader ]
  %.01520 = phi i64 [ %18, %16 ], [ %11, %.lr.ph.preheader ]
  %.01619 = phi i64 [ %19, %16 ], [ %10, %.lr.ph.preheader ]
  %12 = and i64 %.01619, 1
  %.not17 = icmp eq i64 %12, 0
  br i1 %.not17, label %16, label %13

13:                                               ; preds = %.lr.ph
  %14 = mul nuw i64 %.021, %.01520
  %15 = urem i64 %14, %7
  br label %16

16:                                               ; preds = %13, %.lr.ph
  %.1 = phi i64 [ %15, %13 ], [ %.021, %.lr.ph ]
  %17 = mul nuw i64 %.01520, %.01520
  %18 = urem i64 %17, %7
  %19 = lshr i64 %.01619, 1
  %.not = icmp ult i64 %.01619, 2
  br i1 %.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !5

._crit_edge.loopexit:                             ; preds = %16
  %20 = trunc nuw i64 %.1 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %3
  %.0.lcssa = phi i32 [ 1, %3 ], [ %20, %._crit_edge.loopexit ]
  ret i32 %.0.lcssa
}

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


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