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

; 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.EFI_SCSI_SENSE_DATA = type { i8, i8, i8, [4 x i8], i8, [4 x i8], i8, i8, i8, [3 x i8] }

; 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 zeroext i8 @ScsiDiskIsDriveReady(ptr noundef %0, i64 noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  store i8 1, ptr %9, align 1, !tbaa !11
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %10, align 1, !tbaa !11
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %11, ptr %7, align 8, !tbaa !5
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %43, %3
  %13 = load i64, ptr %8, align 8, !tbaa !9
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = icmp ult i64 %13, %14
  br i1 %15, label %16, label %46

16:                                               ; preds = %12
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.EFI_SCSI_SENSE_DATA, ptr %17, i32 0, i32 2
  %19 = load i8, ptr %18, align 1
  %20 = and i8 %19, 15
  %21 = zext i8 %20 to i32
  switch i32 %21, label %39 [
    i32 2, label %22
  ]

22:                                               ; preds = %16
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.EFI_SCSI_SENSE_DATA, ptr %23, i32 0, i32 6
  %25 = load i8, ptr %24, align 1, !tbaa !12
  %26 = zext i8 %25 to i32
  switch i32 %26, label %37 [
    i32 4, label %27
  ]

27:                                               ; preds = %22
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.EFI_SCSI_SENSE_DATA, ptr %28, i32 0, i32 7
  %30 = load i8, ptr %29, align 1, !tbaa !14
  %31 = zext i8 %30 to i32
  switch i32 %31, label %34 [
    i32 1, label %32
  ]

32:                                               ; preds = %27
  store i8 0, ptr %9, align 1, !tbaa !11
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 1, ptr %33, align 1, !tbaa !11
  br label %36

34:                                               ; preds = %27
  store i8 0, ptr %9, align 1, !tbaa !11
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %35, align 1, !tbaa !11
  br label %36

36:                                               ; preds = %34, %32
  br label %38

37:                                               ; preds = %22
  br label %38

38:                                               ; preds = %37, %36
  br label %40

39:                                               ; preds = %16
  br label %40

40:                                               ; preds = %39, %38
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.EFI_SCSI_SENSE_DATA, ptr %41, i32 1
  store ptr %42, ptr %7, align 8, !tbaa !5
  br label %43

43:                                               ; preds = %40
  %44 = load i64, ptr %8, align 8, !tbaa !9
  %45 = add i64 %44, 1
  store i64 %45, ptr %8, align 8, !tbaa !9
  br label %12, !llvm.loop !15

46:                                               ; preds = %12
  %47 = load i8, ptr %9, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  ret i8 %47
}

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 = !{!"long long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !7, i64 12}
!13 = !{!"", !7, i64 0, !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 2, !7, i64 2, !7, i64 2, !7, i64 3, !7, i64 7, !7, i64 8, !7, i64 12, !7, i64 13, !7, i64 14, !7, i64 15}
!14 = !{!13, !7, i64 13}
!15 = distinct !{!15, !16}
!16 = !{!"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: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local zeroext range(i8 0, 2) i8 @ScsiDiskIsDriveReady(ptr nocapture noundef readonly %0, i64 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  store i8 0, ptr %2, align 1, !tbaa !5
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %xtraiter = and i64 %1, 1
  %4 = icmp eq i64 %1, 1
  br i1 %4, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %1, -2
  br label %.lr.ph

.lr.ph:                                           ; preds = %21, %.lr.ph.preheader.new
  %.017 = phi i8 [ 1, %.lr.ph.preheader.new ], [ %.1.1, %21 ]
  %.01415 = phi ptr [ %0, %.lr.ph.preheader.new ], [ %22, %21 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %21 ]
  %5 = getelementptr inbounds i8, ptr %.01415, i64 2
  %6 = load i8, ptr %5, align 1
  %7 = and i8 %6, 15
  %cond = icmp eq i8 %7, 2
  br i1 %cond, label %8, label %.lr.ph.1

8:                                                ; preds = %.lr.ph
  %9 = getelementptr inbounds i8, ptr %.01415, i64 12
  %10 = load i8, ptr %9, align 1, !tbaa !8
  %cond1 = icmp eq i8 %10, 4
  br i1 %cond1, label %.sink.split, label %.lr.ph.1

.sink.split:                                      ; preds = %8
  %11 = getelementptr inbounds i8, ptr %.01415, i64 13
  %12 = load i8, ptr %11, align 1, !tbaa !10
  %cond2 = icmp eq i8 %12, 1
  %. = zext i1 %cond2 to i8
  store i8 %., ptr %2, align 1, !tbaa !5
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.sink.split, %.lr.ph, %8
  %.1 = phi i8 [ %.017, %8 ], [ %.017, %.lr.ph ], [ 0, %.sink.split ]
  %13 = getelementptr inbounds i8, ptr %.01415, i64 20
  %14 = load i8, ptr %13, align 1
  %15 = and i8 %14, 15
  %cond.1 = icmp eq i8 %15, 2
  br i1 %cond.1, label %16, label %21

16:                                               ; preds = %.lr.ph.1
  %17 = getelementptr inbounds i8, ptr %.01415, i64 30
  %18 = load i8, ptr %17, align 1, !tbaa !8
  %cond1.1 = icmp eq i8 %18, 4
  br i1 %cond1.1, label %.sink.split.1, label %21

.sink.split.1:                                    ; preds = %16
  %19 = getelementptr inbounds i8, ptr %.01415, i64 31
  %20 = load i8, ptr %19, align 1, !tbaa !10
  %cond2.1 = icmp eq i8 %20, 1
  %..1 = zext i1 %cond2.1 to i8
  store i8 %..1, ptr %2, align 1, !tbaa !5
  br label %21

21:                                               ; preds = %.sink.split.1, %16, %.lr.ph.1
  %.1.1 = phi i8 [ %.1, %16 ], [ %.1, %.lr.ph.1 ], [ 0, %.sink.split.1 ]
  %22 = getelementptr inbounds i8, ptr %.01415, i64 36
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !11

._crit_edge.loopexit.unr-lcssa:                   ; preds = %21, %.lr.ph.preheader
  %.1.lcssa.ph = phi i8 [ poison, %.lr.ph.preheader ], [ %.1.1, %21 ]
  %.017.unr = phi i8 [ 1, %.lr.ph.preheader ], [ %.1.1, %21 ]
  %.01415.unr = phi ptr [ %0, %.lr.ph.preheader ], [ %22, %21 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %23 = getelementptr inbounds i8, ptr %.01415.unr, i64 2
  %24 = load i8, ptr %23, align 1
  %25 = and i8 %24, 15
  %cond.epil = icmp eq i8 %25, 2
  br i1 %cond.epil, label %26, label %._crit_edge

26:                                               ; preds = %.lr.ph.epil
  %27 = getelementptr inbounds i8, ptr %.01415.unr, i64 12
  %28 = load i8, ptr %27, align 1, !tbaa !8
  %cond1.epil = icmp eq i8 %28, 4
  br i1 %cond1.epil, label %.sink.split.epil, label %._crit_edge

.sink.split.epil:                                 ; preds = %26
  %29 = getelementptr inbounds i8, ptr %.01415.unr, i64 13
  %30 = load i8, ptr %29, align 1, !tbaa !10
  %cond2.epil = icmp eq i8 %30, 1
  %..epil = zext i1 %cond2.epil to i8
  store i8 %..epil, ptr %2, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.sink.split.epil, %26, %.lr.ph.epil, %3
  %.0.lcssa = phi i8 [ 1, %3 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %.017.unr, %26 ], [ %.017.unr, %.lr.ph.epil ], [ 0, %.sink.split.epil ]
  ret i8 %.0.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !6, i64 12}
!9 = !{!"", !6, i64 0, !6, i64 0, !6, i64 1, !6, i64 2, !6, i64 2, !6, i64 2, !6, i64 2, !6, i64 3, !6, i64 7, !6, i64 8, !6, i64 12, !6, i64 13, !6, i64 14, !6, i64 15}
!10 = !{!9, !6, i64 13}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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