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

@i32_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 @i32_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 i32, align 4
  %14 = 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
  %15 = load i64, ptr %8, align 8, !tbaa !9
  %16 = load i64, ptr %10, align 8, !tbaa !9
  %17 = icmp ult i64 %15, %16
  br i1 %17, label %18, label %20

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

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

22:                                               ; preds = %20, %18
  %23 = phi i64 [ %19, %18 ], [ %21, %20 ]
  store i64 %23, 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 %24

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

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

29:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = load i64, ptr %12, align 8, !tbaa !9
  %32 = getelementptr inbounds i32, ptr %30, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !11
  %34 = xor i32 %33, -2147483648
  store i32 %34, ptr %13, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %35 = load i64, ptr @i32_to_dbl_ref.OR_CST, align 8, !tbaa !13
  %36 = load i32, ptr %13, align 4, !tbaa !11
  %37 = zext i32 %36 to i64
  %38 = or i64 %35, %37
  store i64 %38, ptr %14, align 8, !tbaa !13
  %39 = load double, ptr %14, align 8, !tbaa !13
  %40 = fsub double %39, 0x4330000080000000
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = load i64, ptr %12, align 8, !tbaa !9
  %43 = getelementptr inbounds double, ptr %41, i64 %42
  store double %40, ptr %43, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %44

44:                                               ; preds = %29
  %45 = load i64, ptr %12, align 8, !tbaa !9
  %46 = add i64 %45, 1
  store i64 %46, ptr %12, align 8, !tbaa !9
  br label %24, !llvm.loop !16

47:                                               ; preds = %28
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = load i64, ptr %11, align 8, !tbaa !9
  %50 = getelementptr inbounds double, ptr %48, i64 %49
  %51 = load i64, ptr %8, align 8, !tbaa !9
  %52 = load i64, ptr %11, align 8, !tbaa !9
  %53 = sub i64 %51, %52
  %54 = mul i64 %53, 8
  call void @llvm.memset.p0.i64(ptr align 8 %50, i8 0, i64 %54, 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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"double", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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 }

@i32_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 @i32_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 @i32_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
  %.016.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 i32, ptr %3, i64 %index
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  %wide.load = load <2 x i32>, ptr %8, align 4, !tbaa !8
  %wide.load17 = load <2 x i32>, ptr %9, align 4, !tbaa !8
  %10 = xor <2 x i32> %wide.load, <i32 -2147483648, i32 -2147483648>
  %11 = xor <2 x i32> %wide.load17, <i32 -2147483648, i32 -2147483648>
  %12 = zext <2 x i32> %10 to <2 x i64>
  %13 = zext <2 x i32> %11 to <2 x i64>
  %14 = or <2 x i64> %broadcast.splat, %12
  %15 = or <2 x i64> %broadcast.splat, %13
  %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 0xC330000080000000, double 0xC330000080000000>
  %19 = fadd <2 x double> %17, <double 0xC330000080000000, double 0xC330000080000000>
  %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
  %.016 = phi i64 [ %34, %scalar.ph ], [ %.016.ph, %scalar.ph.preheader ]
  %26 = getelementptr inbounds i32, ptr %3, i64 %.016
  %27 = load i32, ptr %26, align 4, !tbaa !8
  %28 = xor i32 %27, -2147483648
  %29 = zext i32 %28 to i64
  %30 = or i64 %7, %29
  %31 = bitcast i64 %30 to double
  %32 = fadd double %31, 0xC330000080000000
  %33 = getelementptr inbounds double, ptr %1, i64 %.016
  store double %32, ptr %33, align 8, !tbaa !10
  %34 = add nuw i64 %.016, 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 = !{!"int", !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,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass