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

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

; Function Attrs: nounwind uwtable
define dso_local i64 @Tpm12ForceClear() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca %struct.TPM_CMD_FORCE_CLEAR, align 1
  %6 = alloca %struct.TPM_RSP_FORCE_CLEAR, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 10, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 10, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 10, ptr %3, align 4, !tbaa !5
  store i32 10, ptr %4, align 4, !tbaa !5
  %9 = call zeroext i16 @SwapBytes16(i16 noundef zeroext 193)
  %10 = getelementptr inbounds %struct.TPM_CMD_FORCE_CLEAR, ptr %5, i32 0, i32 0
  %11 = getelementptr inbounds %struct.tdTPM_RQU_COMMAND_HDR, ptr %10, i32 0, i32 0
  store i16 %9, ptr %11, align 1, !tbaa !9
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = call i32 @SwapBytes32(i32 noundef %12)
  %14 = getelementptr inbounds %struct.TPM_CMD_FORCE_CLEAR, ptr %5, i32 0, i32 0
  %15 = getelementptr inbounds %struct.tdTPM_RQU_COMMAND_HDR, ptr %14, i32 0, i32 1
  store i32 %13, ptr %15, align 1, !tbaa !13
  %16 = call i32 @SwapBytes32(i32 noundef 93)
  %17 = getelementptr inbounds %struct.TPM_CMD_FORCE_CLEAR, ptr %5, i32 0, i32 0
  %18 = getelementptr inbounds %struct.tdTPM_RQU_COMMAND_HDR, ptr %17, i32 0, i32 2
  store i32 %16, ptr %18, align 1, !tbaa !14
  %19 = load i32, ptr %4, align 4, !tbaa !5
  %20 = call i64 @Tpm12SubmitCommand(i32 noundef %19, ptr noundef %5, ptr noundef %3, ptr noundef %6)
  store i64 %20, ptr %2, align 8, !tbaa !15
  %21 = load i64, ptr %2, align 8, !tbaa !15
  %22 = icmp slt i64 %21, 0
  br i1 %22, label %23, label %25

23:                                               ; preds = %0
  %24 = load i64, ptr %2, align 8, !tbaa !15
  store i64 %24, ptr %1, align 8
  store i32 1, ptr %8, align 4
  br label %33

25:                                               ; preds = %0
  %26 = getelementptr inbounds %struct.TPM_RSP_FORCE_CLEAR, ptr %6, i32 0, i32 0
  %27 = getelementptr inbounds %struct.tdTPM_RSP_COMMAND_HDR, ptr %26, i32 0, i32 2
  %28 = load i32, ptr %27, align 1, !tbaa !17
  %29 = call i32 @SwapBytes32(i32 noundef %28)
  store i32 %29, ptr %7, align 4, !tbaa !5
  %30 = load i32, ptr %7, align 4, !tbaa !5
  switch i32 %30, label %32 [
    i32 0, label %31
  ]

31:                                               ; preds = %25
  store i64 0, ptr %1, align 8
  store i32 1, ptr %8, align 4
  br label %33

32:                                               ; preds = %25
  store i64 -9223372036854775801, ptr %1, align 8
  store i32 1, ptr %8, align 4
  br label %33

33:                                               ; preds = %32, %31, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 10, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 10, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  %34 = load i64, ptr %1, align 8
  ret i64 %34
}

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

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

declare i32 @SwapBytes32(i32 noundef) #2

declare i64 @Tpm12SubmitCommand(i32 noundef, ptr noundef, ptr noundef, ptr 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, !12, i64 0}
!10 = !{!"", !11, i64 0}
!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 = !{!16, !16, i64 0}
!16 = !{!"long long", !7, i64 0}
!17 = !{!18, !6, i64 6}
!18 = !{!"", !19, i64 0}
!19 = !{!"tdTPM_RSP_COMMAND_HDR", !12, i64 0, !6, i64 2, !6, i64 6}


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

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775808, 1) i64 @Tpm12ForceClear() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.TPM_CMD_FORCE_CLEAR, align 2
  %3 = alloca %struct.TPM_RSP_FORCE_CLEAR, align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #3
  call void @llvm.lifetime.start.p0(i64 10, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 10, ptr nonnull %3) #3
  store i32 10, ptr %1, align 4, !tbaa !5
  %4 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext 193) #3
  store i16 %4, ptr %2, align 2, !tbaa !9
  %5 = tail call i32 @SwapBytes32(i32 noundef 10) #3
  %6 = getelementptr inbounds i8, ptr %2, i64 2
  store i32 %5, ptr %6, align 2, !tbaa !13
  %7 = tail call i32 @SwapBytes32(i32 noundef 93) #3
  %8 = getelementptr inbounds i8, ptr %2, i64 6
  store i32 %7, ptr %8, align 2, !tbaa !14
  %9 = call i64 @Tpm12SubmitCommand(i32 noundef 10, ptr noundef nonnull %2, ptr noundef nonnull %1, ptr noundef nonnull %3) #3
  %10 = icmp slt i64 %9, 0
  br i1 %10, label %15, label %11

11:                                               ; preds = %0
  %12 = getelementptr inbounds i8, ptr %3, i64 6
  %13 = load i32, ptr %12, align 1, !tbaa !15
  %14 = call i32 @SwapBytes32(i32 noundef %13) #3
  %cond = icmp eq i32 %14, 0
  %. = select i1 %cond, i64 0, i64 -9223372036854775801
  br label %15

15:                                               ; preds = %11, %0
  %.0 = phi i64 [ %9, %0 ], [ %., %11 ]
  call void @llvm.lifetime.end.p0(i64 10, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 10, ptr nonnull %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #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

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

declare i32 @SwapBytes32(i32 noundef) local_unnamed_addr #2

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

; Function Attrs: mustprogress 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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !12, i64 0}
!10 = !{!"", !11, i64 0}
!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 = !{!16, !6, i64 6}
!16 = !{!"", !17, i64 0}
!17 = !{!"tdTPM_RSP_COMMAND_HDR", !12, i64 0, !6, i64 2, !6, i64 6}


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