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

@cache_caches = external hidden global %struct.kmem_cache, align 8
@caches = external hidden global %struct.list_head, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @kmem_cache_create(ptr noundef %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i64 %1, ptr %8, align 8, !tbaa !9
  store i64 %2, ptr %9, align 8, !tbaa !9
  store i64 %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #5
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = call i64 @size_to_page_order(i64 noundef %17)
  %19 = trunc i64 %18 to i32
  store i32 %19, ptr %13, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %20 = load i32, ptr %13, align 4, !tbaa !11
  %21 = sext i32 %20 to i64
  %22 = call i64 @page_order_to_size(i64 noundef %21)
  %23 = trunc i64 %22 to i32
  store i32 %23, ptr %14, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %24 = load i32, ptr %14, align 4, !tbaa !11
  %25 = sext i32 %24 to i64
  %26 = sub i64 %25, 32
  %27 = load i64, ptr %8, align 8, !tbaa !9
  %28 = udiv i64 %26, %27
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr %15, align 4, !tbaa !11
  br label %30

30:                                               ; preds = %38, %5
  %31 = load i32, ptr %15, align 4, !tbaa !11
  %32 = icmp slt i32 %31, 2
  br i1 %32, label %33, label %36

33:                                               ; preds = %30
  %34 = load i32, ptr %13, align 4, !tbaa !11
  %35 = icmp sle i32 %34, 5
  br label %36

36:                                               ; preds = %33, %30
  %37 = phi i1 [ false, %30 ], [ %35, %33 ]
  br i1 %37, label %38, label %51

38:                                               ; preds = %36
  %39 = load i32, ptr %13, align 4, !tbaa !11
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %13, align 4, !tbaa !11
  %41 = load i32, ptr %13, align 4, !tbaa !11
  %42 = sext i32 %41 to i64
  %43 = call i64 @page_order_to_size(i64 noundef %42)
  %44 = trunc i64 %43 to i32
  store i32 %44, ptr %14, align 4, !tbaa !11
  %45 = load i32, ptr %14, align 4, !tbaa !11
  %46 = sext i32 %45 to i64
  %47 = sub i64 %46, 32
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = udiv i64 %47, %48
  %50 = trunc i64 %49 to i32
  store i32 %50, ptr %15, align 4, !tbaa !11
  br label %30, !llvm.loop !13

51:                                               ; preds = %36
  %52 = load i32, ptr %13, align 4, !tbaa !11
  %53 = icmp sgt i32 %52, 5
  br i1 %53, label %54, label %55

54:                                               ; preds = %51
  store ptr null, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %92

55:                                               ; preds = %51
  %56 = call ptr @kmem_cache_alloc(ptr noundef @cache_caches, i64 noundef 0)
  store ptr %56, ptr %12, align 8, !tbaa !5
  %57 = load ptr, ptr %12, align 8, !tbaa !5
  %58 = icmp ne ptr %57, null
  br i1 %58, label %60, label %59

59:                                               ; preds = %55
  store ptr null, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %92

60:                                               ; preds = %55
  %61 = load i64, ptr %8, align 8, !tbaa !9
  %62 = trunc i64 %61 to i16
  %63 = load ptr, ptr %12, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.kmem_cache, ptr %63, i32 0, i32 4
  store i16 %62, ptr %64, align 8, !tbaa !15
  %65 = load i32, ptr %15, align 4, !tbaa !11
  %66 = trunc i32 %65 to i16
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.kmem_cache, ptr %67, i32 0, i32 5
  store i16 %66, ptr %68, align 2, !tbaa !19
  %69 = load i32, ptr %13, align 4, !tbaa !11
  %70 = trunc i32 %69 to i16
  %71 = load ptr, ptr %12, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.kmem_cache, ptr %71, i32 0, i32 6
  store i16 %70, ptr %72, align 4, !tbaa !20
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.kmem_cache, ptr %73, i32 0, i32 7
  store i32 0, ptr %74, align 8, !tbaa !21
  %75 = load ptr, ptr %12, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.kmem_cache, ptr %75, i32 0, i32 10
  %77 = getelementptr inbounds [16 x i8], ptr %76, i64 0, i64 0
  %78 = load ptr, ptr %7, align 8, !tbaa !5
  %79 = call ptr @strncpy(ptr noundef %77, ptr noundef %78, i64 noundef 15) #5
  %80 = load ptr, ptr %12, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.kmem_cache, ptr %80, i32 0, i32 10
  %82 = getelementptr inbounds [16 x i8], ptr %81, i64 0, i64 15
  store i8 0, ptr %82, align 1, !tbaa !22
  %83 = load ptr, ptr %12, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.kmem_cache, ptr %83, i32 0, i32 0
  call void @INIT_LIST_HEAD(ptr noundef %84)
  %85 = load ptr, ptr %12, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.kmem_cache, ptr %85, i32 0, i32 1
  call void @INIT_LIST_HEAD(ptr noundef %86)
  %87 = load ptr, ptr %12, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.kmem_cache, ptr %87, i32 0, i32 2
  call void @INIT_LIST_HEAD(ptr noundef %88)
  %89 = load ptr, ptr %12, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.kmem_cache, ptr %89, i32 0, i32 3
  call void @list_add(ptr noundef %90, ptr noundef @caches)
  %91 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %91, ptr %6, align 8
  store i32 1, ptr %16, align 4
  br label %92

92:                                               ; preds = %60, %59, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  %93 = load ptr, ptr %6, align 8
  ret ptr %93
}

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

