; LLVM IR from Simple-XX/SimpleKernel :: src/kernel/slab.ll (ZN4SLAB5allocEm)

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

%class.SLAB = type <{ %class.ALLOCATOR, [9 x %"class.SLAB::slab_cache_t"], i8, [7 x i8] }>
%class.ALLOCATOR = type { ptr, ptr, i64, i64, i64, i64 }
%"class.SLAB::slab_cache_t" = type <{ i64, %"struct.SLAB::chunk_t", %"struct.SLAB::chunk_t", %"struct.SLAB::chunk_t", i8, [7 x i8] }>
%"struct.SLAB::chunk_t" = type { i64, i64, ptr, ptr }

@.str = external hidden unnamed_addr constant [30 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [76 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [32 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 i32 @err(ptr noundef, ...) #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN4SLAB12slab_cache_t4findEm(ptr noundef nonnull align 8 dereferenceable(105), i64 noundef) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK4SLAB7get_idxEm(ptr noundef nonnull align 8 dereferenceable(1057), i64 noundef) #3 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN4SLAB5allocEm(ptr noundef nonnull align 8 dereferenceable(1057) %0, i64 noundef %1) unnamed_addr #2 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %8 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  store i64 0, ptr %5, align 8, !tbaa !9
  %9 = load i64, ptr %4, align 8, !tbaa !9
  %10 = icmp ugt i64 %9, 0
  br i1 %10, label %11, label %42

11:                                               ; preds = %2
  %12 = load i64, ptr %4, align 8, !tbaa !9
  %13 = icmp ule i64 %12, 65536
  br i1 %13, label %14, label %42

14:                                               ; preds = %11
  %15 = load i64, ptr %4, align 8, !tbaa !9
  %16 = call noundef i64 @_ZN6COMMON5ALIGNImEET_S1_m(i64 noundef %15, i64 noundef 8)
  store i64 %16, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %17 = load i64, ptr %4, align 8, !tbaa !9
  %18 = call noundef i64 @_ZNK4SLAB7get_idxEm(ptr noundef nonnull align 8 dereferenceable(1057) %8, i64 noundef %17)
  store i64 %18, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %19 = getelementptr inbounds %class.SLAB, ptr %8, i32 0, i32 1
  %20 = load i64, ptr %6, align 8, !tbaa !9
  %21 = getelementptr inbounds [9 x %"class.SLAB::slab_cache_t"], ptr %19, i64 0, i64 %20
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = call noundef ptr @_ZN4SLAB12slab_cache_t4findEm(ptr noundef nonnull align 8 dereferenceable(105) %21, i64 noundef %22)
  store ptr %23, ptr %7, align 8, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %41

26:                                               ; preds = %14
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = ptrtoint ptr %27 to i64
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = getelementptr inbounds %"struct.SLAB::chunk_t", ptr %29, i32 0, i32 0
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = icmp eq i64 %28, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %26
  br label %36

34:                                               ; preds = %26
  %35 = call i32 (ptr, ...) @err(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 369, ptr noundef @.str.10)
  br label %36

36:                                               ; preds = %34, %33
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = getelementptr inbounds %"struct.SLAB::chunk_t", ptr %37, i32 0, i32 0
  %39 = load i64, ptr %38, align 8, !tbaa !11
  %40 = add i64 %39, 32
  store i64 %40, ptr %5, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %36, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %42

42:                                               ; preds = %41, %11, %2
  %43 = load i64, ptr %5, align 8, !tbaa !9
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %50

45:                                               ; preds = %42
  %46 = load i64, ptr %4, align 8, !tbaa !9
  %47 = getelementptr inbounds %class.ALLOCATOR, ptr %8, i32 0, i32 5
  %48 = load i64, ptr %47, align 8, !tbaa !13
  %49 = add i64 %48, %46
  store i64 %49, ptr %47, align 8, !tbaa !13
  br label %50

50:                                               ; preds = %45, %42
  %51 = load i64, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret i64 %51
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN6COMMON5ALIGNImEET_S1_m(i64 noundef, i64 noundef) #4

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 = { mustprogress 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 = { mustprogress 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 #4 = { inlinehint mustprogress 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 #5 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTSN4SLAB7chunk_tE", !10, i64 0, !10, i64 8, !6, i64 16, !6, i64 24}
!13 = !{!14, !10, i64 40}
!14 = !{!"_ZTS9ALLOCATOR", !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}


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

%"class.SLAB::slab_cache_t" = type <{ i64, %"struct.SLAB::chunk_t", %"struct.SLAB::chunk_t", %"struct.SLAB::chunk_t", i8, [7 x i8] }>
%"struct.SLAB::chunk_t" = type { i64, i64, ptr, ptr }

@.str = external hidden unnamed_addr constant [30 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [76 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [32 x i8], align 1

declare i32 @err(ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN4SLAB12slab_cache_t4findEm(ptr noundef nonnull align 8 dereferenceable(105), i64 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK4SLAB7get_idxEm(ptr noundef nonnull align 8 dereferenceable(1057), i64 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN4SLAB5allocEm(ptr noundef nonnull align 8 dereferenceable(1057) %0, i64 noundef %1) unnamed_addr #1 align 2 {
  %3 = add i64 %1, -1
  %or.cond = icmp ult i64 %3, 65536
  br i1 %or.cond, label %4, label %.thread

4:                                                ; preds = %2
  %5 = tail call noundef i64 @_ZN6COMMON5ALIGNImEET_S1_m(i64 noundef %1, i64 noundef 8)
  %6 = tail call noundef i64 @_ZNK4SLAB7get_idxEm(ptr noundef nonnull align 8 dereferenceable(1057) %0, i64 noundef %5)
  %7 = getelementptr inbounds i8, ptr %0, i64 48
  %8 = getelementptr inbounds [9 x %"class.SLAB::slab_cache_t"], ptr %7, i64 0, i64 %6
  %9 = tail call noundef ptr @_ZN4SLAB12slab_cache_t4findEm(ptr noundef nonnull align 8 dereferenceable(105) %8, i64 noundef %5)
  %.not = icmp eq ptr %9, null
  br i1 %.not, label %.thread, label %10

10:                                               ; preds = %4
  %11 = ptrtoint ptr %9 to i64
  %12 = load i64, ptr %9, align 8, !tbaa !5
  %13 = icmp eq i64 %12, %11
  br i1 %13, label %16, label %14

14:                                               ; preds = %10
  %15 = tail call i32 (ptr, ...) @err(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 369, ptr noundef nonnull @.str.10)
  %.pre = load i64, ptr %9, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %14, %10
  %17 = phi i64 [ %.pre, %14 ], [ %11, %10 ]
  %18 = add i64 %17, 32
  %.not17 = icmp eq i64 %18, 0
  br i1 %.not17, label %.thread, label %19

19:                                               ; preds = %16
  %20 = getelementptr inbounds i8, ptr %0, i64 40
  %21 = load i64, ptr %20, align 8, !tbaa !11
  %22 = add i64 %21, %5
  store i64 %22, ptr %20, align 8, !tbaa !11
  br label %.thread

.thread:                                          ; preds = %4, %2, %19, %16
  %.121 = phi i64 [ %18, %19 ], [ 0, %16 ], [ 0, %2 ], [ 0, %4 ]
  ret i64 %.121
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN6COMMON5ALIGNImEET_S1_m(i64 noundef, i64 noundef) local_unnamed_addr #3

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 = { mustprogress 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 = { mustprogress 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 = { inlinehint mustprogress 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, !7, i64 0}
!6 = !{!"_ZTSN4SLAB7chunk_tE", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!12, !7, i64 40}
!12 = !{!"_ZTS9ALLOCATOR", !10, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}


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