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

; 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_READ10_CMD = type { i8, i8, [4 x i8], i8, [2 x i8], i8, [2 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] }

@.str.8 = external hidden unnamed_addr constant [43 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 ptr @ZeroMem(ptr noundef, i64 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
declare dso_local i64 @UsbBootExecCmdWithRetry(ptr noundef, ptr noundef, i8 noundef zeroext, i32 noundef, ptr noundef, i32 noundef, i32 noundef) #2

declare i32 @SwapBytes32(i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @UsbBootReadBlocks(ptr noundef %0, i32 noundef %1, i64 noundef %2, ptr noundef %3) #2 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca %struct.USB_BOOT_READ10_CMD, align 1
  %11 = alloca i64, align 8
  %12 = alloca i16, align 2
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !11
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 12, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct._USB_MASS_DEVICE, ptr %17, i32 0, i32 5
  %19 = getelementptr inbounds %struct.EFI_BLOCK_IO_MEDIA, ptr %18, i32 0, i32 6
  %20 = load i32, ptr %19, align 4, !tbaa !13
  store i32 %20, ptr %13, align 4, !tbaa !9
  store i64 0, ptr %11, align 8, !tbaa !11
  br label %21

21:                                               ; preds = %82, %4
  %22 = load i64, ptr %8, align 8, !tbaa !11
  %23 = icmp ugt i64 %22, 0
  br i1 %23, label %24, label %98

24:                                               ; preds = %21
  %25 = load i64, ptr %8, align 8, !tbaa !11
  %26 = icmp ult i64 %25, 128
  br i1 %26, label %27, label %29

27:                                               ; preds = %24
  %28 = load i64, ptr %8, align 8, !tbaa !11
  br label %30

29:                                               ; preds = %24
  br label %30

30:                                               ; preds = %29, %27
  %31 = phi i64 [ %28, %27 ], [ 128, %29 ]
  %32 = trunc i64 %31 to i16
  store i16 %32, ptr %12, align 2, !tbaa !21
  %33 = load i16, ptr %12, align 2, !tbaa !21
  %34 = zext i16 %33 to i32
  %35 = load i32, ptr %13, align 4, !tbaa !9
  %36 = mul i32 %34, %35
  store i32 %36, ptr %14, align 4, !tbaa !9
  store i32 5000000, ptr %15, align 4, !tbaa !9
  %37 = call ptr @ZeroMem(ptr noundef %10, i64 noundef 12)
  %38 = getelementptr inbounds %struct.USB_BOOT_READ10_CMD, ptr %10, i32 0, i32 0
  store i8 40, ptr %38, align 1, !tbaa !22
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct._USB_MASS_DEVICE, ptr %39, i32 0, i32 7
  %41 = load i8, ptr %40, align 1, !tbaa !24
  %42 = zext i8 %41 to i32
  %43 = shl i32 %42, 5
  %44 = trunc i32 %43 to i8
  %45 = getelementptr inbounds %struct.USB_BOOT_READ10_CMD, ptr %10, i32 0, i32 1
  store i8 %44, ptr %45, align 1, !tbaa !25
  %46 = getelementptr inbounds %struct.USB_BOOT_READ10_CMD, ptr %10, i32 0, i32 2
  %47 = getelementptr inbounds [4 x i8], ptr %46, i64 0, i64 0
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = call i32 @SwapBytes32(i32 noundef %48)
  %50 = call i32 @WriteUnaligned32(ptr noundef %47, i32 noundef %49)
  %51 = getelementptr inbounds %struct.USB_BOOT_READ10_CMD, ptr %10, i32 0, i32 4
  %52 = getelementptr inbounds [2 x i8], ptr %51, i64 0, i64 0
  %53 = load i16, ptr %12, align 2, !tbaa !21
  %54 = call zeroext i16 @SwapBytes16(i16 noundef zeroext %53)
  %55 = call zeroext i16 @WriteUnaligned16(ptr noundef %52, i16 noundef zeroext %54)
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = load i32, ptr %14, align 4, !tbaa !9
  %59 = load i32, ptr %15, align 4, !tbaa !9
  %60 = call i64 @UsbBootExecCmdWithRetry(ptr noundef %56, ptr noundef %10, i8 noundef zeroext 12, i32 noundef 0, ptr noundef %57, i32 noundef %58, i32 noundef %59)
  store i64 %60, ptr %11, align 8, !tbaa !11
  %61 = load i64, ptr %11, align 8, !tbaa !11
  %62 = icmp slt i64 %61, 0
  br i1 %62, label %63, label %65

63:                                               ; preds = %30
  %64 = load i64, ptr %11, align 8, !tbaa !11
  store i64 %64, ptr %5, align 8
  store i32 1, ptr %16, align 4
  br label %100

65:                                               ; preds = %30
  br label %66

66:                                               ; preds = %65
  %67 = call zeroext i8 @DebugPrintEnabled()
  %68 = icmp ne i8 %67, 0
  br i1 %68, label %69, label %80

69:                                               ; preds = %66
  br label %70

70:                                               ; preds = %69
  %71 = call zeroext i8 @DebugPrintLevelEnabled(i64 noundef 4096)
  %72 = icmp ne i8 %71, 0
  br i1 %72, label %73, label %77

73:                                               ; preds = %70
  %74 = load i32, ptr %7, align 4, !tbaa !9
  %75 = load i16, ptr %12, align 2, !tbaa !21
  %76 = zext i16 %75 to i32
  call void (i64, ptr, ...) @DebugPrint(i64 noundef 4096, ptr noundef @.str.8, i32 noundef %74, i32 noundef %76)
  br label %77

77:                                               ; preds = %73, %70
  br label %78

78:                                               ; preds = %77
  br label %79

79:                                               ; preds = %78
  br label %80

80:                                               ; preds = %79, %66
  br label %81

81:                                               ; preds = %80
  br label %82

82:                                               ; preds = %81
  %83 = load i16, ptr %12, align 2, !tbaa !21
  %84 = zext i16 %83 to i32
  %85 = load i32, ptr %7, align 4, !tbaa !9
  %86 = add i32 %85, %84
  store i32 %86, ptr %7, align 4, !tbaa !9
  %87 = load i16, ptr %12, align 2, !tbaa !21
  %88 = zext i16 %87 to i32
  %89 = load i32, ptr %13, align 4, !tbaa !9
  %90 = mul i32 %88, %89
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = zext i32 %90 to i64
  %93 = getelementptr inbounds i8, ptr %91, i64 %92
  store ptr %93, ptr %9, align 8, !tbaa !5
  %94 = load i16, ptr %12, align 2, !tbaa !21
  %95 = zext i16 %94 to i64
  %96 = load i64, ptr %8, align 8, !tbaa !11
  %97 = sub i64 %96, %95
  store i64 %97, ptr %8, align 8, !tbaa !11
  br label %21, !llvm.loop !26

98:                                               ; preds = %21
  %99 = load i64, ptr %11, align 8, !tbaa !11
  store i64 %99, ptr %5, align 8
  store i32 1, ptr %16, align 4
  br label %100

100:                                              ; preds = %98, %63
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 12, ptr %10) #3
  %101 = load i64, ptr %5, align 8
  ret i64 %101
}

declare i32 @WriteUnaligned32(ptr noundef, i32 noundef) #1

declare zeroext i16 @WriteUnaligned16(ptr noundef, i16 noundef zeroext) #1

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

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 = !{!"any pointer", !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, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!14, !10, i64 92}
!14 = !{!"_USB_MASS_DEVICE", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !15, 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}
!15 = !{!"_EFI_BLOCK_IO_PROTOCOL", !12, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!16 = !{!"", !10, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !10, i64 12, !10, i64 16, !12, i64 24, !12, i64 32, !10, i64 40, !10, i64 44}
!17 = !{!"_EFI_DISK_INFO_PROTOCOL", !18, i64 0, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!18 = !{!"", !10, 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 = !{!19, !19, i64 0}
!22 = !{!23, !7, i64 0}
!23 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 6, !7, i64 7, !7, i64 9, !7, i64 10}
!24 = !{!14, !7, i64 129}
!25 = !{!23, !7, i64 1}
!26 = distinct !{!26, !27}
!27 = !{!"llvm.loop.mustprogress"}


; 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_READ10_CMD = type { i8, i8, [4 x i8], i8, [2 x i8], i8, [2 x i8] }

@.str.8 = external hidden unnamed_addr constant [43 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 ptr @ZeroMem(ptr noundef, i64 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
declare dso_local i64 @UsbBootExecCmdWithRetry(ptr noundef, ptr noundef, i8 noundef zeroext, i32 noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

declare i32 @SwapBytes32(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @UsbBootReadBlocks(ptr noundef %0, i32 noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #2 {
  %5 = alloca %struct.USB_BOOT_READ10_CMD, align 1
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %5) #4
  %6 = getelementptr inbounds i8, ptr %0, i64 92
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %.not32 = icmp eq i64 %2, 0
  br i1 %.not32, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 129
  %9 = getelementptr inbounds i8, ptr %5, i64 1
  %10 = getelementptr inbounds i8, ptr %5, i64 2
  %11 = getelementptr inbounds i8, ptr %5, i64 7
  br label %12

12:                                               ; preds = %.lr.ph, %31
  %.02535 = phi i32 [ %1, %.lr.ph ], [ %32, %31 ]
  %.02634 = phi i64 [ %2, %.lr.ph ], [ %35, %31 ]
  %.02733 = phi ptr [ %3, %.lr.ph ], [ %34, %31 ]
  %13 = call i64 @llvm.umin.i64(i64 %.02634, i64 128)
  %14 = trunc nuw nsw i64 %13 to i16
  %15 = trunc nuw nsw i64 %13 to i32
  %16 = mul i32 %7, %15
  %17 = call ptr @ZeroMem(ptr noundef nonnull %5, i64 noundef 12) #4
  store i8 40, ptr %5, align 1, !tbaa !18
  %18 = load i8, ptr %8, align 1, !tbaa !20
  %19 = shl i8 %18, 5
  store i8 %19, ptr %9, align 1, !tbaa !21
  %20 = call i32 @SwapBytes32(i32 noundef %.02535) #4
  %21 = call i32 @WriteUnaligned32(ptr noundef nonnull %10, i32 noundef %20) #4
  %22 = call zeroext i16 @SwapBytes16(i16 noundef zeroext %14) #4
  %23 = call zeroext i16 @WriteUnaligned16(ptr noundef nonnull %11, i16 noundef zeroext %22) #4
  %24 = call i64 @UsbBootExecCmdWithRetry(ptr noundef nonnull %0, ptr noundef nonnull %5, i8 noundef zeroext 12, i32 noundef 0, ptr noundef %.02733, i32 noundef %16, i32 noundef 5000000)
  %25 = icmp slt i64 %24, 0
  br i1 %25, label %._crit_edge, label %26

26:                                               ; preds = %12
  %27 = call zeroext i8 @DebugPrintEnabled() #4
  %.not30 = icmp eq i8 %27, 0
  br i1 %.not30, label %31, label %28

28:                                               ; preds = %26
  %29 = call zeroext i8 @DebugPrintLevelEnabled(i64 noundef 4096) #4
  %.not31 = icmp eq i8 %29, 0
  br i1 %.not31, label %31, label %30

30:                                               ; preds = %28
  call void (i64, ptr, ...) @DebugPrint(i64 noundef 4096, ptr noundef nonnull @.str.8, i32 noundef %.02535, i32 noundef %15) #4
  br label %31

31:                                               ; preds = %28, %30, %26
  %32 = add i32 %.02535, %15
  %33 = zext i32 %16 to i64
  %34 = getelementptr inbounds i8, ptr %.02733, i64 %33
  %35 = sub i64 %.02634, %13
  %.not = icmp eq i64 %35, 0
  br i1 %.not, label %._crit_edge, label %12, !llvm.loop !22

._crit_edge:                                      ; preds = %12, %31, %4
  %.0 = phi i64 [ 0, %4 ], [ %24, %31 ], [ %24, %12 ]
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %5) #4
  ret i64 %.0
}

declare i32 @WriteUnaligned32(ptr noundef, i32 noundef) local_unnamed_addr #1

declare zeroext i16 @WriteUnaligned16(ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #3

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 92}
!6 = !{!"_USB_MASS_DEVICE", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !11, i64 32, !13, i64 80, !8, i64 128, !8, i64 129, !8, i64 130, !10, i64 136, !10, i64 144, !14, i64 152, !17, i64 200, !8, i64 236}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_EFI_BLOCK_IO_PROTOCOL", !12, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!12 = !{!"long long", !8, i64 0}
!13 = !{!"", !7, i64 0, !8, i64 4, !8, i64 5, !8, i64 6, !8, i64 7, !8, i64 8, !7, i64 12, !7, i64 16, !12, i64 24, !12, i64 32, !7, i64 40, !7, i64 44}
!14 = !{!"_EFI_DISK_INFO_PROTOCOL", !15, i64 0, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!15 = !{!"", !7, i64 0, !16, i64 4, !16, i64 6, !8, i64 8}
!16 = !{!"short", !8, i64 0}
!17 = !{!"", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 4, !8, i64 5, !8, i64 8, !8, i64 16, !8, i64 32}
!18 = !{!19, !8, i64 0}
!19 = !{!"", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 6, !8, i64 7, !8, i64 9, !8, i64 10}
!20 = !{!6, !8, i64 129}
!21 = !{!19, !8, i64 1}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass