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

%union.dblui64_t = type { double }
%struct.reim_to_tnx_precomp = type { ptr, i64, double, i32, double, i64, i64, 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: nounwind uwtable
define dso_local void @reim_to_tnx_ref(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %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 i64, align 8
  %11 = alloca double, align 8
  %12 = alloca %union.dblui64_t, align 8
  %13 = alloca i64, 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) #2
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %14, i32 0, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = shl i64 %16, 1
  store i64 %17, ptr %7, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %18, i32 0, i32 4
  %20 = load double, ptr %19, align 8, !tbaa !15
  store double %20, ptr %8, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %21, i32 0, i32 5
  %23 = load i64, ptr %22, align 8, !tbaa !17
  store i64 %23, ptr %9, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %24, i32 0, i32 6
  %26 = load i64, ptr %25, align 8, !tbaa !18
  store i64 %26, ptr %10, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %27, i32 0, i32 7
  %29 = load double, ptr %28, align 8, !tbaa !19
  store double %29, ptr %11, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  store i64 0, ptr %13, align 8, !tbaa !14
  br label %30

30:                                               ; preds = %54, %3
  %31 = load i64, ptr %13, align 8, !tbaa !14
  %32 = load i64, ptr %7, align 8, !tbaa !14
  %33 = icmp ult i64 %31, %32
  br i1 %33, label %35, label %34

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

35:                                               ; preds = %30
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = load i64, ptr %13, align 8, !tbaa !14
  %38 = getelementptr inbounds double, ptr %36, i64 %37
  %39 = load double, ptr %38, align 8, !tbaa !16
  %40 = load double, ptr %8, align 8, !tbaa !16
  %41 = fadd double %39, %40
  store double %41, ptr %12, align 8, !tbaa !20
  %42 = load i64, ptr %9, align 8, !tbaa !14
  %43 = load i64, ptr %12, align 8, !tbaa !20
  %44 = and i64 %43, %42
  store i64 %44, ptr %12, align 8, !tbaa !20
  %45 = load i64, ptr %10, align 8, !tbaa !14
  %46 = load i64, ptr %12, align 8, !tbaa !20
  %47 = or i64 %46, %45
  store i64 %47, ptr %12, align 8, !tbaa !20
  %48 = load double, ptr %12, align 8, !tbaa !20
  %49 = load double, ptr %11, align 8, !tbaa !16
  %50 = fsub double %48, %49
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load i64, ptr %13, align 8, !tbaa !14
  %53 = getelementptr inbounds double, ptr %51, i64 %52
  store double %50, ptr %53, align 8, !tbaa !16
  br label %54

54:                                               ; preds = %35
  %55 = load i64, ptr %13, align 8, !tbaa !14
  %56 = add i64 %55, 1
  store i64 %56, ptr %13, align 8, !tbaa !14
  br label %30, !llvm.loop !21

57:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #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 = !{!"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 32}
!16 = !{!12, !12, i64 0}
!17 = !{!10, !11, i64 40}
!18 = !{!10, !11, i64 48}
!19 = !{!10, !12, i64 56}
!20 = !{!7, !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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_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 32
  %8 = load double, ptr %7, align 8, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %0, i64 40
  %10 = load i64, ptr %9, align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %0, i64 48
  %12 = load i64, ptr %11, align 8, !tbaa !15
  %13 = getelementptr inbounds i8, ptr %0, i64 56
  %14 = load double, ptr %13, align 8, !tbaa !16
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %15 = ptrtoint ptr %1 to i64
  %16 = ptrtoint ptr %2 to i64
  %min.iters.check = icmp ult i64 %6, 4
  %17 = sub i64 %15, %16
  %diff.check = icmp ult i64 %17, 32
  %or.cond = or i1 %min.iters.check, %diff.check
  br i1 %or.cond, label %.lr.ph.preheader24, label %vector.ph

.lr.ph.preheader24:                               ; preds = %middle.block, %.lr.ph.preheader
  %.016.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
  %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
  %broadcast.splatinsert18 = insertelement <2 x i64> poison, i64 %10, i64 0
  %broadcast.splat19 = shufflevector <2 x i64> %broadcast.splatinsert18, <2 x i64> poison, <2 x i32> zeroinitializer
  %broadcast.splatinsert20 = insertelement <2 x i64> poison, i64 %12, i64 0
  %broadcast.splat21 = shufflevector <2 x i64> %broadcast.splatinsert20, <2 x i64> poison, <2 x i32> zeroinitializer
  %broadcast.splatinsert22 = insertelement <2 x double> poison, double %14, i64 0
  %broadcast.splat23 = shufflevector <2 x double> %broadcast.splatinsert22, <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 ]
  %18 = getelementptr inbounds double, ptr %2, i64 %index
  %19 = getelementptr inbounds i8, ptr %18, i64 16
  %wide.load = load <2 x double>, ptr %18, align 8, !tbaa !17
  %wide.load17 = load <2 x double>, ptr %19, align 8, !tbaa !17
  %20 = fadd <2 x double> %broadcast.splat, %wide.load
  %21 = fadd <2 x double> %broadcast.splat, %wide.load17
  %22 = bitcast <2 x double> %20 to <2 x i64>
  %23 = bitcast <2 x double> %21 to <2 x i64>
  %24 = and <2 x i64> %broadcast.splat19, %22
  %25 = and <2 x i64> %broadcast.splat19, %23
  %26 = or <2 x i64> %24, %broadcast.splat21
  %27 = or <2 x i64> %25, %broadcast.splat21
  %28 = bitcast <2 x i64> %26 to <2 x double>
  %29 = bitcast <2 x i64> %27 to <2 x double>
  %30 = fsub <2 x double> %28, %broadcast.splat23
  %31 = fsub <2 x double> %29, %broadcast.splat23
  %32 = getelementptr inbounds double, ptr %1, i64 %index
  %33 = getelementptr inbounds i8, ptr %32, i64 16
  store <2 x double> %30, ptr %32, align 8, !tbaa !17
  store <2 x double> %31, ptr %33, align 8, !tbaa !17
  %index.next = add nuw i64 %index, 4
  %34 = icmp eq i64 %index.next, %n.vec
  br i1 %34, label %middle.block, label %vector.body, !llvm.loop !18

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

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %3
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader24
  %.016 = phi i64 [ %.016.ph, %.lr.ph.preheader24 ], [ %54, %.lr.ph ]
  %35 = getelementptr inbounds double, ptr %2, i64 %.016
  %36 = load double, ptr %35, align 8, !tbaa !17
  %37 = fadd double %8, %36
  %38 = bitcast double %37 to i64
  %39 = and i64 %10, %38
  %40 = or i64 %39, %12
  %41 = bitcast i64 %40 to double
  %42 = fsub double %41, %14
  %43 = getelementptr inbounds double, ptr %1, i64 %.016
  store double %42, ptr %43, align 8, !tbaa !17
  %44 = or disjoint i64 %.016, 1
  %45 = getelementptr inbounds double, ptr %2, i64 %44
  %46 = load double, ptr %45, align 8, !tbaa !17
  %47 = fadd double %8, %46
  %48 = bitcast double %47 to i64
  %49 = and i64 %10, %48
  %50 = or i64 %49, %12
  %51 = bitcast i64 %50 to double
  %52 = fsub double %51, %14
  %53 = getelementptr inbounds double, ptr %1, i64 %44
  store double %52, ptr %53, align 8, !tbaa !17
  %54 = add nuw i64 %.016, 2
  %exitcond.not.1 = icmp eq i64 %54, %6
  br i1 %exitcond.not.1, label %._crit_edge, label %.lr.ph, !llvm.loop !22
}

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, !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 32}
!14 = !{!6, !10, i64 40}
!15 = !{!6, !10, i64 48}
!16 = !{!6, !11, i64 56}
!17 = !{!11, !11, i64 0}
!18 = distinct !{!18, !19, !20, !21}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!"llvm.loop.isvectorized", i32 1}
!21 = !{!"llvm.loop.unroll.runtime.disable"}
!22 = distinct !{!22, !19, !20}


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