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

; 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.MemoryPool = type { ptr, i64, ptr, i64, ptr, i64, i64, i64, %union.pthread_mutex_t, %struct.PoolConfig }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.PoolConfig = type { i64, i64, i64, float, i64, i8, i8, i64, i64, i8 }

@size_class_table = external hidden global [64 x i64], 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
define dso_local ptr @pool_malloc(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i16, align 2
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %20

17:                                               ; preds = %2
  %18 = load i64, ptr %5, align 8, !tbaa !9
  %19 = icmp ne i64 %18, 0
  br i1 %19, label %21, label %20

20:                                               ; preds = %17, %2
  store ptr null, ptr %3, align 8
  br label %72

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load i64, ptr %5, align 8, !tbaa !9
  %24 = call ptr @allocate_from_cache(ptr noundef %22, i64 noundef %23)
  store ptr %24, ptr %6, align 8, !tbaa !5
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %29

27:                                               ; preds = %21
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %28, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %71

29:                                               ; preds = %21
  %30 = load i64, ptr %5, align 8, !tbaa !9
  %31 = load atomic i64, ptr getelementptr inbounds ([64 x i64], ptr @size_class_table, i64 0, i64 63) acquire, align 8
  store i64 %31, ptr %8, align 8
  %32 = load i64, ptr %8, align 8, !tbaa !9
  %33 = icmp ule i64 %30, %32
  br i1 %33, label %34, label %40

34:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  %35 = load i64, ptr %5, align 8, !tbaa !9
  %36 = call zeroext i16 @get_size_class(i64 noundef %35)
  store i16 %36, ptr %9, align 2, !tbaa !11
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load i16, ptr %9, align 2, !tbaa !11
  %39 = call ptr @allocate_from_class(ptr noundef %37, i16 noundef zeroext %38)
  store ptr %39, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  br label %44

40:                                               ; preds = %29
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = load i64, ptr %5, align 8, !tbaa !9
  %43 = call ptr @allocate_large_block(ptr noundef %41, i64 noundef %42)
  store ptr %43, ptr %6, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %40, %34
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = icmp ne ptr %45, null
  br i1 %46, label %47, label %69

47:                                               ; preds = %44
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.MemoryPool, ptr %48, i32 0, i32 9
  %50 = getelementptr inbounds %struct.PoolConfig, ptr %49, i32 0, i32 9
  %51 = load i8, ptr %50, align 8, !tbaa !13, !range !18, !noundef !19
  %52 = trunc i8 %51 to i1
  br i1 %52, label %53, label %69

53:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.MemoryPool, ptr %54, i32 0, i32 5
  %56 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %56, ptr %11, align 8, !tbaa !9
  %57 = load i64, ptr %11, align 8
  %58 = atomicrmw add ptr %55, i64 %57 monotonic, align 8
  store i64 %58, ptr %12, align 8
  %59 = load i64, ptr %12, align 8, !tbaa !9
  %60 = load i64, ptr %5, align 8, !tbaa !9
  %61 = add i64 %59, %60
  store i64 %61, ptr %10, align 8, !tbaa !9
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.MemoryPool, ptr %62, i32 0, i32 7
  store i64 1, ptr %13, align 8, !tbaa !9
  %64 = load i64, ptr %13, align 8
  %65 = atomicrmw add ptr %63, i64 %64 monotonic, align 8
  store i64 %65, ptr %14, align 8
  %66 = load i64, ptr %14, align 8, !tbaa !9
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = load i64, ptr %10, align 8, !tbaa !9
  call void @update_peak_memory(ptr noundef %67, i64 noundef %68)
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %69

69:                                               ; preds = %53, %47, %44
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %70, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %71

71:                                               ; preds = %69, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %72

72:                                               ; preds = %71, %20
  %73 = load ptr, ptr %3, align 8
  ret ptr %73
}

; Function Attrs: nounwind uwtable
declare hidden ptr @allocate_from_cache(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @get_size_class(i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @allocate_from_class(ptr noundef, i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare hidden ptr @allocate_large_block(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @update_peak_memory(ptr noundef, i64 noundef) #1

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 = { 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, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !17, i64 168}
!14 = !{!"MemoryPool", !6, i64 0, !10, i64 8, !6, i64 16, !10, i64 24, !6, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !15, i64 104}
!15 = !{!"PoolConfig", !10, i64 0, !10, i64 8, !10, i64 16, !16, i64 24, !10, i64 32, !17, i64 40, !17, i64 41, !10, i64 48, !10, i64 56, !17, i64 64}
!16 = !{!"float", !7, i64 0}
!17 = !{!"_Bool", !7, i64 0}
!18 = !{i8 0, i8 2}
!19 = !{}


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

@size_class_table = external hidden local_unnamed_addr global [64 x i64], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @pool_malloc(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = icmp ne ptr %0, null
  %4 = icmp ne i64 %1, 0
  %or.cond = and i1 %3, %4
  br i1 %or.cond, label %5, label %25

5:                                                ; preds = %2
  %6 = tail call ptr @allocate_from_cache(ptr noundef nonnull %0, i64 noundef %1)
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %7, label %25

7:                                                ; preds = %5
  %8 = load atomic i64, ptr getelementptr inbounds (i8, ptr @size_class_table, i64 504) acquire, align 8
  %.not31 = icmp ult i64 %8, %1
  br i1 %.not31, label %12, label %9

9:                                                ; preds = %7
  %10 = tail call zeroext i16 @get_size_class(i64 noundef %1)
  %11 = tail call ptr @allocate_from_class(ptr noundef nonnull %0, i16 noundef zeroext %10)
  br label %14

12:                                               ; preds = %7
  %13 = tail call ptr @allocate_large_block(ptr noundef nonnull %0, i64 noundef %1)
  br label %14

14:                                               ; preds = %12, %9
  %.027 = phi ptr [ %11, %9 ], [ %13, %12 ]
  %.not32 = icmp eq ptr %.027, null
  br i1 %.not32, label %25, label %15

15:                                               ; preds = %14
  %16 = getelementptr inbounds i8, ptr %0, i64 168
  %17 = load i8, ptr %16, align 8, !tbaa !5, !range !14, !noundef !15
  %18 = trunc nuw i8 %17 to i1
  br i1 %18, label %19, label %25

19:                                               ; preds = %15
  %20 = getelementptr inbounds i8, ptr %0, i64 40
  %21 = atomicrmw add ptr %20, i64 %1 monotonic, align 8
  %22 = add i64 %21, %1
  %23 = getelementptr inbounds i8, ptr %0, i64 56
  %24 = atomicrmw add ptr %23, i64 1 monotonic, align 8
  tail call void @update_peak_memory(ptr noundef nonnull %0, i64 noundef %22)
  br label %25

25:                                               ; preds = %5, %19, %15, %14, %2
  %.1 = phi ptr [ null, %2 ], [ %6, %5 ], [ %.027, %19 ], [ %.027, %15 ], [ null, %14 ]
  ret ptr %.1
}

; Function Attrs: nounwind uwtable
declare hidden ptr @allocate_from_cache(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @get_size_class(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @allocate_from_class(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @allocate_large_block(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @update_peak_memory(ptr noundef, i64 noundef) local_unnamed_addr #0

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

!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, !13, i64 168}
!6 = !{!"MemoryPool", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24, !7, i64 32, !8, i64 40, !8, i64 48, !8, i64 56, !8, i64 64, !11, i64 104}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"PoolConfig", !10, i64 0, !10, i64 8, !10, i64 16, !12, i64 24, !10, i64 32, !13, i64 40, !13, i64 41, !10, i64 48, !10, i64 56, !13, i64 64}
!12 = !{!"float", !8, i64 0}
!13 = !{!"_Bool", !8, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}


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