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

; 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 @dbl_round_to_i32_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 = %39, %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 %42

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 double, ptr %29, i64 %30
  %32 = load double, ptr %31, align 8, !tbaa !11
  %33 = fadd double %32, 0x4338000080000000
  store double %33, ptr %13, align 8, !tbaa !13
  %34 = load i32, ptr %13, align 8, !tbaa !13
  %35 = xor i32 %34, -2147483648
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load i64, ptr %12, align 8, !tbaa !9
  %38 = getelementptr inbounds i32, ptr %36, i64 %37
  store i32 %35, ptr %38, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %39

39:                                               ; preds = %28
  %40 = load i64, ptr %12, align 8, !tbaa !9
  %41 = add i64 %40, 1
  store i64 %41, ptr %12, align 8, !tbaa !9
  br label %23, !llvm.loop !16

42:                                               ; preds = %27
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = load i64, ptr %11, align 8, !tbaa !9
  %45 = getelementptr inbounds i32, ptr %43, i64 %44
  %46 = load i64, ptr %8, align 8, !tbaa !9
  %47 = load i64, ptr %11, align 8, !tbaa !9
  %48 = sub i64 %46, %47
  %49 = mul i64 %48, 4
  call void @llvm.memset.p0.i64(ptr align 4 %45, i8 0, i64 %49, 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 = !{!"double", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !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"

; 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 @dbl_round_to_i32_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.preheader

.lr.ph.preheader:                                 ; preds = %5
  %min.iters.check = icmp ult i64 %6, 4
  br i1 %min.iters.check, label %.lr.ph.preheader17, label %vector.ph

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

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %6, -4
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %7 = getelementptr inbounds double, ptr %3, i64 %index
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  %wide.load = load <2 x double>, ptr %7, align 8, !tbaa !5
  %wide.load16 = load <2 x double>, ptr %8, align 8, !tbaa !5
  %9 = fadd <2 x double> %wide.load, <double 0x4338000080000000, double 0x4338000080000000>
  %10 = fadd <2 x double> %wide.load16, <double 0x4338000080000000, double 0x4338000080000000>
  %11 = bitcast <2 x double> %9 to <2 x i64>
  %12 = bitcast <2 x double> %10 to <2 x i64>
  %13 = trunc <2 x i64> %11 to <2 x i32>
  %14 = trunc <2 x i64> %12 to <2 x i32>
  %15 = xor <2 x i32> %13, <i32 -2147483648, i32 -2147483648>
  %16 = xor <2 x i32> %14, <i32 -2147483648, i32 -2147483648>
  %17 = getelementptr inbounds i32, ptr %1, i64 %index
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  store <2 x i32> %15, ptr %17, align 4, !tbaa !9
  store <2 x i32> %16, ptr %18, align 4, !tbaa !9
  %index.next = add nuw i64 %index, 4
  %19 = icmp eq i64 %index.next, %n.vec
  br i1 %19, label %middle.block, label %vector.body, !llvm.loop !11

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

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %5
  %20 = getelementptr inbounds i32, ptr %1, i64 %6
  %21 = sub i64 %2, %6
  %22 = shl i64 %21, 2
  tail call void @llvm.memset.p0.i64(ptr align 4 %20, i8 0, i64 %22, i1 false)
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader17, %.lr.ph
  %.015 = phi i64 [ %29, %.lr.ph ], [ %.015.ph, %.lr.ph.preheader17 ]
  %23 = getelementptr inbounds double, ptr %3, i64 %.015
  %24 = load double, ptr %23, align 8, !tbaa !5
  %25 = fadd double %24, 0x4338000080000000
  %26 = bitcast double %25 to i64
  %.sroa.0.0.extract.trunc = trunc i64 %26 to i32
  %27 = xor i32 %.sroa.0.0.extract.trunc, -2147483648
  %28 = getelementptr inbounds i32, ptr %1, i64 %.015
  store i32 %27, ptr %28, align 4, !tbaa !9
  %29 = add nuw i64 %.015, 1
  %exitcond.not = icmp eq i64 %29, %6
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15
}

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


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