; LLVM IR from asllop/AppOS :: core/mem/arch/pc32 (mem_arch)

; 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.mmap_struct = type <{ i32, i64, i64, i32 }>
%struct.BlockStruct = type { ptr, i64 }

@grub_struct = external global ptr, align 8
@kernel_end = external global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @scan_blocks(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %13 = load ptr, ptr @grub_struct, align 8, !tbaa !5
  %14 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %14, ptr %4, align 4, !tbaa !9
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = and i32 %15, 64
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %82

18:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %19 = load ptr, ptr @grub_struct, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 44
  %21 = load i32, ptr %20, align 4, !tbaa !9
  store i32 %21, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %22 = load ptr, ptr @grub_struct, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i64 48
  store ptr %23, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  store ptr %25, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %26, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %27

27:                                               ; preds = %63, %18
  %28 = load i32, ptr %9, align 4, !tbaa !9
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = icmp ult i32 %28, %29
  br i1 %30, label %31, label %78

31:                                               ; preds = %27
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mmap_struct, ptr %32, i32 0, i32 3
  %34 = load i32, ptr %33, align 1, !tbaa !11
  %35 = icmp eq i32 %34, 1
  br i1 %35, label %36, label %63

36:                                               ; preds = %31
  %37 = load ptr, ptr %8, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mmap_struct, ptr %37, i32 0, i32 1
  %39 = load i64, ptr %38, align 1, !tbaa !14
  %40 = icmp ugt i64 %39, 0
  br i1 %40, label %41, label %62

41:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %42 = load ptr, ptr @kernel_end, align 8, !tbaa !5
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mmap_struct, ptr %43, i32 0, i32 1
  %45 = load i64, ptr %44, align 1, !tbaa !14
  %46 = sub i64 0, %45
  %47 = getelementptr inbounds i8, ptr %42, i64 %46
  %48 = ptrtoint ptr %47 to i32
  store i32 %48, ptr %10, align 4, !tbaa !9
  %49 = load ptr, ptr @kernel_end, align 8, !tbaa !5
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.BlockStruct, ptr %50, i64 0
  %52 = getelementptr inbounds %struct.BlockStruct, ptr %51, i32 0, i32 0
  store ptr %49, ptr %52, align 8, !tbaa !15
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.mmap_struct, ptr %53, i32 0, i32 2
  %55 = load i64, ptr %54, align 1, !tbaa !17
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = zext i32 %56 to i64
  %58 = sub i64 %55, %57
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.BlockStruct, ptr %59, i64 0
  %61 = getelementptr inbounds %struct.BlockStruct, ptr %60, i32 0, i32 1
  store i64 %58, ptr %61, align 8, !tbaa !18
  store i32 1, ptr %2, align 4
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %79

62:                                               ; preds = %36
  br label %63

63:                                               ; preds = %62, %31
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mmap_struct, ptr %64, i32 0, i32 0
  %66 = load i32, ptr %65, align 1, !tbaa !19
  store i32 %66, ptr %12, align 4, !tbaa !9
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = load ptr, ptr %8, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.mmap_struct, ptr %68, i32 0, i32 0
  %70 = load i32, ptr %69, align 1, !tbaa !19
  %71 = zext i32 %70 to i64
  %72 = getelementptr inbounds i8, ptr %67, i64 %71
  %73 = getelementptr inbounds i8, ptr %72, i64 4
  store ptr %73, ptr %8, align 8, !tbaa !5
  %74 = load i32, ptr %12, align 4, !tbaa !9
  %75 = add i32 %74, 4
  %76 = load i32, ptr %9, align 4, !tbaa !9
  %77 = add i32 %76, %75
  store i32 %77, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %27, !llvm.loop !20

78:                                               ; preds = %27
  store i32 0, ptr %11, align 4
  br label %79

79:                                               ; preds = %78, %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  %80 = load i32, ptr %11, align 4
  switch i32 %80, label %83 [
    i32 0, label %81
  ]

81:                                               ; preds = %79
  br label %82

82:                                               ; preds = %81, %1
  store i32 0, ptr %2, align 4
  store i32 1, ptr %11, align 4
  br label %83

83:                                               ; preds = %82, %79
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %84 = load i32, ptr %2, align 4
  ret i32 %84
}

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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 20}
!12 = !{!"mmap_struct", !10, i64 0, !13, i64 4, !13, i64 12, !10, i64 20}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!16, !6, i64 0}
!16 = !{!"BlockStruct", !6, i64 0, !13, i64 8}
!17 = !{!12, !13, i64 12}
!18 = !{!16, !13, i64 8}
!19 = !{!12, !10, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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

@grub_struct = external local_unnamed_addr global ptr, align 8
@kernel_end = external local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local range(i32 0, 2) i32 @scan_blocks(ptr nocapture noundef writeonly %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @grub_struct, align 8, !tbaa !5
  %3 = load i32, ptr %2, align 4, !tbaa !9
  %4 = and i32 %3, 64
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %.critedge, label %5

5:                                                ; preds = %1
  %6 = getelementptr inbounds i8, ptr %2, i64 44
  %7 = load i32, ptr %6, align 4, !tbaa !9
  %.not2224.not = icmp eq i32 %7, 0
  br i1 %.not2224.not, label %.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %5
  %8 = getelementptr inbounds i8, ptr %2, i64 48
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %26
  %.01926 = phi ptr [ %30, %26 ], [ %9, %.lr.ph.preheader ]
  %.02025 = phi i32 [ %32, %26 ], [ 0, %.lr.ph.preheader ]
  %10 = getelementptr inbounds i8, ptr %.01926, i64 20
  %11 = load i32, ptr %10, align 1, !tbaa !11
  %12 = icmp eq i32 %11, 1
  br i1 %12, label %13, label %26

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.01926, i64 4
  %15 = load i64, ptr %14, align 1, !tbaa !14
  %.not21 = icmp eq i64 %15, 0
  br i1 %.not21, label %26, label %16

16:                                               ; preds = %13
  %17 = load ptr, ptr @kernel_end, align 8, !tbaa !5
  %18 = sub i64 0, %15
  %19 = getelementptr inbounds i8, ptr %17, i64 %18
  %20 = ptrtoint ptr %19 to i64
  store ptr %17, ptr %0, align 8, !tbaa !15
  %21 = getelementptr inbounds i8, ptr %.01926, i64 12
  %22 = load i64, ptr %21, align 1, !tbaa !17
  %23 = and i64 %20, 4294967295
  %24 = sub i64 %22, %23
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %24, ptr %25, align 8, !tbaa !18
  br label %.critedge

26:                                               ; preds = %13, %.lr.ph
  %27 = load i32, ptr %.01926, align 1, !tbaa !19
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %.01926, i64 %28
  %30 = getelementptr inbounds i8, ptr %29, i64 4
  %31 = add i32 %.02025, 4
  %32 = add i32 %31, %27
  %.not22 = icmp ult i32 %32, %7
  br i1 %.not22, label %.lr.ph, label %.critedge, !llvm.loop !20

.critedge:                                        ; preds = %26, %5, %1, %16
  %.1 = phi i32 [ 1, %16 ], [ 0, %1 ], [ 0, %5 ], [ 0, %26 ]
  ret i32 %.1
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, 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, !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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 20}
!12 = !{!"mmap_struct", !10, i64 0, !13, i64 4, !13, i64 12, !10, i64 20}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!16, !6, i64 0}
!16 = !{!"BlockStruct", !6, i64 0, !13, i64 8}
!17 = !{!12, !13, i64 12}
!18 = !{!16, !13, i64 8}
!19 = !{!12, !10, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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