; LLVM IR from tenok-rtos/tenok :: kernel/mm (slab)

; 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.kmem_cache = type { %struct.list_head, %struct.list_head, %struct.list_head, %struct.list_head, i16, i16, i16, i32, i32, i32, [16 x i8] }
%struct.list_head = type { ptr, ptr }
%struct.slab = type { [1 x i64], i32, %struct.list_head, [0 x i8] }

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

; Function Attrs: nounwind uwtable
define dso_local ptr @kmem_cache_alloc(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 ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.kmem_cache, ptr %10, i32 0, i32 1
  %12 = call i32 @list_empty(ptr noundef %11)
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %14, label %45

14:                                               ; preds = %2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.kmem_cache, ptr %15, i32 0, i32 0
  %17 = call i32 @list_empty(ptr noundef %16)
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %30

19:                                               ; preds = %14
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.kmem_cache, ptr %20, i32 0, i32 9
  %22 = load i32, ptr %21, align 8, !tbaa !11
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %21, align 8, !tbaa !11
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = call ptr @kmem_cache_grow(ptr noundef %24)
  store ptr %25, ptr %6, align 8, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = icmp ne ptr %26, null
  br i1 %27, label %29, label %28

28:                                               ; preds = %19
  store ptr null, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %96

29:                                               ; preds = %19
  br label %40

30:                                               ; preds = %14
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.kmem_cache, ptr %31, i32 0, i32 8
  %33 = load i32, ptr %32, align 4, !tbaa !16
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %32, align 4, !tbaa !16
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.kmem_cache, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds %struct.list_head, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !17
  %39 = getelementptr inbounds i8, ptr %38, i64 -16
  store ptr %39, ptr %6, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %30, %29
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.slab, ptr %41, i32 0, i32 2
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.kmem_cache, ptr %43, i32 0, i32 1
  call void @list_move(ptr noundef %42, ptr noundef %44)
  br label %55

45:                                               ; preds = %2
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.kmem_cache, ptr %46, i32 0, i32 8
  %48 = load i32, ptr %47, align 4, !tbaa !16
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %47, align 4, !tbaa !16
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.kmem_cache, ptr %50, i32 0, i32 1
  %52 = getelementptr inbounds %struct.list_head, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !18
  %54 = getelementptr inbounds i8, ptr %53, i64 -16
  store ptr %54, ptr %6, align 8, !tbaa !5
  br label %55

55:                                               ; preds = %45, %40
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.slab, ptr %56, i32 0, i32 0
  %58 = getelementptr inbounds [1 x i64], ptr %57, i64 0, i64 0
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.kmem_cache, ptr %59, i32 0, i32 5
  %61 = load i16, ptr %60, align 2, !tbaa !19
  %62 = zext i16 %61 to i64
  %63 = call i64 @find_first_zero_bit(ptr noundef %58, i64 noundef %62)
  %64 = trunc i64 %63 to i32
  store i32 %64, ptr %9, align 4, !tbaa !20
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.slab, ptr %65, i32 0, i32 0
  %67 = getelementptr inbounds [1 x i64], ptr %66, i64 0, i64 0
  %68 = load i32, ptr %9, align 4, !tbaa !20
  %69 = sext i32 %68 to i64
  call void @bitmap_set_bit(ptr noundef %67, i64 noundef %69)
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.slab, ptr %70, i32 0, i32 3
  %72 = getelementptr inbounds [0 x i8], ptr %71, i64 0, i64 0
  %73 = load i32, ptr %9, align 4, !tbaa !20
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.kmem_cache, ptr %74, i32 0, i32 4
  %76 = load i16, ptr %75, align 8, !tbaa !21
  %77 = zext i16 %76 to i32
  %78 = mul nsw i32 %73, %77
  %79 = sext i32 %78 to i64
  %80 = getelementptr inbounds i8, ptr %72, i64 %79
  store ptr %80, ptr %7, align 8, !tbaa !5
  %81 = load ptr, ptr %6, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.slab, ptr %81, i32 0, i32 1
  %83 = load i32, ptr %82, align 8, !tbaa !22
  %84 = add nsw i32 %83, -1
  store i32 %84, ptr %82, align 8, !tbaa !22
  %85 = load ptr, ptr %6, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.slab, ptr %85, i32 0, i32 1
  %87 = load i32, ptr %86, align 8, !tbaa !22
  %88 = icmp ne i32 %87, 0
  br i1 %88, label %94, label %89

89:                                               ; preds = %55
  %90 = load ptr, ptr %6, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.slab, ptr %90, i32 0, i32 2
  %92 = load ptr, ptr %4, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.kmem_cache, ptr %92, i32 0, i32 2
  call void @list_move(ptr noundef %91, ptr noundef %93)
  br label %94

94:                                               ; preds = %89, %55
  %95 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %95, ptr %3, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %96

96:                                               ; preds = %94, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %97 = load ptr, ptr %3, align 8
  ret ptr %97
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @list_empty(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @kmem_cache_grow(ptr noundef) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_move(ptr noundef, ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @find_first_zero_bit(ptr noundef, i64 noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @bitmap_set_bit(ptr noundef, i64 noundef) #2

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 = { inlinehint 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 = { 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, !15, i64 80}
!12 = !{!"kmem_cache", !13, i64 0, !13, i64 16, !13, i64 32, !13, i64 48, !14, i64 64, !14, i64 66, !14, i64 68, !15, i64 72, !15, i64 76, !15, i64 80, !7, i64 84}
!13 = !{!"list_head", !6, i64 0, !6, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!12, !15, i64 76}
!17 = !{!12, !6, i64 0}
!18 = !{!12, !6, i64 16}
!19 = !{!12, !14, i64 66}
!20 = !{!15, !15, i64 0}
!21 = !{!12, !14, i64 64}
!22 = !{!23, !15, i64 8}
!23 = !{!"slab", !7, i64 0, !15, i64 8, !13, i64 16, !7, 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"

; Function Attrs: nounwind uwtable
define dso_local ptr @kmem_cache_alloc(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = tail call i32 @list_empty(ptr noundef nonnull %3)
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %20, label %5

5:                                                ; preds = %2
  %6 = tail call i32 @list_empty(ptr noundef %0)
  %.not25 = icmp eq i32 %6, 0
  br i1 %.not25, label %12, label %7

7:                                                ; preds = %5
  %8 = getelementptr inbounds i8, ptr %0, i64 80
  %9 = load i32, ptr %8, align 8, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr %8, align 8, !tbaa !5
  %11 = tail call ptr @kmem_cache_grow(ptr noundef %0)
  %.not26 = icmp eq ptr %11, null
  br i1 %.not26, label %44, label %18

12:                                               ; preds = %5
  %13 = getelementptr inbounds i8, ptr %0, i64 76
  %14 = load i32, ptr %13, align 4, !tbaa !13
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %13, align 4, !tbaa !13
  %16 = load ptr, ptr %0, align 8, !tbaa !14
  %17 = getelementptr inbounds i8, ptr %16, i64 -16
  br label %18

18:                                               ; preds = %7, %12
  %.023 = phi ptr [ %11, %7 ], [ %17, %12 ]
  %19 = getelementptr inbounds i8, ptr %.023, i64 16
  tail call void @list_move(ptr noundef nonnull %19, ptr noundef nonnull %3)
  br label %26

20:                                               ; preds = %2
  %21 = getelementptr inbounds i8, ptr %0, i64 76
  %22 = load i32, ptr %21, align 4, !tbaa !13
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %21, align 4, !tbaa !13
  %24 = load ptr, ptr %3, align 8, !tbaa !15
  %25 = getelementptr inbounds i8, ptr %24, i64 -16
  br label %26

26:                                               ; preds = %20, %18
  %.1 = phi ptr [ %.023, %18 ], [ %25, %20 ]
  %27 = getelementptr inbounds i8, ptr %0, i64 66
  %28 = load i16, ptr %27, align 2, !tbaa !16
  %29 = zext i16 %28 to i64
  %30 = tail call i64 @find_first_zero_bit(ptr noundef nonnull %.1, i64 noundef %29)
  %sext = shl i64 %30, 32
  %31 = ashr exact i64 %sext, 32
  tail call void @bitmap_set_bit(ptr noundef nonnull %.1, i64 noundef %31)
  %32 = getelementptr inbounds i8, ptr %.1, i64 32
  %33 = getelementptr inbounds i8, ptr %0, i64 64
  %34 = load i16, ptr %33, align 8, !tbaa !17
  %35 = zext i16 %34 to i64
  %sext27 = mul i64 %sext, %35
  %36 = ashr exact i64 %sext27, 32
  %37 = getelementptr inbounds i8, ptr %32, i64 %36
  %38 = getelementptr inbounds i8, ptr %.1, i64 8
  %39 = load i32, ptr %38, align 8, !tbaa !18
  %40 = add nsw i32 %39, -1
  store i32 %40, ptr %38, align 8, !tbaa !18
  %.not28 = icmp eq i32 %40, 0
  br i1 %.not28, label %41, label %44

41:                                               ; preds = %26
  %42 = getelementptr inbounds i8, ptr %.1, i64 16
  %43 = getelementptr inbounds i8, ptr %0, i64 32
  tail call void @list_move(ptr noundef nonnull %42, ptr noundef nonnull %43)
  br label %44

44:                                               ; preds = %26, %41, %7
  %.0 = phi ptr [ null, %7 ], [ %37, %41 ], [ %37, %26 ]
  ret ptr %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @list_empty(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_move(ptr noundef, ptr noundef) local_unnamed_addr #1

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

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

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 = { inlinehint 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, !12, i64 80}
!6 = !{!"kmem_cache", !7, i64 0, !7, i64 16, !7, i64 32, !7, i64 48, !11, i64 64, !11, i64 66, !11, i64 68, !12, i64 72, !12, i64 76, !12, i64 80, !9, i64 84}
!7 = !{!"list_head", !8, i64 0, !8, i64 8}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"short", !9, i64 0}
!12 = !{!"int", !9, i64 0}
!13 = !{!6, !12, i64 76}
!14 = !{!6, !8, i64 0}
!15 = !{!6, !8, i64 16}
!16 = !{!6, !11, i64 66}
!17 = !{!6, !11, i64 64}
!18 = !{!19, !12, i64 8}
!19 = !{!"slab", !9, i64 0, !12, i64 8, !7, i64 16, !9, i64 32}


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