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

; 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.SHELL_INFO = type { ptr, ptr, i8, i8, %struct.SHELL_INIT_SETTINGS, %struct.BUFFER_LIST, %struct.SHELL_VIEWING_SETTINGS, ptr, i64, ptr, ptr, ptr, ptr, ptr, %struct.SHELL_PROTOCOL_HANDLE_LIST, %struct.SPLIT_LIST, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i8 }
%struct.SHELL_INIT_SETTINGS = type { %union.SHELL_BIT_UNION, i64, ptr, ptr }
%union.SHELL_BIT_UNION = type { %struct.SHELL_BITS }
%struct.SHELL_BITS = type { i16, [2 x i8] }
%struct.BUFFER_LIST = type { %struct._LIST_ENTRY, ptr }
%struct._LIST_ENTRY = type { ptr, ptr }
%struct.SHELL_VIEWING_SETTINGS = type { %struct.BUFFER_LIST, i64, i64, i8 }
%struct.SHELL_PROTOCOL_HANDLE_LIST = type { %struct._LIST_ENTRY, ptr, ptr }
%struct.SPLIT_LIST = type { %struct._LIST_ENTRY, ptr, ptr }
%struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL = type { %struct._EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr, ptr, i64 }
%struct._EFI_SIMPLE_TEXT_INPUT_PROTOCOL = type { ptr, ptr, ptr }
%struct.EFI_INPUT_KEY = type { i16, i16 }
%struct._EFI_SHELL_PROTOCOL = type { 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, i32, i32, ptr, ptr, ptr, ptr }

@ShellInfoObject = external global %struct.SHELL_INFO, align 8

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

10:                                               ; preds = %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %10, %2
  store i64 -9223372036854775806, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %47

14:                                               ; preds = %10
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %15, i32 0, i32 3
  %17 = load i64, ptr %16, align 8, !tbaa !9
  %18 = icmp eq i64 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %14
  store i64 -9223372036854775802, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %47

20:                                               ; preds = %14
  store i64 2, ptr %6, align 8, !tbaa !13
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %21, i32 0, i32 3
  %23 = load i64, ptr %22, align 8, !tbaa !9
  %24 = load i64, ptr %6, align 8, !tbaa !13
  %25 = icmp ugt i64 %23, %24
  br i1 %25, label %26, label %32

26:                                               ; preds = %20
  %27 = load i64, ptr %6, align 8, !tbaa !13
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %28, i32 0, i32 3
  %30 = load i64, ptr %29, align 8, !tbaa !9
  %31 = sub i64 %30, %27
  store i64 %31, ptr %29, align 8, !tbaa !9
  br label %35

32:                                               ; preds = %20
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %33, i32 0, i32 3
  store i64 0, ptr %34, align 8, !tbaa !9
  br label %35

35:                                               ; preds = %32, %26
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.EFI_INPUT_KEY, ptr %36, i32 0, i32 0
  store i16 0, ptr %37, align 2, !tbaa !14
  %38 = load ptr, ptr getelementptr inbounds (%struct.SHELL_INFO, ptr @ShellInfoObject, i32 0, i32 1), align 8, !tbaa !17
  %39 = getelementptr inbounds %struct._EFI_SHELL_PROTOCOL, ptr %38, i32 0, i32 27
  %40 = load ptr, ptr %39, align 8, !tbaa !24
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !27
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.EFI_INPUT_KEY, ptr %44, i32 0, i32 1
  %46 = call i64 %40(ptr noundef %43, ptr noundef %6, ptr noundef %45)
  store i64 %46, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %47

47:                                               ; preds = %35, %19, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %48 = load i64, ptr %3, align 8
  ret i64 %48
}

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

; 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 = { 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, !12, i64 40}
!10 = !{!"", !11, i64 0, !6, i64 24, !6, i64 32, !12, i64 40}
!11 = !{!"_EFI_SIMPLE_TEXT_INPUT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16}
!12 = !{!"long long", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!15, !16, i64 0}
!15 = !{!"", !16, i64 0, !16, i64 2}
!16 = !{!"short", !7, i64 0}
!17 = !{!18, !6, i64 8}
!18 = !{!"", !6, i64 0, !6, i64 8, !7, i64 16, !7, i64 17, !19, i64 24, !20, i64 56, !22, i64 80, !6, i64 128, !12, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !23, i64 184, !23, i64 216, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !7, i64 312}
!19 = !{!"", !7, i64 0, !12, i64 8, !6, i64 16, !6, i64 24}
!20 = !{!"", !21, i64 0, !6, i64 16}
!21 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!22 = !{!"", !20, i64 0, !12, i64 24, !12, i64 32, !7, i64 40}
!23 = !{!"", !21, i64 0, !6, i64 16, !6, i64 24}
!24 = !{!25, !6, i64 216}
!25 = !{!"_EFI_SHELL_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !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, !26, i64 320, !26, i64 324, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352}
!26 = !{!"int", !7, i64 0}
!27 = !{!10, !6, i64 24}


; 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.SHELL_INFO = type { ptr, ptr, i8, i8, %struct.SHELL_INIT_SETTINGS, %struct.BUFFER_LIST, %struct.SHELL_VIEWING_SETTINGS, ptr, i64, ptr, ptr, ptr, ptr, ptr, %struct.SHELL_PROTOCOL_HANDLE_LIST, %struct.SPLIT_LIST, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i8 }
%struct.SHELL_INIT_SETTINGS = type { %union.SHELL_BIT_UNION, i64, ptr, ptr }
%union.SHELL_BIT_UNION = type { %struct.SHELL_BITS }
%struct.SHELL_BITS = type { i16, [2 x i8] }
%struct.BUFFER_LIST = type { %struct._LIST_ENTRY, ptr }
%struct._LIST_ENTRY = type { ptr, ptr }
%struct.SHELL_VIEWING_SETTINGS = type { %struct.BUFFER_LIST, i64, i64, i8 }
%struct.SHELL_PROTOCOL_HANDLE_LIST = type { %struct._LIST_ENTRY, ptr, ptr }
%struct.SPLIT_LIST = type { %struct._LIST_ENTRY, ptr, ptr }

@ShellInfoObject = external local_unnamed_addr global %struct.SHELL_INFO, align 8

; Function Attrs: nounwind uwtable
define dso_local i64 @FileBasedSimpleTextInReadKeyStroke(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %4 = icmp eq ptr %1, null
  %5 = icmp eq ptr %0, null
  %or.cond = or i1 %5, %4
  br i1 %or.cond, label %18, label %6

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 40
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = icmp eq i64 %8, 0
  br i1 %9, label %18, label %10

10:                                               ; preds = %6
  store i64 2, ptr %3, align 8, !tbaa !12
  %storemerge = tail call i64 @llvm.usub.sat.i64(i64 %8, i64 2)
  store i64 %storemerge, ptr %7, align 8, !tbaa !5
  store i16 0, ptr %1, align 2, !tbaa !13
  %11 = load ptr, ptr getelementptr inbounds (i8, ptr @ShellInfoObject, i64 8), align 8, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %11, i64 216
  %13 = load ptr, ptr %12, align 8, !tbaa !23
  %14 = getelementptr inbounds i8, ptr %0, i64 24
  %15 = load ptr, ptr %14, align 8, !tbaa !26
  %16 = getelementptr inbounds i8, ptr %1, i64 2
  %17 = call i64 %13(ptr noundef %15, ptr noundef nonnull %3, ptr noundef nonnull %16) #3
  br label %18

18:                                               ; preds = %6, %2, %10
  %.0 = phi i64 [ %17, %10 ], [ -9223372036854775806, %2 ], [ -9223372036854775802, %6 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret i64 %.0
}

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.usub.sat.i64(i64, i64) #2

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !11, i64 40}
!6 = !{!"", !7, i64 0, !8, i64 24, !8, i64 32, !11, i64 40}
!7 = !{!"_EFI_SIMPLE_TEXT_INPUT_PROTOCOL", !8, i64 0, !8, i64 8, !8, i64 16}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long long", !9, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"", !15, i64 0, !15, i64 2}
!15 = !{!"short", !9, i64 0}
!16 = !{!17, !8, i64 8}
!17 = !{!"", !8, i64 0, !8, i64 8, !9, i64 16, !9, i64 17, !18, i64 24, !19, i64 56, !21, i64 80, !8, i64 128, !11, i64 136, !8, i64 144, !8, i64 152, !8, i64 160, !8, i64 168, !8, i64 176, !22, i64 184, !22, i64 216, !8, i64 248, !8, i64 256, !8, i64 264, !8, i64 272, !8, i64 280, !8, i64 288, !8, i64 296, !8, i64 304, !9, i64 312}
!18 = !{!"", !9, i64 0, !11, i64 8, !8, i64 16, !8, i64 24}
!19 = !{!"", !20, i64 0, !8, i64 16}
!20 = !{!"_LIST_ENTRY", !8, i64 0, !8, i64 8}
!21 = !{!"", !19, i64 0, !11, i64 24, !11, i64 32, !9, i64 40}
!22 = !{!"", !20, i64 0, !8, i64 16, !8, i64 24}
!23 = !{!24, !8, i64 216}
!24 = !{!"_EFI_SHELL_PROTOCOL", !8, i64 0, !8, i64 8, !8, i64 16, !8, i64 24, !8, i64 32, !8, i64 40, !8, i64 48, !8, i64 56, !8, i64 64, !8, i64 72, !8, i64 80, !8, i64 88, !8, i64 96, !8, i64 104, !8, i64 112, !8, i64 120, !8, i64 128, !8, i64 136, !8, i64 144, !8, i64 152, !8, i64 160, !8, i64 168, !8, i64 176, !8, i64 184, !8, i64 192, !8, i64 200, !8, i64 208, !8, i64 216, !8, i64 224, !8, i64 232, !8, i64 240, !8, i64 248, !8, i64 256, !8, i64 264, !8, i64 272, !8, i64 280, !8, i64 288, !8, i64 296, !8, i64 304, !8, i64 312, !25, i64 320, !25, i64 324, !8, i64 328, !8, i64 336, !8, i64 344, !8, i64 352}
!25 = !{!"int", !9, i64 0}
!26 = !{!6, !8, i64 24}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass