; LLVM IR from tfhe/spqlios-arithmetic :: test/testlib/mod_q120.ll (Z8modq_powlil)

; 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: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z9centermodll(i64 noundef, i64 noundef) #0

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z8modq_powlil(i64 noundef %0, i32 noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = load i64, ptr %6, align 8, !tbaa !5
  %12 = sub nsw i64 %11, 1
  %13 = srem i64 %10, %12
  %14 = load i64, ptr %6, align 8, !tbaa !5
  %15 = add nsw i64 %13, %14
  %16 = sub nsw i64 %15, 1
  %17 = load i64, ptr %6, align 8, !tbaa !5
  %18 = sub nsw i64 %17, 1
  %19 = srem i64 %16, %18
  %20 = trunc i64 %19 to i32
  store i32 %20, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 1, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %21 = load i64, ptr %4, align 8, !tbaa !5
  %22 = load i64, ptr %6, align 8, !tbaa !5
  %23 = call noundef i64 @_Z9centermodll(i64 noundef %21, i64 noundef %22)
  store i64 %23, ptr %8, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %37, %3
  %25 = load i32, ptr %5, align 4, !tbaa !9
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %45

27:                                               ; preds = %24
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = and i32 %28, 1
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %37

31:                                               ; preds = %27
  %32 = load i64, ptr %7, align 8, !tbaa !5
  %33 = load i64, ptr %8, align 8, !tbaa !5
  %34 = mul nsw i64 %32, %33
  %35 = load i64, ptr %6, align 8, !tbaa !5
  %36 = call noundef i64 @_Z9centermodll(i64 noundef %34, i64 noundef %35)
  store i64 %36, ptr %7, align 8, !tbaa !5
  br label %37

37:                                               ; preds = %31, %27
  %38 = load i64, ptr %8, align 8, !tbaa !5
  %39 = load i64, ptr %8, align 8, !tbaa !5
  %40 = mul nsw i64 %38, %39
  %41 = load i64, ptr %6, align 8, !tbaa !5
  %42 = call noundef i64 @_Z9centermodll(i64 noundef %40, i64 noundef %41)
  store i64 %42, ptr %8, align 8, !tbaa !5
  %43 = load i32, ptr %5, align 4, !tbaa !9
  %44 = ashr i32 %43, 1
  store i32 %44, ptr %5, align 4, !tbaa !9
  br label %24, !llvm.loop !11

45:                                               ; preds = %24
  %46 = load i64, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i64 %46
}

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

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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: mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z9centermodll(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z8modq_powlil(i64 noundef %0, i32 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = sext i32 %1 to i64
  %5 = add i64 %2, -1
  %6 = srem i64 %4, %5
  %7 = add i64 %5, %6
  %8 = srem i64 %7, %5
  %9 = trunc i64 %8 to i32
  %10 = tail call noundef i64 @_Z9centermodll(i64 noundef %0, i64 noundef %2)
  %.not19 = icmp eq i32 %9, 0
  br i1 %.not19, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %15
  %.022 = phi i64 [ %17, %15 ], [ %10, %3 ]
  %.01621 = phi i64 [ %.1, %15 ], [ 1, %3 ]
  %.01720 = phi i32 [ %18, %15 ], [ %9, %3 ]
  %11 = and i32 %.01720, 1
  %.not18 = icmp eq i32 %11, 0
  br i1 %.not18, label %15, label %12

12:                                               ; preds = %.lr.ph
  %13 = mul nsw i64 %.022, %.01621
  %14 = tail call noundef i64 @_Z9centermodll(i64 noundef %13, i64 noundef %2)
  br label %15

15:                                               ; preds = %12, %.lr.ph
  %.1 = phi i64 [ %14, %12 ], [ %.01621, %.lr.ph ]
  %16 = mul nsw i64 %.022, %.022
  %17 = tail call noundef i64 @_Z9centermodll(i64 noundef %16, i64 noundef %2)
  %18 = ashr i32 %.01720, 1
  %.not = icmp ult i32 %.01720, 2
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %15, %3
  %.016.lcssa = phi i64 [ 1, %3 ], [ %.1, %15 ]
  ret i64 %.016.lcssa
}

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

!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,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass