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

; 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
@HBufferImageNeedRefresh = external dso_local global i8, align 1
@HBufferImageOnlyLineNeedRefresh = external dso_local global 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

; Function Attrs: nounwind uwtable
declare dso_local i64 @HBufferImageDeleteCharacterFromBuffer(i64 noundef, i64 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @HBufferImageDoDelete() #1 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %7 = load i64, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 6), align 8, !tbaa !5
  %8 = sub i64 %7, 1
  %9 = mul i64 %8, 16
  %10 = load i64, ptr getelementptr inbounds (%struct.HEFI_EDITOR_POSITION, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 6), i32 0, i32 1), align 8, !tbaa !13
  %11 = add i64 %9, %10
  %12 = sub i64 %11, 1
  store i64 %12, ptr %5, align 8, !tbaa !14
  %13 = load i64, ptr getelementptr inbounds (%struct.HEFI_EDITOR_POSITION, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 6), i32 0, i32 1), align 8, !tbaa !13
  store i64 %13, ptr %4, align 8, !tbaa !14
  %14 = load ptr, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 3), align 8, !tbaa !15
  store ptr %14, ptr %2, align 8, !tbaa !16
  %15 = load i64, ptr %4, align 8, !tbaa !14
  %16 = load ptr, ptr %2, align 8, !tbaa !16
  %17 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %16, i32 0, i32 2
  %18 = load i64, ptr %17, align 8, !tbaa !17
  %19 = icmp ugt i64 %15, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %0
  store i64 0, ptr %1, align 8
  store i32 1, ptr %6, align 4
  br label %41

21:                                               ; preds = %0
  store i8 0, ptr %3, align 1, !tbaa !20
  %22 = load ptr, ptr %2, align 8, !tbaa !16
  %23 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %22, i32 0, i32 3
  %24 = getelementptr inbounds %struct._LIST_ENTRY, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !21
  %26 = load ptr, ptr @HBufferImage, align 8, !tbaa !22
  %27 = icmp eq ptr %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %21
  store i8 1, ptr %3, align 1, !tbaa !20
  br label %29

29:                                               ; preds = %28, %21
  %30 = load i64, ptr %5, align 8, !tbaa !14
  %31 = call i64 @HBufferImageDeleteCharacterFromBuffer(i64 noundef %30, i64 noundef 1, ptr noundef null)
  %32 = load i8, ptr %3, align 1, !tbaa !20
  %33 = icmp ne i8 %32, 0
  br i1 %33, label %34, label %35

34:                                               ; preds = %29
  store i8 0, ptr @HBufferImageNeedRefresh, align 1, !tbaa !20
  store i8 1, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !20
  br label %36

35:                                               ; preds = %29
  store i8 1, ptr @HBufferImageNeedRefresh, align 1, !tbaa !20
  store i8 0, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !20
  br label %36

36:                                               ; preds = %35, %34
  %37 = load i8, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 9), align 1, !tbaa !23
  %38 = icmp ne i8 %37, 0
  br i1 %38, label %40, label %39

39:                                               ; preds = %36
  store i8 1, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 9), align 1, !tbaa !23
  br label %40

40:                                               ; preds = %39, %36
  store i64 0, ptr %1, align 8
  store i32 1, ptr %6, align 4
  br label %41

41:                                               ; preds = %40, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  %42 = load i64, ptr %1, align 8
  ret i64 %42
}

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 = { 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 64}
!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 = !{!6, !10, i64 72}
!14 = !{!10, !10, i64 0}
!15 = !{!6, !7, i64 24}
!16 = !{!7, !7, i64 0}
!17 = !{!18, !10, i64 24}
!18 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !8, i64 8, !10, i64 24, !19, i64 32}
!19 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!20 = !{!8, !8, i64 0}
!21 = !{!18, !7, i64 32}
!22 = !{!6, !7, i64 0}
!23 = !{!6, !8, i64 89}


; 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
@HBufferImageNeedRefresh = external dso_local local_unnamed_addr global i8, align 1
@HBufferImageOnlyLineNeedRefresh = external dso_local local_unnamed_addr global i8, align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @HBufferImageDeleteCharacterFromBuffer(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @HBufferImageDoDelete() local_unnamed_addr #0 {
  %1 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 72), align 8, !tbaa !5
  %2 = load ptr, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 24), align 8, !tbaa !13
  %3 = getelementptr inbounds i8, ptr %2, i64 24
  %4 = load i64, ptr %3, align 8, !tbaa !14
  %5 = icmp ugt i64 %1, %4
  br i1 %5, label %18, label %6

6:                                                ; preds = %0
  %7 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 64), align 8, !tbaa !17
  %8 = shl i64 %7, 4
  %9 = add i64 %1, -17
  %10 = add i64 %9, %8
  %11 = getelementptr inbounds i8, ptr %2, i64 32
  %12 = load ptr, ptr %11, align 8, !tbaa !18
  %13 = load ptr, ptr @HBufferImage, align 8, !tbaa !19
  %14 = icmp ne ptr %12, %13
  %15 = tail call i64 @HBufferImageDeleteCharacterFromBuffer(i64 noundef %10, i64 noundef 1, ptr noundef null)
  %. = zext i1 %14 to i8
  %not. = xor i1 %14, true
  %.6 = zext i1 %not. to i8
  store i8 %., ptr @HBufferImageNeedRefresh, align 1, !tbaa !20
  store i8 %.6, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !20
  %16 = load i8, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 89), align 1, !tbaa !21
  %.not = icmp eq i8 %16, 0
  br i1 %.not, label %17, label %18

17:                                               ; preds = %6
  store i8 1, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 89), align 1, !tbaa !21
  br label %18

18:                                               ; preds = %6, %17, %0
  ret i64 0
}

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

!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 72}
!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 = !{!6, !7, i64 24}
!14 = !{!15, !10, i64 24}
!15 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !8, i64 8, !10, i64 24, !16, i64 32}
!16 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!17 = !{!6, !10, i64 64}
!18 = !{!15, !7, i64 32}
!19 = !{!6, !7, i64 0}
!20 = !{!8, !8, i64 0}
!21 = !{!6, !8, i64 89}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass