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

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

17:                                               ; preds = %4
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = icmp ugt i64 %18, 31
  br i1 %19, label %20, label %21

20:                                               ; preds = %17, %4
  store i64 -9223372036854775806, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %95

21:                                               ; preds = %17
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store i64 -9223372036854775806, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %95

25:                                               ; preds = %21
  store i64 0, ptr %10, align 8, !tbaa !9
  %26 = load ptr, ptr @gBS, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %26, i32 0, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %29 = call i64 %28(i64 noundef 31)
  store i64 %29, ptr %11, align 8, !tbaa !9
  %30 = call zeroext i8 @IsListEmpty(ptr noundef @mDpcEntryFreeList)
  %31 = icmp ne i8 %30, 0
  br i1 %31, label %32, label %65

32:                                               ; preds = %25
  %33 = load i64, ptr %11, align 8, !tbaa !9
  %34 = icmp ugt i64 %33, 16
  br i1 %34, label %35, label %36

35:                                               ; preds = %32
  store i64 -9223372036854775799, ptr %10, align 8, !tbaa !9
  br label %89

36:                                               ; preds = %32
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %61, %36
  %38 = load i64, ptr %13, align 8, !tbaa !9
  %39 = icmp ult i64 %38, 64
  br i1 %39, label %40, label %64

40:                                               ; preds = %37
  %41 = load ptr, ptr @gBS, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %41, i32 0, i32 2
  %43 = load ptr, ptr %42, align 8, !tbaa !15
  %44 = load i64, ptr %11, align 8, !tbaa !9
  call void %43(i64 noundef %44)
  %45 = call ptr @AllocatePool(i64 noundef 32)
  store ptr %45, ptr %12, align 8, !tbaa !5
  %46 = load ptr, ptr @gBS, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !11
  %49 = call i64 %48(i64 noundef 31)
  %50 = load ptr, ptr %12, align 8, !tbaa !5
  %51 = icmp eq ptr %50, null
  br i1 %51, label %52, label %57

52:                                               ; preds = %40
  %53 = call zeroext i8 @IsListEmpty(ptr noundef @mDpcEntryFreeList)
  %54 = icmp ne i8 %53, 0
  br i1 %54, label %55, label %56

55:                                               ; preds = %52
  store i64 -9223372036854775799, ptr %10, align 8, !tbaa !9
  br label %89

56:                                               ; preds = %52
  br label %57

57:                                               ; preds = %56, %40
  %58 = load ptr, ptr %12, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.DPC_ENTRY, ptr %58, i32 0, i32 0
  %60 = call ptr @InsertTailList(ptr noundef @mDpcEntryFreeList, ptr noundef %59)
  br label %61

61:                                               ; preds = %57
  %62 = load i64, ptr %13, align 8, !tbaa !9
  %63 = add i64 %62, 1
  store i64 %63, ptr %13, align 8, !tbaa !9
  br label %37, !llvm.loop !16

64:                                               ; preds = %37
  br label %65

65:                                               ; preds = %64, %25
  %66 = call ptr @GetFirstNode(ptr noundef @mDpcEntryFreeList)
  store ptr %66, ptr %12, align 8, !tbaa !5
  %67 = load ptr, ptr %12, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.DPC_ENTRY, ptr %67, i32 0, i32 0
  %69 = call ptr @RemoveEntryList(ptr noundef %68)
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = load ptr, ptr %12, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.DPC_ENTRY, ptr %71, i32 0, i32 1
  store ptr %70, ptr %72, align 8, !tbaa !18
  %73 = load ptr, ptr %9, align 8, !tbaa !5
  %74 = load ptr, ptr %12, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.DPC_ENTRY, ptr %74, i32 0, i32 2
  store ptr %73, ptr %75, align 8, !tbaa !21
  %76 = load i64, ptr %7, align 8, !tbaa !9
  %77 = getelementptr inbounds [32 x %struct._LIST_ENTRY], ptr @mDpcQueue, i64 0, i64 %76
  %78 = load ptr, ptr %12, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.DPC_ENTRY, ptr %78, i32 0, i32 0
  %80 = call ptr @InsertTailList(ptr noundef %77, ptr noundef %79)
  %81 = load i64, ptr @mDpcQueueDepth, align 8, !tbaa !9
  %82 = add i64 %81, 1
  store i64 %82, ptr @mDpcQueueDepth, align 8, !tbaa !9
  %83 = load i64, ptr @mDpcQueueDepth, align 8, !tbaa !9
  %84 = load i64, ptr @mMaxDpcQueueDepth, align 8, !tbaa !9
  %85 = icmp ugt i64 %83, %84
  br i1 %85, label %86, label %88

86:                                               ; preds = %65
  %87 = load i64, ptr @mDpcQueueDepth, align 8, !tbaa !9
  store i64 %87, ptr @mMaxDpcQueueDepth, align 8, !tbaa !9
  br label %88

88:                                               ; preds = %86, %65
  br label %89

89:                                               ; preds = %88, %55, %35
  %90 = load ptr, ptr @gBS, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.EFI_BOOT_SERVICES, ptr %90, i32 0, i32 2
  %92 = load ptr, ptr %91, align 8, !tbaa !15
  %93 = load i64, ptr %11, align 8, !tbaa !9
  call void %92(i64 noundef %93)
  %94 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %94, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %95

95:                                               ; preds = %89, %24, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %96 = load i64, ptr %5, align 8
  ret i64 %96
}

; 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 @AllocatePool(i64 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 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!19, !6, i64 16}
!19 = !{!"", !20, i64 0, !6, i64 16, !6, i64 24}
!20 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!21 = !{!19, !6, i64 24}


; 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
@mMaxDpcQueueDepth = 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 -9223372036854775806, 1) i64 @DpcQueueDpc(ptr nocapture noundef readnone %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = add i64 %1, -32
  %or.cond = icmp ult i64 %5, -28
  %6 = icmp eq ptr %2, null
  %or.cond26 = or i1 %or.cond, %6
  br i1 %or.cond26, label %43, label %7

7:                                                ; preds = %4
  %8 = load ptr, ptr @gBS, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %8, i64 24
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = tail call i64 %10(i64 noundef 31) #2
  %12 = tail call zeroext i8 @IsListEmpty(ptr noundef nonnull @mDpcEntryFreeList) #2
  %.not = icmp eq i8 %12, 0
  br i1 %.not, label %.loopexit, label %13

13:                                               ; preds = %7
  %14 = icmp ugt i64 %11, 16
  br i1 %14, label %.loopexit27, label %.preheader

.preheader:                                       ; preds = %13, %26
  %.028 = phi i64 [ %28, %26 ], [ 0, %13 ]
  %15 = load ptr, ptr @gBS, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 32
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  tail call void %17(i64 noundef %11) #2
  %18 = tail call ptr @AllocatePool(i64 noundef 32) #2
  %19 = load ptr, ptr @gBS, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 24
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  %22 = tail call i64 %21(i64 noundef 31) #2
  %23 = icmp eq ptr %18, null
  br i1 %23, label %24, label %26

24:                                               ; preds = %.preheader
  %25 = tail call zeroext i8 @IsListEmpty(ptr noundef nonnull @mDpcEntryFreeList) #2
  %.not25 = icmp eq i8 %25, 0
  br i1 %.not25, label %26, label %.loopexit27

26:                                               ; preds = %24, %.preheader
  %27 = tail call ptr @InsertTailList(ptr noundef nonnull @mDpcEntryFreeList, ptr noundef %18) #2
  %28 = add nuw nsw i64 %.028, 1
  %exitcond.not = icmp eq i64 %28, 64
  br i1 %exitcond.not, label %.loopexit, label %.preheader, !llvm.loop !15

.loopexit:                                        ; preds = %26, %7
  %29 = tail call ptr @GetFirstNode(ptr noundef nonnull @mDpcEntryFreeList) #2
  %30 = tail call ptr @RemoveEntryList(ptr noundef %29) #2
  %31 = getelementptr inbounds i8, ptr %29, i64 16
  store ptr %2, ptr %31, align 8, !tbaa !17
  %32 = getelementptr inbounds i8, ptr %29, i64 24
  store ptr %3, ptr %32, align 8, !tbaa !20
  %33 = getelementptr inbounds [32 x %struct._LIST_ENTRY], ptr @mDpcQueue, i64 0, i64 %1
  %34 = tail call ptr @InsertTailList(ptr noundef nonnull %33, ptr noundef %29) #2
  %35 = load i64, ptr @mDpcQueueDepth, align 8, !tbaa !21
  %36 = add i64 %35, 1
  store i64 %36, ptr @mDpcQueueDepth, align 8, !tbaa !21
  %37 = load i64, ptr @mMaxDpcQueueDepth, align 8, !tbaa !21
  %38 = icmp ugt i64 %36, %37
  br i1 %38, label %39, label %.loopexit27

39:                                               ; preds = %.loopexit
  store i64 %36, ptr @mMaxDpcQueueDepth, align 8, !tbaa !21
  br label %.loopexit27

.loopexit27:                                      ; preds = %24, %13, %.loopexit, %39
  %.020 = phi i64 [ 0, %39 ], [ 0, %.loopexit ], [ -9223372036854775799, %13 ], [ -9223372036854775799, %24 ]
  %40 = load ptr, ptr @gBS, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i64 32
  %42 = load ptr, ptr %41, align 8, !tbaa !14
  tail call void %42(i64 noundef %11) #2
  br label %43

43:                                               ; preds = %4, %.loopexit27
  %.019 = phi i64 [ %.020, %.loopexit27 ], [ -9223372036854775806, %4 ]
  ret i64 %.019
}

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

declare ptr @AllocatePool(i64 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 = !{!10, !6, i64 32}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!18, !6, i64 16}
!18 = !{!"", !19, i64 0, !6, i64 16, !6, i64 24}
!19 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!20 = !{!18, !6, i64 24}
!21 = !{!12, !12, i64 0}


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