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

@.str = external hidden unnamed_addr constant [23 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1
@__cpu_model = external dso_local global { i32, i32, i32, [1 x i32] }

; 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
declare dso_local void @reim_to_tnx_ref(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @init_reim_to_tnx_precomp(ptr noundef %0, i32 noundef %1, double noundef %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca double, align 8
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca %union.dblui64_t, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store double %2, ptr %8, align 8, !tbaa !11
  store i32 %3, ptr %9, align 4, !tbaa !9
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = sub i32 %13, 1
  %15 = and i32 %12, %14
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %19

17:                                               ; preds = %4
  %18 = call ptr @spqlios_error(ptr noundef @.str)
  store ptr %18, ptr %5, align 8
  br label %85

19:                                               ; preds = %4
  %20 = call i64 @is_not_pow2_double(ptr noundef %8)
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %19
  %23 = call ptr @spqlios_error(ptr noundef @.str.1)
  store ptr %23, ptr %5, align 8
  br label %85

24:                                               ; preds = %19
  %25 = load i32, ptr %9, align 4, !tbaa !9
  %26 = icmp ugt i32 %25, 52
  br i1 %26, label %27, label %29

27:                                               ; preds = %24
  %28 = call ptr @spqlios_error(ptr noundef @.str.2)
  store ptr %28, ptr %5, align 8
  br label %85

29:                                               ; preds = %24
  %30 = load i32, ptr %7, align 4, !tbaa !9
  %31 = zext i32 %30 to i64
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %32, i32 0, i32 1
  store i64 %31, ptr %33, align 8, !tbaa !13
  %34 = load double, ptr %8, align 8, !tbaa !11
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %35, i32 0, i32 2
  store double %34, ptr %36, align 8, !tbaa !16
  %37 = load i32, ptr %9, align 4, !tbaa !9
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %38, i32 0, i32 3
  store i32 %37, ptr %39, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = sub i32 50, %40
  %42 = zext i32 %41 to i64
  store i64 %42, ptr %10, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %43 = load i32, ptr %9, align 4, !tbaa !9
  %44 = shl i32 6, %43
  %45 = sitofp i32 %44 to double
  %46 = fadd double 5.000000e-01, %45
  store double %46, ptr %11, align 8, !tbaa !19
  %47 = load double, ptr %11, align 8, !tbaa !19
  %48 = load double, ptr %8, align 8, !tbaa !11
  %49 = fmul double %47, %48
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %50, i32 0, i32 4
  store double %49, ptr %51, align 8, !tbaa !20
  %52 = load i64, ptr %10, align 8, !tbaa !18
  %53 = shl i64 1, %52
  %54 = sub i64 %53, 1
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %55, i32 0, i32 5
  store i64 %54, ptr %56, align 8, !tbaa !21
  %57 = load i64, ptr %11, align 8, !tbaa !19
  %58 = load i64, ptr %10, align 8, !tbaa !18
  %59 = shl i64 -1, %58
  %60 = and i64 %57, %59
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %61, i32 0, i32 6
  store i64 %60, ptr %62, align 8, !tbaa !22
  %63 = load double, ptr %11, align 8, !tbaa !19
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %64, i32 0, i32 7
  store double %63, ptr %65, align 8, !tbaa !23
  %66 = load i32, ptr getelementptr inbounds ({ i32, i32, i32, [1 x i32] }, ptr @__cpu_model, i32 0, i32 3, i32 0), align 4
  %67 = and i32 %66, 1024
  %68 = icmp eq i32 %67, 1024
  %69 = and i1 true, %68
  br i1 %69, label %70, label %80

70:                                               ; preds = %29
  %71 = load i32, ptr %7, align 4, !tbaa !9
  %72 = icmp uge i32 %71, 8
  br i1 %72, label %73, label %76

73:                                               ; preds = %70
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %74, i32 0, i32 0
  store ptr @reim_to_tnx_avx, ptr %75, align 8, !tbaa !24
  br label %79

76:                                               ; preds = %70
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %77, i32 0, i32 0
  store ptr @reim_to_tnx_ref, ptr %78, align 8, !tbaa !24
  br label %79

79:                                               ; preds = %76, %73
  br label %83

80:                                               ; preds = %29
  %81 = load ptr, ptr %6, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.reim_to_tnx_precomp, ptr %81, i32 0, i32 0
  store ptr @reim_to_tnx_ref, ptr %82, align 8, !tbaa !24
  br label %83

83:                                               ; preds = %80, %79
  %84 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %84, ptr %5, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %85

85:                                               ; preds = %83, %27, %22, %17
  %86 = load ptr, ptr %5, align 8
  ret ptr %86
}

declare ptr @spqlios_error(ptr noundef) #2

declare i64 @is_not_pow2_double(ptr noundef) #2

declare void @reim_to_tnx_avx(ptr noundef, ptr noundef, ptr noundef) #2

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 = { "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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !15, i64 8}
!14 = !{!"reim_to_tnx_precomp", !6, i64 0, !15, i64 8, !12, i64 16, !10, i64 24, !12, i64 32, !15, i64 40, !15, i64 48, !12, i64 56}
!15 = !{!"long", !7, i64 0}
!16 = !{!14, !12, i64 16}
!17 = !{!14, !10, i64 24}
!18 = !{!15, !15, i64 0}
!19 = !{!7, !7, i64 0}
!20 = !{!14, !12, i64 32}
!21 = !{!14, !15, i64 40}
!22 = !{!14, !15, i64 48}
!23 = !{!14, !12, i64 56}
!24 = !{!14, !6, i64 0}


; 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 [23 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [26 x i8], align 1
@__cpu_model = external dso_local local_unnamed_addr global { i32, i32, i32, [1 x i32] }

; Function Attrs: nounwind uwtable
declare dso_local void @reim_to_tnx_ref(ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @init_reim_to_tnx_precomp(ptr noundef writeonly %0, i32 noundef %1, double noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = alloca double, align 8
  store double %2, ptr %5, align 8, !tbaa !5
  %6 = tail call range(i32 0, 33) i32 @llvm.ctpop.i32(i32 %1)
  %.not = icmp ult i32 %6, 2
  br i1 %.not, label %9, label %7

7:                                                ; preds = %4
  %8 = tail call ptr @spqlios_error(ptr noundef nonnull @.str) #3
  br label %43

9:                                                ; preds = %4
  %10 = call i64 @is_not_pow2_double(ptr noundef nonnull %5) #3
  %.not23 = icmp eq i64 %10, 0
  br i1 %.not23, label %13, label %11

11:                                               ; preds = %9
  %12 = call ptr @spqlios_error(ptr noundef nonnull @.str.1) #3
  br label %43

13:                                               ; preds = %9
  %14 = icmp ugt i32 %3, 52
  br i1 %14, label %15, label %17

15:                                               ; preds = %13
  %16 = call ptr @spqlios_error(ptr noundef nonnull @.str.2) #3
  br label %43

17:                                               ; preds = %13
  %18 = zext i32 %1 to i64
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %18, ptr %19, align 8, !tbaa !9
  %20 = load double, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %0, i64 16
  store double %20, ptr %21, align 8, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %3, ptr %22, align 8, !tbaa !15
  %23 = sub nsw i32 50, %3
  %24 = zext nneg i32 %23 to i64
  %25 = shl i32 6, %3
  %26 = sitofp i32 %25 to double
  %27 = fadd double %26, 5.000000e-01
  %28 = fmul double %27, %20
  %29 = getelementptr inbounds i8, ptr %0, i64 32
  store double %28, ptr %29, align 8, !tbaa !16
  %notmask = shl nsw i64 -1, %24
  %30 = xor i64 %notmask, -1
  %31 = getelementptr inbounds i8, ptr %0, i64 40
  store i64 %30, ptr %31, align 8, !tbaa !17
  %32 = bitcast double %27 to i64
  %33 = and i64 %notmask, %32
  %34 = getelementptr inbounds i8, ptr %0, i64 48
  store i64 %33, ptr %34, align 8, !tbaa !18
  %35 = getelementptr inbounds i8, ptr %0, i64 56
  store double %27, ptr %35, align 8, !tbaa !19
  %36 = load i32, ptr getelementptr inbounds (i8, ptr @__cpu_model, i64 12), align 4
  %37 = and i32 %36, 1024
  %.not24 = icmp eq i32 %37, 0
  br i1 %.not24, label %42, label %38

38:                                               ; preds = %17
  %39 = icmp ugt i32 %1, 7
  br i1 %39, label %40, label %41

40:                                               ; preds = %38
  store ptr @reim_to_tnx_avx, ptr %0, align 8, !tbaa !20
  br label %43

41:                                               ; preds = %38
  store ptr @reim_to_tnx_ref, ptr %0, align 8, !tbaa !20
  br label %43

42:                                               ; preds = %17
  store ptr @reim_to_tnx_ref, ptr %0, align 8, !tbaa !20
  br label %43

43:                                               ; preds = %42, %41, %40, %15, %11, %7
  %.0 = phi ptr [ %8, %7 ], [ %12, %11 ], [ %16, %15 ], [ %0, %40 ], [ %0, %41 ], [ %0, %42 ]
  ret ptr %.0
}

declare ptr @spqlios_error(ptr noundef) local_unnamed_addr #1

declare i64 @is_not_pow2_double(ptr noundef) local_unnamed_addr #1

declare void @reim_to_tnx_avx(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.ctpop.i32(i32) #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 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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 8}
!10 = !{!"reim_to_tnx_precomp", !11, i64 0, !12, i64 8, !6, i64 16, !13, i64 24, !6, i64 32, !12, i64 40, !12, i64 48, !6, i64 56}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!10, !6, i64 16}
!15 = !{!10, !13, i64 24}
!16 = !{!10, !6, i64 32}
!17 = !{!10, !12, i64 40}
!18 = !{!10, !12, i64 48}
!19 = !{!10, !6, i64 56}
!20 = !{!10, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass