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

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

; 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 i64 @LineStrInsert(ptr noundef %0, i16 noundef zeroext %1, i64 noundef %2, i64 noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i16 %1, ptr %7, align 2, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !11
  store i64 %3, ptr %9, align 8, !tbaa !11
  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
  %14 = load i64, ptr %9, align 8, !tbaa !11
  %15 = mul i64 %14, 2
  store i64 %15, ptr %10, align 8, !tbaa !11
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !13
  store ptr %18, ptr %12, align 8, !tbaa !5
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %19, i32 0, i32 3
  %21 = load i64, ptr %20, align 8, !tbaa !17
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %22, i32 0, i32 2
  %24 = load i64, ptr %23, align 8, !tbaa !18
  %25 = icmp ule i64 %21, %24
  br i1 %25, label %26, label %40

26:                                               ; preds = %4
  %27 = load i64, ptr %10, align 8, !tbaa !11
  %28 = load i64, ptr %10, align 8, !tbaa !11
  %29 = add i64 %28, 16
  %30 = load ptr, ptr %12, align 8, !tbaa !5
  %31 = call ptr @ReallocatePool(i64 noundef %27, i64 noundef %29, ptr noundef %30)
  store ptr %31, ptr %12, align 8, !tbaa !5
  %32 = load ptr, ptr %12, align 8, !tbaa !5
  %33 = icmp eq ptr %32, null
  br i1 %33, label %34, label %35

34:                                               ; preds = %26
  store i64 0, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %73

35:                                               ; preds = %26
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %36, i32 0, i32 3
  %38 = load i64, ptr %37, align 8, !tbaa !17
  %39 = add i64 %38, 8
  store i64 %39, ptr %37, align 8, !tbaa !17
  br label %40

40:                                               ; preds = %35, %4
  %41 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %41, ptr %11, align 8, !tbaa !5
  %42 = load i64, ptr %9, align 8, !tbaa !11
  store i64 %42, ptr %10, align 8, !tbaa !11
  br label %43

43:                                               ; preds = %56, %40
  %44 = load i64, ptr %10, align 8, !tbaa !11
  %45 = load i64, ptr %8, align 8, !tbaa !11
  %46 = icmp ugt i64 %44, %45
  br i1 %46, label %47, label %59

47:                                               ; preds = %43
  %48 = load ptr, ptr %11, align 8, !tbaa !5
  %49 = load i64, ptr %10, align 8, !tbaa !11
  %50 = sub i64 %49, 1
  %51 = getelementptr inbounds i16, ptr %48, i64 %50
  %52 = load i16, ptr %51, align 2, !tbaa !9
  %53 = load ptr, ptr %11, align 8, !tbaa !5
  %54 = load i64, ptr %10, align 8, !tbaa !11
  %55 = getelementptr inbounds i16, ptr %53, i64 %54
  store i16 %52, ptr %55, align 2, !tbaa !9
  br label %56

56:                                               ; preds = %47
  %57 = load i64, ptr %10, align 8, !tbaa !11
  %58 = add i64 %57, -1
  store i64 %58, ptr %10, align 8, !tbaa !11
  br label %43, !llvm.loop !19

59:                                               ; preds = %43
  %60 = load i16, ptr %7, align 2, !tbaa !9
  %61 = load ptr, ptr %11, align 8, !tbaa !5
  %62 = load i64, ptr %10, align 8, !tbaa !11
  %63 = getelementptr inbounds i16, ptr %61, i64 %62
  store i16 %60, ptr %63, align 2, !tbaa !9
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %65, i32 0, i32 1
  store ptr %64, ptr %66, align 8, !tbaa !13
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %67, i32 0, i32 2
  %69 = load i64, ptr %68, align 8, !tbaa !18
  %70 = add i64 %69, 1
  store i64 %70, ptr %68, align 8, !tbaa !18
  %71 = load i64, ptr %9, align 8, !tbaa !11
  %72 = add i64 %71, 1
  store i64 %72, ptr %5, align 8
  store i32 1, ptr %13, align 4
  br label %73

73:                                               ; preds = %59, %34
  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
  %74 = load i64, ptr %5, align 8
  ret i64 %74
}

declare ptr @ReallocatePool(i64 noundef, i64 noundef, ptr noundef) #2

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 = { "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 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"_EFI_EDITOR_LINE", !12, i64 0, !6, i64 8, !12, i64 16, !12, i64 24, !15, i64 32, !16, i64 40}
!15 = !{!"int", !7, i64 0}
!16 = !{!"_LIST_ENTRY", !6, i64 0, !6, i64 8}
!17 = !{!14, !12, i64 24}
!18 = !{!14, !12, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"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: nounwind uwtable
define dso_local noundef i64 @LineStrInsert(ptr nocapture noundef %0, i16 noundef zeroext %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = load i64, ptr %7, align 8, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load i64, ptr %9, align 8, !tbaa !14
  %.not = icmp ugt i64 %8, %10
  br i1 %.not, label %19, label %11

11:                                               ; preds = %4
  %12 = shl i64 %3, 1
  %13 = add i64 %12, 16
  %14 = tail call ptr @ReallocatePool(i64 noundef %12, i64 noundef %13, ptr noundef %6) #2
  %15 = icmp eq ptr %14, null
  br i1 %15, label %39, label %16

16:                                               ; preds = %11
  %17 = load i64, ptr %7, align 8, !tbaa !13
  %18 = add i64 %17, 8
  store i64 %18, ptr %7, align 8, !tbaa !13
  br label %19

19:                                               ; preds = %16, %4
  %.0 = phi ptr [ %14, %16 ], [ %6, %4 ]
  %20 = icmp ugt i64 %3, %2
  br i1 %20, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %19
  %21 = sub i64 %3, %2
  %min.iters.check = icmp ult i64 %21, 16
  br i1 %min.iters.check, label %.lr.ph.preheader34, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %21, -16
  %ind.end = sub i64 %3, %n.vec
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = sub i64 %3, %index
  %22 = add i64 %offset.idx, -1
  %23 = getelementptr inbounds i16, ptr %.0, i64 %22
  %24 = getelementptr inbounds i8, ptr %23, i64 -14
  %25 = getelementptr inbounds i8, ptr %23, i64 -30
  %wide.load = load <8 x i16>, ptr %24, align 2, !tbaa !15
  %wide.load30 = load <8 x i16>, ptr %25, align 2, !tbaa !15
  %26 = getelementptr inbounds i16, ptr %.0, i64 %offset.idx
  %27 = getelementptr inbounds i8, ptr %26, i64 -14
  %28 = getelementptr inbounds i8, ptr %26, i64 -30
  store <8 x i16> %wide.load, ptr %27, align 2, !tbaa !15
  store <8 x i16> %wide.load30, ptr %28, align 2, !tbaa !15
  %index.next = add nuw i64 %index, 16
  %29 = icmp eq i64 %index.next, %n.vec
  br i1 %29, label %middle.block, label %vector.body, !llvm.loop !17

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %21, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader34

.lr.ph.preheader34:                               ; preds = %middle.block, %.lr.ph.preheader
  %.02628.ph = phi i64 [ %3, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader34, %.lr.ph
  %.02628 = phi i64 [ %30, %.lr.ph ], [ %.02628.ph, %.lr.ph.preheader34 ]
  %30 = add i64 %.02628, -1
  %31 = getelementptr inbounds i16, ptr %.0, i64 %30
  %32 = load i16, ptr %31, align 2, !tbaa !15
  %33 = getelementptr inbounds i16, ptr %.0, i64 %.02628
  store i16 %32, ptr %33, align 2, !tbaa !15
  %34 = icmp ugt i64 %30, %2
  br i1 %34, label %.lr.ph, label %._crit_edge, !llvm.loop !21

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %19
  %.026.lcssa = phi i64 [ %3, %19 ], [ %2, %middle.block ], [ %2, %.lr.ph ]
  %35 = getelementptr inbounds i16, ptr %.0, i64 %.026.lcssa
  store i16 %1, ptr %35, align 2, !tbaa !15
  store ptr %.0, ptr %5, align 8, !tbaa !5
  %36 = load i64, ptr %9, align 8, !tbaa !14
  %37 = add i64 %36, 1
  store i64 %37, ptr %9, align 8, !tbaa !14
  %38 = add i64 %3, 1
  br label %39

39:                                               ; preds = %11, %._crit_edge
  %.025 = phi i64 [ %38, %._crit_edge ], [ 0, %11 ]
  ret i64 %.025
}

declare ptr @ReallocatePool(i64 noundef, i64 noundef, 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, !10, i64 8}
!6 = !{!"_EFI_EDITOR_LINE", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24, !11, i64 32, !12, i64 40}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"_LIST_ENTRY", !10, i64 0, !10, i64 8}
!13 = !{!6, !7, i64 24}
!14 = !{!6, !7, i64 16}
!15 = !{!16, !16, i64 0}
!16 = !{!"short", !8, i64 0}
!17 = distinct !{!17, !18, !19, !20}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !18, !20, !19}


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