; 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
define dso_local i32 @pmm_mmap_first_free() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %46, %0
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = load i32, ptr getelementptr inbounds (%struct.PMM_INFO, ptr @g_pmm_info, i32 0, i32 1), align 4, !tbaa !9
  %9 = icmp ult i32 %7, %8
  br i1 %9, label %10, label %49

10:                                               ; preds = %6
  %11 = load ptr, ptr getelementptr inbounds (%struct.PMM_INFO, ptr @g_pmm_info, i32 0, i32 2), align 8, !tbaa !12
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds i32, ptr %11, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp ne i32 %15, -1
  br i1 %16, label %17, label %45

17:                                               ; preds = %10
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %41, %17
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = icmp ult i32 %19, 32
  br i1 %20, label %21, label %44

21:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = shl i32 1, %22
  store i32 %23, ptr %4, align 4, !tbaa !5
  %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 %2, 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 = load i32, ptr %4, align 4, !tbaa !5
  %30 = and i32 %28, %29
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %37, label %32

32:                                               ; preds = %21
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = mul i32 %33, 32
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = add i32 %34, %35
  store i32 %36, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %38

37:                                               ; preds = %21
  store i32 0, ptr %5, align 4
  br label %38

38:                                               ; preds = %37, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %39 = load i32, ptr %5, align 4
  switch i32 %39, label %50 [
    i32 0, label %40
  ]

40:                                               ; preds = %38
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %3, align 4, !tbaa !5
  %43 = add i32 %42, 1
  store i32 %43, ptr %3, align 4, !tbaa !5
  br label %18, !llvm.loop !13

44:                                               ; preds = %18
  br label %45

45:                                               ; preds = %44, %10
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %2, align 4, !tbaa !5
  %48 = add i32 %47, 1
  store i32 %48, ptr %2, align 4, !tbaa !5
  br label %6, !llvm.loop !15

49:                                               ; preds = %6
  store i32 -1, ptr %1, align 4
  store i32 1, ptr %5, align 4
  br label %50

50:                                               ; preds = %49, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  %51 = load i32, ptr %1, align 4
  ret i32 %51
}

; 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

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}


; 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: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @pmm_mmap_first_free() local_unnamed_addr #0 {
  %1 = load i32, ptr getelementptr inbounds (i8, ptr @g_pmm_info, i64 4), align 4, !tbaa !5
  %.not21 = icmp eq i32 %1, 0
  br i1 %.not21, label %.loopexit17, label %.lr.ph

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

3:                                                ; preds = %.lr.ph, %.loopexit
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %.loopexit ]
  %4 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv
  %5 = load i32, ptr %4, align 4, !tbaa !12
  %.not = icmp eq i32 %5, -1
  br i1 %.not, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %6 = and i32 %5, 1
  %.not15.not = icmp eq i32 %6, 0
  br i1 %.not15.not, label %.loopexit16, label %.preheader.1

.preheader.1:                                     ; preds = %.preheader.preheader
  %7 = and i32 %5, 2
  %.not15.not.1 = icmp eq i32 %7, 0
  br i1 %.not15.not.1, label %.loopexit16, label %.preheader.2

.preheader.2:                                     ; preds = %.preheader.1
  %8 = and i32 %5, 4
  %.not15.not.2 = icmp eq i32 %8, 0
  br i1 %.not15.not.2, label %.loopexit16, label %.preheader.3

.preheader.3:                                     ; preds = %.preheader.2
  %9 = and i32 %5, 8
  %.not15.not.3 = icmp eq i32 %9, 0
  br i1 %.not15.not.3, label %.loopexit16, label %.preheader.4

.preheader.4:                                     ; preds = %.preheader.3
  %10 = and i32 %5, 16
  %.not15.not.4 = icmp eq i32 %10, 0
  br i1 %.not15.not.4, label %.loopexit16, label %.preheader.5

.preheader.5:                                     ; preds = %.preheader.4
  %11 = and i32 %5, 32
  %.not15.not.5 = icmp eq i32 %11, 0
  br i1 %.not15.not.5, label %.loopexit16, label %.preheader.6

.preheader.6:                                     ; preds = %.preheader.5
  %12 = and i32 %5, 64
  %.not15.not.6 = icmp eq i32 %12, 0
  br i1 %.not15.not.6, label %.loopexit16, label %.preheader.7

.preheader.7:                                     ; preds = %.preheader.6
  %13 = and i32 %5, 128
  %.not15.not.7 = icmp eq i32 %13, 0
  br i1 %.not15.not.7, label %.loopexit16, label %.preheader.8

.preheader.8:                                     ; preds = %.preheader.7
  %14 = and i32 %5, 256
  %.not15.not.8 = icmp eq i32 %14, 0
  br i1 %.not15.not.8, label %.loopexit16, label %.preheader.9

.preheader.9:                                     ; preds = %.preheader.8
  %15 = and i32 %5, 512
  %.not15.not.9 = icmp eq i32 %15, 0
  br i1 %.not15.not.9, label %.loopexit16, label %.preheader.10

.preheader.10:                                    ; preds = %.preheader.9
  %16 = and i32 %5, 1024
  %.not15.not.10 = icmp eq i32 %16, 0
  br i1 %.not15.not.10, label %.loopexit16, label %.preheader.11

.preheader.11:                                    ; preds = %.preheader.10
  %17 = and i32 %5, 2048
  %.not15.not.11 = icmp eq i32 %17, 0
  br i1 %.not15.not.11, label %.loopexit16, label %.preheader.12

.preheader.12:                                    ; preds = %.preheader.11
  %18 = and i32 %5, 4096
  %.not15.not.12 = icmp eq i32 %18, 0
  br i1 %.not15.not.12, label %.loopexit16, label %.preheader.13

.preheader.13:                                    ; preds = %.preheader.12
  %19 = and i32 %5, 8192
  %.not15.not.13 = icmp eq i32 %19, 0
  br i1 %.not15.not.13, label %.loopexit16, label %.preheader.14

.preheader.14:                                    ; preds = %.preheader.13
  %20 = and i32 %5, 16384
  %.not15.not.14 = icmp eq i32 %20, 0
  br i1 %.not15.not.14, label %.loopexit16, label %.preheader.15

.preheader.15:                                    ; preds = %.preheader.14
  %21 = and i32 %5, 32768
  %.not15.not.15 = icmp eq i32 %21, 0
  br i1 %.not15.not.15, label %.loopexit16, label %.preheader.16

.preheader.16:                                    ; preds = %.preheader.15
  %22 = and i32 %5, 65536
  %.not15.not.16 = icmp eq i32 %22, 0
  br i1 %.not15.not.16, label %.loopexit16, label %.preheader.17

.preheader.17:                                    ; preds = %.preheader.16
  %23 = and i32 %5, 131072
  %.not15.not.17 = icmp eq i32 %23, 0
  br i1 %.not15.not.17, label %.loopexit16, label %.preheader.18

.preheader.18:                                    ; preds = %.preheader.17
  %24 = and i32 %5, 262144
  %.not15.not.18 = icmp eq i32 %24, 0
  br i1 %.not15.not.18, label %.loopexit16, label %.preheader.19

.preheader.19:                                    ; preds = %.preheader.18
  %25 = and i32 %5, 524288
  %.not15.not.19 = icmp eq i32 %25, 0
  br i1 %.not15.not.19, label %.loopexit16, label %.preheader.20

.preheader.20:                                    ; preds = %.preheader.19
  %26 = and i32 %5, 1048576
  %.not15.not.20 = icmp eq i32 %26, 0
  br i1 %.not15.not.20, label %.loopexit16, label %.preheader.21

.preheader.21:                                    ; preds = %.preheader.20
  %27 = and i32 %5, 2097152
  %.not15.not.21 = icmp eq i32 %27, 0
  br i1 %.not15.not.21, label %.loopexit16, label %.preheader.22

.preheader.22:                                    ; preds = %.preheader.21
  %28 = and i32 %5, 4194304
  %.not15.not.22 = icmp eq i32 %28, 0
  br i1 %.not15.not.22, label %.loopexit16, label %.preheader.23

.preheader.23:                                    ; preds = %.preheader.22
  %29 = and i32 %5, 8388608
  %.not15.not.23 = icmp eq i32 %29, 0
  br i1 %.not15.not.23, label %.loopexit16, label %.preheader.24

.preheader.24:                                    ; preds = %.preheader.23
  %30 = and i32 %5, 16777216
  %.not15.not.24 = icmp eq i32 %30, 0
  br i1 %.not15.not.24, label %.loopexit16, label %.preheader.25

.preheader.25:                                    ; preds = %.preheader.24
  %31 = and i32 %5, 33554432
  %.not15.not.25 = icmp eq i32 %31, 0
  br i1 %.not15.not.25, label %.loopexit16, label %.preheader.26

.preheader.26:                                    ; preds = %.preheader.25
  %32 = and i32 %5, 67108864
  %.not15.not.26 = icmp eq i32 %32, 0
  br i1 %.not15.not.26, label %.loopexit16, label %.preheader.27

.preheader.27:                                    ; preds = %.preheader.26
  %33 = and i32 %5, 134217728
  %.not15.not.27 = icmp eq i32 %33, 0
  br i1 %.not15.not.27, label %.loopexit16, label %.preheader.28

.preheader.28:                                    ; preds = %.preheader.27
  %34 = and i32 %5, 268435456
  %.not15.not.28 = icmp eq i32 %34, 0
  br i1 %.not15.not.28, label %.loopexit16, label %.preheader.29

.preheader.29:                                    ; preds = %.preheader.28
  %35 = and i32 %5, 536870912
  %.not15.not.29 = icmp eq i32 %35, 0
  br i1 %.not15.not.29, label %.loopexit16, label %.preheader.30

.preheader.30:                                    ; preds = %.preheader.29
  %36 = and i32 %5, 1073741824
  %.not15.not.30 = icmp eq i32 %36, 0
  br i1 %.not15.not.30, label %.loopexit16, label %.preheader.31

.preheader.31:                                    ; preds = %.preheader.30
  %.not15.not.31 = icmp sgt i32 %5, -1
  br i1 %.not15.not.31, label %.loopexit16, label %.loopexit

.loopexit:                                        ; preds = %.preheader.31, %3
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit17, label %3, !llvm.loop !13

.loopexit16:                                      ; preds = %.preheader.31, %.preheader.30, %.preheader.29, %.preheader.28, %.preheader.27, %.preheader.26, %.preheader.25, %.preheader.24, %.preheader.23, %.preheader.22, %.preheader.21, %.preheader.20, %.preheader.19, %.preheader.18, %.preheader.17, %.preheader.16, %.preheader.15, %.preheader.14, %.preheader.13, %.preheader.12, %.preheader.11, %.preheader.10, %.preheader.9, %.preheader.8, %.preheader.7, %.preheader.6, %.preheader.5, %.preheader.4, %.preheader.3, %.preheader.2, %.preheader.1, %.preheader.preheader
  %.01319.lcssa = phi i32 [ 0, %.preheader.preheader ], [ 1, %.preheader.1 ], [ 2, %.preheader.2 ], [ 3, %.preheader.3 ], [ 4, %.preheader.4 ], [ 5, %.preheader.5 ], [ 6, %.preheader.6 ], [ 7, %.preheader.7 ], [ 8, %.preheader.8 ], [ 9, %.preheader.9 ], [ 10, %.preheader.10 ], [ 11, %.preheader.11 ], [ 12, %.preheader.12 ], [ 13, %.preheader.13 ], [ 14, %.preheader.14 ], [ 15, %.preheader.15 ], [ 16, %.preheader.16 ], [ 17, %.preheader.17 ], [ 18, %.preheader.18 ], [ 19, %.preheader.19 ], [ 20, %.preheader.20 ], [ 21, %.preheader.21 ], [ 22, %.preheader.22 ], [ 23, %.preheader.23 ], [ 24, %.preheader.24 ], [ 25, %.preheader.25 ], [ 26, %.preheader.26 ], [ 27, %.preheader.27 ], [ 28, %.preheader.28 ], [ 29, %.preheader.29 ], [ 30, %.preheader.30 ], [ 31, %.preheader.31 ]
  %37 = trunc nuw i64 %indvars.iv to i32
  %38 = shl i32 %37, 5
  %39 = or disjoint i32 %.01319.lcssa, %38
  br label %.loopexit17

.loopexit17:                                      ; preds = %.loopexit, %0, %.loopexit16
  %.4 = phi i32 [ %39, %.loopexit16 ], [ -1, %0 ], [ -1, %.loopexit ]
  ret i32 %.4
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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"}


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