; LLVM IR from tsotchke/quantum_geometric_tensor :: src/quantum_geometric/core (numerical_backend_selector)

; 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.anon = type { i32, i8, ptr }
%struct.numerical_config_t = type { i32, i64, i8, i8, i8, i64, ptr }

@backend_selector = external hidden global %struct.anon, align 8

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @select_optimal_backend(ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @initialize_numerical_backend(ptr noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca %struct.numerical_config_t, align 8
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %10, label %9

9:                                                ; preds = %1
  store i1 false, ptr %2, align 1
  br label %33

10:                                               ; preds = %1
  %11 = load i8, ptr getelementptr inbounds (%struct.anon, ptr @backend_selector, i32 0, i32 1), align 4, !tbaa !9, !range !13, !noundef !14
  %12 = trunc i8 %11 to i1
  br i1 %12, label %13, label %14

13:                                               ; preds = %10
  call void @shutdown_numerical_backend()
  br label %14

14:                                               ; preds = %13, %10
  call void @llvm.lifetime.start.p0(i64 40, ptr %4) #4
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %4, ptr align 8 %15, i64 40, i1 false), !tbaa.struct !15
  %16 = call zeroext i1 @select_optimal_backend(ptr noundef %4)
  br i1 %16, label %18, label %17

17:                                               ; preds = %14
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %32

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #4
  store i8 0, ptr %6, align 1, !tbaa !19
  %19 = load i32, ptr @backend_selector, align 8, !tbaa !20
  switch i32 %19, label %23 [
    i32 0, label %20
  ]

20:                                               ; preds = %18
  %21 = call zeroext i1 @initialize_numerical_backend_cpu(ptr noundef %4)
  %22 = zext i1 %21 to i8
  store i8 %22, ptr %6, align 1, !tbaa !19
  br label %24

23:                                               ; preds = %18
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %31

24:                                               ; preds = %20
  %25 = load i8, ptr %6, align 1, !tbaa !19, !range !13, !noundef !14
  %26 = trunc i8 %25 to i1
  br i1 %26, label %27, label %28

27:                                               ; preds = %24
  store i8 1, ptr getelementptr inbounds (%struct.anon, ptr @backend_selector, i32 0, i32 1), align 4, !tbaa !9
  br label %28

28:                                               ; preds = %27, %24
  %29 = load i8, ptr %6, align 1, !tbaa !19, !range !13, !noundef !14
  %30 = trunc i8 %29 to i1
  store i1 %30, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %31

31:                                               ; preds = %28, %23
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #4
  br label %32

32:                                               ; preds = %31, %17
  call void @llvm.lifetime.end.p0(i64 40, ptr %4) #4
  br label %33

33:                                               ; preds = %32, %9
  %34 = load i1, ptr %2, align 1
  ret i1 %34
}

; Function Attrs: nounwind uwtable
declare dso_local void @shutdown_numerical_backend() #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare zeroext i1 @initialize_numerical_backend_cpu(ptr noundef) #3

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 = { 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, !12, i64 4}
!10 = !{!"", !11, i64 0, !12, i64 4, !6, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{i64 0, i64 4, !16, i64 8, i64 8, !17, i64 16, i64 1, !19, i64 17, i64 1, !19, i64 18, i64 1, !19, i64 24, i64 8, !17, i64 32, i64 8, !5}
!16 = !{!11, !11, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"long", !7, i64 0}
!19 = !{!12, !12, i64 0}
!20 = !{!10, !11, 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"

%struct.anon = type { i32, i8, ptr }
%struct.numerical_config_t = type { i32, i64, i8, i8, i8, i64, ptr }

@backend_selector = external hidden local_unnamed_addr global %struct.anon, align 8

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @select_optimal_backend(ptr noundef) local_unnamed_addr #0

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @initialize_numerical_backend(ptr noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca %struct.numerical_config_t, align 8
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %14, label %3

3:                                                ; preds = %1
  %4 = load i8, ptr getelementptr inbounds (i8, ptr @backend_selector, i64 4), align 4, !tbaa !5, !range !12, !noundef !13
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %6, label %7

6:                                                ; preds = %3
  tail call void @shutdown_numerical_backend()
  br label %7

7:                                                ; preds = %6, %3
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %2) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(40) %2, ptr noundef nonnull align 8 dereferenceable(40) %0, i64 40, i1 false), !tbaa.struct !14
  %8 = call zeroext i1 @select_optimal_backend(ptr noundef nonnull %2)
  %9 = load i32, ptr @backend_selector, align 8
  %cond = icmp eq i32 %9, 0
  %or.cond = select i1 %8, i1 %cond, i1 false
  br i1 %or.cond, label %10, label %13

10:                                               ; preds = %7
  %11 = call zeroext i1 @initialize_numerical_backend_cpu(ptr noundef nonnull %2) #4
  br i1 %11, label %12, label %13

12:                                               ; preds = %10
  store i8 1, ptr getelementptr inbounds (i8, ptr @backend_selector, i64 4), align 4, !tbaa !5
  br label %13

13:                                               ; preds = %12, %10, %7
  %.1 = phi i1 [ false, %7 ], [ true, %12 ], [ false, %10 ]
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %2) #4
  br label %14

14:                                               ; preds = %1, %13
  %.2 = phi i1 [ %.1, %13 ], [ false, %1 ]
  ret i1 %.2
}

; Function Attrs: nounwind uwtable
declare dso_local void @shutdown_numerical_backend() local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare zeroext i1 @initialize_numerical_backend_cpu(ptr noundef) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { "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 = { 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, !10, i64 4}
!6 = !{!"", !7, i64 0, !10, i64 4, !11, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"_Bool", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{i8 0, i8 2}
!13 = !{}
!14 = !{i64 0, i64 4, !15, i64 8, i64 8, !16, i64 16, i64 1, !18, i64 17, i64 1, !18, i64 18, i64 1, !18, i64 24, i64 8, !16, i64 32, i64 8, !19}
!15 = !{!7, !7, i64 0}
!16 = !{!17, !17, i64 0}
!17 = !{!"long", !8, i64 0}
!18 = !{!10, !10, i64 0}
!19 = !{!11, !11, i64 0}


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