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

; 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.s_TxCB = type { %struct.CB_Header, i32, i16, i8, i8, [64 x i8], [16 x %struct.TBD], i32, i32, ptr, ptr, i64 }
%struct.CB_Header = type { i16, i16, i32 }
%struct.TBD = type { i32, i32 }

; 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
declare dso_local void @DelayIt(ptr noundef, i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local signext i8 @CommandWaitForCompletion(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #2
  store i16 5000, ptr %6, align 2, !tbaa !9
  br label %8

8:                                                ; preds = %22, %2
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.s_TxCB, ptr %9, i32 0, i32 0
  %11 = getelementptr inbounds %struct.CB_Header, ptr %10, i32 0, i32 0
  %12 = load i16, ptr %11, align 1, !tbaa !11
  %13 = zext i16 %12 to i32
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %20

15:                                               ; preds = %8
  %16 = load i16, ptr %6, align 2, !tbaa !9
  %17 = add i16 %16, -1
  store i16 %17, ptr %6, align 2, !tbaa !9
  %18 = sext i16 %17 to i32
  %19 = icmp sgt i32 %18, 0
  br label %20

20:                                               ; preds = %15, %8
  %21 = phi i1 [ false, %8 ], [ %19, %15 ]
  br i1 %21, label %22, label %24

22:                                               ; preds = %20
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  call void @DelayIt(ptr noundef %23, i16 noundef zeroext 10)
  br label %8, !llvm.loop !16

24:                                               ; preds = %20
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.s_TxCB, ptr %25, i32 0, i32 0
  %27 = getelementptr inbounds %struct.CB_Header, ptr %26, i32 0, i32 0
  %28 = load i16, ptr %27, align 1, !tbaa !11
  %29 = zext i16 %28 to i32
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %24
  store i8 -1, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %33

32:                                               ; preds = %24
  store i8 0, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %33

33:                                               ; preds = %32, %31
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #2
  %34 = load i8, ptr %3, align 1
  ret i8 %34
}

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 = { 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 = !{!"short", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"s_TxCB", !13, i64 0, !14, i64 8, !10, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 80, !14, i64 208, !14, i64 212, !6, i64 216, !6, i64 224, !15, i64 232}
!13 = !{!"CB_Header", !10, i64 0, !10, i64 2, !14, i64 4}
!14 = !{!"int", !7, i64 0}
!15 = !{!"long long", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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: nounwind uwtable
declare dso_local void @DelayIt(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local signext range(i8 -1, 1) i8 @CommandWaitForCompletion(ptr nocapture noundef readonly %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = load i16, ptr %0, align 1, !tbaa !5
  %4 = icmp eq i16 %3, 0
  br i1 %4, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %2, %6
  %.05 = phi i16 [ %5, %6 ], [ 5000, %2 ]
  %5 = add nsw i16 %.05, -1
  %.not = icmp eq i16 %5, 0
  br i1 %.not, label %.critedge.loopexit, label %6

6:                                                ; preds = %.lr.ph
  tail call void @DelayIt(ptr noundef %1, i16 noundef zeroext 10)
  %7 = load i16, ptr %0, align 1, !tbaa !5
  %8 = icmp eq i16 %7, 0
  br i1 %8, label %.lr.ph, label %.critedge.loopexit, !llvm.loop !14

.critedge.loopexit:                               ; preds = %6, %.lr.ph
  %9 = sext i1 %.not to i8
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %2
  %.lcssa = phi i8 [ 0, %2 ], [ %9, %.critedge.loopexit ]
  ret i8 %.lcssa
}

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" }

!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, !8, i64 0}
!6 = !{!"s_TxCB", !7, i64 0, !11, i64 8, !8, i64 12, !9, i64 14, !9, i64 15, !9, i64 16, !9, i64 80, !11, i64 208, !11, i64 212, !12, i64 216, !12, i64 224, !13, i64 232}
!7 = !{!"CB_Header", !8, i64 0, !8, i64 2, !11, i64 4}
!8 = !{!"short", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"int", !9, i64 0}
!12 = !{!"any pointer", !9, i64 0}
!13 = !{!"long long", !9, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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