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

; 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.QEMU_VIDEO_PRIVATE_DATA = type { i64, ptr, ptr, i64, %struct._EFI_GRAPHICS_OUTPUT_PROTOCOL, ptr, i64, i64, ptr, ptr, i32 }
%struct._EFI_GRAPHICS_OUTPUT_PROTOCOL = type { ptr, ptr, ptr, ptr }
%struct.EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE = type { i32, i32, ptr, i64, i64, i64 }
%struct.QEMU_VIDEO_MODE_DATA = type { i32, i32, i32, i32, i32 }
%struct.EFI_GRAPHICS_OUTPUT_MODE_INFORMATION = type { i32, i32, i32, i32, %struct.EFI_PIXEL_BITMASK, i32 }
%struct.EFI_PIXEL_BITMASK = type { i32, i32, i32, i32 }

@.str = external hidden unnamed_addr constant [128 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i64 @QemuVideoGraphicsOutputQueryMode(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %13 = call zeroext i8 @DebugAssertEnabled()
  %14 = zext i8 %13 to i32
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %26

16:                                               ; preds = %4
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = ptrtoint ptr %17 to i64
  %19 = sub i64 %18, ptrtoint (ptr getelementptr inbounds (%struct.QEMU_VIDEO_PRIVATE_DATA, ptr null, i32 0, i32 4) to i64)
  %20 = inttoptr i64 %19 to ptr
  %21 = getelementptr inbounds %struct.QEMU_VIDEO_PRIVATE_DATA, ptr %20, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = icmp ne i64 %22, 1145656913
  br i1 %23, label %24, label %26

24:                                               ; preds = %16
  call void @DebugAssert(ptr noundef @.str, i64 noundef 115, ptr noundef @.str.1)
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  br label %31

26:                                               ; preds = %16, %4
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = ptrtoint ptr %27 to i64
  %29 = sub i64 %28, ptrtoint (ptr getelementptr inbounds (%struct.QEMU_VIDEO_PRIVATE_DATA, ptr null, i32 0, i32 4) to i64)
  %30 = inttoptr i64 %29 to ptr
  br label %31

31:                                               ; preds = %26, %24
  %32 = phi ptr [ %25, %24 ], [ %30, %26 ]
  store ptr %32, ptr %10, align 8, !tbaa !5
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = icmp eq ptr %33, null
  br i1 %34, label %46, label %35

35:                                               ; preds = %31
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = icmp eq ptr %36, null
  br i1 %37, label %46, label %38

38:                                               ; preds = %35
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct._EFI_GRAPHICS_OUTPUT_PROTOCOL, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = getelementptr inbounds %struct.EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 8, !tbaa !16
  %45 = icmp uge i32 %39, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %38, %35, %31
  store i64 -9223372036854775806, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %77

47:                                               ; preds = %38
  %48 = call ptr @AllocatePool(i64 noundef 36)
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %48, ptr %49, align 8, !tbaa !5
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  %51 = load ptr, ptr %50, align 8, !tbaa !5
  %52 = icmp eq ptr %51, null
  br i1 %52, label %53, label %54

53:                                               ; preds = %47
  store i64 -9223372036854775799, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %77

54:                                               ; preds = %47
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 36, ptr %55, align 8, !tbaa !18
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.QEMU_VIDEO_PRIVATE_DATA, ptr %56, i32 0, i32 8
  %58 = load ptr, ptr %57, align 8, !tbaa !19
  %59 = load i32, ptr %7, align 4, !tbaa !9
  %60 = zext i32 %59 to i64
  %61 = getelementptr inbounds %struct.QEMU_VIDEO_MODE_DATA, ptr %58, i64 %60
  store ptr %61, ptr %11, align 8, !tbaa !5
  %62 = load ptr, ptr %11, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.QEMU_VIDEO_MODE_DATA, ptr %62, i32 0, i32 1
  %64 = load i32, ptr %63, align 4, !tbaa !20
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = load ptr, ptr %65, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.EFI_GRAPHICS_OUTPUT_MODE_INFORMATION, ptr %66, i32 0, i32 1
  store i32 %64, ptr %67, align 4, !tbaa !22
  %68 = load ptr, ptr %11, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.QEMU_VIDEO_MODE_DATA, ptr %68, i32 0, i32 2
  %70 = load i32, ptr %69, align 4, !tbaa !25
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = load ptr, ptr %71, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.EFI_GRAPHICS_OUTPUT_MODE_INFORMATION, ptr %72, i32 0, i32 2
  store i32 %70, ptr %73, align 4, !tbaa !26
  %74 = load ptr, ptr %11, align 8, !tbaa !5
  %75 = load ptr, ptr %9, align 8, !tbaa !5
  %76 = load ptr, ptr %75, align 8, !tbaa !5
  call void @QemuVideoCompleteModeInfo(ptr noundef %74, ptr noundef %76)
  store i64 0, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %77

77:                                               ; preds = %54, %53, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %78 = load i64, ptr %5, align 8
  ret i64 %78
}

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

declare zeroext i8 @DebugAssertEnabled() #2

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) #2

declare ptr @AllocatePool(i64 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @QemuVideoCompleteModeInfo(ptr noundef, ptr noundef) #0

; 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !6, i64 8, !6, i64 16, !13, i64 24, !14, i64 32, !6, i64 64, !13, i64 72, !13, i64 80, !6, i64 88, !6, i64 96, !10, i64 104}
!13 = !{!"long long", !7, i64 0}
!14 = !{!"_EFI_GRAPHICS_OUTPUT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!14, !6, i64 24}
!16 = !{!17, !10, i64 0}
!17 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8, !13, i64 16, !13, i64 24, !13, i64 32}
!18 = !{!13, !13, i64 0}
!19 = !{!12, !6, i64 88}
!20 = !{!21, !10, i64 4}
!21 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16}
!22 = !{!23, !10, i64 4}
!23 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !24, i64 16, !10, i64 32}
!24 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12}
!25 = !{!21, !10, i64 8}
!26 = !{!23, !10, i64 8}


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

@.str = external hidden unnamed_addr constant [128 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775806, 1) i64 @QemuVideoGraphicsOutputQueryMode(ptr noundef %0, i32 noundef %1, ptr noundef writeonly %2, ptr noundef writeonly %3) local_unnamed_addr #0 {
  %5 = tail call zeroext i8 @DebugAssertEnabled() #2
  %.not = icmp eq i8 %5, 0
  %.pre = ptrtoint ptr %0 to i64
  %.pre24 = add i64 %.pre, -32
  %.pre26 = inttoptr i64 %.pre24 to ptr
  br i1 %.not, label %._crit_edge, label %6

6:                                                ; preds = %4
  %7 = load i64, ptr %.pre26, align 8, !tbaa !5
  %.not22 = icmp eq i64 %7, 1145656913
  br i1 %.not22, label %._crit_edge, label %8

8:                                                ; preds = %6
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 115, ptr noundef nonnull @.str.1) #2
  br label %._crit_edge

._crit_edge:                                      ; preds = %4, %6, %8
  %9 = phi ptr [ %0, %8 ], [ %.pre26, %6 ], [ %.pre26, %4 ]
  %10 = icmp eq ptr %3, null
  %11 = icmp eq ptr %2, null
  %or.cond = or i1 %11, %10
  br i1 %or.cond, label %27, label %12

12:                                               ; preds = %._crit_edge
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = load i32, ptr %14, align 8, !tbaa !14
  %.not23 = icmp ugt i32 %15, %1
  br i1 %.not23, label %16, label %27

16:                                               ; preds = %12
  %17 = tail call ptr @AllocatePool(i64 noundef 36) #2
  store ptr %17, ptr %3, align 8, !tbaa !16
  %18 = icmp eq ptr %17, null
  br i1 %18, label %27, label %19

19:                                               ; preds = %16
  store i64 36, ptr %2, align 8, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %9, i64 88
  %21 = load ptr, ptr %20, align 8, !tbaa !18
  %22 = zext i32 %1 to i64
  %23 = getelementptr inbounds %struct.QEMU_VIDEO_MODE_DATA, ptr %21, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 4
  %25 = getelementptr inbounds i8, ptr %17, i64 4
  %26 = load <2 x i32>, ptr %24, align 4, !tbaa !19
  store <2 x i32> %26, ptr %25, align 4, !tbaa !19
  tail call void @QemuVideoCompleteModeInfo(ptr noundef %23, ptr noundef nonnull %17)
  br label %27

27:                                               ; preds = %16, %._crit_edge, %12, %19
  %.0 = phi i64 [ 0, %19 ], [ -9223372036854775806, %12 ], [ -9223372036854775806, %._crit_edge ], [ -9223372036854775799, %16 ]
  ret i64 %.0
}

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #1

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

declare ptr @AllocatePool(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @QemuVideoCompleteModeInfo(ptr noundef, ptr noundef) local_unnamed_addr #0

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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !11, i64 32, !10, i64 64, !7, i64 72, !7, i64 80, !10, i64 88, !10, i64 96, !12, i64 104}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_EFI_GRAPHICS_OUTPUT_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!12 = !{!"int", !8, i64 0}
!13 = !{!11, !10, i64 24}
!14 = !{!15, !12, i64 0}
!15 = !{!"", !12, i64 0, !12, i64 4, !10, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!16 = !{!10, !10, i64 0}
!17 = !{!7, !7, i64 0}
!18 = !{!6, !10, i64 88}
!19 = !{!12, !12, i64 0}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass