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

; 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.SLAB::chunk_t" = type { i64, i64, ptr, ptr }
%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] }>

@.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
@.str.11 = external hidden unnamed_addr constant [16 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 void @_ZN4SLAB12slab_cache_t6removeEPNS_7chunk_tE(ptr noundef nonnull align 8 dereferenceable(105), ptr 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 void @_ZN4SLAB4freeEmm(ptr noundef nonnull align 8 dereferenceable(1057) %0, i64 noundef %1, i64 noundef %2) unnamed_addr #2 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store i64 %2, ptr %6, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8
  %11 = load i64, ptr %5, align 8, !tbaa !9
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  br label %51

14:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %15 = load i64, ptr %5, align 8, !tbaa !9
  %16 = sub i64 %15, 32
  %17 = inttoptr i64 %16 to ptr
  store ptr %17, ptr %7, align 8, !tbaa !5
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = ptrtoint ptr %18 to i64
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds %"struct.SLAB::chunk_t", ptr %20, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = icmp eq i64 %19, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %14
  br label %27

25:                                               ; preds = %14
  %26 = call i32 (ptr, ...) @err(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 399, ptr noundef @.str.10)
  br label %27

27:                                               ; preds = %25, %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = getelementptr inbounds %"struct.SLAB::chunk_t", ptr %28, i32 0, i32 1
  %30 = load i64, ptr %29, align 8, !tbaa !13
  store i64 %30, ptr %8, align 8, !tbaa !9
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %"struct.SLAB::chunk_t", ptr %31, i32 0, i32 1
  %33 = load i64, ptr %32, align 8, !tbaa !13
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %35, label %36

35:                                               ; preds = %27
  br label %38

36:                                               ; preds = %27
  %37 = call i32 (ptr, ...) @err(ptr noundef @.str, ptr noundef @.str.1, i32 noundef 402, ptr noundef @.str.11)
  br label %38

38:                                               ; preds = %36, %35
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %39 = load ptr, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds %"struct.SLAB::chunk_t", ptr %39, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !13
  %42 = call noundef i64 @_ZNK4SLAB7get_idxEm(ptr noundef nonnull align 8 dereferenceable(1057) %10, i64 noundef %41)
  store i64 %42, ptr %9, align 8, !tbaa !9
  %43 = getelementptr inbounds %class.SLAB, ptr %10, i32 0, i32 1
  %44 = load i64, ptr %9, align 8, !tbaa !9
  %45 = getelementptr inbounds [9 x %"class.SLAB::slab_cache_t"], ptr %43, i64 0, i64 %44
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  call void @_ZN4SLAB12slab_cache_t6removeEPNS_7chunk_tE(ptr noundef nonnull align 8 dereferenceable(105) %45, ptr noundef %46)
  %47 = load i64, ptr %8, align 8, !tbaa !9
  %48 = getelementptr inbounds %class.ALLOCATOR, ptr %10, i32 0, i32 5
  %49 = load i64, ptr %48, align 8, !tbaa !14
  %50 = sub i64 %49, %47
  store i64 %50, ptr %48, align 8, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %51

51:                                               ; preds = %38, %13
  ret void
}

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 = { 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 = !{!12, !10, i64 8}
!14 = !{!15, !10, i64 40}
!15 = !{!"_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
@.str.11 = external hidden unnamed_addr constant [16 x i8], align 1

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

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN4SLAB12slab_cache_t6removeEPNS_7chunk_tE(ptr noundef nonnull align 8 dereferenceable(105), ptr 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 void @_ZN4SLAB4freeEmm(ptr noundef nonnull align 8 dereferenceable(1057) %0, i64 noundef %1, i64 noundef %2) unnamed_addr #1 align 2 {
  %4 = icmp eq i64 %1, 0
  br i1 %4, label %25, label %5

5:                                                ; preds = %3
  %6 = add i64 %1, -32
  %7 = inttoptr i64 %6 to ptr
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = icmp eq i64 %6, %8
  br i1 %9, label %12, label %10

10:                                               ; preds = %5
  %11 = tail call i32 (ptr, ...) @err(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 399, ptr noundef nonnull @.str.10)
  br label %12

12:                                               ; preds = %5, %10
  %13 = getelementptr inbounds i8, ptr %7, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %.not = icmp eq i64 %14, 0
  br i1 %.not, label %15, label %17

15:                                               ; preds = %12
  %16 = tail call i32 (ptr, ...) @err(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i32 noundef 402, ptr noundef nonnull @.str.11)
  %.pre = load i64, ptr %13, align 8, !tbaa !11
  br label %17

17:                                               ; preds = %12, %15
  %18 = phi i64 [ %14, %12 ], [ %.pre, %15 ]
  %19 = tail call noundef i64 @_ZNK4SLAB7get_idxEm(ptr noundef nonnull align 8 dereferenceable(1057) %0, i64 noundef %18)
  %20 = getelementptr inbounds i8, ptr %0, i64 48
  %21 = getelementptr inbounds [9 x %"class.SLAB::slab_cache_t"], ptr %20, i64 0, i64 %19
  tail call void @_ZN4SLAB12slab_cache_t6removeEPNS_7chunk_tE(ptr noundef nonnull align 8 dereferenceable(105) %21, ptr noundef nonnull %7)
  %22 = getelementptr inbounds i8, ptr %0, i64 40
  %23 = load i64, ptr %22, align 8, !tbaa !12
  %24 = sub i64 %23, %14
  store i64 %24, ptr %22, align 8, !tbaa !12
  br label %25

25:                                               ; preds = %3, %17
  ret void
}

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

!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 = !{!6, !7, i64 8}
!12 = !{!13, !7, i64 40}
!13 = !{!"_ZTS9ALLOCATOR", !10, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}


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