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

; 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_CBI_PROTOCOL = type { %struct.USB_INTERFACE_DESCRIPTOR, ptr, ptr, ptr, ptr }
%struct.USB_INTERFACE_DESCRIPTOR = type { i8, i8, i8, i8, i8, i8, i8, i8, i8 }
%struct.USB_ENDPOINT_DESCRIPTOR = type <{ i8, i8, i8, i8, i16, i8 }>
%struct._EFI_USB_IO_PROTOCOL = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

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

; 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
define dso_local i64 @UsbCbiDataTransfer(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, i32 noundef %4) #1 {
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  %15 = alloca i64, align 8
  %16 = alloca i64, align 8
  %17 = alloca ptr, align 8
  %18 = alloca i64, align 8
  %19 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store ptr %3, ptr %10, align 8, !tbaa !5
  store i32 %4, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #3
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = icmp eq i32 %20, 2
  br i1 %21, label %26, label %22

22:                                               ; preds = %5
  %23 = load ptr, ptr %10, align 8, !tbaa !5
  %24 = load i64, ptr %23, align 8, !tbaa !11
  %25 = icmp eq i64 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %22, %5
  store i64 0, ptr %6, align 8
  store i32 1, ptr %19, align 4
  br label %128

27:                                               ; preds = %22
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %34

30:                                               ; preds = %27
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.USB_CBI_PROTOCOL, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  store ptr %33, ptr %12, align 8, !tbaa !5
  br label %38

34:                                               ; preds = %27
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.USB_CBI_PROTOCOL, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !16
  store ptr %37, ptr %12, align 8, !tbaa !5
  br label %38

38:                                               ; preds = %34, %30
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %39, ptr %17, align 8, !tbaa !5
  %40 = load ptr, ptr %10, align 8, !tbaa !5
  %41 = load i64, ptr %40, align 8, !tbaa !11
  store i64 %41, ptr %15, align 8, !tbaa !11
  store i64 0, ptr %18, align 8, !tbaa !11
  store i64 0, ptr %13, align 8, !tbaa !11
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = udiv i32 %42, 1000
  store i32 %43, ptr %11, align 4, !tbaa !9
  br label %44

44:                                               ; preds = %114, %101, %38
  %45 = load i64, ptr %15, align 8, !tbaa !11
  %46 = icmp ugt i64 %45, 0
  br i1 %46, label %47, label %121

47:                                               ; preds = %44
  store i32 0, ptr %14, align 4, !tbaa !9
  %48 = load i64, ptr %15, align 8, !tbaa !11
  %49 = load ptr, ptr %12, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.USB_ENDPOINT_DESCRIPTOR, ptr %49, i32 0, i32 4
  %51 = load i16, ptr %50, align 1, !tbaa !17
  %52 = zext i16 %51 to i64
  %53 = mul i64 16, %52
  %54 = icmp ugt i64 %48, %53
  br i1 %54, label %55, label %62

55:                                               ; preds = %47
  %56 = load ptr, ptr %12, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.USB_ENDPOINT_DESCRIPTOR, ptr %56, i32 0, i32 4
  %58 = load i16, ptr %57, align 1, !tbaa !17
  %59 = zext i16 %58 to i32
  %60 = mul nsw i32 16, %59
  %61 = sext i32 %60 to i64
  store i64 %61, ptr %16, align 8, !tbaa !11
  br label %64

62:                                               ; preds = %47
  %63 = load i64, ptr %15, align 8, !tbaa !11
  store i64 %63, ptr %16, align 8, !tbaa !11
  br label %64

64:                                               ; preds = %62, %55
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.USB_CBI_PROTOCOL, ptr %65, i32 0, i32 4
  %67 = load ptr, ptr %66, align 8, !tbaa !20
  %68 = getelementptr inbounds %struct._EFI_USB_IO_PROTOCOL, ptr %67, i32 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !21
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.USB_CBI_PROTOCOL, ptr %70, i32 0, i32 4
  %72 = load ptr, ptr %71, align 8, !tbaa !20
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.USB_ENDPOINT_DESCRIPTOR, ptr %73, i32 0, i32 2
  %75 = load i8, ptr %74, align 1, !tbaa !23
  %76 = load ptr, ptr %17, align 8, !tbaa !5
  %77 = load i32, ptr %11, align 4, !tbaa !9
  %78 = zext i32 %77 to i64
  %79 = call i64 %69(ptr noundef %72, i8 noundef zeroext %75, ptr noundef %76, ptr noundef %16, i64 noundef %78, ptr noundef %14)
  store i64 %79, ptr %13, align 8, !tbaa !11
  %80 = load i64, ptr %13, align 8, !tbaa !11
  %81 = icmp slt i64 %80, 0
  br i1 %81, label %82, label %114

82:                                               ; preds = %64
  %83 = load i32, ptr %14, align 4, !tbaa !9
  %84 = icmp eq i32 %83, 16
  br i1 %84, label %85, label %102

85:                                               ; preds = %82
  %86 = load i64, ptr %16, align 8, !tbaa !11
  %87 = icmp eq i64 %86, 0
  br i1 %87, label %88, label %94

88:                                               ; preds = %85
  %89 = load i64, ptr %18, align 8, !tbaa !11
  %90 = add i64 %89, 1
  store i64 %90, ptr %18, align 8, !tbaa !11
  %91 = icmp ugt i64 %90, 3
  br i1 %91, label %92, label %93

92:                                               ; preds = %88
  br label %122

93:                                               ; preds = %88
  br label %101

94:                                               ; preds = %85
  %95 = load i64, ptr %16, align 8, !tbaa !11
  %96 = load ptr, ptr %17, align 8, !tbaa !5
  %97 = getelementptr inbounds i8, ptr %96, i64 %95
  store ptr %97, ptr %17, align 8, !tbaa !5
  %98 = load i64, ptr %16, align 8, !tbaa !11
  %99 = load i64, ptr %15, align 8, !tbaa !11
  %100 = sub i64 %99, %98
  store i64 %100, ptr %15, align 8, !tbaa !11
  store i64 0, ptr %18, align 8, !tbaa !11
  br label %101

101:                                              ; preds = %94, %93
  br label %44, !llvm.loop !24

102:                                              ; preds = %82
  %103 = load i32, ptr %14, align 4, !tbaa !9
  %104 = icmp eq i32 %103, 2
  br i1 %104, label %105, label %113

105:                                              ; preds = %102
  %106 = load ptr, ptr %7, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.USB_CBI_PROTOCOL, ptr %106, i32 0, i32 4
  %108 = load ptr, ptr %107, align 8, !tbaa !20
  %109 = load ptr, ptr %12, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.USB_ENDPOINT_DESCRIPTOR, ptr %109, i32 0, i32 2
  %111 = load i8, ptr %110, align 1, !tbaa !23
  %112 = call i64 @UsbClearEndpointStall(ptr noundef %108, i8 noundef zeroext %111)
  br label %113

113:                                              ; preds = %105, %102
  br label %122

114:                                              ; preds = %64
  %115 = load i64, ptr %16, align 8, !tbaa !11
  %116 = load ptr, ptr %17, align 8, !tbaa !5
  %117 = getelementptr inbounds i8, ptr %116, i64 %115
  store ptr %117, ptr %17, align 8, !tbaa !5
  %118 = load i64, ptr %16, align 8, !tbaa !11
  %119 = load i64, ptr %15, align 8, !tbaa !11
  %120 = sub i64 %119, %118
  store i64 %120, ptr %15, align 8, !tbaa !11
  br label %44, !llvm.loop !24

121:                                              ; preds = %44
  br label %122

122:                                              ; preds = %121, %113, %92
  %123 = load i64, ptr %15, align 8, !tbaa !11
  %124 = load ptr, ptr %10, align 8, !tbaa !5
  %125 = load i64, ptr %124, align 8, !tbaa !11
  %126 = sub i64 %125, %123
  store i64 %126, ptr %124, align 8, !tbaa !11
  %127 = load i64, ptr %13, align 8, !tbaa !11
  store i64 %127, ptr %6, align 8
  store i32 1, ptr %19, align 4
  br label %128

128:                                              ; preds = %122, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %129 = load i64, ptr %6, align 8
  ret i64 %129
}

declare i64 @UsbClearEndpointStall(ptr noundef, i8 noundef zeroext) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!14, !6, i64 16}
!14 = !{!"", !15, i64 0, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!15 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8}
!16 = !{!14, !6, i64 24}
!17 = !{!18, !19, i64 4}
!18 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !19, i64 4, !7, i64 6}
!19 = !{!"short", !7, i64 0}
!20 = !{!14, !6, i64 40}
!21 = !{!22, !6, i64 8}
!22 = !{!"_EFI_USB_IO_PROTOCOL", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96}
!23 = !{!18, !7, i64 2}
!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"

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

; 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
define dso_local i64 @UsbCbiDataTransfer(ptr nocapture noundef readonly %0, i32 noundef %1, ptr noundef %2, ptr nocapture noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  %8 = icmp eq i32 %1, 2
  br i1 %8, label %50, label %9

9:                                                ; preds = %5
  %10 = load i64, ptr %3, align 8, !tbaa !5
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %50, label %12

12:                                               ; preds = %9
  %13 = icmp eq i32 %1, 0
  %.035.in.v = select i1 %13, i64 16, i64 24
  %.035.in = getelementptr inbounds i8, ptr %0, i64 %.035.in.v
  %.035 = load ptr, ptr %.035.in, align 8, !tbaa !9
  %14 = udiv i32 %4, 1000
  %15 = getelementptr inbounds i8, ptr %.035, i64 4
  %16 = getelementptr inbounds i8, ptr %0, i64 40
  %17 = getelementptr inbounds i8, ptr %.035, i64 2
  %18 = zext nneg i32 %14 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %12, %.outer
  %.0.ph63 = phi i64 [ 0, %12 ], [ %.1, %.outer ]
  %.029.ph62 = phi ptr [ %2, %12 ], [ %.130, %.outer ]
  %.031.ph61 = phi i64 [ %10, %12 ], [ %.132, %.outer ]
  br label %19

19:                                               ; preds = %.lr.ph, %44
  %.02957 = phi ptr [ %.029.ph62, %.lr.ph ], [ %46, %44 ]
  %.03156 = phi i64 [ %.031.ph61, %.lr.ph ], [ %47, %44 ]
  store i32 0, ptr %6, align 4, !tbaa !11
  %20 = load i16, ptr %15, align 1, !tbaa !13
  %21 = zext i16 %20 to i64
  %22 = shl nuw nsw i64 %21, 4
  %storemerge = call i64 @llvm.umin.i64(i64 %.03156, i64 %22)
  store i64 %storemerge, ptr %7, align 8, !tbaa !5
  %23 = load ptr, ptr %16, align 8, !tbaa !16
  %24 = getelementptr inbounds i8, ptr %23, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !19
  %26 = load i8, ptr %17, align 1, !tbaa !21
  %27 = call i64 %25(ptr noundef %23, i8 noundef zeroext %26, ptr noundef %.02957, ptr noundef nonnull %7, i64 noundef %18, ptr noundef nonnull %6) #4
  %28 = icmp slt i64 %27, 0
  br i1 %28, label %29, label %44

29:                                               ; preds = %19
  %30 = load i32, ptr %6, align 4, !tbaa !11
  switch i32 %30, label %.loopexit [
    i32 16, label %31
    i32 2, label %40
  ]

31:                                               ; preds = %29
  %32 = load i64, ptr %7, align 8, !tbaa !5
  %33 = icmp eq i64 %32, 0
  br i1 %33, label %34, label %37

34:                                               ; preds = %31
  %35 = add nuw nsw i64 %.0.ph63, 1
  %36 = icmp ugt i64 %.0.ph63, 2
  br i1 %36, label %.loopexit, label %.outer

37:                                               ; preds = %31
  %38 = getelementptr inbounds i8, ptr %.02957, i64 %32
  %39 = sub i64 %.03156, %32
  br label %.outer

.outer:                                           ; preds = %34, %37
  %.132 = phi i64 [ %.03156, %34 ], [ %39, %37 ]
  %.130 = phi ptr [ %.02957, %34 ], [ %38, %37 ]
  %.1 = phi i64 [ %35, %34 ], [ 0, %37 ]
  %.not55 = icmp eq i64 %.132, 0
  br i1 %.not55, label %.loopexit, label %.lr.ph, !llvm.loop !22

40:                                               ; preds = %29
  %41 = load ptr, ptr %16, align 8, !tbaa !16
  %42 = load i8, ptr %17, align 1, !tbaa !21
  %43 = call i64 @UsbClearEndpointStall(ptr noundef %41, i8 noundef zeroext %42) #4
  br label %.loopexit

44:                                               ; preds = %19
  %45 = load i64, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds i8, ptr %.02957, i64 %45
  %47 = sub i64 %.03156, %45
  %.not = icmp eq i64 %47, 0
  br i1 %.not, label %.loopexit, label %19, !llvm.loop !22

.loopexit:                                        ; preds = %.outer, %29, %34, %44, %40
  %.03146 = phi i64 [ %.03156, %40 ], [ 0, %44 ], [ 0, %.outer ], [ %.03156, %29 ], [ %.03156, %34 ]
  %48 = load i64, ptr %3, align 8, !tbaa !5
  %49 = sub i64 %48, %.03146
  store i64 %49, ptr %3, align 8, !tbaa !5
  br label %50

50:                                               ; preds = %5, %9, %.loopexit
  %.036 = phi i64 [ %27, %.loopexit ], [ 0, %9 ], [ 0, %5 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #4
  ret i64 %.036
}

declare i64 @UsbClearEndpointStall(ptr noundef, i8 noundef zeroext) local_unnamed_addr #2

; 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 = { 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 #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 = { 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, !6, i64 0}
!6 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !15, i64 4}
!14 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !15, i64 4, !7, i64 6}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !10, i64 40}
!17 = !{!"", !18, i64 0, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40}
!18 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !7, i64 8}
!19 = !{!20, !10, i64 8}
!20 = !{!"_EFI_USB_IO_PROTOCOL", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96}
!21 = !{!14, !7, i64 2}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


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