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

; 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_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.10 = external hidden unnamed_addr constant [46 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 i64 @SwapBytes64(i64 noundef) #1

declare i32 @SwapBytes32(i32 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @UsbBootReadBlocks16(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) #2 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca [16 x i8], align 16
  %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 i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, 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 !11
  store i32 %20, ptr %13, align 4, !tbaa !20
  store i64 0, ptr %11, align 8, !tbaa !9
  br label %21

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

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

27:                                               ; preds = %24
  %28 = load i64, ptr %8, align 8, !tbaa !9
  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 !20
  %36 = mul i32 %34, %35
  store i32 %36, ptr %14, align 4, !tbaa !20
  store i32 5000000, ptr %15, align 4, !tbaa !20
  %37 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 0
  %38 = call ptr @ZeroMem(ptr noundef %37, i64 noundef 16)
  %39 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 0
  store i8 -120, ptr %39, align 16, !tbaa !22
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct._USB_MASS_DEVICE, ptr %40, i32 0, i32 7
  %42 = load i8, ptr %41, align 1, !tbaa !23
  %43 = zext i8 %42 to i32
  %44 = shl i32 %43, 5
  %45 = and i32 %44, 224
  %46 = trunc i32 %45 to i8
  %47 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 1
  store i8 %46, ptr %47, align 1, !tbaa !22
  %48 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 2
  %49 = load i64, ptr %7, align 8, !tbaa !9
  %50 = call i64 @SwapBytes64(i64 noundef %49)
  %51 = call i64 @WriteUnaligned64(ptr noundef %48, i64 noundef %50)
  %52 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 10
  %53 = load i16, ptr %12, align 2, !tbaa !21
  %54 = zext i16 %53 to i32
  %55 = call i32 @SwapBytes32(i32 noundef %54)
  %56 = call i32 @WriteUnaligned32(ptr noundef %52, i32 noundef %55)
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds [16 x i8], ptr %10, i64 0, i64 0
  %59 = load ptr, ptr %9, align 8, !tbaa !5
  %60 = load i32, ptr %14, align 4, !tbaa !20
  %61 = load i32, ptr %15, align 4, !tbaa !20
  %62 = call i64 @UsbBootExecCmdWithRetry(ptr noundef %57, ptr noundef %58, i8 noundef zeroext 16, i32 noundef 0, ptr noundef %59, i32 noundef %60, i32 noundef %61)
  store i64 %62, ptr %11, align 8, !tbaa !9
  %63 = load i64, ptr %11, align 8, !tbaa !9
  %64 = icmp slt i64 %63, 0
  br i1 %64, label %65, label %67

65:                                               ; preds = %30
  %66 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %66, ptr %5, align 8
  store i32 1, ptr %16, align 4
  br label %102

67:                                               ; preds = %30
  br label %68

68:                                               ; preds = %67
  %69 = call zeroext i8 @DebugPrintEnabled()
  %70 = icmp ne i8 %69, 0
  br i1 %70, label %71, label %82

71:                                               ; preds = %68
  br label %72

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

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

79:                                               ; preds = %75, %72
  br label %80

80:                                               ; preds = %79
  br label %81

81:                                               ; preds = %80
  br label %82

82:                                               ; preds = %81, %68
  br label %83

83:                                               ; preds = %82
  br label %84

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

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

102:                                              ; preds = %100, %65
  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 16, ptr %10) #3
  %103 = load i64, ptr %5, align 8
  ret i64 %103
}

declare i64 @WriteUnaligned64(ptr noundef, i64 noundef) #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 = !{!"long long", !7, i64 0}
!11 = !{!12, !13, i64 92}
!12 = !{!"_USB_MASS_DEVICE", !13, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !14, i64 32, !15, i64 80, !7, i64 128, !7, i64 129, !7, i64 130, !6, i64 136, !6, i64 144, !16, i64 152, !19, i64 200, !7, i64 236}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_EFI_BLOCK_IO_PROTOCOL", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!15 = !{!"", !13, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8, !13, i64 12, !13, i64 16, !10, i64 24, !10, i64 32, !13, i64 40, !13, i64 44}
!16 = !{!"_EFI_DISK_INFO_PROTOCOL", !17, i64 0, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!17 = !{!"", !13, 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 = !{!13, !13, i64 0}
!21 = !{!18, !18, i64 0}
!22 = !{!7, !7, i64 0}
!23 = !{!12, !7, i64 129}
!24 = distinct !{!24, !25}
!25 = !{!"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"

@.str.10 = external hidden unnamed_addr constant [46 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 i64 @SwapBytes64(i64 noundef) local_unnamed_addr #1

declare i32 @SwapBytes32(i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @UsbBootReadBlocks16(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #2 {
  %5 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, 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 10
  br label %12

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

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

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

29:                                               ; preds = %27
  call void (i64, ptr, ...) @DebugPrint(i64 noundef 4096, ptr noundef nonnull @.str.10, i64 noundef %.02535, i32 noundef %14) #4
  br label %30

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

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

declare i64 @WriteUnaligned64(ptr noundef, i64 noundef) 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 = !{!8, !8, i64 0}
!19 = !{!6, !8, i64 129}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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