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

; 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.HEFI_EDITOR_BUFFER_IMAGE = type { ptr, ptr, i64, ptr, %struct.HEFI_EDITOR_POSITION, %struct.HEFI_EDITOR_POSITION, %struct.HEFI_EDITOR_POSITION, i64, i8, i8, i32, ptr, ptr, ptr }
%struct.HEFI_EDITOR_POSITION = type { i64, i64 }
%struct._HEFI_EDITOR_LINE = type { i64, [16 x i8], i64, %struct._LIST_ENTRY }
%struct._LIST_ENTRY = type { ptr, ptr }

@HBufferImage = external dso_local global %struct.HEFI_EDITOR_BUFFER_IMAGE, align 8
@.str.10 = external hidden unnamed_addr constant [167 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [21 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

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

declare zeroext i8 @DebugAssertEnabled() #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @HBufferImageListToBuffer(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load ptr, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 1), align 8, !tbaa !11
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  store i64 0, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %86

15:                                               ; preds = %2
  %16 = load ptr, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 1), align 8, !tbaa !11
  %17 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %16, i32 0, i32 3
  store ptr %17, ptr %9, align 8, !tbaa !5
  store i64 0, ptr %6, align 8, !tbaa !9
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %18, ptr %10, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %71, %15
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = load ptr, ptr @HBufferImage, align 8, !tbaa !15
  %22 = icmp ne ptr %20, %21
  br i1 %22, label %23, label %85

23:                                               ; preds = %19
  %24 = call zeroext i8 @DebugAssertEnabled()
  %25 = zext i8 %24 to i32
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %37

27:                                               ; preds = %23
  %28 = load ptr, ptr %9, align 8, !tbaa !5
  %29 = ptrtoint ptr %28 to i64
  %30 = sub i64 %29, ptrtoint (ptr getelementptr inbounds (%struct._HEFI_EDITOR_LINE, ptr null, i32 0, i32 3) to i64)
  %31 = inttoptr i64 %30 to ptr
  %32 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %31, i32 0, i32 0
  %33 = load i64, ptr %32, align 8, !tbaa !16
  %34 = icmp ne i64 %33, 1819043173
  br i1 %34, label %35, label %37

35:                                               ; preds = %27
  call void @DebugAssert(ptr noundef @.str.10, i64 noundef 2276, ptr noundef @.str.11)
  %36 = load ptr, ptr %9, align 8, !tbaa !5
  br label %42

37:                                               ; preds = %27, %23
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = ptrtoint ptr %38 to i64
  %40 = sub i64 %39, ptrtoint (ptr getelementptr inbounds (%struct._HEFI_EDITOR_LINE, ptr null, i32 0, i32 3) to i64)
  %41 = inttoptr i64 %40 to ptr
  br label %42

42:                                               ; preds = %37, %35
  %43 = phi ptr [ %36, %35 ], [ %41, %37 ]
  store ptr %43, ptr %8, align 8, !tbaa !5
  %44 = load i64, ptr %6, align 8, !tbaa !9
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %45, i32 0, i32 2
  %47 = load i64, ptr %46, align 8, !tbaa !19
  %48 = add i64 %44, %47
  %49 = load i64, ptr %5, align 8, !tbaa !9
  %50 = icmp ugt i64 %48, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %42
  store i64 0, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %86

52:                                               ; preds = %42
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %53

53:                                               ; preds = %68, %52
  %54 = load i64, ptr %7, align 8, !tbaa !9
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %55, i32 0, i32 2
  %57 = load i64, ptr %56, align 8, !tbaa !19
  %58 = icmp ult i64 %54, %57
  br i1 %58, label %59, label %71

59:                                               ; preds = %53
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %60, i32 0, i32 1
  %62 = load i64, ptr %7, align 8, !tbaa !9
  %63 = getelementptr inbounds [16 x i8], ptr %61, i64 0, i64 %62
  %64 = load i8, ptr %63, align 1, !tbaa !20
  %65 = load ptr, ptr %10, align 8, !tbaa !5
  %66 = load i64, ptr %7, align 8, !tbaa !9
  %67 = getelementptr inbounds i8, ptr %65, i64 %66
  store i8 %64, ptr %67, align 1, !tbaa !20
  br label %68

68:                                               ; preds = %59
  %69 = load i64, ptr %7, align 8, !tbaa !9
  %70 = add i64 %69, 1
  store i64 %70, ptr %7, align 8, !tbaa !9
  br label %53, !llvm.loop !21

71:                                               ; preds = %53
  %72 = load ptr, ptr %8, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %72, i32 0, i32 2
  %74 = load i64, ptr %73, align 8, !tbaa !19
  %75 = load i64, ptr %6, align 8, !tbaa !9
  %76 = add i64 %75, %74
  store i64 %76, ptr %6, align 8, !tbaa !9
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %77, i32 0, i32 2
  %79 = load i64, ptr %78, align 8, !tbaa !19
  %80 = load ptr, ptr %10, align 8, !tbaa !5
  %81 = getelementptr inbounds i8, ptr %80, i64 %79
  store ptr %81, ptr %10, align 8, !tbaa !5
  %82 = load ptr, ptr %9, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct._LIST_ENTRY, ptr %82, i32 0, i32 0
  %84 = load ptr, ptr %83, align 8, !tbaa !23
  store ptr %84, ptr %9, align 8, !tbaa !5
  br label %19, !llvm.loop !24

85:                                               ; preds = %19
  store i64 0, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %86

86:                                               ; preds = %85, %51, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %87 = load i64, ptr %3, align 8
  ret i64 %87
}

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 = !{!12, !6, i64 8}
!12 = !{!"", !6, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !13, i64 32, !13, i64 48, !13, i64 64, !10, i64 80, !7, i64 88, !7, i64 89, !14, i64 92, !6, i64 96, !6, i64 104, !6, i64 112}
!13 = !{!"", !10, i64 0, !10, i64 8}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !6, i64 0}
!16 = !{!17, !10, i64 0}
!17 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !7, i64 8, !10, i64 24, !18, i64 32}
!18 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!19 = !{!17, !10, i64 24}
!20 = !{!7, !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!18, !6, i64 0}
!24 = distinct !{!24, !22}


; 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.HEFI_EDITOR_BUFFER_IMAGE = type { ptr, ptr, i64, ptr, %struct.HEFI_EDITOR_POSITION, %struct.HEFI_EDITOR_POSITION, %struct.HEFI_EDITOR_POSITION, i64, i8, i8, i32, ptr, ptr, ptr }
%struct.HEFI_EDITOR_POSITION = type { i64, i64 }

@HBufferImage = external dso_local local_unnamed_addr global %struct.HEFI_EDITOR_BUFFER_IMAGE, align 8
@.str.10 = external hidden unnamed_addr constant [167 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [21 x i8], align 1

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @HBufferImageListToBuffer(ptr nocapture noundef writeonly %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = load ptr, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 8), align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  %5 = getelementptr inbounds i8, ptr %3, i64 32
  %6 = load ptr, ptr @HBufferImage, align 8
  %.not29 = icmp eq ptr %5, %6
  %or.cond = select i1 %4, i1 true, i1 %.not29
  br i1 %or.cond, label %.loopexit, label %.lr.ph35

.lr.ph35:                                         ; preds = %2, %._crit_edge
  %.033 = phi ptr [ %25, %._crit_edge ], [ %0, %2 ]
  %.02031 = phi ptr [ %26, %._crit_edge ], [ %5, %2 ]
  %.02330 = phi i64 [ %24, %._crit_edge ], [ 0, %2 ]
  %7 = tail call zeroext i8 @DebugAssertEnabled() #2
  %.not26 = icmp eq i8 %7, 0
  %.pre = ptrtoint ptr %.02031 to i64
  %.pre40 = add i64 %.pre, -32
  %.pre42 = inttoptr i64 %.pre40 to ptr
  br i1 %.not26, label %.lr.ph35._crit_edge, label %8

8:                                                ; preds = %.lr.ph35
  %9 = load i64, ptr %.pre42, align 8, !tbaa !13
  %.not27 = icmp eq i64 %9, 1819043173
  br i1 %.not27, label %.lr.ph35._crit_edge, label %10

10:                                               ; preds = %8
  tail call void @DebugAssert(ptr noundef nonnull @.str.10, i64 noundef 2276, ptr noundef nonnull @.str.11) #2
  br label %.lr.ph35._crit_edge

.lr.ph35._crit_edge:                              ; preds = %.lr.ph35, %8, %10
  %11 = phi ptr [ %.02031, %10 ], [ %.pre42, %8 ], [ %.pre42, %.lr.ph35 ]
  %12 = getelementptr inbounds i8, ptr %11, i64 24
  %13 = load i64, ptr %12, align 8, !tbaa !16
  %14 = add i64 %13, %.02330
  %15 = icmp ugt i64 %14, %1
  br i1 %15, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %.lr.ph35._crit_edge
  %.not38 = icmp eq i64 %13, 0
  br i1 %.not38, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %16 = getelementptr inbounds i8, ptr %11, i64 8
  br label %17

17:                                               ; preds = %.lr.ph, %17
  %.02228 = phi i64 [ 0, %.lr.ph ], [ %21, %17 ]
  %18 = getelementptr inbounds [16 x i8], ptr %16, i64 0, i64 %.02228
  %19 = load i8, ptr %18, align 1, !tbaa !17
  %20 = getelementptr inbounds i8, ptr %.033, i64 %.02228
  store i8 %19, ptr %20, align 1, !tbaa !17
  %21 = add nuw i64 %.02228, 1
  %22 = load i64, ptr %12, align 8, !tbaa !16
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %17, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %17, %.preheader
  %.lcssa = phi i64 [ 0, %.preheader ], [ %22, %17 ]
  %24 = add i64 %.lcssa, %.02330
  %25 = getelementptr inbounds i8, ptr %.033, i64 %.lcssa
  %26 = load ptr, ptr %.02031, align 8, !tbaa !20
  %27 = load ptr, ptr @HBufferImage, align 8, !tbaa !21
  %.not = icmp eq ptr %26, %27
  br i1 %.not, label %.loopexit, label %.lr.ph35, !llvm.loop !22

.loopexit:                                        ; preds = %.lr.ph35._crit_edge, %._crit_edge, %2
  ret i64 0
}

attributes #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 = { 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, !7, i64 8}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !11, i64 32, !11, i64 48, !11, i64 64, !10, i64 80, !8, i64 88, !8, i64 89, !12, i64 92, !7, i64 96, !7, i64 104, !7, i64 112}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long long", !8, i64 0}
!11 = !{!"", !10, i64 0, !10, i64 8}
!12 = !{!"int", !8, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !8, i64 8, !10, i64 24, !15, i64 32}
!15 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!16 = !{!14, !10, i64 24}
!17 = !{!8, !8, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!15, !7, i64 0}
!21 = !{!6, !7, i64 0}
!22 = distinct !{!22, !19}


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