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

@cplx_to_tnx32_simple.precomp = external hidden thread_local global [32 x %struct.LAST_CPLX_TO_TNX32_PRECOMP], align 16

; 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 ptr @init_cplx_to_tnx32_precomp(ptr noundef, i32 noundef, double noundef, i32 noundef) #1

declare i32 @log2m(i32 noundef) #2

; Function Attrs: noreturn nounwind
declare void @abort() #3

; Function Attrs: nounwind uwtable
define dso_local void @cplx_to_tnx32_simple(i32 noundef %0, double noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca double, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  store i32 %0, ptr %6, align 4, !tbaa !5
  store double %1, ptr %7, align 8, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !11
  store ptr %4, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %12 = call align 16 ptr @llvm.threadlocal.address.p0(ptr align 16 @cplx_to_tnx32_simple.precomp)
  %13 = getelementptr inbounds [32 x %struct.LAST_CPLX_TO_TNX32_PRECOMP], ptr %12, i64 0, i64 0
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = call i32 @log2m(i32 noundef %14)
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %13, i64 %16
  store ptr %17, ptr %11, align 8, !tbaa !11
  %18 = load ptr, ptr %11, align 8, !tbaa !11
  %19 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %18, i32 0, i32 0
  %20 = getelementptr inbounds %struct.cplx_to_tnx32_precomp, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %36

23:                                               ; preds = %5
  %24 = load double, ptr %7, align 8, !tbaa !9
  %25 = load ptr, ptr %11, align 8, !tbaa !11
  %26 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %25, i32 0, i32 1
  %27 = load double, ptr %26, align 8, !tbaa !17
  %28 = fcmp une double %24, %27
  br i1 %28, label %36, label %29

29:                                               ; preds = %23
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = uitofp i32 %30 to double
  %32 = load ptr, ptr %11, align 8, !tbaa !11
  %33 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %32, i32 0, i32 2
  %34 = load double, ptr %33, align 8, !tbaa !18
  %35 = fcmp une double %31, %34
  br i1 %35, label %36, label %54

36:                                               ; preds = %29, %23, %5
  %37 = load ptr, ptr %11, align 8, !tbaa !11
  call void @llvm.memset.p0.i64(ptr align 8 %37, i8 0, i64 40, i1 false)
  %38 = load ptr, ptr %11, align 8, !tbaa !11
  %39 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = load double, ptr %7, align 8, !tbaa !9
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = call ptr @init_cplx_to_tnx32_precomp(ptr noundef %39, i32 noundef %40, double noundef %41, i32 noundef %42)
  %44 = icmp ne ptr %43, null
  br i1 %44, label %46, label %45

45:                                               ; preds = %36
  call void @abort() #7
  unreachable

46:                                               ; preds = %36
  %47 = load double, ptr %7, align 8, !tbaa !9
  %48 = load ptr, ptr %11, align 8, !tbaa !11
  %49 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %48, i32 0, i32 1
  store double %47, ptr %49, align 8, !tbaa !17
  %50 = load i32, ptr %8, align 4, !tbaa !5
  %51 = uitofp i32 %50 to double
  %52 = load ptr, ptr %11, align 8, !tbaa !11
  %53 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %52, i32 0, i32 2
  store double %51, ptr %53, align 8, !tbaa !18
  br label %54

54:                                               ; preds = %46, %29
  %55 = load ptr, ptr %11, align 8, !tbaa !11
  %56 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %55, i32 0, i32 0
  %57 = getelementptr inbounds %struct.cplx_to_tnx32_precomp, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !13
  %59 = load ptr, ptr %11, align 8, !tbaa !11
  %60 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %9, align 8, !tbaa !11
  %62 = load ptr, ptr %10, align 8, !tbaa !11
  call void %58(ptr noundef %60, ptr noundef %61, ptr noundef %62)
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #4

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #5

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 = { noreturn nounwind "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 #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { nounwind }
attributes #7 = { noreturn 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"LAST_CPLX_TO_TNX32_PRECOMP", !15, i64 0, !10, i64 24, !10, i64 32}
!15 = !{!"cplx_to_tnx32_precomp", !12, i64 0, !16, i64 8, !10, i64 16}
!16 = !{!"long", !7, i64 0}
!17 = !{!14, !10, i64 24}
!18 = !{!14, !10, i64 32}


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

%struct.LAST_CPLX_TO_TNX32_PRECOMP = type { %struct.cplx_to_tnx32_precomp, double, double }
%struct.cplx_to_tnx32_precomp = type { ptr, i64, double }

@cplx_to_tnx32_simple.precomp = external hidden thread_local global [32 x %struct.LAST_CPLX_TO_TNX32_PRECOMP], align 16

; Function Attrs: nounwind uwtable
declare dso_local ptr @init_cplx_to_tnx32_precomp(ptr noundef, i32 noundef, double noundef, i32 noundef) local_unnamed_addr #0

declare i32 @log2m(i32 noundef) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @abort() local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @cplx_to_tnx32_simple(i32 noundef %0, double noundef %1, i32 noundef %2, ptr noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = tail call align 16 ptr @llvm.threadlocal.address.p0(ptr align 16 @cplx_to_tnx32_simple.precomp)
  %7 = tail call i32 @log2m(i32 noundef %0) #5
  %8 = zext i32 %7 to i64
  %9 = getelementptr inbounds %struct.LAST_CPLX_TO_TNX32_PRECOMP, ptr %6, i64 %8
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %20, label %11

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %9, i64 24
  %13 = load double, ptr %12, align 8, !tbaa !13
  %14 = fcmp une double %13, %1
  br i1 %14, label %20, label %15

15:                                               ; preds = %11
  %16 = uitofp i32 %2 to double
  %17 = getelementptr inbounds i8, ptr %9, i64 32
  %18 = load double, ptr %17, align 8, !tbaa !14
  %19 = fcmp une double %18, %16
  br i1 %19, label %20, label %27

20:                                               ; preds = %15, %11, %5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(40) %9, i8 0, i64 40, i1 false)
  %21 = tail call ptr @init_cplx_to_tnx32_precomp(ptr noundef nonnull %9, i32 noundef %0, double noundef %1, i32 noundef %2)
  %.not18 = icmp eq ptr %21, null
  br i1 %.not18, label %22, label %23

22:                                               ; preds = %20
  tail call void @abort() #6
  unreachable

23:                                               ; preds = %20
  %24 = getelementptr inbounds i8, ptr %9, i64 24
  store double %1, ptr %24, align 8, !tbaa !13
  %25 = uitofp i32 %2 to double
  %26 = getelementptr inbounds i8, ptr %9, i64 32
  store double %25, ptr %26, align 8, !tbaa !14
  %.pre = load ptr, ptr %9, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %23, %15
  %28 = phi ptr [ %.pre, %23 ], [ %10, %15 ]
  tail call void %28(ptr noundef nonnull %9, ptr noundef %3, ptr noundef %4) #5
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #3

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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 = { noreturn nounwind "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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { noreturn 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, !8, i64 0}
!6 = !{!"LAST_CPLX_TO_TNX32_PRECOMP", !7, i64 0, !12, i64 24, !12, i64 32}
!7 = !{!"cplx_to_tnx32_precomp", !8, i64 0, !11, i64 8, !12, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"double", !9, i64 0}
!13 = !{!6, !12, i64 24}
!14 = !{!6, !12, i64 32}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,TailCallElimPass