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

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

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

28:                                               ; preds = %1
  %29 = load i64, ptr %4, align 8, !tbaa !17
  store i64 %29, ptr %2, align 8
  store i32 1, ptr %10, align 4
  br label %38

30:                                               ; preds = %1
  %31 = getelementptr inbounds %struct.TPM_RSP_START_UP, ptr %8, i32 0, i32 0
  %32 = getelementptr inbounds %struct.tdTPM_RSP_COMMAND_HDR, ptr %31, i32 0, i32 2
  %33 = load i32, ptr %32, align 1, !tbaa !19
  %34 = call i32 @SwapBytes32(i32 noundef %33)
  store i32 %34, ptr %9, align 4, !tbaa !9
  %35 = load i32, ptr %9, align 4, !tbaa !9
  switch i32 %35, label %37 [
    i32 0, label %36
    i32 38, label %36
  ]

36:                                               ; preds = %30, %30
  store i64 0, ptr %2, align 8
  store i32 1, ptr %10, align 4
  br label %38

37:                                               ; preds = %30
  store i64 -9223372036854775801, ptr %2, align 8
  store i32 1, ptr %10, align 4
  br label %38

38:                                               ; preds = %37, %36, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 10, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 12, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %39 = load i64, ptr %2, align 8
  ret i64 %39
}

; 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !13, i64 0, !6, i64 10}
!13 = !{!"tdTPM_RQU_COMMAND_HDR", !6, i64 0, !10, i64 2, !10, i64 6}
!14 = !{!12, !10, i64 2}
!15 = !{!12, !10, i64 6}
!16 = !{!12, !6, i64 10}
!17 = !{!18, !18, i64 0}
!18 = !{!"long long", !7, i64 0}
!19 = !{!20, !10, i64 6}
!20 = !{!"", !21, i64 0}
!21 = !{!"tdTPM_RSP_COMMAND_HDR", !6, i64 0, !10, i64 2, !10, 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_START_UP = type { %struct.tdTPM_RQU_COMMAND_HDR, i16 }
%struct.tdTPM_RQU_COMMAND_HDR = type <{ i16, i32, i32 }>
%struct.TPM_RSP_START_UP = 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 @Tpm12Startup(i16 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = alloca i32, align 4
  %3 = alloca %struct.TPM_CMD_START_UP, align 2
  %4 = alloca %struct.TPM_RSP_START_UP, align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 10, ptr nonnull %4) #3
  store i32 10, ptr %2, align 4, !tbaa !5
  %5 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext 193) #3
  store i16 %5, ptr %3, align 2, !tbaa !9
  %6 = tail call i32 @SwapBytes32(i32 noundef 12) #3
  %7 = getelementptr inbounds i8, ptr %3, i64 2
  store i32 %6, ptr %7, align 2, !tbaa !13
  %8 = tail call i32 @SwapBytes32(i32 noundef 153) #3
  %9 = getelementptr inbounds i8, ptr %3, i64 6
  store i32 %8, ptr %9, align 2, !tbaa !14
  %10 = tail call zeroext i16 @SwapBytes16(i16 noundef zeroext %0) #3
  %11 = getelementptr inbounds i8, ptr %3, i64 10
  store i16 %10, ptr %11, align 2, !tbaa !15
  %12 = call i64 @Tpm12SubmitCommand(i32 noundef 12, ptr noundef nonnull %3, ptr noundef nonnull %2, ptr noundef nonnull %4) #3
  %13 = icmp slt i64 %12, 0
  br i1 %13, label %19, label %14

14:                                               ; preds = %1
  %15 = getelementptr inbounds i8, ptr %4, i64 6
  %16 = load i32, ptr %15, align 1, !tbaa !16
  %17 = call i32 @SwapBytes32(i32 noundef %16) #3
  %switch.selectcmp.case1 = icmp eq i32 %17, 0
  %switch.selectcmp.case2 = icmp eq i32 %17, 38
  %switch.selectcmp = or i1 %switch.selectcmp.case1, %switch.selectcmp.case2
  %18 = select i1 %switch.selectcmp, i64 0, i64 -9223372036854775801
  br label %19

19:                                               ; preds = %14, %1
  %.0 = phi i64 [ %12, %1 ], [ %18, %14 ]
  call void @llvm.lifetime.end.p0(i64 10, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #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, !12, i64 10}
!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, !12, i64 10}
!16 = !{!17, !6, i64 6}
!17 = !{!"", !18, i64 0}
!18 = !{!"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