; LLVM IR from tfhe/spqlios-arithmetic :: spqlios/coeffs (coeffs_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 void @rnx_mul_xp_minus_one_inplace(i64 noundef %0, i64 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca double, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  %15 = alloca double, align 8
  store i64 %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = mul i64 2, %16
  %18 = sub i64 %17, 1
  store i64 %18, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %19 = load i64, ptr %4, align 8, !tbaa !5
  %20 = sub i64 %19, 1
  store i64 %20, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store i64 0, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  store i64 0, ptr %10, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %70, %3
  %22 = load i64, ptr %9, align 8, !tbaa !5
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %25, label %73

25:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %26 = load i64, ptr %10, align 8, !tbaa !5
  store i64 %26, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %27 = load ptr, ptr %6, align 8, !tbaa !9
  %28 = load i64, ptr %11, align 8, !tbaa !5
  %29 = getelementptr inbounds double, ptr %27, i64 %28
  %30 = load double, ptr %29, align 8, !tbaa !11
  store double %30, ptr %12, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %66, %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %32 = load i64, ptr %11, align 8, !tbaa !5
  %33 = load i64, ptr %5, align 8, !tbaa !5
  %34 = add i64 %32, %33
  %35 = load i64, ptr %7, align 8, !tbaa !5
  %36 = and i64 %34, %35
  store i64 %36, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #2
  %37 = load i64, ptr %13, align 8, !tbaa !5
  %38 = load i64, ptr %8, align 8, !tbaa !5
  %39 = and i64 %37, %38
  store i64 %39, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #2
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  %41 = load i64, ptr %14, align 8, !tbaa !5
  %42 = getelementptr inbounds double, ptr %40, i64 %41
  %43 = load double, ptr %42, align 8, !tbaa !11
  store double %43, ptr %15, align 8, !tbaa !11
  %44 = load i64, ptr %13, align 8, !tbaa !5
  %45 = load i64, ptr %4, align 8, !tbaa !5
  %46 = icmp ult i64 %44, %45
  br i1 %46, label %47, label %49

47:                                               ; preds = %31
  %48 = load double, ptr %12, align 8, !tbaa !11
  br label %52

49:                                               ; preds = %31
  %50 = load double, ptr %12, align 8, !tbaa !11
  %51 = fneg double %50
  br label %52

52:                                               ; preds = %49, %47
  %53 = phi double [ %48, %47 ], [ %51, %49 ]
  %54 = load ptr, ptr %6, align 8, !tbaa !9
  %55 = load i64, ptr %14, align 8, !tbaa !5
  %56 = getelementptr inbounds double, ptr %54, i64 %55
  %57 = load double, ptr %56, align 8, !tbaa !11
  %58 = fsub double %53, %57
  %59 = load ptr, ptr %6, align 8, !tbaa !9
  %60 = load i64, ptr %14, align 8, !tbaa !5
  %61 = getelementptr inbounds double, ptr %59, i64 %60
  store double %58, ptr %61, align 8, !tbaa !11
  %62 = load double, ptr %15, align 8, !tbaa !11
  store double %62, ptr %12, align 8, !tbaa !11
  %63 = load i64, ptr %9, align 8, !tbaa !5
  %64 = add i64 %63, 1
  store i64 %64, ptr %9, align 8, !tbaa !5
  %65 = load i64, ptr %14, align 8, !tbaa !5
  store i64 %65, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %66

66:                                               ; preds = %52
  %67 = load i64, ptr %11, align 8, !tbaa !5
  %68 = load i64, ptr %10, align 8, !tbaa !5
  %69 = icmp ne i64 %67, %68
  br i1 %69, label %31, label %70, !llvm.loop !13

70:                                               ; preds = %66
  %71 = load i64, ptr %10, align 8, !tbaa !5
  %72 = add i64 %71, 1
  store i64 %72, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %21, !llvm.loop !15

73:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  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 void
}

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 = !{!"long", !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 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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(argmem: readwrite) uwtable
define dso_local void @rnx_mul_xp_minus_one_inplace(i64 noundef %0, i64 noundef %1, ptr nocapture noundef %2) local_unnamed_addr #0 {
  %4 = shl i64 %0, 1
  %5 = add i64 %4, -1
  %6 = add i64 %0, -1
  %.not33 = icmp eq i64 %0, 0
  br i1 %.not33, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %20
  %.032 = phi i64 [ %19, %20 ], [ 0, %3 ]
  %.02731 = phi i64 [ %21, %20 ], [ 0, %3 ]
  %7 = getelementptr inbounds double, ptr %2, i64 %.02731
  %8 = load double, ptr %7, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %9, %.lr.ph
  %.029 = phi double [ %8, %.lr.ph ], [ %14, %9 ]
  %.028 = phi i64 [ %.02731, %.lr.ph ], [ %12, %9 ]
  %.1 = phi i64 [ %.032, %.lr.ph ], [ %19, %9 ]
  %10 = add i64 %.028, %1
  %11 = and i64 %10, %5
  %12 = and i64 %11, %6
  %13 = getelementptr inbounds double, ptr %2, i64 %12
  %14 = load double, ptr %13, align 8, !tbaa !5
  %15 = icmp ult i64 %11, %0
  %16 = fneg double %.029
  %17 = select i1 %15, double %.029, double %16
  %18 = fsub double %17, %14
  store double %18, ptr %13, align 8, !tbaa !5
  %19 = add i64 %.1, 1
  %.not = icmp eq i64 %12, %.02731
  br i1 %.not, label %20, label %9, !llvm.loop !9

20:                                               ; preds = %9
  %21 = add i64 %.02731, 1
  %22 = icmp ult i64 %19, %0
  br i1 %22, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %20, %3
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = !{!6, !6, i64 0}
!6 = !{!"double", !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: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass