; LLVM IR from tfhe/spqlios-arithmetic :: spqlios/reim (reim_to_tnx_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"

%struct.reim_to_tnx_precomp = type { ptr, i64, double, i32, double, i64, i64, double }

; Function Attrs: nounwind uwtable
define dso_local void @reim_to_tnx_basic_ref(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca double, align 8
  %9 = alloca i64, align 8
  %10 = alloca double, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %11, i32 0, i32 1
  %13 = load i64, ptr %12, align 8, !tbaa !9
  %14 = shl i64 %13, 1
  store i64 %14, ptr %7, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %15, i32 0, i32 2
  %17 = load double, ptr %16, align 8, !tbaa !15
  store double %17, ptr %8, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !14
  br label %18

18:                                               ; preds = %37, %3
  %19 = load i64, ptr %9, align 8, !tbaa !14
  %20 = load i64, ptr %7, align 8, !tbaa !14
  %21 = icmp ult i64 %19, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %40

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load i64, ptr %9, align 8, !tbaa !14
  %26 = getelementptr inbounds double, ptr %24, i64 %25
  %27 = load double, ptr %26, align 8, !tbaa !16
  %28 = load double, ptr %8, align 8, !tbaa !16
  %29 = fdiv double %27, %28
  store double %29, ptr %10, align 8, !tbaa !16
  %30 = load double, ptr %10, align 8, !tbaa !16
  %31 = load double, ptr %10, align 8, !tbaa !16
  %32 = call double @llvm.rint.f64(double %31)
  %33 = fsub double %30, %32
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = load i64, ptr %9, align 8, !tbaa !14
  %36 = getelementptr inbounds double, ptr %34, i64 %35
  store double %33, ptr %36, align 8, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %37

37:                                               ; preds = %23
  %38 = load i64, ptr %9, align 8, !tbaa !14
  %39 = add i64 %38, 1
  store i64 %39, ptr %9, align 8, !tbaa !14
  br label %18, !llvm.loop !17

40:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.rint.f64(double) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !11, i64 8}
!10 = !{!"reim_to_tnx_precomp", !6, i64 0, !11, i64 8, !12, i64 16, !13, i64 24, !12, i64 32, !11, i64 40, !11, i64 48, !12, i64 56}
!11 = !{!"long", !7, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!11, !11, i64 0}
!15 = !{!10, !12, i64 16}
!16 = !{!12, !12, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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 @reim_to_tnx_basic_ref(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = shl i64 %5, 1
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load double, ptr %7, align 8, !tbaa !13
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %._crit_edge, label %vector.memcheck

vector.memcheck:                                  ; preds = %3
  %9 = ptrtoint ptr %1 to i64
  %10 = ptrtoint ptr %2 to i64
  %11 = sub i64 %9, %10
  %diff.check = icmp ult i64 %11, 16
  br i1 %diff.check, label %.lr.ph, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %broadcast.splatinsert = insertelement <2 x double> poison, double %8, i64 0
  %broadcast.splat = shufflevector <2 x double> %broadcast.splatinsert, <2 x double> 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 ]
  %12 = getelementptr inbounds double, ptr %2, i64 %index
  %wide.load = load <2 x double>, ptr %12, align 8, !tbaa !14
  %13 = fdiv <2 x double> %wide.load, %broadcast.splat
  %14 = tail call <2 x double> @llvm.rint.v2f64(<2 x double> %13)
  %15 = fsub <2 x double> %13, %14
  %16 = getelementptr inbounds double, ptr %1, i64 %index
  store <2 x double> %15, ptr %16, align 8, !tbaa !14
  %index.next = add nuw i64 %index, 2
  %17 = icmp eq i64 %index.next, %6
  br i1 %17, label %._crit_edge, label %vector.body, !llvm.loop !15

._crit_edge:                                      ; preds = %vector.body, %.lr.ph, %3
  ret void

.lr.ph:                                           ; preds = %vector.memcheck, %.lr.ph
  %.011 = phi i64 [ %31, %.lr.ph ], [ 0, %vector.memcheck ]
  %18 = getelementptr inbounds double, ptr %2, i64 %.011
  %19 = load double, ptr %18, align 8, !tbaa !14
  %20 = fdiv double %19, %8
  %21 = tail call double @llvm.rint.f64(double %20)
  %22 = fsub double %20, %21
  %23 = getelementptr inbounds double, ptr %1, i64 %.011
  store double %22, ptr %23, align 8, !tbaa !14
  %24 = or disjoint i64 %.011, 1
  %25 = getelementptr inbounds double, ptr %2, i64 %24
  %26 = load double, ptr %25, align 8, !tbaa !14
  %27 = fdiv double %26, %8
  %28 = tail call double @llvm.rint.f64(double %27)
  %29 = fsub double %27, %28
  %30 = getelementptr inbounds double, ptr %1, i64 %24
  store double %29, ptr %30, align 8, !tbaa !14
  %31 = add nuw i64 %.011, 2
  %exitcond.not.1 = icmp eq i64 %31, %6
  br i1 %exitcond.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !19
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.rint.f64(double) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x double> @llvm.rint.v2f64(<2 x double>) #2

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !10, i64 8}
!6 = !{!"reim_to_tnx_precomp", !7, i64 0, !10, i64 8, !11, i64 16, !12, i64 24, !11, i64 32, !10, i64 40, !10, i64 48, !11, i64 56}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"double", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !11, i64 16}
!14 = !{!11, !11, i64 0}
!15 = distinct !{!15, !16, !17, !18}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!"llvm.loop.isvectorized", i32 1}
!18 = !{!"llvm.loop.unroll.runtime.disable"}
!19 = distinct !{!19, !16, !17}


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