declare i64 @size_to_page_order(i64 noundef) #2

declare i64 @page_order_to_size(i64 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @kmem_cache_alloc(ptr noundef, i64 noundef) #0

; Function Attrs: nounwind
declare ptr @strncpy(ptr noundef, ptr noundef, i64 noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @INIT_LIST_HEAD(ptr noundef) #4

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 "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 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !18, i64 64}
!16 = !{!"kmem_cache", !17, i64 0, !17, i64 16, !17, i64 32, !17, i64 48, !18, i64 64, !18, i64 66, !18, i64 68, !12, i64 72, !12, i64 76, !12, i64 80, !7, i64 84}
!17 = !{!"list_head", !6, i64 0, !6, i64 8}
!18 = !{!"short", !7, i64 0}
!19 = !{!16, !18, i64 66}
!20 = !{!16, !18, i64 68}
!21 = !{!16, !12, i64 72}
!22 = !{!7, !7, 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.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 }

@cache_caches = external hidden global %struct.kmem_cache, align 8
@caches = external hidden global %struct.list_head, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @kmem_cache_create(ptr nocapture noundef readonly %0, i64 noundef %1, i64 noundef %2, i64 noundef %3, ptr nocapture noundef readnone %4) local_unnamed_addr #0 {
  %6 = tail call i64 @size_to_page_order(i64 noundef %1) #4
  %7 = trunc i64 %6 to i32
  %sext = shl i64 %6, 32
  %8 = ashr exact i64 %sext, 32
  %9 = tail call i64 @page_order_to_size(i64 noundef %8) #4
  %.pn.in.in32 = shl i64 %9, 32
  %.pn.in33 = ashr exact i64 %.pn.in.in32, 32
  %.pn34 = add nsw i64 %.pn.in33, -32
  %.0.in35 = udiv i64 %.pn34, %1
  %.036 = trunc i64 %.0.in35 to i32
  %10 = icmp slt i32 %.036, 2
  %11 = icmp slt i32 %7, 6
  %12 = and i1 %11, %10
  br i1 %12, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %5
  %sext41 = shl i64 %6, 32
  %13 = ashr exact i64 %sext41, 32
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %13, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %14 = tail call i64 @page_order_to_size(i64 noundef %indvars.iv.next) #4
  %.pn.in.in = shl i64 %14, 32
  %.pn.in = ashr exact i64 %.pn.in.in, 32
  %.pn = add nsw i64 %.pn.in, -32
  %.0.in = udiv i64 %.pn, %1
  %.0 = trunc i64 %.0.in to i32
  %15 = icmp slt i32 %.0, 2
  %16 = icmp slt i64 %indvars.iv, 5
  %17 = and i1 %16, %15
  br i1 %17, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !5

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %18 = trunc nsw i64 %indvars.iv.next to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %5
  %.027.lcssa = phi i32 [ %7, %5 ], [ %18, %._crit_edge.loopexit ]
  %.0.in.lcssa = phi i64 [ %.0.in35, %5 ], [ %.0.in, %._crit_edge.loopexit ]
  %19 = icmp sgt i32 %.027.lcssa, 5
  br i1 %19, label %36, label %20

20:                                               ; preds = %._crit_edge
  %21 = tail call ptr @kmem_cache_alloc(ptr noundef nonnull @cache_caches, i64 noundef 0)
  %.not = icmp eq ptr %21, null
  br i1 %.not, label %36, label %22

22:                                               ; preds = %20
  %23 = trunc i64 %1 to i16
  %24 = getelementptr inbounds i8, ptr %21, i64 64
  store i16 %23, ptr %24, align 8, !tbaa !7
  %25 = trunc i64 %.0.in.lcssa to i16
  %26 = getelementptr inbounds i8, ptr %21, i64 66
  store i16 %25, ptr %26, align 2, !tbaa !15
  %27 = trunc i32 %.027.lcssa to i16
  %28 = getelementptr inbounds i8, ptr %21, i64 68
  store i16 %27, ptr %28, align 4, !tbaa !16
  %29 = getelementptr inbounds i8, ptr %21, i64 72
  store i32 0, ptr %29, align 8, !tbaa !17
  %30 = getelementptr inbounds i8, ptr %21, i64 84
  %31 = tail call ptr @strncpy(ptr noundef nonnull dereferenceable(1) %30, ptr noundef nonnull dereferenceable(1) %0, i64 noundef 15) #4
  %32 = getelementptr inbounds i8, ptr %21, i64 99
  store i8 0, ptr %32, align 1, !tbaa !18
  tail call void @INIT_LIST_HEAD(ptr noundef nonnull %21)
  %33 = getelementptr inbounds i8, ptr %21, i64 16
  tail call void @INIT_LIST_HEAD(ptr noundef nonnull %33)
  %34 = getelementptr inbounds i8, ptr %21, i64 32
  tail call void @INIT_LIST_HEAD(ptr noundef nonnull %34)
  %35 = getelementptr inbounds i8, ptr %21, i64 48
  tail call void @list_add(ptr noundef nonnull %35, ptr noundef nonnull @caches)
  br label %36

36:                                               ; preds = %20, %._crit_edge, %22
  %.028 = phi ptr [ %21, %22 ], [ null, %._crit_edge ], [ null, %20 ]
  ret ptr %.028
}

declare i64 @size_to_page_order(i64 noundef) local_unnamed_addr #1

declare i64 @page_order_to_size(i64 noundef) local_unnamed_addr #1

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strncpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly, i64 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @INIT_LIST_HEAD(ptr noundef) local_unnamed_addr #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @list_add(ptr noundef, 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 = { "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 nofree nounwind willreturn memory(argmem: readwrite) "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 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !13, i64 64}
!8 = !{!"kmem_cache", !9, i64 0, !9, i64 16, !9, i64 32, !9, i64 48, !13, i64 64, !13, i64 66, !13, i64 68, !14, i64 72, !14, i64 76, !14, i64 80, !11, i64 84}
!9 = !{!"list_head", !10, i64 0, !10, i64 8}
!10 = !{!"any pointer", !11, i64 0}
!11 = !{!"omnipotent char", !12, i64 0}
!12 = !{!"Simple C/C++ TBAA"}
!13 = !{!"short", !11, i64 0}
!14 = !{!"int", !11, i64 0}
!15 = !{!8, !13, i64 66}
!16 = !{!8, !13, i64 68}
!17 = !{!8, !14, i64 72}
!18 = !{!11, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass