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

; 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.GUID = type { i32, i16, i16, [8 x i8] }
%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_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
@gEfiSimpleTextInProtocolGuid = external global %struct.GUID, align 4

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @FileBasedSimpleTextInReset(ptr noundef, i8 noundef zeroext) #0

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

; 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

; Function Attrs: nounwind uwtable
define dso_local ptr @CreateSimpleTextInOnFile(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = 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) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %16, label %13

13:                                               ; preds = %2
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %17

16:                                               ; preds = %13, %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %73

17:                                               ; preds = %13
  %18 = call ptr @AllocateZeroPool(i64 noundef 48)
  store ptr %18, ptr %6, align 8, !tbaa !5
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

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

22:                                               ; preds = %17
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = call i64 @ShellGetFileSize(ptr noundef %23, ptr noundef %9)
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = call i64 @ShellGetFilePosition(ptr noundef %25, ptr noundef %8)
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = load i64, ptr %8, align 8, !tbaa !9
  %29 = sub i64 %27, %28
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %30, i32 0, i32 3
  store i64 %29, ptr %31, align 8, !tbaa !11
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %33, i32 0, i32 1
  store ptr %32, ptr %34, align 8, !tbaa !14
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds %struct._EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %36, i32 0, i32 0
  store ptr @FileBasedSimpleTextInReset, ptr %37, align 8, !tbaa !15
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %38, i32 0, i32 0
  %40 = getelementptr inbounds %struct._EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %39, i32 0, i32 1
  store ptr @FileBasedSimpleTextInReadKeyStroke, ptr %40, align 8, !tbaa !16
  %41 = load ptr, ptr @gBS, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %41, i32 0, i32 8
  %43 = load ptr, ptr %42, align 8, !tbaa !17
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %46, i32 0, i32 0
  %48 = getelementptr inbounds %struct._EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %47, i32 0, i32 2
  %49 = call i64 %43(i32 noundef 256, i64 noundef 16, ptr noundef @ConInWaitForKey, ptr noundef %45, ptr noundef %48)
  store i64 %49, ptr %7, align 8, !tbaa !9
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = icmp slt i64 %50, 0
  br i1 %51, label %52, label %54

52:                                               ; preds = %22
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  call void @FreePool(ptr noundef %53)
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %73

54:                                               ; preds = %22
  %55 = load ptr, ptr @gBS, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %55, i32 0, i32 14
  %57 = load ptr, ptr %56, align 8, !tbaa !21
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %60, i32 0, i32 0
  %62 = call i64 %57(ptr noundef %59, ptr noundef @gEfiSimpleTextInProtocolGuid, i32 noundef 0, ptr noundef %61)
  store i64 %62, ptr %7, align 8, !tbaa !9
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = icmp slt i64 %63, 0
  br i1 %64, label %71, label %65

65:                                               ; preds = %54
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.SHELL_EFI_SIMPLE_TEXT_INPUT_PROTOCOL, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !22
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %68, ptr %69, align 8, !tbaa !5
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %70, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %73

71:                                               ; preds = %54
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  call void @FreePool(ptr noundef %72)
  store ptr null, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %73

73:                                               ; preds = %71, %65, %52, %21, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, 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
  %74 = load ptr, ptr %3, align 8
  ret ptr %74
}

declare ptr @AllocateZeroPool(i64 noundef) #2

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

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

declare void @FreePool(ptr noundef) #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 = { 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 = !{!"long long", !7, i64 0}
!11 = !{!12, !10, i64 40}
!12 = !{!"", !13, i64 0, !6, i64 24, !6, i64 32, !10, i64 40}
!13 = !{!"_EFI_SIMPLE_TEXT_INPUT_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16}
!14 = !{!12, !6, i64 24}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !6, i64 8}
!17 = !{!18, !6, i64 80}
!18 = !{!"", !19, 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}
!19 = !{!"", !10, i64 0, !20, i64 8, !20, i64 12, !20, i64 16, !20, i64 20}
!20 = !{!"int", !7, i64 0}
!21 = !{!18, !6, i64 128}
!22 = !{!12, !6, i64 32}


; 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.GUID = type { i32, i16, i16, [8 x i8] }

@gBS = external local_unnamed_addr global ptr, align 8
@gEfiSimpleTextInProtocolGuid = external global %struct.GUID, align 4

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @FileBasedSimpleTextInReset(ptr noundef, i8 noundef zeroext) #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @FileBasedSimpleTextInReadKeyStroke(ptr noundef, ptr noundef) #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: nounwind uwtable
define dso_local ptr @CreateSimpleTextInOnFile(ptr noundef %0, ptr noundef writeonly %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %5 = icmp eq ptr %1, null
  %6 = icmp eq ptr %0, null
  %or.cond = or i1 %6, %5
  br i1 %or.cond, label %36, label %7

7:                                                ; preds = %2
  %8 = tail call ptr @AllocateZeroPool(i64 noundef 48) #3
  %9 = icmp eq ptr %8, null
  br i1 %9, label %36, label %10

10:                                               ; preds = %7
  %11 = call i64 @ShellGetFileSize(ptr noundef nonnull %0, ptr noundef nonnull %4) #3
  %12 = call i64 @ShellGetFilePosition(ptr noundef nonnull %0, ptr noundef nonnull %3) #3
  %13 = load i64, ptr %4, align 8, !tbaa !5
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = sub i64 %13, %14
  %16 = getelementptr inbounds i8, ptr %8, i64 40
  store i64 %15, ptr %16, align 8, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %8, i64 24
  store ptr %0, ptr %17, align 8, !tbaa !13
  store ptr @FileBasedSimpleTextInReset, ptr %8, align 8, !tbaa !14
  %18 = getelementptr inbounds i8, ptr %8, i64 8
  store ptr @FileBasedSimpleTextInReadKeyStroke, ptr %18, align 8, !tbaa !15
  %19 = load ptr, ptr @gBS, align 8, !tbaa !16
  %20 = getelementptr inbounds i8, ptr %19, i64 80
  %21 = load ptr, ptr %20, align 8, !tbaa !17
  %22 = getelementptr inbounds i8, ptr %8, i64 16
  %23 = call i64 %21(i32 noundef 256, i64 noundef 16, ptr noundef nonnull @ConInWaitForKey, ptr noundef nonnull %8, ptr noundef nonnull %22) #3
  %24 = icmp slt i64 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %10
  call void @FreePool(ptr noundef nonnull %8) #3
  br label %36

26:                                               ; preds = %10
  %27 = load ptr, ptr @gBS, align 8, !tbaa !16
  %28 = getelementptr inbounds i8, ptr %27, i64 128
  %29 = load ptr, ptr %28, align 8, !tbaa !21
  %30 = getelementptr inbounds i8, ptr %8, i64 32
  %31 = call i64 %29(ptr noundef nonnull %30, ptr noundef nonnull @gEfiSimpleTextInProtocolGuid, i32 noundef 0, ptr noundef nonnull %8) #3
  %32 = icmp slt i64 %31, 0
  br i1 %32, label %35, label %33

33:                                               ; preds = %26
  %34 = load ptr, ptr %30, align 8, !tbaa !22
  store ptr %34, ptr %1, align 8, !tbaa !16
  br label %36

35:                                               ; preds = %26
  call void @FreePool(ptr noundef nonnull %8) #3
  br label %36

36:                                               ; preds = %7, %2, %35, %33, %25
  %.0 = phi ptr [ null, %25 ], [ null, %35 ], [ %8, %33 ], [ null, %2 ], [ null, %7 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret ptr %.0
}

declare ptr @AllocateZeroPool(i64 noundef) local_unnamed_addr #2

declare i64 @ShellGetFileSize(ptr noundef, ptr noundef) local_unnamed_addr #2

declare i64 @ShellGetFilePosition(ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @FreePool(ptr noundef) local_unnamed_addr #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 = { "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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 40}
!10 = !{!"", !11, i64 0, !12, i64 24, !12, i64 32, !6, i64 40}
!11 = !{!"_EFI_SIMPLE_TEXT_INPUT_PROTOCOL", !12, i64 0, !12, i64 8, !12, i64 16}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!10, !12, i64 24}
!14 = !{!10, !12, i64 0}
!15 = !{!10, !12, i64 8}
!16 = !{!12, !12, i64 0}
!17 = !{!18, !12, i64 80}
!18 = !{!"", !19, i64 0, !12, i64 24, !12, i64 32, !12, i64 40, !12, i64 48, !12, i64 56, !12, i64 64, !12, i64 72, !12, i64 80, !12, i64 88, !12, i64 96, !12, i64 104, !12, i64 112, !12, i64 120, !12, i64 128, !12, i64 136, !12, i64 144, !12, i64 152, !12, i64 160, !12, i64 168, !12, i64 176, !12, i64 184, !12, i64 192, !12, i64 200, !12, i64 208, !12, i64 216, !12, i64 224, !12, i64 232, !12, i64 240, !12, i64 248, !12, i64 256, !12, i64 264, !12, i64 272, !12, i64 280, !12, i64 288, !12, i64 296, !12, i64 304, !12, i64 312, !12, i64 320, !12, i64 328, !12, i64 336, !12, i64 344, !12, i64 352, !12, i64 360, !12, i64 368}
!19 = !{!"", !6, i64 0, !20, i64 8, !20, i64 12, !20, i64 16, !20, i64 20}
!20 = !{!"int", !7, i64 0}
!21 = !{!18, !12, i64 128}
!22 = !{!10, !12, i64 32}


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