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

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

%union.di_t = type { double }

@i64_to_dbl_ref.OR_CST = external hidden constant %union.di_t, align 8

; 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local void @i64_to_dbl_ref(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) #2 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca %union.di_t, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  store i64 %4, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %14 = load i64, ptr %8, align 8, !tbaa !9
  %15 = load i64, ptr %10, align 8, !tbaa !9
  %16 = icmp ult i64 %14, %15
  br i1 %16, label %17, label %19

17:                                               ; preds = %5
  %18 = load i64, ptr %8, align 8, !tbaa !9
  br label %21

19:                                               ; preds = %5
  %20 = load i64, ptr %10, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %19, %17
  %22 = phi i64 [ %18, %17 ], [ %20, %19 ]
  store i64 %22, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %42, %21
  %24 = load i64, ptr %12, align 8, !tbaa !9
  %25 = load i64, ptr %11, align 8, !tbaa !9
  %26 = icmp ult i64 %24, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %45

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = load i64, ptr %12, align 8, !tbaa !9
  %31 = getelementptr inbounds i64, ptr %29, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = add i64 %32, 2251799813685248
  %34 = and i64 %33, 4503599627370495
  %35 = load i64, ptr @i64_to_dbl_ref.OR_CST, align 8, !tbaa !11
  %36 = or i64 %34, %35
  store i64 %36, ptr %13, align 8, !tbaa !11
  %37 = load double, ptr %13, align 8, !tbaa !11
  %38 = fsub double %37, 0x4338000000000000
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = load i64, ptr %12, align 8, !tbaa !9
  %41 = getelementptr inbounds double, ptr %39, i64 %40
  store double %38, ptr %41, align 8, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %42

42:                                               ; preds = %28
  %43 = load i64, ptr %12, align 8, !tbaa !9
  %44 = add i64 %43, 1
  store i64 %44, ptr %12, align 8, !tbaa !9
  br label %23, !llvm.loop !14

45:                                               ; preds = %27
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load i64, ptr %11, align 8, !tbaa !9
  %48 = getelementptr inbounds double, ptr %46, i64 %47
  %49 = load i64, ptr %8, align 8, !tbaa !9
  %50 = load i64, ptr %11, align 8, !tbaa !9
  %51 = sub i64 %49, %50
  %52 = mul i64 %51, 8
  call void @llvm.memset.p0.i64(ptr align 8 %48, i8 0, i64 %52, i1 false)
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"any pointer", !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 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"double", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

%union.di_t = type { double }

@i64_to_dbl_ref.OR_CST = external hidden local_unnamed_addr constant %union.di_t, align 8

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @i64_to_dbl_ref(ptr nocapture noundef readnone %0, ptr nocapture noundef writeonly %1, i64 noundef %2, ptr nocapture noundef readonly %3, i64 noundef %4) local_unnamed_addr #1 {
  %6 = tail call i64 @llvm.umin.i64(i64 %2, i64 %4)
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %7 = load i64, ptr @i64_to_dbl_ref.OR_CST, align 8, !tbaa !5
  %min.iters.check = icmp ult i64 %6, 4
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %.015.ph = phi i64 [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.vec = and i64 %6, -4
  %broadcast.splatinsert = insertelement <2 x i64> poison, i64 %7, i64 0
  %broadcast.splat = shufflevector <2 x i64> %broadcast.splatinsert, <2 x i64> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %8 = getelementptr inbounds i64, ptr %3, i64 %index
  %9 = getelementptr inbounds i8, ptr %8, i64 16
  %wide.load = load <2 x i64>, ptr %8, align 8, !tbaa !8
  %wide.load16 = load <2 x i64>, ptr %9, align 8, !tbaa !8
  %10 = and <2 x i64> %wide.load, <i64 4503599627370495, i64 4503599627370495>
  %11 = and <2 x i64> %wide.load16, <i64 4503599627370495, i64 4503599627370495>
  %12 = xor <2 x i64> %10, <i64 2251799813685248, i64 2251799813685248>
  %13 = xor <2 x i64> %11, <i64 2251799813685248, i64 2251799813685248>
  %14 = or <2 x i64> %12, %broadcast.splat
  %15 = or <2 x i64> %13, %broadcast.splat
  %16 = bitcast <2 x i64> %14 to <2 x double>
  %17 = bitcast <2 x i64> %15 to <2 x double>
  %18 = fadd <2 x double> %16, <double 0xC338000000000000, double 0xC338000000000000>
  %19 = fadd <2 x double> %17, <double 0xC338000000000000, double 0xC338000000000000>
  %20 = getelementptr inbounds double, ptr %1, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  store <2 x double> %18, ptr %20, align 8, !tbaa !10
  store <2 x double> %19, ptr %21, align 8, !tbaa !10
  %index.next = add nuw i64 %index, 4
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, label %middle.block, label %vector.body, !llvm.loop !12

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %6, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %5
  %23 = getelementptr inbounds double, ptr %1, i64 %6
  %24 = sub i64 %2, %6
  %25 = shl i64 %24, 3
  tail call void @llvm.memset.p0.i64(ptr align 8 %23, i8 0, i64 %25, i1 false)
  ret void

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %.015 = phi i64 [ %34, %scalar.ph ], [ %.015.ph, %scalar.ph.preheader ]
  %26 = getelementptr inbounds i64, ptr %3, i64 %.015
  %27 = load i64, ptr %26, align 8, !tbaa !8
  %28 = and i64 %27, 4503599627370495
  %29 = xor i64 %28, 2251799813685248
  %30 = or i64 %29, %7
  %31 = bitcast i64 %30 to double
  %32 = fadd double %31, 0xC338000000000000
  %33 = getelementptr inbounds double, ptr %1, i64 %.015
  store double %32, ptr %33, align 8, !tbaa !10
  %34 = add nuw i64 %.015, 1
  %exitcond.not = icmp eq i64 %34, %6
  br i1 %exitcond.not, label %._crit_edge, label %scalar.ph, !llvm.loop !16
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #2

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { 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" }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"long", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"double", !6, i64 0}
!12 = distinct !{!12, !13, !14, !15}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!"llvm.loop.isvectorized", i32 1}
!15 = !{!"llvm.loop.unroll.runtime.disable"}
!16 = distinct !{!16, !13, !15, !14}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass