; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Universal (DpcDispatchDpc)

; 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._LIST_ENTRY = type { ptr, ptr }
%struct.EFI_BOOT_SERVICES = type { %struct.EFI_TABLE_HEADER, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.EFI_TABLE_HEADER = type { i64, i32, i32, i32, i32 }
%struct.DPC_ENTRY = type { %struct._LIST_ENTRY, ptr, ptr }

@mDpcQueueDepth = external dso_local global i64, align 8
@mDpcEntryFreeList = external dso_local global %struct._LIST_ENTRY, align 8
@gBS = external global ptr, align 8
@mDpcQueue = external dso_local global [32 x %struct._LIST_ENTRY], align 16

; Function Attrs: nounwind uwtable
define dso_local i64 @DpcDispatchDpc(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 -9223372036854775794, ptr %3, align 8, !tbaa !9
  %7 = load ptr, ptr @gBS, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %7, i32 0, i32 1
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = call i64 %9(i64 noundef 31)
  store i64 %10, ptr %4, align 8, !tbaa !9
  %11 = load i64, ptr @mDpcQueueDepth, align 8, !tbaa !9
  %12 = icmp ugt i64 %11, 0
  br i1 %12, label %13, label %56

13:                                               ; preds = %1
  store i64 31, ptr %5, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %52, %13
  %15 = load i64, ptr %5, align 8, !tbaa !9
  %16 = load i64, ptr %4, align 8, !tbaa !9
  %17 = icmp uge i64 %15, %16
  br i1 %17, label %18, label %55

18:                                               ; preds = %14
  br label %19

19:                                               ; preds = %25, %18
  %20 = load i64, ptr %5, align 8, !tbaa !9
  %21 = getelementptr inbounds [32 x %struct._LIST_ENTRY], ptr @mDpcQueue, i64 0, i64 %20
  %22 = call zeroext i8 @IsListEmpty(ptr noundef %21)
  %23 = icmp ne i8 %22, 0
  %24 = xor i1 %23, true
  br i1 %24, label %25, label %51

25:                                               ; preds = %19
  %26 = load i64, ptr %5, align 8, !tbaa !9
  %27 = getelementptr inbounds [32 x %struct._LIST_ENTRY], ptr @mDpcQueue, i64 0, i64 %26
  %28 = call ptr @GetFirstNode(ptr noundef %27)
  store ptr %28, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.DPC_ENTRY, ptr %29, i32 0, i32 0
  %31 = call ptr @RemoveEntryList(ptr noundef %30)
  %32 = load i64, ptr @mDpcQueueDepth, align 8, !tbaa !9
  %33 = add i64 %32, -1
  store i64 %33, ptr @mDpcQueueDepth, align 8, !tbaa !9
  %34 = load ptr, ptr @gBS, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = load i64, ptr %5, align 8, !tbaa !9
  call void %36(i64 noundef %37)
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.DPC_ENTRY, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.DPC_ENTRY, ptr %41, i32 0, i32 2
  %43 = load ptr, ptr %42, align 8, !tbaa !19
  call void %40(ptr noundef %43)
  store i64 0, ptr %3, align 8, !tbaa !9
  %44 = load ptr, ptr @gBS, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = call i64 %46(i64 noundef 31)
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.DPC_ENTRY, ptr %48, i32 0, i32 0
  %50 = call ptr @InsertTailList(ptr noundef @mDpcEntryFreeList, ptr noundef %49)
  br label %19, !llvm.loop !20

51:                                               ; preds = %19
  br label %52

52:                                               ; preds = %51
  %53 = load i64, ptr %5, align 8, !tbaa !9
  %54 = add i64 %53, -1
  store i64 %54, ptr %5, align 8, !tbaa !9
  br label %14, !llvm.loop !22

55:                                               ; preds = %14
  br label %56

56:                                               ; preds = %55, %1
  %57 = load ptr, ptr @gBS, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %58, align 8, !tbaa !15
  %60 = load i64, ptr %4, align 8, !tbaa !9
  call void %59(i64 noundef %60)
  %61 = load i64, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret i64 %61
}

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

declare zeroext i8 @IsListEmpty(ptr noundef) #2

declare ptr @InsertTailList(ptr noundef, ptr noundef) #2

declare ptr @GetFirstNode(ptr noundef) #2

declare ptr @RemoveEntryList(ptr noundef) #2

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

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"long long", !7, i64 0}
!11 = !{!12, !6, i64 24}
!12 = !{!"", !13, i64 0, !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, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !6, i64 368}
!13 = !{!"", !10, i64 0, !14, i64 8, !14, i64 12, !14, i64 16, !14, i64 20}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 32}
!16 = !{!17, !6, i64 16}
!17 = !{!"", !18, i64 0, !6, i64 16, !6, i64 24}
!18 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!19 = !{!17, !6, i64 24}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}


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

%struct._LIST_ENTRY = type { ptr, ptr }

@mDpcQueueDepth = external dso_local local_unnamed_addr global i64, align 8
@mDpcEntryFreeList = external dso_local global %struct._LIST_ENTRY, align 8
@gBS = external local_unnamed_addr global ptr, align 8
@mDpcQueue = external dso_local global [32 x %struct._LIST_ENTRY], align 16

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775794, 1) i64 @DpcDispatchDpc(ptr nocapture noundef readnone %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @gBS, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 24
  %4 = load ptr, ptr %3, align 8, !tbaa !9
  %5 = tail call i64 %4(i64 noundef 31) #2
  %6 = load i64, ptr @mDpcQueueDepth, align 8, !tbaa !14
  %.not = icmp eq i64 %6, 0
  %.not1216 = icmp ugt i64 %5, 31
  %or.cond = select i1 %.not, i1 true, i1 %.not1216
  br i1 %or.cond, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %1, %._crit_edge
  %.018 = phi i64 [ %.1.lcssa, %._crit_edge ], [ -9223372036854775794, %1 ]
  %.01117 = phi i64 [ %26, %._crit_edge ], [ 31, %1 ]
  %7 = getelementptr inbounds [32 x %struct._LIST_ENTRY], ptr @mDpcQueue, i64 0, i64 %.01117
  %8 = tail call zeroext i8 @IsListEmpty(ptr noundef nonnull %7) #2
  %.not1315 = icmp eq i8 %8, 0
  br i1 %.not1315, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %9 = tail call ptr @GetFirstNode(ptr noundef nonnull %7) #2
  %10 = tail call ptr @RemoveEntryList(ptr noundef %9) #2
  %11 = load i64, ptr @mDpcQueueDepth, align 8, !tbaa !14
  %12 = add i64 %11, -1
  store i64 %12, ptr @mDpcQueueDepth, align 8, !tbaa !14
  %13 = load ptr, ptr @gBS, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 32
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  tail call void %15(i64 noundef %.01117) #2
  %16 = getelementptr inbounds i8, ptr %9, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !16
  %18 = getelementptr inbounds i8, ptr %9, i64 24
  %19 = load ptr, ptr %18, align 8, !tbaa !19
  tail call void %17(ptr noundef %19) #2
  %20 = load ptr, ptr @gBS, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 24
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %23 = tail call i64 %22(i64 noundef 31) #2
  %24 = tail call ptr @InsertTailList(ptr noundef nonnull @mDpcEntryFreeList, ptr noundef %9) #2
  %25 = tail call zeroext i8 @IsListEmpty(ptr noundef nonnull %7) #2
  %.not13 = icmp eq i8 %25, 0
  br i1 %.not13, label %.lr.ph, label %._crit_edge, !llvm.loop !20

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.1.lcssa = phi i64 [ %.018, %.preheader ], [ 0, %.lr.ph ]
  %26 = add i64 %.01117, -1
  %.not12 = icmp ult i64 %26, %5
  br i1 %.not12, label %.loopexit, label %.preheader, !llvm.loop !22

.loopexit:                                        ; preds = %._crit_edge, %1
  %.2 = phi i64 [ -9223372036854775794, %1 ], [ %.1.lcssa, %._crit_edge ]
  %27 = load ptr, ptr @gBS, align 8, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %27, i64 32
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  tail call void %29(i64 noundef %5) #2
  ret i64 %.2
}

declare zeroext i8 @IsListEmpty(ptr noundef) local_unnamed_addr #1

declare ptr @InsertTailList(ptr noundef, ptr noundef) local_unnamed_addr #1

declare ptr @GetFirstNode(ptr noundef) local_unnamed_addr #1

declare ptr @RemoveEntryList(ptr noundef) local_unnamed_addr #1

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

!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, !6, i64 24}
!10 = !{!"", !11, i64 0, !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, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !6, i64 136, !6, i64 144, !6, i64 152, !6, i64 160, !6, i64 168, !6, i64 176, !6, i64 184, !6, i64 192, !6, i64 200, !6, i64 208, !6, i64 216, !6, i64 224, !6, i64 232, !6, i64 240, !6, i64 248, !6, i64 256, !6, i64 264, !6, i64 272, !6, i64 280, !6, i64 288, !6, i64 296, !6, i64 304, !6, i64 312, !6, i64 320, !6, i64 328, !6, i64 336, !6, i64 344, !6, i64 352, !6, i64 360, !6, i64 368}
!11 = !{!"", !12, i64 0, !13, i64 8, !13, i64 12, !13, i64 16, !13, i64 20}
!12 = !{!"long long", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !12, i64 0}
!15 = !{!10, !6, i64 32}
!16 = !{!17, !6, i64 16}
!17 = !{!"", !18, i64 0, !6, i64 16, !6, i64 24}
!18 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!19 = !{!17, !6, i64 24}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}


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