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

; 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.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 }
%struct._EFI_TCG_PROTOCOL = type { ptr, ptr, ptr, ptr, ptr }
%struct.tdTPM_RSP_COMMAND_HDR = type <{ i16, i32, i32 }>

@mTcgProtocol = external dso_local global ptr, align 8
@gBS = external global ptr, align 8
@gEfiTcgProtocolGuid = external global %struct.GUID, align 4
@.str = external hidden unnamed_addr constant [31 x i8], align 1

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

15:                                               ; preds = %4
  %16 = load ptr, ptr @gBS, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %16, i32 0, i32 38
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = call i64 %18(ptr noundef @gEfiTcgProtocolGuid, ptr noundef null, ptr noundef @mTcgProtocol)
  store i64 %19, ptr %10, align 8, !tbaa !15
  %20 = load i64, ptr %10, align 8, !tbaa !15
  %21 = icmp slt i64 %20, 0
  br i1 %21, label %22, label %38

22:                                               ; preds = %15
  br label %23

23:                                               ; preds = %22
  %24 = call zeroext i8 @DebugPrintEnabled()
  %25 = icmp ne i8 %24, 0
  br i1 %25, label %26, label %35

26:                                               ; preds = %23
  br label %27

27:                                               ; preds = %26
  %28 = call zeroext i8 @DebugPrintLevelEnabled(i64 noundef 2147483648)
  %29 = icmp ne i8 %28, 0
  br i1 %29, label %30, label %32

30:                                               ; preds = %27
  %31 = load i64, ptr %10, align 8, !tbaa !15
  call void (i64, ptr, ...) @DebugPrint(i64 noundef 2147483648, ptr noundef @.str, i64 noundef %31)
  br label %32

32:                                               ; preds = %30, %27
  br label %33

33:                                               ; preds = %32
  br label %34

34:                                               ; preds = %33
  br label %35

35:                                               ; preds = %34, %23
  br label %36

36:                                               ; preds = %35
  br label %37

37:                                               ; preds = %36
  store i64 -9223372036854775794, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %61

38:                                               ; preds = %15
  br label %39

39:                                               ; preds = %38, %4
  %40 = load ptr, ptr @mTcgProtocol, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct._EFI_TCG_PROTOCOL, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !16
  %43 = load ptr, ptr @mTcgProtocol, align 8, !tbaa !9
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = load ptr, ptr %7, align 8, !tbaa !9
  %46 = load ptr, ptr %8, align 8, !tbaa !9
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = load ptr, ptr %9, align 8, !tbaa !9
  %49 = call i64 %42(ptr noundef %43, i32 noundef %44, ptr noundef %45, i32 noundef %47, ptr noundef %48)
  store i64 %49, ptr %10, align 8, !tbaa !15
  %50 = load i64, ptr %10, align 8, !tbaa !15
  %51 = icmp slt i64 %50, 0
  br i1 %51, label %52, label %54

52:                                               ; preds = %39
  %53 = load i64, ptr %10, align 8, !tbaa !15
  store i64 %53, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %61

54:                                               ; preds = %39
  %55 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %55, ptr %11, align 8, !tbaa !9
  %56 = load ptr, ptr %11, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.tdTPM_RSP_COMMAND_HDR, ptr %56, i32 0, i32 1
  %58 = load i32, ptr %57, align 1, !tbaa !18
  %59 = call i32 @SwapBytes32(i32 noundef %58)
  %60 = load ptr, ptr %8, align 8, !tbaa !9
  store i32 %59, ptr %60, align 4, !tbaa !5
  store i64 0, ptr %5, align 8
  store i32 1, ptr %12, align 4
  br label %61

61:                                               ; preds = %54, %52, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %62 = load i64, ptr %5, align 8
  ret i64 %62
}

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

declare zeroext i8 @DebugPrintEnabled() #2

declare zeroext i8 @DebugPrintLevelEnabled(i64 noundef) #2

declare void @DebugPrint(i64 noundef, ptr noundef, ...) #2

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


; 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] }

@mTcgProtocol = external dso_local global ptr, align 8
@gBS = external local_unnamed_addr global ptr, align 8
@gEfiTcgProtocolGuid = external global %struct.GUID, align 4
@.str = external hidden unnamed_addr constant [31 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775808, 1) i64 @Tpm12SubmitCommand(i32 noundef %0, ptr noundef %1, ptr nocapture noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = load ptr, ptr @mTcgProtocol, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %18

7:                                                ; preds = %4
  %8 = load ptr, ptr @gBS, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 320
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = tail call i64 %10(ptr noundef nonnull @gEfiTcgProtocolGuid, ptr noundef null, ptr noundef nonnull @mTcgProtocol) #2
  %12 = icmp slt i64 %11, 0
  br i1 %12, label %13, label %._crit_edge

._crit_edge:                                      ; preds = %7
  %.pre = load ptr, ptr @mTcgProtocol, align 8, !tbaa !5
  br label %18

13:                                               ; preds = %7
  %14 = tail call zeroext i8 @DebugPrintEnabled() #2
  %.not = icmp eq i8 %14, 0
  br i1 %.not, label %29, label %15

15:                                               ; preds = %13
  %16 = tail call zeroext i8 @DebugPrintLevelEnabled(i64 noundef 2147483648) #2
  %.not13 = icmp eq i8 %16, 0
  br i1 %.not13, label %29, label %17

17:                                               ; preds = %15
  tail call void (i64, ptr, ...) @DebugPrint(i64 noundef 2147483648, ptr noundef nonnull @.str, i64 noundef %11) #2
  br label %29

18:                                               ; preds = %._crit_edge, %4
  %19 = phi ptr [ %.pre, %._crit_edge ], [ %5, %4 ]
  %20 = getelementptr inbounds i8, ptr %19, i64 24
  %21 = load ptr, ptr %20, align 8, !tbaa !14
  %22 = load i32, ptr %2, align 4, !tbaa !16
  %23 = tail call i64 %21(ptr noundef %19, i32 noundef %0, ptr noundef %1, i32 noundef %22, ptr noundef %3) #2
  %24 = icmp slt i64 %23, 0
  br i1 %24, label %29, label %25

25:                                               ; preds = %18
  %26 = getelementptr inbounds i8, ptr %3, i64 2
  %27 = load i32, ptr %26, align 1, !tbaa !17
  %28 = tail call i32 @SwapBytes32(i32 noundef %27) #2
  store i32 %28, ptr %2, align 4, !tbaa !16
  br label %29

29:                                               ; preds = %18, %13, %17, %15, %25
  %.0 = phi i64 [ 0, %25 ], [ -9223372036854775794, %15 ], [ -9223372036854775794, %17 ], [ -9223372036854775794, %13 ], [ %23, %18 ]
  ret i64 %.0
}

declare zeroext i8 @DebugPrintEnabled() local_unnamed_addr #1

declare zeroext i8 @DebugPrintLevelEnabled(i64 noundef) local_unnamed_addr #1

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

declare i32 @SwapBytes32(i32 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_TCG_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!16 = !{!13, !13, i64 0}
!17 = !{!18, !13, i64 2}
!18 = !{!"tdTPM_RSP_COMMAND_HDR", !19, i64 0, !13, i64 2, !13, i64 6}
!19 = !{!"short", !7, i64 0}


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