; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/OvmfPkg/Library (LoadLinuxAllocateKernelPages)

; 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.boot_params = type { %struct.screen_info, [20 x i8], [4 x i8], i64, [16 x i8], [16 x i8], [16 x i8], [16 x i8], [16 x i8], [16 x i8], [128 x i8], [128 x i8], %struct.efi_info, i32, i32, i8, i8, i8, [6 x i8], %struct.setup_header, [40 x i8], [16 x i32], [128 x %struct.e820_entry], [48 x i8], [492 x i8], [276 x i8] }
%struct.screen_info = type <{ i8, i8, i16, i16, i8, i8, i8, i8, i16, i16, i8, i8, i16, i16, i16, i16, i32, i32, i16, i16, i16, i8, i8, i8, i8, i8, i8, i8, i8, i16, i16, i16, i16, i32, [6 x i8] }>
%struct.efi_info = type { i32, i32, i32, i32, i32, i32, i32, i32 }
%struct.setup_header = type <{ i8, i16, i32, i16, i16, i16, i16, i16, i32, i16, i16, i16, i16, i16, i8, i8, i16, i32, i32, i32, i32, i16, i8, i8, i32, i32, i32, i8, i8, i16, i32, i32, i64, i32, i32, i64, i64, i32, i32 }>
%struct.e820_entry = type <{ i64, i64, i32 }>
%struct.EFI_BOOT_SERVICES = type { %struct.EFI_TABLE_HEADER, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.EFI_TABLE_HEADER = type { i64, i32, i32, i32, i32 }

@gBS = 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
declare hidden i64 @BasicKernelSetupCheck(ptr noundef) #1

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

14:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %44

15:                                               ; preds = %2
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %16, ptr %9, align 8, !tbaa !5
  store i32 1, ptr %8, align 4, !tbaa !11
  br label %17

17:                                               ; preds = %40, %15
  %18 = load i32, ptr %8, align 4, !tbaa !11
  %19 = icmp ult i32 %18, 512
  br i1 %19, label %20, label %43

20:                                               ; preds = %17
  %21 = load ptr, ptr %9, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.boot_params, ptr %21, i32 0, i32 19
  %23 = getelementptr inbounds %struct.setup_header, ptr %22, i32 0, i32 26
  %24 = load i32, ptr %23, align 1, !tbaa !13
  %25 = mul i32 2, %24
  %26 = zext i32 %25 to i64
  %27 = load i32, ptr %8, align 4, !tbaa !11
  %28 = call i64 @MultU64x32(i64 noundef %26, i32 noundef %27)
  store i64 %28, ptr %7, align 8, !tbaa !9
  %29 = load ptr, ptr @gBS, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !19
  %32 = load i64, ptr %5, align 8, !tbaa !9
  %33 = call i64 %31(i32 noundef 2, i32 noundef 2, i64 noundef %32, ptr noundef %7)
  store i64 %33, ptr %6, align 8, !tbaa !9
  %34 = load i64, ptr %6, align 8, !tbaa !9
  %35 = icmp slt i64 %34, 0
  br i1 %35, label %39, label %36

36:                                               ; preds = %20
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = inttoptr i64 %37 to ptr
  store ptr %38, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %44

39:                                               ; preds = %20
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %8, align 4, !tbaa !11
  %42 = add i32 %41, 1
  store i32 %42, ptr %8, align 4, !tbaa !11
  br label %17, !llvm.loop !22

43:                                               ; preds = %17
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %44

44:                                               ; preds = %43, %36, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %45 = load ptr, ptr %3, align 8
  ret ptr %45
}

declare i64 @MultU64x32(i64 noundef, i32 noundef) #2

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

!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 long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 560}
!14 = !{!"boot_params", !15, i64 0, !7, i64 64, !7, i64 84, !10, i64 88, !7, i64 96, !7, i64 112, !7, i64 128, !7, i64 144, !7, i64 160, !7, i64 176, !7, i64 192, !7, i64 320, !17, i64 448, !12, i64 480, !12, i64 484, !7, i64 488, !7, i64 489, !7, i64 490, !7, i64 491, !18, i64 497, !7, i64 616, !7, i64 656, !7, i64 720, !7, i64 3280, !7, i64 3328, !7, i64 3820}
!15 = !{!"screen_info", !7, i64 0, !7, i64 1, !16, i64 2, !16, i64 4, !7, i64 6, !7, i64 7, !7, i64 8, !7, i64 9, !16, i64 10, !16, i64 12, !7, i64 14, !7, i64 15, !16, i64 16, !16, i64 18, !16, i64 20, !16, i64 22, !12, i64 24, !12, i64 28, !16, i64 32, !16, i64 34, !16, i64 36, !7, i64 38, !7, i64 39, !7, i64 40, !7, i64 41, !7, i64 42, !7, i64 43, !7, i64 44, !7, i64 45, !16, i64 46, !16, i64 48, !16, i64 50, !16, i64 52, !12, i64 54, !7, i64 58}
!16 = !{!"short", !7, i64 0}
!17 = !{!"efi_info", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28}
!18 = !{!"setup_header", !7, i64 0, !16, i64 1, !12, i64 3, !16, i64 7, !16, i64 9, !16, i64 11, !16, i64 13, !16, i64 15, !12, i64 17, !16, i64 21, !16, i64 23, !16, i64 25, !16, i64 27, !16, i64 29, !7, i64 31, !7, i64 32, !16, i64 33, !12, i64 35, !12, i64 39, !12, i64 43, !12, i64 47, !16, i64 51, !7, i64 53, !7, i64 54, !12, i64 55, !12, i64 59, !12, i64 63, !7, i64 67, !7, i64 68, !16, i64 69, !12, i64 71, !12, i64 75, !10, i64 79, !12, i64 87, !12, i64 91, !10, i64 95, !10, i64 103, !12, i64 111, !12, i64 115}
!19 = !{!20, !6, i64 40}
!20 = !{!"", !21, i64 0, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !6, i64 368}
!21 = !{!"", !10, i64 0, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20}
!22 = distinct !{!22, !23}
!23 = !{!"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"

@gBS = external local_unnamed_addr global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
declare hidden i64 @BasicKernelSetupCheck(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @LoadLinuxAllocateKernelPages(ptr noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %4 = tail call i64 @BasicKernelSetupCheck(ptr noundef %0)
  %5 = icmp slt i64 %4, 0
  br i1 %5, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 560
  br label %9

7:                                                ; preds = %9
  %8 = add nuw nsw i32 %.089, 1
  %exitcond.not = icmp eq i32 %8, 512
  br i1 %exitcond.not, label %.loopexit, label %9, !llvm.loop !5

9:                                                ; preds = %.preheader, %7
  %.089 = phi i32 [ 1, %.preheader ], [ %8, %7 ]
  %10 = load i32, ptr %6, align 1, !tbaa !7
  %11 = shl i32 %10, 1
  %12 = zext i32 %11 to i64
  %13 = call i64 @MultU64x32(i64 noundef %12, i32 noundef %.089) #3
  store i64 %13, ptr %3, align 8, !tbaa !17
  %14 = load ptr, ptr @gBS, align 8, !tbaa !18
  %15 = getelementptr inbounds i8, ptr %14, i64 40
  %16 = load ptr, ptr %15, align 8, !tbaa !20
  %17 = call i64 %16(i32 noundef 2, i32 noundef 2, i64 noundef %1, ptr noundef nonnull %3) #3
  %18 = icmp slt i64 %17, 0
  br i1 %18, label %7, label %19

19:                                               ; preds = %9
  %20 = load i64, ptr %3, align 8, !tbaa !17
  %21 = inttoptr i64 %20 to ptr
  br label %.loopexit

.loopexit:                                        ; preds = %7, %2, %19
  %.0 = phi ptr [ %21, %19 ], [ null, %2 ], [ null, %7 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret ptr %.0
}

declare i64 @MultU64x32(i64 noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { "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 }

!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 560}
!8 = !{!"boot_params", !9, i64 0, !10, i64 64, !10, i64 84, !14, i64 88, !10, i64 96, !10, i64 112, !10, i64 128, !10, i64 144, !10, i64 160, !10, i64 176, !10, i64 192, !10, i64 320, !15, i64 448, !13, i64 480, !13, i64 484, !10, i64 488, !10, i64 489, !10, i64 490, !10, i64 491, !16, i64 497, !10, i64 616, !10, i64 656, !10, i64 720, !10, i64 3280, !10, i64 3328, !10, i64 3820}
!9 = !{!"screen_info", !10, i64 0, !10, i64 1, !12, i64 2, !12, i64 4, !10, i64 6, !10, i64 7, !10, i64 8, !10, i64 9, !12, i64 10, !12, i64 12, !10, i64 14, !10, i64 15, !12, i64 16, !12, i64 18, !12, i64 20, !12, i64 22, !13, i64 24, !13, i64 28, !12, i64 32, !12, i64 34, !12, i64 36, !10, i64 38, !10, i64 39, !10, i64 40, !10, i64 41, !10, i64 42, !10, i64 43, !10, i64 44, !10, i64 45, !12, i64 46, !12, i64 48, !12, i64 50, !12, i64 52, !13, i64 54, !10, i64 58}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!"short", !10, i64 0}
!13 = !{!"int", !10, i64 0}
!14 = !{!"long long", !10, i64 0}
!15 = !{!"efi_info", !13, i64 0, !13, i64 4, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20, !13, i64 24, !13, i64 28}
!16 = !{!"setup_header", !10, i64 0, !12, i64 1, !13, i64 3, !12, i64 7, !12, i64 9, !12, i64 11, !12, i64 13, !12, i64 15, !13, i64 17, !12, i64 21, !12, i64 23, !12, i64 25, !12, i64 27, !12, i64 29, !10, i64 31, !10, i64 32, !12, i64 33, !13, i64 35, !13, i64 39, !13, i64 43, !13, i64 47, !12, i64 51, !10, i64 53, !10, i64 54, !13, i64 55, !13, i64 59, !13, i64 63, !10, i64 67, !10, i64 68, !12, i64 69, !13, i64 71, !13, i64 75, !14, i64 79, !13, i64 87, !13, i64 91, !14, i64 95, !14, i64 103, !13, i64 111, !13, i64 115}
!17 = !{!14, !14, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"any pointer", !10, i64 0}
!20 = !{!21, !19, i64 40}
!21 = !{!"", !22, i64 0, !19, i64 24, !19, i64 32, !19, i64 40, !19, i64 48, !19, i64 56, !19, i64 64, !19, i64 72, !19, i64 80, !19, i64 88, !19, i64 96, !19, i64 104, !19, i64 112, !19, i64 120, !19, i64 128, !19, i64 136, !19, i64 144, !19, i64 152, !19, i64 160, !19, i64 168, !19, i64 176, !19, i64 184, !19, i64 192, !19, i64 200, !19, i64 208, !19, i64 216, !19, i64 224, !19, i64 232, !19, i64 240, !19, i64 248, !19, i64 256, !19, i64 264, !19, i64 272, !19, i64 280, !19, i64 288, !19, i64 296, !19, i64 304, !19, i64 312, !19, i64 320, !19, i64 328, !19, i64 336, !19, i64 344, !19, i64 352, !19, i64 360, !19, i64 368}
!22 = !{!"", !14, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass