; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/OptionRomPkg (CirrusLogic5430GraphicsOutputQueryMode)

; 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.CIRRUS_LOGIC_5430_PRIVATE_DATA = type { i64, ptr, ptr, i64, %struct._EFI_UGA_DRAW_PROTOCOL, %struct._EFI_GRAPHICS_OUTPUT_PROTOCOL, %struct.EFI_EDID_DISCOVERED_PROTOCOL, %struct.EFI_EDID_ACTIVE_PROTOCOL, ptr, ptr, i64, i64, [3 x %struct.CIRRUS_LOGIC_5430_MODE_DATA], ptr, i8 }
%struct._EFI_UGA_DRAW_PROTOCOL = type { ptr, ptr, ptr }
%struct._EFI_GRAPHICS_OUTPUT_PROTOCOL = type { ptr, ptr, ptr, ptr }
%struct.EFI_EDID_DISCOVERED_PROTOCOL = type { i32, ptr }
%struct.EFI_EDID_ACTIVE_PROTOCOL = type { i32, ptr }
%struct.CIRRUS_LOGIC_5430_MODE_DATA = type { i32, i32, i32, i32, i32 }
%struct.EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE = type { i32, i32, ptr, i64, i64, i64 }
%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 [165 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i64 @CirrusLogic5430GraphicsOutputQueryMode(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 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
  %12 = call zeroext i8 @DebugAssertEnabled()
  %13 = zext i8 %12 to i32
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %25

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

23:                                               ; preds = %15
  call void @DebugAssert(ptr noundef @.str, i64 noundef 103, ptr noundef @.str.1)
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  br label %30

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

30:                                               ; preds = %25, %23
  %31 = phi ptr [ %24, %23 ], [ %29, %25 ]
  store ptr %31, ptr %10, align 8, !tbaa !5
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.CIRRUS_LOGIC_5430_PRIVATE_DATA, ptr %32, i32 0, i32 14
  %34 = load i8, ptr %33, align 8, !tbaa !17
  %35 = icmp ne i8 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %30
  store i64 -9223372036854775789, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %83

37:                                               ; preds = %30
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = icmp eq ptr %38, null
  br i1 %39, label %51, label %40

40:                                               ; preds = %37
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = icmp eq ptr %41, null
  br i1 %42, label %51, label %43

43:                                               ; preds = %40
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct._EFI_GRAPHICS_OUTPUT_PROTOCOL, ptr %45, i32 0, i32 3
  %47 = load ptr, ptr %46, align 8, !tbaa !18
  %48 = getelementptr inbounds %struct.EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !19
  %50 = icmp uge i32 %44, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %43, %40, %37
  store i64 -9223372036854775806, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %83

52:                                               ; preds = %43
  %53 = call ptr @AllocatePool(i64 noundef 36)
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %53, ptr %54, align 8, !tbaa !5
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = load ptr, ptr %55, align 8, !tbaa !5
  %57 = icmp eq ptr %56, null
  br i1 %57, label %58, label %59

58:                                               ; preds = %52
  store i64 -9223372036854775799, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %83

59:                                               ; preds = %52
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  store i64 36, ptr %60, align 8, !tbaa !21
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.CIRRUS_LOGIC_5430_PRIVATE_DATA, ptr %61, i32 0, i32 12
  %63 = load i32, ptr %7, align 4, !tbaa !9
  %64 = zext i32 %63 to i64
  %65 = getelementptr inbounds [3 x %struct.CIRRUS_LOGIC_5430_MODE_DATA], ptr %62, i64 0, i64 %64
  %66 = getelementptr inbounds %struct.CIRRUS_LOGIC_5430_MODE_DATA, ptr %65, i32 0, i32 1
  %67 = load i32, ptr %66, align 4, !tbaa !22
  %68 = load ptr, ptr %9, align 8, !tbaa !5
  %69 = load ptr, ptr %68, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.EFI_GRAPHICS_OUTPUT_MODE_INFORMATION, ptr %69, i32 0, i32 1
  store i32 %67, ptr %70, align 4, !tbaa !24
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.CIRRUS_LOGIC_5430_PRIVATE_DATA, ptr %71, i32 0, i32 12
  %73 = load i32, ptr %7, align 4, !tbaa !9
  %74 = zext i32 %73 to i64
  %75 = getelementptr inbounds [3 x %struct.CIRRUS_LOGIC_5430_MODE_DATA], ptr %72, i64 0, i64 %74
  %76 = getelementptr inbounds %struct.CIRRUS_LOGIC_5430_MODE_DATA, ptr %75, i32 0, i32 2
  %77 = load i32, ptr %76, align 4, !tbaa !27
  %78 = load ptr, ptr %9, align 8, !tbaa !5
  %79 = load ptr, ptr %78, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.EFI_GRAPHICS_OUTPUT_MODE_INFORMATION, ptr %79, i32 0, i32 2
  store i32 %77, ptr %80, align 4, !tbaa !28
  %81 = load ptr, ptr %9, align 8, !tbaa !5
  %82 = load ptr, ptr %81, align 8, !tbaa !5
  call void @CirrusLogic5430CompleteModeInfo(ptr noundef %82)
  store i64 0, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %83

83:                                               ; preds = %59, %58, %51, %36
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %84 = load i64, ptr %5, align 8
  ret i64 %84
}

; 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 @CirrusLogic5430CompleteModeInfo(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, !15, i64 56, !16, i64 88, !16, i64 104, !6, i64 120, !6, i64 128, !13, i64 136, !13, i64 144, !7, i64 152, !6, i64 216, !7, i64 224}
!13 = !{!"long long", !7, i64 0}
!14 = !{!"_EFI_UGA_DRAW_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!"_EFI_GRAPHICS_OUTPUT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!16 = !{!"", !10, i64 0, !6, i64 8}
!17 = !{!12, !7, i64 224}
!18 = !{!15, !6, i64 24}
!19 = !{!20, !10, i64 0}
!20 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8, !13, i64 16, !13, i64 24, !13, i64 32}
!21 = !{!13, !13, i64 0}
!22 = !{!23, !10, i64 4}
!23 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16}
!24 = !{!25, !10, i64 4}
!25 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !26, i64 16, !10, i64 32}
!26 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12}
!27 = !{!23, !10, i64 8}
!28 = !{!25, !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.CIRRUS_LOGIC_5430_MODE_DATA = type { i32, i32, i32, i32, i32 }

@.str = external hidden unnamed_addr constant [165 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 @CirrusLogic5430GraphicsOutputQueryMode(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
  %.pre25 = add i64 %.pre, -56
  %.pre27 = inttoptr i64 %.pre25 to ptr
  br i1 %.not, label %._crit_edge, label %6

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

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

._crit_edge:                                      ; preds = %4, %6, %8
  %9 = phi ptr [ %0, %8 ], [ %.pre27, %6 ], [ %.pre27, %4 ]
  %10 = getelementptr inbounds i8, ptr %9, i64 224
  %11 = load i8, ptr %10, align 8, !tbaa !15
  %.not23 = icmp eq i8 %11, 0
  br i1 %.not23, label %12, label %28

12:                                               ; preds = %._crit_edge
  %13 = icmp eq ptr %3, null
  %14 = icmp eq ptr %2, null
  %or.cond = or i1 %14, %13
  br i1 %or.cond, label %28, label %15

15:                                               ; preds = %12
  %16 = getelementptr inbounds i8, ptr %0, i64 24
  %17 = load ptr, ptr %16, align 8, !tbaa !16
  %18 = load i32, ptr %17, align 8, !tbaa !17
  %.not24 = icmp ugt i32 %18, %1
  br i1 %.not24, label %19, label %28

19:                                               ; preds = %15
  %20 = tail call ptr @AllocatePool(i64 noundef 36) #2
  store ptr %20, ptr %3, align 8, !tbaa !19
  %21 = icmp eq ptr %20, null
  br i1 %21, label %28, label %22

22:                                               ; preds = %19
  store i64 36, ptr %2, align 8, !tbaa !20
  %23 = getelementptr inbounds i8, ptr %9, i64 152
  %24 = zext i32 %1 to i64
  %25 = getelementptr inbounds [3 x %struct.CIRRUS_LOGIC_5430_MODE_DATA], ptr %23, i64 0, i64 %24, i32 1
  %26 = getelementptr inbounds i8, ptr %20, i64 4
  %27 = load <2 x i32>, ptr %25, align 4, !tbaa !21
  store <2 x i32> %27, ptr %26, align 4, !tbaa !21
  tail call void @CirrusLogic5430CompleteModeInfo(ptr noundef nonnull %20)
  br label %28

28:                                               ; preds = %19, %12, %15, %._crit_edge, %22
  %.0 = phi i64 [ 0, %22 ], [ -9223372036854775789, %._crit_edge ], [ -9223372036854775806, %15 ], [ -9223372036854775806, %12 ], [ -9223372036854775799, %19 ]
  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 @CirrusLogic5430CompleteModeInfo(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, !12, i64 56, !13, i64 88, !13, i64 104, !10, i64 120, !10, i64 128, !7, i64 136, !7, i64 144, !8, i64 152, !10, i64 216, !8, i64 224}
!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_UGA_DRAW_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16}
!12 = !{!"_EFI_GRAPHICS_OUTPUT_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!"", !14, i64 0, !10, i64 8}
!14 = !{!"int", !8, i64 0}
!15 = !{!6, !8, i64 224}
!16 = !{!12, !10, i64 24}
!17 = !{!18, !14, i64 0}
!18 = !{!"", !14, i64 0, !14, i64 4, !10, i64 8, !7, i64 16, !7, i64 24, !7, i64 32}
!19 = !{!10, !10, i64 0}
!20 = !{!7, !7, i64 0}
!21 = !{!14, !14, i64 0}


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