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

; 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.HEFI_EDITOR_MEM_IMAGE = type { ptr, i64, i64 }
%struct.GUID = type { i32, i16, i16, [8 x i8] }
%struct._EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL = type { ptr, ptr, ptr, %struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS, %struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS, %struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32 }
%struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS = type { ptr, ptr }
%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 }

@HMemImageConst = external dso_local global %struct.HEFI_EDITOR_MEM_IMAGE, align 8
@HMemImage = external dso_local global %struct.HEFI_EDITOR_MEM_IMAGE, align 8
@gBS = external global ptr, align 8
@gEfiPciRootBridgeIoProtocolGuid = external global %struct.GUID, align 4
@DummyPciRootBridgeIo = external dso_local global %struct._EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, align 8

; Function Attrs: nounwind uwtable
declare dso_local i64 @DummyMemRead(ptr noundef, i32 noundef, i64 noundef, i64 noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @DummyMemWrite(ptr noundef, i32 noundef, i64 noundef, i64 noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @HMemImageInit() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %4 = call ptr @CopyMem(ptr noundef @HMemImage, ptr noundef @HMemImageConst, i64 noundef 24)
  %5 = load ptr, ptr @gBS, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %5, i32 0, i32 38
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = call i64 %7(ptr noundef @gEfiPciRootBridgeIoProtocolGuid, ptr noundef null, ptr noundef @HMemImage)
  store i64 %8, ptr %2, align 8, !tbaa !14
  %9 = load i64, ptr %2, align 8, !tbaa !14
  %10 = icmp eq i64 %9, -9223372036854775794
  br i1 %10, label %11, label %13

11:                                               ; preds = %0
  %12 = call ptr @ZeroMem(ptr noundef @DummyPciRootBridgeIo, i64 noundef 152)
  store ptr @DummyMemRead, ptr getelementptr inbounds (%struct._EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, ptr @DummyPciRootBridgeIo, i32 0, i32 3), align 8, !tbaa !15
  store ptr @DummyMemWrite, ptr getelementptr inbounds (%struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS, ptr getelementptr inbounds (%struct._EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, ptr @DummyPciRootBridgeIo, i32 0, i32 3), i32 0, i32 1), align 8, !tbaa !18
  store ptr @DummyPciRootBridgeIo, ptr @HMemImage, align 8, !tbaa !19
  store i64 0, ptr %2, align 8, !tbaa !14
  br label %13

13:                                               ; preds = %11, %0
  %14 = load i64, ptr %2, align 8, !tbaa !14
  %15 = icmp slt i64 %14, 0
  br i1 %15, label %17, label %16

16:                                               ; preds = %13
  store i64 0, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %18

17:                                               ; preds = %13
  store i64 -9223372036854775807, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %18

18:                                               ; preds = %17, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  %19 = load i64, ptr %1, align 8
  ret i64 %19
}

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

declare ptr @CopyMem(ptr noundef, ptr noundef, i64 noundef) #2

declare ptr @ZeroMem(ptr noundef, i64 noundef) #2

; 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 = { "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, !6, i64 320}
!10 = !{!"", !11, 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}
!11 = !{!"", !12, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !12, i64 0}
!15 = !{!16, !6, i64 24}
!16 = !{!"_EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !17, i64 24, !17, i64 40, !17, i64 56, !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, !13, i64 144}
!17 = !{!"", !6, i64 0, !6, i64 8}
!18 = !{!16, !6, i64 32}
!19 = !{!20, !6, i64 0}
!20 = !{!"", !6, i64 0, !12, i64 8, !12, i64 16}


; 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.HEFI_EDITOR_MEM_IMAGE = type { ptr, i64, i64 }
%struct.GUID = type { i32, i16, i16, [8 x i8] }
%struct._EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL = type { ptr, ptr, ptr, %struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS, %struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS, %struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32 }
%struct.EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_ACCESS = type { ptr, ptr }

@HMemImageConst = external dso_local global %struct.HEFI_EDITOR_MEM_IMAGE, align 8
@HMemImage = external dso_local global %struct.HEFI_EDITOR_MEM_IMAGE, align 8
@gBS = external local_unnamed_addr global ptr, align 8
@gEfiPciRootBridgeIoProtocolGuid = external global %struct.GUID, align 4
@DummyPciRootBridgeIo = external dso_local global %struct._EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, align 8

; Function Attrs: nounwind uwtable
declare dso_local i64 @DummyMemRead(ptr noundef, i32 noundef, i64 noundef, i64 noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @DummyMemWrite(ptr noundef, i32 noundef, i64 noundef, i64 noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775807, 1) i64 @HMemImageInit() local_unnamed_addr #0 {
  %1 = tail call ptr @CopyMem(ptr noundef nonnull @HMemImage, ptr noundef nonnull @HMemImageConst, i64 noundef 24) #2
  %2 = load ptr, ptr @gBS, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 320
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  %5 = tail call i64 %4(ptr noundef nonnull @gEfiPciRootBridgeIoProtocolGuid, ptr noundef null, ptr noundef nonnull @HMemImage) #2
  %.fr = freeze i64 %5
  %6 = icmp eq i64 %.fr, -9223372036854775794
  br i1 %6, label %.thread, label %8

.thread:                                          ; preds = %0
  %7 = tail call ptr @ZeroMem(ptr noundef nonnull @DummyPciRootBridgeIo, i64 noundef 152) #2
  store ptr @DummyMemRead, ptr getelementptr inbounds (i8, ptr @DummyPciRootBridgeIo, i64 24), align 8, !tbaa !14
  store ptr @DummyMemWrite, ptr getelementptr inbounds (i8, ptr @DummyPciRootBridgeIo, i64 32), align 8, !tbaa !17
  store ptr @DummyPciRootBridgeIo, ptr @HMemImage, align 8, !tbaa !18
  br label %10

8:                                                ; preds = %0
  %9 = icmp slt i64 %.fr, 0
  %spec.select = select i1 %9, i64 -9223372036854775807, i64 0
  br label %10

10:                                               ; preds = %8, %.thread
  %11 = phi i64 [ 0, %.thread ], [ %spec.select, %8 ]
  ret i64 %11
}

declare ptr @CopyMem(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare ptr @ZeroMem(ptr noundef, i64 noundef) local_unnamed_addr #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 = { "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, !6, i64 320}
!10 = !{!"", !11, 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}
!11 = !{!"", !12, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 24}
!15 = !{!"_EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !16, i64 24, !16, i64 40, !16, i64 56, !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, !13, i64 144}
!16 = !{!"", !6, i64 0, !6, i64 8}
!17 = !{!15, !6, i64 32}
!18 = !{!19, !6, i64 0}
!19 = !{!"", !6, i64 0, !12, i64 8, !12, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass