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

; 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.TPM_CMD_NV_READ_VALUE = type <{ %struct.tdTPM_RQU_COMMAND_HDR, i32, i32, i32 }>
%struct.tdTPM_RQU_COMMAND_HDR = type <{ i16, i32, i32 }>
%struct.TPM_RSP_NV_READ_VALUE = type <{ %struct.tdTPM_RSP_COMMAND_HDR, i32, [1024 x i8] }>
%struct.tdTPM_RSP_COMMAND_HDR = type <{ i16, i32, i32 }>

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

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

declare zeroext i16 @SwapBytes16(i16 noundef zeroext) #1

declare i32 @SwapBytes32(i32 noundef) #1

declare ptr @CopyMem(ptr noundef, ptr noundef, i64 noundef) #1

declare i64 @Tpm12SubmitCommand(i32 noundef, ptr noundef, ptr noundef, ptr noundef) #1

declare zeroext i8 @DebugPrintEnabled() #1

declare zeroext i8 @DebugPrintLevelEnabled(i64 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @Tpm12NvReadValue(i32 noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #2 {
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca %struct.TPM_CMD_NV_READ_VALUE, align 1
  %14 = alloca %struct.TPM_RSP_NV_READ_VALUE, align 1
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  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 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 22, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 1038, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  store i32 1038, ptr %11, align 4, !tbaa !5
  store i32 22, ptr %12, align 4, !tbaa !5
  %17 = call zeroext i16 @SwapBytes16(i16 noundef zeroext 193)
  %18 = getelementptr inbounds %struct.TPM_CMD_NV_READ_VALUE, ptr %13, i32 0, i32 0
  %19 = getelementptr inbounds %struct.tdTPM_RQU_COMMAND_HDR, ptr %18, i32 0, i32 0
  store i16 %17, ptr %19, align 1, !tbaa !11
  %20 = call i32 @SwapBytes32(i32 noundef 22)
  %21 = getelementptr inbounds %struct.TPM_CMD_NV_READ_VALUE, ptr %13, i32 0, i32 0
  %22 = getelementptr inbounds %struct.tdTPM_RQU_COMMAND_HDR, ptr %21, i32 0, i32 1
  store i32 %20, ptr %22, align 1, !tbaa !15
  %23 = call i32 @SwapBytes32(i32 noundef 207)
  %24 = getelementptr inbounds %struct.TPM_CMD_NV_READ_VALUE, ptr %13, i32 0, i32 0
  %25 = getelementptr inbounds %struct.tdTPM_RQU_COMMAND_HDR, ptr %24, i32 0, i32 2
  store i32 %23, ptr %25, align 1, !tbaa !16
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = call i32 @SwapBytes32(i32 noundef %26)
  %28 = getelementptr inbounds %struct.TPM_CMD_NV_READ_VALUE, ptr %13, i32 0, i32 1
  store i32 %27, ptr %28, align 1, !tbaa !17
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = call i32 @SwapBytes32(i32 noundef %29)
  %31 = getelementptr inbounds %struct.TPM_CMD_NV_READ_VALUE, ptr %13, i32 0, i32 2
  store i32 %30, ptr %31, align 1, !tbaa !18
  %32 = load ptr, ptr %8, align 8, !tbaa !9
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = call i32 @SwapBytes32(i32 noundef %33)
  %35 = getelementptr inbounds %struct.TPM_CMD_NV_READ_VALUE, ptr %13, i32 0, i32 3
  store i32 %34, ptr %35, align 1, !tbaa !19
  %36 = load i32, ptr %12, align 4, !tbaa !5
  %37 = call i64 @Tpm12SubmitCommand(i32 noundef %36, ptr noundef %13, ptr noundef %11, ptr noundef %14)
  store i64 %37, ptr %10, align 8, !tbaa !20
  %38 = load i64, ptr %10, align 8, !tbaa !20
  %39 = icmp slt i64 %38, 0
  br i1 %39, label %40, label %42

40:                                               ; preds = %4
  %41 = load i64, ptr %10, align 8, !tbaa !20
  store i64 %41, ptr %5, align 8
  store i32 1, ptr %16, align 4
  br label %75

42:                                               ; preds = %4
  %43 = getelementptr inbounds %struct.TPM_RSP_NV_READ_VALUE, ptr %14, i32 0, i32 0
  %44 = getelementptr inbounds %struct.tdTPM_RSP_COMMAND_HDR, ptr %43, i32 0, i32 2
  %45 = load i32, ptr %44, align 1, !tbaa !22
  %46 = call i32 @SwapBytes32(i32 noundef %45)
  store i32 %46, ptr %15, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %42
  %48 = call zeroext i8 @DebugPrintEnabled()
  %49 = icmp ne i8 %48, 0
  br i1 %49, label %50, label %59

50:                                               ; preds = %47
  br label %51

51:                                               ; preds = %50
  %52 = call zeroext i8 @DebugPrintLevelEnabled(i64 noundef 64)
  %53 = icmp ne i8 %52, 0
  br i1 %53, label %54, label %56

54:                                               ; preds = %51
  %55 = load i32, ptr %15, align 4, !tbaa !5
  call void (i64, ptr, ...) @DebugPrint(i64 noundef 64, ptr noundef @.str.1, i32 noundef %55)
  br label %56

56:                                               ; preds = %54, %51
  br label %57

57:                                               ; preds = %56
  br label %58

58:                                               ; preds = %57
  br label %59

59:                                               ; preds = %58, %47
  br label %60

60:                                               ; preds = %59
  br label %61

61:                                               ; preds = %60
  %62 = load i32, ptr %15, align 4, !tbaa !5
  switch i32 %62, label %63 [
    i32 0, label %64
  ]

63:                                               ; preds = %61
  store i64 -9223372036854775801, ptr %5, align 8
  store i32 1, ptr %16, align 4
  br label %75

64:                                               ; preds = %61
  %65 = getelementptr inbounds %struct.TPM_RSP_NV_READ_VALUE, ptr %14, i32 0, i32 1
  %66 = load i32, ptr %65, align 1, !tbaa !25
  %67 = call i32 @SwapBytes32(i32 noundef %66)
  %68 = load ptr, ptr %8, align 8, !tbaa !9
  store i32 %67, ptr %68, align 4, !tbaa !5
  %69 = load ptr, ptr %9, align 8, !tbaa !9
  %70 = getelementptr inbounds %struct.TPM_RSP_NV_READ_VALUE, ptr %14, i32 0, i32 2
  %71 = load ptr, ptr %8, align 8, !tbaa !9
  %72 = load i32, ptr %71, align 4, !tbaa !5
  %73 = zext i32 %72 to i64
  %74 = call ptr @CopyMem(ptr noundef %69, ptr noundef %70, i64 noundef %73)
  store i64 0, ptr %5, align 8
  store i32 1, ptr %16, align 4
  br label %75

75:                                               ; preds = %64, %63, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 1038, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 22, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %76 = load i64, ptr %5, align 8
  ret i64 %76
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !14, i64 0}
!12 = !{!"", !13, i64 0, !6, i64 10, !6, i64 14, !6, i64 18}
!13 = !{!"tdTPM_RQU_COMMAND_HDR", !14, i64 0, !6, i64 2, !6, i64 6}
!14 = !{!"short", !7, i64 0}
!15 = !{!12, !6, i64 2}
!16 = !{!12, !6, i64 6}
!17 = !{!12, !6, i64 10}
!18 = !{!12, !6, i64 14}
!19 = !{!12, !6, i64 18}
!20 = !{!21, !21, i64 0}
!21 = !{!"long long", !7, i64 0}
!22 = !{!23, !6, i64 6}
!23 = !{!"", !24, i64 0, !6, i64 10, !7, i64 14}
!24 = !{!"tdTPM_RSP_COMMAND_HDR", !14, i64 0, !6, i64 2, !6, i64 6}
!25 = !{!23, !6, i64 10}


; 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.TPM_CMD_NV_READ_VALUE = type <{ %struct.tdTPM_RQU_COMMAND_HDR, i32, i32, i32 }>
%struct.tdTPM_RQU_COMMAND_HDR = type <{ i16, i32, i32 }>
%struct.TPM_RSP_NV_READ_VALUE = type <{ %struct.tdTPM_RSP_COMMAND_HDR, i32, [1024 x i8] }>
%struct.tdTPM_RSP_COMMAND_HDR = type <{ i16, i32, i32 }>

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

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

declare zeroext i16 @SwapBytes16(i16 noundef zeroext) local_unnamed_addr #1

declare i32 @SwapBytes32(i32 noundef) local_unnamed_addr #1

declare ptr @CopyMem(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare i64 @Tpm12SubmitCommand(i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775808, 1) i64 @Tpm12NvReadValue(i32 noundef %0, i32 noundef %1, ptr nocapture noundef %2, ptr noundef %3) local_unnamed_addr #2 {
  %5 = alloca i32, align 4
  %6 = alloca %struct.TPM_CMD_NV_READ_VALUE, align 2
  %7 = alloca %struct.TPM_RSP_NV_READ_VALUE, align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 22, ptr nonnull %6) #3
  call void @llvm.lifetime.start.p0(i64 1038, ptr nonnull %7) #3
  store i32 1038, ptr %5, align 4, !tbaa !5
  %8 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext 193) #3
  store i16 %8, ptr %6, align 2, !tbaa !9
  %9 = tail call i32 @SwapBytes32(i32 noundef 22) #3
  %10 = getelementptr inbounds i8, ptr %6, i64 2
  store i32 %9, ptr %10, align 2, !tbaa !13
  %11 = tail call i32 @SwapBytes32(i32 noundef 207) #3
  %12 = getelementptr inbounds i8, ptr %6, i64 6
  store i32 %11, ptr %12, align 2, !tbaa !14
  %13 = tail call i32 @SwapBytes32(i32 noundef %0) #3
  %14 = getelementptr inbounds i8, ptr %6, i64 10
  store i32 %13, ptr %14, align 2, !tbaa !15
  %15 = tail call i32 @SwapBytes32(i32 noundef %1) #3
  %16 = getelementptr inbounds i8, ptr %6, i64 14
  store i32 %15, ptr %16, align 2, !tbaa !16
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = tail call i32 @SwapBytes32(i32 noundef %17) #3
  %19 = getelementptr inbounds i8, ptr %6, i64 18
  store i32 %18, ptr %19, align 2, !tbaa !17
  %20 = call i64 @Tpm12SubmitCommand(i32 noundef 22, ptr noundef nonnull %6, ptr noundef nonnull %5, ptr noundef nonnull %7) #3
  %21 = icmp slt i64 %20, 0
  br i1 %21, label %38, label %22

22:                                               ; preds = %4
  %23 = getelementptr inbounds i8, ptr %7, i64 6
  %24 = load i32, ptr %23, align 1, !tbaa !18
  %25 = call i32 @SwapBytes32(i32 noundef %24) #3
  %26 = call zeroext i8 @DebugPrintEnabled() #3
  %.not = icmp eq i8 %26, 0
  br i1 %.not, label %30, label %27

27:                                               ; preds = %22
  %28 = call zeroext i8 @DebugPrintLevelEnabled(i64 noundef 64) #3
  %.not12 = icmp eq i8 %28, 0
  br i1 %.not12, label %30, label %29

29:                                               ; preds = %27
  call void (i64, ptr, ...) @DebugPrint(i64 noundef 64, ptr noundef nonnull @.str.1, i32 noundef %25) #3
  br label %30

30:                                               ; preds = %27, %29, %22
  %cond = icmp eq i32 %25, 0
  br i1 %cond, label %31, label %38

31:                                               ; preds = %30
  %32 = getelementptr inbounds i8, ptr %7, i64 10
  %33 = load i32, ptr %32, align 1, !tbaa !21
  %34 = call i32 @SwapBytes32(i32 noundef %33) #3
  store i32 %34, ptr %2, align 4, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %7, i64 14
  %36 = zext i32 %34 to i64
  %37 = call ptr @CopyMem(ptr noundef %3, ptr noundef nonnull %35, i64 noundef %36) #3
  br label %38

38:                                               ; preds = %30, %4, %31
  %.0 = phi i64 [ 0, %31 ], [ %20, %4 ], [ -9223372036854775801, %30 ]
  call void @llvm.lifetime.end.p0(i64 1038, ptr nonnull %7) #3
  call void @llvm.lifetime.end.p0(i64 22, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #3
  ret i64 %.0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !12, i64 0}
!10 = !{!"", !11, i64 0, !6, i64 10, !6, i64 14, !6, i64 18}
!11 = !{!"tdTPM_RQU_COMMAND_HDR", !12, i64 0, !6, i64 2, !6, i64 6}
!12 = !{!"short", !7, i64 0}
!13 = !{!10, !6, i64 2}
!14 = !{!10, !6, i64 6}
!15 = !{!10, !6, i64 10}
!16 = !{!10, !6, i64 14}
!17 = !{!10, !6, i64 18}
!18 = !{!19, !6, i64 6}
!19 = !{!"", !20, i64 0, !6, i64 10, !7, i64 14}
!20 = !{!"tdTPM_RSP_COMMAND_HDR", !12, i64 0, !6, i64 2, !6, i64 6}
!21 = !{!19, !6, i64 10}


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