; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Bus/Usb (UsbBootReadCapacity)

; 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.USB_BOOT_READ_CAPACITY_CMD = type { i8, i8, [8 x i8], [2 x i8] }
%struct.USB_BOOT_READ_CAPACITY_DATA = type { [4 x i8], [4 x i8] }
%struct._USB_MASS_DEVICE = type { i32, ptr, ptr, ptr, %struct._EFI_BLOCK_IO_PROTOCOL, %struct.EFI_BLOCK_IO_MEDIA, i8, i8, i8, ptr, ptr, %struct._EFI_DISK_INFO_PROTOCOL, %struct.USB_BOOT_INQUIRY_DATA, i8 }
%struct._EFI_BLOCK_IO_PROTOCOL = type { i64, ptr, ptr, ptr, ptr, ptr }
%struct.EFI_BLOCK_IO_MEDIA = type { i32, i8, i8, i8, i8, i8, i32, i32, i64, i64, i32, i32 }
%struct._EFI_DISK_INFO_PROTOCOL = type { %struct.GUID, ptr, ptr, ptr, ptr }
%struct.GUID = type { i32, i16, i16, [8 x i8] }
%struct.USB_BOOT_INQUIRY_DATA = type { i8, i8, [2 x i8], i8, [3 x i8], [8 x i8], [16 x i8], [4 x i8] }

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

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

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

; 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
declare dso_local i64 @UsbBootExecCmdWithRetry(ptr noundef, ptr noundef, i8 noundef zeroext, i32 noundef, ptr noundef, i32 noundef, i32 noundef) #0

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

declare i32 @SwapBytes32(i32 noundef) #2

declare i32 @ReadUnaligned32(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i64 @UsbBootReadCapacity(ptr noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca %struct.USB_BOOT_READ_CAPACITY_CMD, align 1
  %5 = alloca %struct.USB_BOOT_READ_CAPACITY_DATA, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 12, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  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 4, ptr %8) #3
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct._USB_MASS_DEVICE, ptr %10, i32 0, i32 5
  store ptr %11, ptr %6, align 8, !tbaa !5
  %12 = call ptr @ZeroMem(ptr noundef %4, i64 noundef 12)
  %13 = call ptr @ZeroMem(ptr noundef %5, i64 noundef 8)
  %14 = getelementptr inbounds %struct.USB_BOOT_READ_CAPACITY_CMD, ptr %4, i32 0, i32 0
  store i8 37, ptr %14, align 1, !tbaa !9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct._USB_MASS_DEVICE, ptr %15, i32 0, i32 7
  %17 = load i8, ptr %16, align 1, !tbaa !11
  %18 = zext i8 %17 to i32
  %19 = shl i32 %18, 5
  %20 = trunc i32 %19 to i8
  %21 = getelementptr inbounds %struct.USB_BOOT_READ_CAPACITY_CMD, ptr %4, i32 0, i32 1
  store i8 %20, ptr %21, align 1, !tbaa !21
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = call i64 @UsbBootExecCmdWithRetry(ptr noundef %22, ptr noundef %4, i8 noundef zeroext 12, i32 noundef 0, ptr noundef %5, i32 noundef 8, i32 noundef 5000000)
  store i64 %23, ptr %7, align 8, !tbaa !22
  %24 = load i64, ptr %7, align 8, !tbaa !22
  %25 = icmp slt i64 %24, 0
  br i1 %25, label %26, label %28

26:                                               ; preds = %1
  %27 = load i64, ptr %7, align 8, !tbaa !22
  store i64 %27, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %67

28:                                               ; preds = %1
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.EFI_BLOCK_IO_MEDIA, ptr %29, i32 0, i32 2
  store i8 1, ptr %30, align 1, !tbaa !23
  %31 = getelementptr inbounds %struct.USB_BOOT_READ_CAPACITY_DATA, ptr %5, i32 0, i32 0
  %32 = getelementptr inbounds [4 x i8], ptr %31, i64 0, i64 0
  %33 = call i32 @ReadUnaligned32(ptr noundef %32)
  %34 = call i32 @SwapBytes32(i32 noundef %33)
  %35 = zext i32 %34 to i64
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.EFI_BLOCK_IO_MEDIA, ptr %36, i32 0, i32 8
  store i64 %35, ptr %37, align 8, !tbaa !24
  %38 = getelementptr inbounds %struct.USB_BOOT_READ_CAPACITY_DATA, ptr %5, i32 0, i32 1
  %39 = getelementptr inbounds [4 x i8], ptr %38, i64 0, i64 0
  %40 = call i32 @ReadUnaligned32(ptr noundef %39)
  %41 = call i32 @SwapBytes32(i32 noundef %40)
  store i32 %41, ptr %8, align 4, !tbaa !25
  %42 = load i32, ptr %8, align 4, !tbaa !25
  %43 = icmp eq i32 %42, 0
  br i1 %43, label %44, label %47

44:                                               ; preds = %28
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = call i64 @UsbBootRequestSense(ptr noundef %45)
  store i64 %46, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %67

47:                                               ; preds = %28
  %48 = load i32, ptr %8, align 4, !tbaa !25
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.EFI_BLOCK_IO_MEDIA, ptr %49, i32 0, i32 6
  store i32 %48, ptr %50, align 4, !tbaa !26
  br label %51

51:                                               ; preds = %47
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.EFI_BLOCK_IO_MEDIA, ptr %52, i32 0, i32 8
  %54 = load i64, ptr %53, align 8, !tbaa !24
  %55 = icmp eq i64 %54, 4294967295
  br i1 %55, label %56, label %65

56:                                               ; preds = %51
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = call i64 @UsbBootReadCapacity16(ptr noundef %57)
  store i64 %58, ptr %7, align 8, !tbaa !22
  %59 = load i64, ptr %7, align 8, !tbaa !22
  %60 = icmp slt i64 %59, 0
  br i1 %60, label %64, label %61

61:                                               ; preds = %56
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct._USB_MASS_DEVICE, ptr %62, i32 0, i32 13
  store i8 1, ptr %63, align 4, !tbaa !27
  br label %64

64:                                               ; preds = %61, %56
  br label %65

65:                                               ; preds = %64, %51
  %66 = load i64, ptr %7, align 8, !tbaa !22
  store i64 %66, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %67

67:                                               ; preds = %65, %44, %26
  call void @llvm.lifetime.end.p0(i64 4, 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
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 12, ptr %4) #3
  %68 = load i64, ptr %2, align 8
  ret i64 %68
}

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, !7, i64 0}
!10 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 10}
!11 = !{!12, !7, i64 129}
!12 = !{!"_USB_MASS_DEVICE", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !14, i64 32, !16, i64 80, !7, i64 128, !7, i64 129, !7, i64 130, !6, i64 136, !6, i64 144, !17, i64 152, !20, i64 200, !7, i64 236}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_EFI_BLOCK_IO_PROTOCOL", !15, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!15 = !{!"long long", !7, i64 0}
!16 = !{!"", !13, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !13, i64 12, !13, i64 16, !15, i64 24, !15, i64 32, !13, i64 40, !13, i64 44}
!17 = !{!"_EFI_DISK_INFO_PROTOCOL", !18, i64 0, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!18 = !{!"", !13, i64 0, !19, i64 4, !19, i64 6, !7, i64 8}
!19 = !{!"short", !7, i64 0}
!20 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 4, !7, i64 5, !7, i64 8, !7, i64 16, !7, i64 32}
!21 = !{!10, !7, i64 1}
!22 = !{!15, !15, i64 0}
!23 = !{!16, !7, i64 5}
!24 = !{!16, !15, i64 24}
!25 = !{!13, !13, i64 0}
!26 = !{!16, !13, i64 12}
!27 = !{!12, !7, i64 236}


; 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.USB_BOOT_READ_CAPACITY_CMD = type { i8, i8, [8 x i8], [2 x i8] }
%struct.USB_BOOT_READ_CAPACITY_DATA = type { [4 x i8], [4 x i8] }

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

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

declare ptr @ZeroMem(ptr noundef, i64 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

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

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

declare i32 @SwapBytes32(i32 noundef) local_unnamed_addr #2

declare i32 @ReadUnaligned32(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i64 @UsbBootReadCapacity(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct.USB_BOOT_READ_CAPACITY_CMD, align 1
  %3 = alloca %struct.USB_BOOT_READ_CAPACITY_DATA, align 1
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %4 = call ptr @ZeroMem(ptr noundef nonnull %2, i64 noundef 12) #3
  %5 = call ptr @ZeroMem(ptr noundef nonnull %3, i64 noundef 8) #3
  store i8 37, ptr %2, align 1, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 129
  %7 = load i8, ptr %6, align 1, !tbaa !9
  %8 = shl i8 %7, 5
  %9 = getelementptr inbounds i8, ptr %2, i64 1
  store i8 %8, ptr %9, align 1, !tbaa !20
  %10 = call i64 @UsbBootExecCmdWithRetry(ptr noundef %0, ptr noundef nonnull %2, i8 noundef zeroext 12, i32 noundef 0, ptr noundef nonnull %3, i32 noundef 8, i32 noundef 5000000)
  %11 = icmp slt i64 %10, 0
  br i1 %11, label %33, label %12

12:                                               ; preds = %1
  %13 = getelementptr inbounds i8, ptr %0, i64 85
  store i8 1, ptr %13, align 1, !tbaa !21
  %14 = call i32 @ReadUnaligned32(ptr noundef nonnull %3) #3
  %15 = call i32 @SwapBytes32(i32 noundef %14) #3
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds i8, ptr %0, i64 104
  store i64 %16, ptr %17, align 8, !tbaa !22
  %18 = getelementptr inbounds i8, ptr %3, i64 4
  %19 = call i32 @ReadUnaligned32(ptr noundef nonnull %18) #3
  %20 = call i32 @SwapBytes32(i32 noundef %19) #3
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %12
  %23 = call i64 @UsbBootRequestSense(ptr noundef nonnull %0)
  br label %33

24:                                               ; preds = %12
  %25 = getelementptr inbounds i8, ptr %0, i64 92
  store i32 %20, ptr %25, align 4, !tbaa !23
  %26 = load i64, ptr %17, align 8, !tbaa !22
  %27 = icmp eq i64 %26, 4294967295
  br i1 %27, label %28, label %33

28:                                               ; preds = %24
  %29 = call i64 @UsbBootReadCapacity16(ptr noundef nonnull %0)
  %30 = icmp slt i64 %29, 0
  br i1 %30, label %33, label %31

31:                                               ; preds = %28
  %32 = getelementptr inbounds i8, ptr %0, i64 236
  store i8 1, ptr %32, align 4, !tbaa !24
  br label %33

33:                                               ; preds = %24, %31, %28, %1, %22
  %.0 = phi i64 [ %23, %22 ], [ %10, %1 ], [ %29, %28 ], [ %29, %31 ], [ %10, %24 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %2) #3
  ret i64 %.0
}

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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 10}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !7, i64 129}
!10 = !{!"_USB_MASS_DEVICE", !11, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !13, i64 32, !15, i64 80, !7, i64 128, !7, i64 129, !7, i64 130, !12, i64 136, !12, i64 144, !16, i64 152, !19, i64 200, !7, i64 236}
!11 = !{!"int", !7, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"_EFI_BLOCK_IO_PROTOCOL", !14, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40}
!14 = !{!"long long", !7, i64 0}
!15 = !{!"", !11, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !11, i64 12, !11, i64 16, !14, i64 24, !14, i64 32, !11, i64 40, !11, i64 44}
!16 = !{!"_EFI_DISK_INFO_PROTOCOL", !17, i64 0, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40}
!17 = !{!"", !11, i64 0, !18, i64 4, !18, i64 6, !7, i64 8}
!18 = !{!"short", !7, i64 0}
!19 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 4, !7, i64 5, !7, i64 8, !7, i64 16, !7, i64 32}
!20 = !{!6, !7, i64 1}
!21 = !{!15, !7, i64 5}
!22 = !{!15, !14, i64 24}
!23 = !{!15, !11, i64 12}
!24 = !{!10, !7, i64 236}


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