; LLVM IR from tfhe/spqlios-arithmetic :: spqlios/reim (reim_conversions)

; 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_znx64_precomp = type { ptr, i64, double }

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

; 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

declare ptr @spqlios_error(ptr noundef) #1

declare i64 @is_not_pow2_double(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local ptr @init_reim_to_znx64_precomp(ptr noundef %0, i32 noundef %1, double noundef %2, i32 noundef %3) #2 {
  %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 ptr, 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
  %11 = load i32, ptr %7, align 4, !tbaa !9
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = sub i32 %12, 1
  %14 = and i32 %11, %13
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %18

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

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

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

23:                                               ; preds = %18
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = icmp ugt i32 %24, 64
  br i1 %25, label %26, label %28

26:                                               ; preds = %23
  %27 = call ptr @spqlios_error(ptr noundef @.str.6)
  store ptr %27, ptr %5, align 8
  br label %54

28:                                               ; preds = %23
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = zext i32 %29 to i64
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.reim_to_znx64_precomp, ptr %31, i32 0, i32 1
  store i64 %30, ptr %32, align 8, !tbaa !13
  %33 = load double, ptr %8, align 8, !tbaa !11
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.reim_to_znx64_precomp, ptr %34, i32 0, i32 2
  store double %33, ptr %35, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store ptr @reim_to_znx64_ref, ptr %10, align 8, !tbaa !5
  %36 = load i32, ptr getelementptr inbounds ({ i32, i32, i32, [1 x i32] }, ptr @__cpu_model, i32 0, i32 3, i32 0), align 4
  %37 = and i32 %36, 1024
  %38 = icmp eq i32 %37, 1024
  %39 = and i1 true, %38
  br i1 %39, label %40, label %49

40:                                               ; preds = %28
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = icmp uge i32 %41, 8
  br i1 %42, label %43, label %49

43:                                               ; preds = %40
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = icmp ule i32 %44, 50
  br i1 %45, label %46, label %47

46:                                               ; preds = %43
  store ptr @reim_to_znx64_avx2_bnd50_fma, ptr %10, align 8, !tbaa !5
  br label %48

47:                                               ; preds = %43
  store ptr @reim_to_znx64_avx2_bnd63_fma, ptr %10, align 8, !tbaa !5
  br label %48

48:                                               ; preds = %47, %46
  br label %49

49:                                               ; preds = %48, %40, %28
  %50 = load ptr, ptr %10, align 8, !tbaa !5
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.reim_to_znx64_precomp, ptr %51, i32 0, i32 0
  store ptr %50, ptr %52, align 8, !tbaa !17
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %53, ptr %5, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %54

54:                                               ; preds = %49, %26, %21, %16
  %55 = load ptr, ptr %5, align 8
  ret ptr %55
}

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

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!14, !15, i64 8}
!14 = !{!"reim_to_znx64_precomp", !6, i64 0, !15, i64 8, !12, i64 16}
!15 = !{!"long", !7, i64 0}
!16 = !{!14, !12, i64 16}
!17 = !{!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
@__cpu_model = external dso_local local_unnamed_addr global { i32, i32, i32, [1 x i32] }
@.str.4 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [23 x i8], align 1

declare ptr @spqlios_error(ptr noundef) local_unnamed_addr #0

declare i64 @is_not_pow2_double(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local ptr @init_reim_to_znx64_precomp(ptr noundef writeonly %0, i32 noundef %1, double noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %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 %27

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

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

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

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

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 !13
  %22 = load i32, ptr getelementptr inbounds (i8, ptr @__cpu_model, i64 12), align 4
  %23 = and i32 %22, 1024
  %24 = icmp ne i32 %23, 0
  %25 = icmp ugt i32 %1, 7
  %or.cond = and i1 %25, %24
  %26 = icmp ult i32 %3, 51
  %reim_to_znx64_avx2_bnd50_fma.reim_to_znx64_avx2_bnd63_fma = select i1 %26, ptr @reim_to_znx64_avx2_bnd50_fma, ptr @reim_to_znx64_avx2_bnd63_fma
  %.0 = select i1 %or.cond, ptr %reim_to_znx64_avx2_bnd50_fma.reim_to_znx64_avx2_bnd63_fma, ptr @reim_to_znx64_ref
  store ptr %.0, ptr %0, align 8, !tbaa !14
  br label %27

27:                                               ; preds = %17, %15, %11, %7
  %.012 = phi ptr [ %8, %7 ], [ %12, %11 ], [ %16, %15 ], [ %0, %17 ]
  ret ptr %.012
}

declare void @reim_to_znx64_avx2_bnd50_fma(ptr noundef, ptr noundef, ptr noundef) #0

declare void @reim_to_znx64_avx2_bnd63_fma(ptr noundef, ptr noundef, ptr noundef) #0

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

attributes #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 = { 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 = { 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_znx64_precomp", !11, i64 0, !12, i64 8, !6, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !6, i64 16}
!14 = !{!10, !11, i64 0}


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