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

; 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.ALLOCATOR = type { ptr, ptr, i64, i64, i64, i64 }

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN8FIRSTFIT3setEm(ptr noundef nonnull align 8 dereferenceable(4144), i64 noundef) #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK8FIRSTFIT4testEm(ptr noundef nonnull align 8 dereferenceable(4144), i64 noundef) #0 align 2

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN8FIRSTFIT5allocEmm(ptr noundef nonnull align 8 dereferenceable(4144) %0, i64 noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  %12 = load ptr, ptr %5, align 8
  %13 = load i64, ptr %6, align 8, !tbaa !9
  %14 = getelementptr inbounds %class.ALLOCATOR, ptr %12, i32 0, i32 2
  %15 = load i64, ptr %14, align 8, !tbaa !11
  %16 = icmp ult i64 %13, %15
  br i1 %16, label %26, label %17

17:                                               ; preds = %3
  %18 = load i64, ptr %6, align 8, !tbaa !9
  %19 = getelementptr inbounds %class.ALLOCATOR, ptr %12, i32 0, i32 2
  %20 = load i64, ptr %19, align 8, !tbaa !11
  %21 = getelementptr inbounds %class.ALLOCATOR, ptr %12, i32 0, i32 3
  %22 = load i64, ptr %21, align 8, !tbaa !13
  %23 = mul i64 %22, 4096
  %24 = add i64 %20, %23
  %25 = icmp uge i64 %18, %24
  br i1 %25, label %26, label %27

26:                                               ; preds = %17, %3
  store i1 false, ptr %4, align 1
  br label %77

27:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = getelementptr inbounds %class.ALLOCATOR, ptr %12, i32 0, i32 2
  %30 = load i64, ptr %29, align 8, !tbaa !11
  %31 = sub i64 %28, %30
  %32 = udiv i64 %31, 4096
  store i64 %32, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %33 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %33, ptr %9, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %48, %27
  %35 = load i64, ptr %9, align 8, !tbaa !9
  %36 = load i64, ptr %8, align 8, !tbaa !9
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = add i64 %36, %37
  %39 = icmp ult i64 %35, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %34
  store i32 2, ptr %10, align 4
  br label %51

41:                                               ; preds = %34
  %42 = load i64, ptr %9, align 8, !tbaa !9
  %43 = call noundef zeroext i1 @_ZNK8FIRSTFIT4testEm(ptr noundef nonnull align 8 dereferenceable(4144) %12, i64 noundef %42)
  %44 = zext i1 %43 to i32
  %45 = icmp eq i32 %44, 1
  br i1 %45, label %46, label %47

46:                                               ; preds = %41
  store i1 false, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %51

47:                                               ; preds = %41
  br label %48

48:                                               ; preds = %47
  %49 = load i64, ptr %9, align 8, !tbaa !9
  %50 = add i64 %49, 1
  store i64 %50, ptr %9, align 8, !tbaa !9
  br label %34, !llvm.loop !14

51:                                               ; preds = %46, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  %52 = load i32, ptr %10, align 4
  switch i32 %52, label %76 [
    i32 2, label %53
  ]

53:                                               ; preds = %51
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %54 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %54, ptr %11, align 8, !tbaa !9
  br label %55

55:                                               ; preds = %64, %53
  %56 = load i64, ptr %11, align 8, !tbaa !9
  %57 = load i64, ptr %8, align 8, !tbaa !9
  %58 = load i64, ptr %7, align 8, !tbaa !9
  %59 = add i64 %57, %58
  %60 = icmp ult i64 %56, %59
  br i1 %60, label %62, label %61

61:                                               ; preds = %55
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %67

62:                                               ; preds = %55
  %63 = load i64, ptr %11, align 8, !tbaa !9
  call void @_ZN8FIRSTFIT3setEm(ptr noundef nonnull align 8 dereferenceable(4144) %12, i64 noundef %63)
  br label %64

64:                                               ; preds = %62
  %65 = load i64, ptr %11, align 8, !tbaa !9
  %66 = add i64 %65, 1
  store i64 %66, ptr %11, align 8, !tbaa !9
  br label %55, !llvm.loop !16

67:                                               ; preds = %61
  %68 = load i64, ptr %7, align 8, !tbaa !9
  %69 = getelementptr inbounds %class.ALLOCATOR, ptr %12, i32 0, i32 4
  %70 = load i64, ptr %69, align 8, !tbaa !17
  %71 = sub i64 %70, %68
  store i64 %71, ptr %69, align 8, !tbaa !17
  %72 = load i64, ptr %7, align 8, !tbaa !9
  %73 = getelementptr inbounds %class.ALLOCATOR, ptr %12, i32 0, i32 5
  %74 = load i64, ptr %73, align 8, !tbaa !18
  %75 = add i64 %74, %72
  store i64 %75, ptr %73, align 8, !tbaa !18
  store i1 true, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %76

76:                                               ; preds = %67, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %77

77:                                               ; preds = %76, %26
  %78 = load i1, ptr %4, align 1
  ret i1 %78
}

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"_ZTS9ALLOCATOR", !6, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!13 = !{!12, !10, i64 24}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!12, !10, i64 32}
!18 = !{!12, !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"

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN8FIRSTFIT3setEm(ptr noundef nonnull align 8 dereferenceable(4144), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK8FIRSTFIT4testEm(ptr noundef nonnull align 8 dereferenceable(4144), i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_ZN8FIRSTFIT5allocEmm(ptr noundef nonnull align 8 dereferenceable(4144) %0, i64 noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = icmp ugt i64 %5, %1
  br i1 %6, label %.loopexit, label %7

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  %9 = load i64, ptr %8, align 8, !tbaa !11
  %10 = shl i64 %9, 12
  %11 = add i64 %10, %5
  %.not = icmp ugt i64 %11, %1
  br i1 %.not, label %12, label %.loopexit

12:                                               ; preds = %7
  %13 = sub i64 %1, %5
  %14 = lshr i64 %13, 12
  %15 = add i64 %14, %2
  %.not2426.not = icmp ult i64 %14, %15
  br i1 %.not2426.not, label %.lr.ph, label %.critedge._crit_edge

16:                                               ; preds = %.lr.ph
  %17 = add i64 %.02027, 1
  %exitcond.not = icmp eq i64 %17, %15
  br i1 %exitcond.not, label %.critedge.preheader, label %.lr.ph, !llvm.loop !12

.critedge.preheader:                              ; preds = %16
  br i1 %.not2426.not, label %.critedge, label %.critedge._crit_edge

.lr.ph:                                           ; preds = %12, %16
  %.02027 = phi i64 [ %17, %16 ], [ %14, %12 ]
  %18 = tail call noundef zeroext i1 @_ZNK8FIRSTFIT4testEm(ptr noundef nonnull align 8 dereferenceable(4144) %0, i64 noundef %.02027)
  br i1 %18, label %.loopexit, label %16

.critedge._crit_edge:                             ; preds = %.critedge, %12, %.critedge.preheader
  %19 = getelementptr inbounds i8, ptr %0, i64 32
  %20 = load i64, ptr %19, align 8, !tbaa !14
  %21 = sub i64 %20, %2
  store i64 %21, ptr %19, align 8, !tbaa !14
  %22 = getelementptr inbounds i8, ptr %0, i64 40
  %23 = load i64, ptr %22, align 8, !tbaa !15
  %24 = add i64 %23, %2
  store i64 %24, ptr %22, align 8, !tbaa !15
  br label %.loopexit

.critedge:                                        ; preds = %.critedge.preheader, %.critedge
  %.028 = phi i64 [ %25, %.critedge ], [ %14, %.critedge.preheader ]
  tail call void @_ZN8FIRSTFIT3setEm(ptr noundef nonnull align 8 dereferenceable(4144) %0, i64 noundef %.028)
  %25 = add i64 %.028, 1
  %exitcond31.not = icmp eq i64 %25, %15
  br i1 %exitcond31.not, label %.critedge._crit_edge, label %.critedge, !llvm.loop !16

.loopexit:                                        ; preds = %.lr.ph, %.critedge._crit_edge, %3, %7
  %.2 = phi i1 [ false, %7 ], [ false, %3 ], [ true, %.critedge._crit_edge ], [ false, %.lr.ph ]
  ret i1 %.2
}

attributes #0 = { 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, !10, i64 16}
!6 = !{!"_ZTS9ALLOCATOR", !7, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 24}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !10, i64 32}
!15 = !{!6, !10, i64 40}
!16 = distinct !{!16, !13}


; Effective O3 passes: ConstraintEliminationPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass