; LLVM IR from tfhe/spqlios-arithmetic :: spqlios/arithmetic (vec_rnx_approxdecomp_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 = type { double }
%struct.tnxdbl_approxdecomp_gadget_t = type { i64, i64, double, i64, i64, double }

@.str = external hidden unnamed_addr constant [41 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @new_tnxdbl_approxdecomp_gadget(ptr noundef %0, i64 noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca %union.di, align 8
  %10 = alloca i64, align 8
  %11 = alloca %union.di, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  %12 = load i64, ptr %6, align 8, !tbaa !9
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = mul i64 %12, %13
  %15 = icmp ugt i64 %14, 50
  br i1 %15, label %16, label %18

16:                                               ; preds = %3
  %17 = call ptr @spqlios_error(ptr noundef @.str)
  store ptr %17, ptr %4, align 8
  br label %69

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %19 = call ptr @spqlios_alloc(i64 noundef 48)
  store ptr %19, ptr %8, align 8, !tbaa !5
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.tnxdbl_approxdecomp_gadget_t, ptr %21, i32 0, i32 0
  store i64 %20, ptr %22, align 8, !tbaa !11
  %23 = load i64, ptr %7, align 8, !tbaa !9
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.tnxdbl_approxdecomp_gadget_t, ptr %24, i32 0, i32 1
  store i64 %23, ptr %25, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = load i64, ptr %6, align 8, !tbaa !9
  %28 = mul i64 %26, %27
  %29 = sub i64 51, %28
  %30 = shl i64 3, %29
  %31 = uitofp i64 %30 to double
  store double %31, ptr %9, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %46, %18
  %33 = load i64, ptr %10, align 8, !tbaa !9
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = icmp ult i64 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %49

37:                                               ; preds = %32
  %38 = load i64, ptr %10, align 8, !tbaa !9
  %39 = add i64 %38, 1
  %40 = load i64, ptr %6, align 8, !tbaa !9
  %41 = mul i64 %39, %40
  %42 = sub i64 %41, 1
  %43 = shl i64 1, %42
  %44 = load i64, ptr %9, align 8, !tbaa !15
  %45 = or i64 %44, %43
  store i64 %45, ptr %9, align 8, !tbaa !15
  br label %46

46:                                               ; preds = %37
  %47 = load i64, ptr %10, align 8, !tbaa !9
  %48 = add i64 %47, 1
  store i64 %48, ptr %10, align 8, !tbaa !9
  br label %32, !llvm.loop !16

49:                                               ; preds = %36
  %50 = load double, ptr %9, align 8, !tbaa !15
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.tnxdbl_approxdecomp_gadget_t, ptr %51, i32 0, i32 2
  store double %50, ptr %52, align 8, !tbaa !18
  %53 = load i64, ptr %6, align 8, !tbaa !9
  %54 = shl i64 1, %53
  %55 = sub i64 %54, 1
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.tnxdbl_approxdecomp_gadget_t, ptr %56, i32 0, i32 3
  store i64 %55, ptr %57, align 8, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  store double 0x4330000000000000, ptr %11, align 8, !tbaa !15
  %58 = load i64, ptr %11, align 8, !tbaa !15
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.tnxdbl_approxdecomp_gadget_t, ptr %59, i32 0, i32 4
  store i64 %58, ptr %60, align 8, !tbaa !20
  %61 = load i64, ptr %6, align 8, !tbaa !9
  %62 = sub i64 %61, 1
  %63 = shl i64 1, %62
  %64 = add i64 4503599627370496, %63
  %65 = uitofp i64 %64 to double
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.tnxdbl_approxdecomp_gadget_t, ptr %66, i32 0, i32 5
  store double %65, ptr %67, align 8, !tbaa !21
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %68, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %69

69:                                               ; preds = %49, %16
  %70 = load ptr, ptr %4, align 8
  ret ptr %70
}

declare ptr @spqlios_error(ptr noundef) #1

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

declare ptr @spqlios_alloc(i64 noundef) #1

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

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 = { "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 willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!12 = !{!"tnxdbl_approxdecomp_gadget_t", !10, i64 0, !10, i64 8, !13, i64 16, !10, i64 24, !10, i64 32, !13, i64 40}
!13 = !{!"double", !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!12, !13, i64 16}
!19 = !{!12, !10, i64 24}
!20 = !{!12, !10, i64 32}
!21 = !{!12, !13, i64 40}


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

@.str = external hidden unnamed_addr constant [41 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @new_tnxdbl_approxdecomp_gadget(ptr nocapture noundef readnone %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = mul i64 %2, %1
  %5 = icmp ugt i64 %4, 50
  br i1 %5, label %6, label %8

6:                                                ; preds = %3
  %7 = tail call ptr @spqlios_error(ptr noundef nonnull @.str) #2
  br label %51

8:                                                ; preds = %3
  %9 = tail call ptr @spqlios_alloc(i64 noundef 48) #2
  store i64 %1, ptr %9, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  store i64 %2, ptr %10, align 8, !tbaa !11
  %11 = sub nuw nsw i64 51, %4
  %12 = shl nuw nsw i64 3, %11
  %13 = uitofp nneg i64 %12 to double
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %8
  %14 = bitcast double %13 to i64
  %xtraiter = and i64 %2, 3
  %15 = icmp ult i64 %2, 4
  br i1 %15, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %2, -4
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %50, %.lr.ph ]
  %.02124.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %46, %.lr.ph ]
  %.sroa.03.023.unr = phi i64 [ %14, %.lr.ph.preheader ], [ %50, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.02124.epil = phi i64 [ %16, %.lr.ph.epil ], [ %.02124.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.sroa.03.023.epil = phi i64 [ %20, %.lr.ph.epil ], [ %.sroa.03.023.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %16 = add nuw i64 %.02124.epil, 1
  %17 = mul i64 %16, %1
  %18 = add i64 %17, -1
  %19 = shl nuw i64 1, %18
  %20 = or i64 %19, %.sroa.03.023.epil
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.loopexit, label %.lr.ph.epil, !llvm.loop !12

._crit_edge.loopexit:                             ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa
  %.lcssa = phi i64 [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %20, %.lr.ph.epil ]
  %21 = bitcast i64 %.lcssa to double
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %8
  %.sroa.03.0.lcssa = phi double [ %13, %8 ], [ %21, %._crit_edge.loopexit ]
  %22 = getelementptr inbounds i8, ptr %9, i64 16
  store double %.sroa.03.0.lcssa, ptr %22, align 8, !tbaa !14
  %notmask = shl nsw i64 -1, %1
  %23 = xor i64 %notmask, -1
  %24 = getelementptr inbounds i8, ptr %9, i64 24
  store i64 %23, ptr %24, align 8, !tbaa !15
  %25 = getelementptr inbounds i8, ptr %9, i64 32
  store i64 4841369599423283200, ptr %25, align 8, !tbaa !16
  %26 = add i64 %1, -1
  %27 = shl nuw i64 1, %26
  %28 = add nuw i64 %27, 4503599627370496
  %29 = uitofp i64 %28 to double
  %30 = getelementptr inbounds i8, ptr %9, i64 40
  store double %29, ptr %30, align 8, !tbaa !17
  br label %51

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.02124 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %46, %.lr.ph ]
  %.sroa.03.023 = phi i64 [ %14, %.lr.ph.preheader.new ], [ %50, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %31 = or disjoint i64 %.02124, 1
  %32 = mul i64 %31, %1
  %33 = add i64 %32, -1
  %34 = shl nuw i64 1, %33
  %35 = or i64 %34, %.sroa.03.023
  %36 = or disjoint i64 %.02124, 2
  %37 = mul i64 %36, %1
  %38 = add i64 %37, -1
  %39 = shl nuw i64 1, %38
  %40 = or i64 %39, %35
  %41 = or disjoint i64 %.02124, 3
  %42 = mul i64 %41, %1
  %43 = add i64 %42, -1
  %44 = shl nuw i64 1, %43
  %45 = or i64 %44, %40
  %46 = add nuw i64 %.02124, 4
  %47 = mul i64 %46, %1
  %48 = add i64 %47, -1
  %49 = shl nuw i64 1, %48
  %50 = or i64 %49, %45
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !18

51:                                               ; preds = %._crit_edge, %6
  %.0 = phi ptr [ %7, %6 ], [ %9, %._crit_edge ]
  ret ptr %.0
}

declare ptr @spqlios_error(ptr noundef) local_unnamed_addr #1

declare ptr @spqlios_alloc(i64 noundef) local_unnamed_addr #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 = { "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, !7, i64 0}
!6 = !{!"tnxdbl_approxdecomp_gadget_t", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !7, i64 32, !10, i64 40}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"double", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}
!14 = !{!6, !10, i64 16}
!15 = !{!6, !7, i64 24}
!16 = !{!6, !7, i64 32}
!17 = !{!6, !10, i64 40}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass