; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/SourceLevelDebugPkg (XhcSyncEventRing)

; 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._EVENT_RING = type <{ i64, i64, i32, i64, i64, i32 }>
%struct._TRB_TEMPLATE = type { i32, i32, i32, i32 }

@.str = external hidden unnamed_addr constant [191 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [18 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 zeroext i8 @DebugAssertEnabled() #1

declare void @DebugAssert(ptr noundef, 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
define dso_local i64 @XhcSyncEventRing(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  br label %7

7:                                                ; preds = %2
  %8 = call zeroext i8 @DebugAssertEnabled()
  %9 = icmp ne i8 %8, 0
  br i1 %9, label %10, label %15

10:                                               ; preds = %7
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  call void @DebugAssert(ptr noundef @.str, i64 noundef 98, ptr noundef @.str.5)
  br label %14

14:                                               ; preds = %13, %10
  br label %15

15:                                               ; preds = %14, %7
  br label %16

16:                                               ; preds = %15
  br label %17

17:                                               ; preds = %16
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct._EVENT_RING, ptr %18, i32 0, i32 4
  %20 = load i64, ptr %19, align 1, !tbaa !9
  %21 = inttoptr i64 %20 to ptr
  store ptr %21, ptr %6, align 8, !tbaa !5
  store i64 0, ptr %5, align 8, !tbaa !13
  br label %22

22:                                               ; preds = %68, %17
  %23 = load i64, ptr %5, align 8, !tbaa !13
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct._EVENT_RING, ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 1, !tbaa !14
  %27 = zext i32 %26 to i64
  %28 = icmp ult i64 %23, %27
  br i1 %28, label %29, label %71

29:                                               ; preds = %22
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct._TRB_TEMPLATE, ptr %30, i32 0, i32 3
  %32 = load i32, ptr %31, align 1
  %33 = and i32 %32, 1
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct._EVENT_RING, ptr %34, i32 0, i32 5
  %36 = load i32, ptr %35, align 1, !tbaa !15
  %37 = icmp ne i32 %33, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %29
  br label %71

39:                                               ; preds = %29
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct._TRB_TEMPLATE, ptr %40, i32 1
  store ptr %41, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = ptrtoint ptr %42 to i64
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._EVENT_RING, ptr %44, i32 0, i32 1
  %46 = load i64, ptr %45, align 1, !tbaa !16
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct._EVENT_RING, ptr %47, i32 0, i32 2
  %49 = load i32, ptr %48, align 1, !tbaa !14
  %50 = zext i32 %49 to i64
  %51 = mul i64 16, %50
  %52 = add i64 %46, %51
  %53 = icmp uge i64 %43, %52
  br i1 %53, label %54, label %67

54:                                               ; preds = %39
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct._EVENT_RING, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 1, !tbaa !16
  %58 = inttoptr i64 %57 to ptr
  store ptr %58, ptr %6, align 8, !tbaa !5
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct._EVENT_RING, ptr %59, i32 0, i32 5
  %61 = load i32, ptr %60, align 1, !tbaa !15
  %62 = icmp ne i32 %61, 0
  %63 = zext i1 %62 to i64
  %64 = select i1 %62, i32 0, i32 1
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct._EVENT_RING, ptr %65, i32 0, i32 5
  store i32 %64, ptr %66, align 1, !tbaa !15
  br label %67

67:                                               ; preds = %54, %39
  br label %68

68:                                               ; preds = %67
  %69 = load i64, ptr %5, align 8, !tbaa !13
  %70 = add i64 %69, 1
  store i64 %70, ptr %5, align 8, !tbaa !13
  br label %22, !llvm.loop !17

71:                                               ; preds = %38, %22
  %72 = load i64, ptr %5, align 8, !tbaa !13
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct._EVENT_RING, ptr %73, i32 0, i32 2
  %75 = load i32, ptr %74, align 1, !tbaa !14
  %76 = zext i32 %75 to i64
  %77 = icmp ult i64 %72, %76
  br i1 %77, label %78, label %83

78:                                               ; preds = %71
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = ptrtoint ptr %79 to i64
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct._EVENT_RING, ptr %81, i32 0, i32 3
  store i64 %80, ptr %82, align 1, !tbaa !19
  br label %91

83:                                               ; preds = %71
  br label %84

84:                                               ; preds = %83
  %85 = call zeroext i8 @DebugAssertEnabled()
  %86 = icmp ne i8 %85, 0
  br i1 %86, label %87, label %88

87:                                               ; preds = %84
  call void @DebugAssert(ptr noundef @.str, i64 noundef 122, ptr noundef @.str.6)
  br label %88

88:                                               ; preds = %87, %84
  br label %89

89:                                               ; preds = %88
  br label %90

90:                                               ; preds = %89
  br label %91

91:                                               ; preds = %90, %78
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret i64 0
}

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, !11, i64 28}
!10 = !{!"_EVENT_RING", !11, i64 0, !11, i64 8, !12, i64 16, !11, i64 20, !11, i64 28, !12, i64 36}
!11 = !{!"long long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !12, i64 16}
!15 = !{!10, !12, i64 36}
!16 = !{!10, !11, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!10, !11, i64 20}


; 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 = external hidden unnamed_addr constant [191 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [18 x i8], align 1

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #0

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @XhcSyncEventRing(ptr nocapture noundef readnone %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = tail call zeroext i8 @DebugAssertEnabled() #2
  %4 = icmp eq i8 %3, 0
  %5 = icmp ne ptr %1, null
  %or.cond = or i1 %5, %4
  br i1 %or.cond, label %7, label %6

6:                                                ; preds = %2
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 98, ptr noundef nonnull @.str.5) #2
  br label %7

7:                                                ; preds = %6, %2
  %8 = getelementptr inbounds i8, ptr %1, i64 16
  %9 = load i32, ptr %8, align 1, !tbaa !5
  %10 = zext i32 %9 to i64
  %.not27 = icmp eq i32 %9, 0
  br i1 %.not27, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7
  %11 = getelementptr inbounds i8, ptr %1, i64 28
  %12 = load i64, ptr %11, align 1, !tbaa !11
  %13 = inttoptr i64 %12 to ptr
  %14 = getelementptr inbounds i8, ptr %1, i64 36
  %15 = getelementptr inbounds i8, ptr %1, i64 8
  %16 = shl nuw nsw i64 %10, 4
  %.promoted = load i32, ptr %14, align 1, !tbaa !12
  br label %17

17:                                               ; preds = %.lr.ph, %30
  %18 = phi i32 [ %.promoted, %.lr.ph ], [ %31, %30 ]
  %.026 = phi ptr [ %13, %.lr.ph ], [ %.1, %30 ]
  %.01825 = phi i64 [ 0, %.lr.ph ], [ %32, %30 ]
  %19 = getelementptr inbounds i8, ptr %.026, i64 12
  %20 = load i32, ptr %19, align 1
  %21 = and i32 %20, 1
  %.not = icmp eq i32 %21, %18
  br i1 %.not, label %22, label %33

22:                                               ; preds = %17
  %23 = getelementptr inbounds i8, ptr %.026, i64 16
  %24 = ptrtoint ptr %23 to i64
  %25 = load i64, ptr %15, align 1, !tbaa !13
  %26 = add i64 %25, %16
  %.not21 = icmp ugt i64 %26, %24
  br i1 %.not21, label %30, label %27

27:                                               ; preds = %22
  %28 = inttoptr i64 %25 to ptr
  %.not22 = icmp eq i32 %18, 0
  %29 = zext i1 %.not22 to i32
  store i32 %29, ptr %14, align 1, !tbaa !12
  br label %30

30:                                               ; preds = %22, %27
  %31 = phi i32 [ %29, %27 ], [ %18, %22 ]
  %.1 = phi ptr [ %28, %27 ], [ %23, %22 ]
  %32 = add nuw nsw i64 %.01825, 1
  %exitcond.not = icmp eq i64 %32, %10
  br i1 %exitcond.not, label %._crit_edge, label %17, !llvm.loop !14

33:                                               ; preds = %17
  %34 = ptrtoint ptr %.026 to i64
  %35 = getelementptr inbounds i8, ptr %1, i64 20
  store i64 %34, ptr %35, align 1, !tbaa !16
  br label %38

._crit_edge:                                      ; preds = %30, %7
  %36 = tail call zeroext i8 @DebugAssertEnabled() #2
  %.not23 = icmp eq i8 %36, 0
  br i1 %.not23, label %38, label %37

37:                                               ; preds = %._crit_edge
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 122, ptr noundef nonnull @.str.6) #2
  br label %38

38:                                               ; preds = %._crit_edge, %37, %33
  ret i64 0
}

attributes #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 = { 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 = { 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, !10, i64 16}
!6 = !{!"_EVENT_RING", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 20, !7, i64 28, !10, i64 36}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 28}
!12 = !{!6, !10, i64 36}
!13 = !{!6, !7, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !7, i64 20}


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