; LLVM IR from xing1357/SimpleOS :: src (pmm)

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

@g_pmm_info = external hidden global %struct.PMM_INFO, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @pmm_mmap_first_free() #0

; 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: nounwind uwtable
define dso_local i32 @pmm_mmap_first_free_by_size(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %88

13:                                               ; preds = %1
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = icmp eq i32 %14, 1
  br i1 %15, label %16, label %18

16:                                               ; preds = %13
  %17 = call i32 @pmm_mmap_first_free()
  store i32 %17, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %88

18:                                               ; preds = %13
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %84, %18
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = load i32, ptr getelementptr inbounds (%struct.PMM_INFO, ptr @g_pmm_info, i32 0, i32 1), align 4, !tbaa !9
  %22 = icmp ult i32 %20, %21
  br i1 %22, label %23, label %87

23:                                               ; preds = %19
  %24 = load ptr, ptr getelementptr inbounds (%struct.PMM_INFO, ptr @g_pmm_info, i32 0, i32 2), align 8, !tbaa !12
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = zext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = icmp ne i32 %28, -1
  br i1 %29, label %30, label %83

30:                                               ; preds = %23
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %79, %30
  %32 = load i32, ptr %5, align 4, !tbaa !5
  %33 = icmp ult i32 %32, 32
  br i1 %33, label %34, label %82

34:                                               ; preds = %31
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = shl i32 1, %35
  store i32 %36, ptr %8, align 4, !tbaa !5
  %37 = load ptr, ptr getelementptr inbounds (%struct.PMM_INFO, ptr @g_pmm_info, i32 0, i32 2), align 8, !tbaa !12
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = zext i32 %38 to i64
  %40 = getelementptr inbounds i32, ptr %37, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = and i32 %41, %42
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %78, label %45

45:                                               ; preds = %34
  %46 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %46, ptr %6, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %74, %45
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = icmp ult i32 %48, 32
  br i1 %49, label %50, label %77

50:                                               ; preds = %47
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = shl i32 1, %51
  store i32 %52, ptr %8, align 4, !tbaa !5
  %53 = load ptr, ptr getelementptr inbounds (%struct.PMM_INFO, ptr @g_pmm_info, i32 0, i32 2), align 8, !tbaa !12
  %54 = load i32, ptr %4, align 4, !tbaa !5
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds i32, ptr %53, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = load i32, ptr %8, align 4, !tbaa !5
  %59 = and i32 %57, %58
  %60 = icmp ne i32 %59, 0
  br i1 %60, label %64, label %61

61:                                               ; preds = %50
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = add i32 %62, 1
  store i32 %63, ptr %7, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %61, %50
  %65 = load i32, ptr %7, align 4, !tbaa !5
  %66 = load i32, ptr %3, align 4, !tbaa !5
  %67 = icmp eq i32 %65, %66
  br i1 %67, label %68, label %73

68:                                               ; preds = %64
  %69 = load i32, ptr %4, align 4, !tbaa !5
  %70 = mul i32 %69, 32
  %71 = load i32, ptr %5, align 4, !tbaa !5
  %72 = add i32 %70, %71
  store i32 %72, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %88

73:                                               ; preds = %64
  br label %74

74:                                               ; preds = %73
  %75 = load i32, ptr %6, align 4, !tbaa !5
  %76 = add i32 %75, 1
  store i32 %76, ptr %6, align 4, !tbaa !5
  br label %47, !llvm.loop !13

77:                                               ; preds = %47
  br label %78

78:                                               ; preds = %77, %34
  br label %79

79:                                               ; preds = %78
  %80 = load i32, ptr %5, align 4, !tbaa !5
  %81 = add i32 %80, 1
  store i32 %81, ptr %5, align 4, !tbaa !5
  br label %31, !llvm.loop !15

82:                                               ; preds = %31
  br label %83

83:                                               ; preds = %82, %23
  br label %84

84:                                               ; preds = %83
  %85 = load i32, ptr %4, align 4, !tbaa !5
  %86 = add i32 %85, 1
  store i32 %86, ptr %4, align 4, !tbaa !5
  br label %19, !llvm.loop !16

87:                                               ; preds = %19
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %88

88:                                               ; preds = %87, %68, %16, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %89 = load i32, ptr %2, align 4
  ret i32 %89
}

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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 4}
!10 = !{!"", !6, i64 0, !6, i64 4, !11, i64 8, !6, i64 16, !6, i64 20}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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

@g_pmm_info = external hidden local_unnamed_addr global %struct.PMM_INFO, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @pmm_mmap_first_free() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @pmm_mmap_first_free_by_size(i32 noundef %0) local_unnamed_addr #0 {
  switch i32 %0, label %.preheader31 [
    i32 0, label %.loopexit32
    i32 1, label %4
  ]

.preheader31:                                     ; preds = %1
  %2 = load i32, ptr getelementptr inbounds (i8, ptr @g_pmm_info, i64 4), align 4, !tbaa !5
  %.not45 = icmp eq i32 %2, 0
  br i1 %.not45, label %.loopexit32, label %.lr.ph44

.lr.ph44:                                         ; preds = %.preheader31
  %3 = load ptr, ptr getelementptr inbounds (i8, ptr @g_pmm_info, i64 8), align 8, !tbaa !11
  %wide.trip.count = zext i32 %2 to i64
  br label %6

4:                                                ; preds = %1
  %5 = tail call i32 @pmm_mmap_first_free()
  br label %.loopexit32

6:                                                ; preds = %.lr.ph44, %.loopexit30
  %indvars.iv = phi i64 [ 0, %.lr.ph44 ], [ %indvars.iv.next, %.loopexit30 ]
  %.043 = phi i32 [ 0, %.lr.ph44 ], [ %.5, %.loopexit30 ]
  %7 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv
  %8 = load i32, ptr %7, align 4, !tbaa !12
  %.not = icmp eq i32 %8, -1
  br i1 %.not, label %.loopexit30, label %.preheader29

.preheader29:                                     ; preds = %6
  %9 = xor i32 %8, -1
  br label %10

10:                                               ; preds = %.preheader29, %.loopexit
  %.141 = phi i32 [ %.043, %.preheader29 ], [ %.4, %.loopexit ]
  %.02339 = phi i32 [ 0, %.preheader29 ], [ %22, %.loopexit ]
  %11 = shl nuw i32 1, %.02339
  %12 = and i32 %11, %8
  %.not27 = icmp eq i32 %12, 0
  br i1 %.not27, label %.lr.ph, label %.loopexit

13:                                               ; preds = %.lr.ph
  %14 = add nuw nsw i32 %.02237, 1
  %exitcond.not = icmp eq i32 %14, 32
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !13

.lr.ph:                                           ; preds = %10, %13
  %.238 = phi i32 [ %spec.select, %13 ], [ %.141, %10 ]
  %.02237 = phi i32 [ %14, %13 ], [ %.02339, %10 ]
  %15 = lshr i32 %9, %.02237
  %16 = and i32 %15, 1
  %spec.select = add i32 %16, %.238
  %17 = icmp eq i32 %spec.select, %0
  br i1 %17, label %18, label %13

18:                                               ; preds = %.lr.ph
  %19 = trunc nuw i64 %indvars.iv to i32
  %20 = shl i32 %19, 5
  %21 = or disjoint i32 %.02339, %20
  br label %.loopexit32

.loopexit:                                        ; preds = %13, %10
  %.4 = phi i32 [ %.141, %10 ], [ %spec.select, %13 ]
  %22 = add nuw nsw i32 %.02339, 1
  %exitcond49.not = icmp eq i32 %22, 32
  br i1 %exitcond49.not, label %.loopexit30, label %10, !llvm.loop !15

.loopexit30:                                      ; preds = %.loopexit, %6
  %.5 = phi i32 [ %.043, %6 ], [ %.4, %.loopexit ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond51.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond51.not, label %.loopexit32, label %6, !llvm.loop !16

.loopexit32:                                      ; preds = %.loopexit30, %.preheader31, %1, %18, %4
  %.025 = phi i32 [ %5, %4 ], [ %21, %18 ], [ -1, %1 ], [ -1, %.preheader31 ], [ -1, %.loopexit30 ]
  ret i32 %.025
}

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

!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 4}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8, !7, i64 16, !7, i64 20}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!7, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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