; LLVM IR from thalium/icebox :: third_party/virtualbox/src/VBox/Devices/EFI/Firmware/ShellPkg/Library (GetHIDevicePath)

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

%union.EFI_DEV_PATH = type <{ %struct.INFINIBAND_DEVICE_PATH, [12 x i8] }>
%struct.INFINIBAND_DEVICE_PATH = type { %struct.EFI_DEVICE_PATH_PROTOCOL, i32, [16 x i8], i64, i64, i64 }
%struct.EFI_DEVICE_PATH_PROTOCOL = type { i8, i8, [2 x i8] }

@.str = external hidden unnamed_addr constant [155 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [27 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 ptr @AllocateZeroPool(i64 noundef) #1

declare zeroext i8 @DebugAssertEnabled() #1

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) #1

declare void @FreePool(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
declare dso_local zeroext i8 @IsHIDevicePathNode(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @GetHIDevicePath(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca %union.EFI_DEV_PATH, align 1
  %6 = alloca ptr, align 8
  %7 = 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 60, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  br label %8

8:                                                ; preds = %1
  %9 = call zeroext i8 @DebugAssertEnabled()
  %10 = icmp ne i8 %9, 0
  br i1 %10, label %11, label %16

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

14:                                               ; preds = %11
  call void @DebugAssert(ptr noundef @.str, i64 noundef 1138, ptr noundef @.str.19)
  br label %15

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

16:                                               ; preds = %15, %8
  br label %17

17:                                               ; preds = %16
  br label %18

18:                                               ; preds = %17
  store i64 0, ptr %3, align 8, !tbaa !9
  %19 = call ptr @AllocateZeroPool(i64 noundef 4)
  store ptr %19, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  call void @SetDevicePathEndNode(ptr noundef %20)
  %21 = getelementptr inbounds %struct.EFI_DEVICE_PATH_PROTOCOL, ptr %5, i32 0, i32 0
  store i8 127, ptr %21, align 1, !tbaa !11
  %22 = getelementptr inbounds %struct.EFI_DEVICE_PATH_PROTOCOL, ptr %5, i32 0, i32 1
  store i8 1, ptr %22, align 1, !tbaa !11
  %23 = getelementptr inbounds %struct.EFI_DEVICE_PATH_PROTOCOL, ptr %5, i32 0, i32 2
  %24 = getelementptr inbounds [2 x i8], ptr %23, i64 0, i64 0
  store i8 4, ptr %24, align 1, !tbaa !11
  %25 = getelementptr inbounds %struct.EFI_DEVICE_PATH_PROTOCOL, ptr %5, i32 0, i32 2
  %26 = getelementptr inbounds [2 x i8], ptr %25, i64 0, i64 1
  store i8 0, ptr %26, align 1, !tbaa !11
  br label %27

27:                                               ; preds = %58, %18
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = call zeroext i8 @IsDevicePathEnd(ptr noundef %28)
  %30 = icmp ne i8 %29, 0
  %31 = xor i1 %30, true
  br i1 %31, label %32, label %61

32:                                               ; preds = %27
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = call zeroext i8 @IsHIDevicePathNode(ptr noundef %33)
  %35 = icmp ne i8 %34, 0
  br i1 %35, label %36, label %55

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

37:                                               ; preds = %46, %36
  %38 = load i64, ptr %4, align 8, !tbaa !9
  %39 = load i64, ptr %3, align 8, !tbaa !9
  %40 = icmp ult i64 %38, %39
  br i1 %40, label %41, label %49

41:                                               ; preds = %37
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = call ptr @AppendDevicePathNode(ptr noundef %42, ptr noundef %5)
  store ptr %43, ptr %7, align 8, !tbaa !5
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  call void @FreePool(ptr noundef %44)
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %45, ptr %6, align 8, !tbaa !5
  br label %46

46:                                               ; preds = %41
  %47 = load i64, ptr %4, align 8, !tbaa !9
  %48 = add i64 %47, 1
  store i64 %48, ptr %4, align 8, !tbaa !9
  br label %37, !llvm.loop !12

49:                                               ; preds = %37
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = call ptr @AppendDevicePathNode(ptr noundef %50, ptr noundef %51)
  store ptr %52, ptr %7, align 8, !tbaa !5
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  call void @FreePool(ptr noundef %53)
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %54, ptr %6, align 8, !tbaa !5
  br label %58

55:                                               ; preds = %32
  %56 = load i64, ptr %3, align 8, !tbaa !9
  %57 = add i64 %56, 1
  store i64 %57, ptr %3, align 8, !tbaa !9
  br label %58

58:                                               ; preds = %55, %49
  %59 = load ptr, ptr %2, align 8, !tbaa !5
  %60 = call ptr @NextDevicePathNode(ptr noundef %59)
  store ptr %60, ptr %2, align 8, !tbaa !5
  br label %27, !llvm.loop !14

61:                                               ; preds = %27
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 60, 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 ptr %62
}

declare void @SetDevicePathEndNode(ptr noundef) #1

declare zeroext i8 @IsDevicePathEnd(ptr noundef) #1

declare ptr @AppendDevicePathNode(ptr noundef, ptr noundef) #1

declare ptr @NextDevicePathNode(ptr noundef) #1

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, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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

%union.EFI_DEV_PATH = type <{ %struct.INFINIBAND_DEVICE_PATH, [12 x i8] }>
%struct.INFINIBAND_DEVICE_PATH = type { %struct.EFI_DEVICE_PATH_PROTOCOL, i32, [16 x i8], i64, i64, i64 }
%struct.EFI_DEVICE_PATH_PROTOCOL = type { i8, i8, [2 x i8] }

@.str = external hidden unnamed_addr constant [155 x i8], align 1
@.str.19 = external hidden unnamed_addr constant [27 x i8], align 1

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

declare ptr @AllocateZeroPool(i64 noundef) local_unnamed_addr #1

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #1

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

declare void @FreePool(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress 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 zeroext i8 @IsHIDevicePathNode(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local ptr @GetHIDevicePath(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca %union.EFI_DEV_PATH, align 4
  call void @llvm.lifetime.start.p0(i64 60, ptr nonnull %2) #3
  %3 = tail call zeroext i8 @DebugAssertEnabled() #3
  %4 = icmp eq i8 %3, 0
  %5 = icmp ne ptr %0, null
  %or.cond = or i1 %5, %4
  br i1 %or.cond, label %7, label %6

6:                                                ; preds = %1
  tail call void @DebugAssert(ptr noundef nonnull @.str, i64 noundef 1138, ptr noundef nonnull @.str.19) #3
  br label %7

7:                                                ; preds = %6, %1
  %8 = tail call ptr @AllocateZeroPool(i64 noundef 4) #3
  tail call void @SetDevicePathEndNode(ptr noundef %8) #3
  store <4 x i8> <i8 127, i8 1, i8 4, i8 0>, ptr %2, align 4, !tbaa !5
  %9 = tail call zeroext i8 @IsDevicePathEnd(ptr noundef %0) #3
  %.not24 = icmp eq i8 %9, 0
  br i1 %.not24, label %.lr.ph29, label %._crit_edge30

.lr.ph29:                                         ; preds = %7, %16
  %.027 = phi ptr [ %.2, %16 ], [ %8, %7 ]
  %.01826 = phi i64 [ %.119, %16 ], [ 0, %7 ]
  %.02025 = phi ptr [ %17, %16 ], [ %0, %7 ]
  %10 = call zeroext i8 @IsHIDevicePathNode(ptr noundef %.02025)
  %.not21 = icmp eq i8 %10, 0
  br i1 %.not21, label %14, label %.preheader

.preheader:                                       ; preds = %.lr.ph29
  %.not32 = icmp eq i64 %.01826, 0
  br i1 %.not32, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.123 = phi ptr [ %11, %.lr.ph ], [ %.027, %.preheader ]
  %.01722 = phi i64 [ %12, %.lr.ph ], [ 0, %.preheader ]
  %11 = call ptr @AppendDevicePathNode(ptr noundef %.123, ptr noundef nonnull %2) #3
  call void @FreePool(ptr noundef %.123) #3
  %12 = add nuw i64 %.01722, 1
  %exitcond.not = icmp eq i64 %12, %.01826
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %.1.lcssa = phi ptr [ %.027, %.preheader ], [ %11, %.lr.ph ]
  %13 = call ptr @AppendDevicePathNode(ptr noundef %.1.lcssa, ptr noundef %.02025) #3
  call void @FreePool(ptr noundef %.1.lcssa) #3
  br label %16

14:                                               ; preds = %.lr.ph29
  %15 = add i64 %.01826, 1
  br label %16

16:                                               ; preds = %14, %._crit_edge
  %.119 = phi i64 [ %.01826, %._crit_edge ], [ %15, %14 ]
  %.2 = phi ptr [ %13, %._crit_edge ], [ %.027, %14 ]
  %17 = call ptr @NextDevicePathNode(ptr noundef %.02025) #3
  %18 = call zeroext i8 @IsDevicePathEnd(ptr noundef %17) #3
  %.not = icmp eq i8 %18, 0
  br i1 %.not, label %.lr.ph29, label %._crit_edge30, !llvm.loop !10

._crit_edge30:                                    ; preds = %16, %7
  %.0.lcssa = phi ptr [ %8, %7 ], [ %.2, %16 ]
  call void @llvm.lifetime.end.p0(i64 60, ptr nonnull %2) #3
  ret ptr %.0.lcssa
}

declare void @SetDevicePathEndNode(ptr noundef) local_unnamed_addr #1

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

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

declare ptr @NextDevicePathNode(ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass