; 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_automorphism_f64(i64 noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  store i64 %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !9
  %12 = load ptr, ptr %8, align 8, !tbaa !9
  %13 = getelementptr inbounds double, ptr %12, i64 0
  %14 = load double, ptr %13, align 8, !tbaa !11
  %15 = load ptr, ptr %7, align 8, !tbaa !9
  %16 = getelementptr inbounds double, ptr %15, i64 0
  store double %14, ptr %16, align 8, !tbaa !11
  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
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %18 = mul i64 2, %17
  %19 = sub i64 %18, 1
  store i64 %19, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  store i64 1, ptr %11, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %54, %4
  %21 = load i64, ptr %11, align 8, !tbaa !5
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %57

25:                                               ; preds = %20
  %26 = load i64, ptr %9, align 8, !tbaa !5
  %27 = load i64, ptr %6, align 8, !tbaa !5
  %28 = add i64 %26, %27
  %29 = load i64, ptr %10, align 8, !tbaa !5
  %30 = and i64 %28, %29
  store i64 %30, ptr %9, align 8, !tbaa !5
  %31 = load i64, ptr %9, align 8, !tbaa !5
  %32 = load i64, ptr %5, align 8, !tbaa !5
  %33 = icmp ult i64 %31, %32
  br i1 %33, label %34, label %42

34:                                               ; preds = %25
  %35 = load ptr, ptr %8, align 8, !tbaa !9
  %36 = load i64, ptr %11, align 8, !tbaa !5
  %37 = getelementptr inbounds double, ptr %35, i64 %36
  %38 = load double, ptr %37, align 8, !tbaa !11
  %39 = load ptr, ptr %7, align 8, !tbaa !9
  %40 = load i64, ptr %9, align 8, !tbaa !5
  %41 = getelementptr inbounds double, ptr %39, i64 %40
  store double %38, ptr %41, align 8, !tbaa !11
  br label %53

42:                                               ; preds = %25
  %43 = load ptr, ptr %8, align 8, !tbaa !9
  %44 = load i64, ptr %11, align 8, !tbaa !5
  %45 = getelementptr inbounds double, ptr %43, i64 %44
  %46 = load double, ptr %45, align 8, !tbaa !11
  %47 = fneg double %46
  %48 = load ptr, ptr %7, align 8, !tbaa !9
  %49 = load i64, ptr %9, align 8, !tbaa !5
  %50 = load i64, ptr %5, align 8, !tbaa !5
  %51 = sub i64 %49, %50
  %52 = getelementptr inbounds double, ptr %48, i64 %51
  store double %47, ptr %52, align 8, !tbaa !11
  br label %53

53:                                               ; preds = %42, %34
  br label %54

54:                                               ; preds = %53
  %55 = load i64, ptr %11, align 8, !tbaa !5
  %56 = add i64 %55, 1
  store i64 %56, ptr %11, align 8, !tbaa !5
  br label %20, !llvm.loop !13

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


; 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_automorphism_f64(i64 noundef %0, i64 noundef %1, ptr nocapture noundef writeonly %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = load double, ptr %3, align 8, !tbaa !5
  store double %5, ptr %2, align 8, !tbaa !5
  %6 = shl i64 %0, 1
  %7 = add i64 %6, -1
  %8 = icmp ugt i64 %0, 1
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %4
  %9 = add i64 %0, -1
  %xtraiter = and i64 %9, 1
  %10 = icmp eq i64 %0, 2
  br i1 %10, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %9, -2
  %invariant.gep = getelementptr i8, ptr %3, i64 8
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.023.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %34, %.lr.ph ]
  %.01922.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %28, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %11 = add i64 %.01922.unr, %1
  %12 = and i64 %11, %7
  %13 = icmp ult i64 %12, %0
  %14 = getelementptr inbounds double, ptr %3, i64 %.023.unr
  %15 = load double, ptr %14, align 8, !tbaa !5
  %16 = fneg double %15
  %17 = select i1 %13, i64 0, i64 %0
  %.sink25.epil = sub i64 %12, %17
  %.sink.epil = select i1 %13, double %15, double %16
  %18 = getelementptr inbounds double, ptr %2, i64 %.sink25.epil
  store double %.sink.epil, ptr %18, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %4
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.023 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %34, %.lr.ph ]
  %.01922 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %28, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %19 = add i64 %.01922, %1
  %20 = and i64 %19, %7
  %21 = icmp ult i64 %20, %0
  %22 = getelementptr inbounds double, ptr %3, i64 %.023
  %23 = load double, ptr %22, align 8, !tbaa !5
  %24 = fneg double %23
  %25 = select i1 %21, i64 0, i64 %0
  %.sink25 = sub i64 %20, %25
  %.sink = select i1 %21, double %23, double %24
  %26 = getelementptr inbounds double, ptr %2, i64 %.sink25
  store double %.sink, ptr %26, align 8, !tbaa !5
  %27 = add i64 %20, %1
  %28 = and i64 %27, %7
  %29 = icmp ult i64 %28, %0
  %gep = getelementptr double, ptr %invariant.gep, i64 %.023
  %30 = load double, ptr %gep, align 8, !tbaa !5
  %31 = fneg double %30
  %32 = select i1 %29, i64 0, i64 %0
  %.sink25.1 = sub i64 %28, %32
  %.sink.1 = select i1 %29, double %30, double %31
  %33 = getelementptr inbounds double, ptr %2, i64 %.sink25.1
  store double %.sink.1, ptr %33, align 8, !tbaa !5
  %34 = add nuw i64 %.023, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9
}

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass