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

; 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
define dso_local i64 @HBufferImageDoBackspace() #1 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  store i8 0, ptr %5, align 1, !tbaa !5
  %7 = load i64, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 6), align 8, !tbaa !8
  %8 = icmp eq i64 %7, 1
  br i1 %8, label %9, label %13

9:                                                ; preds = %0
  %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 !14
  %11 = icmp eq i64 %10, 1
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  store i64 0, ptr %1, align 8
  store i32 1, ptr %6, align 4
  br label %45

13:                                               ; preds = %9, %0
  %14 = load i64, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 6), align 8, !tbaa !8
  %15 = sub i64 %14, 1
  %16 = mul i64 %15, 16
  %17 = 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 !14
  %18 = add i64 %16, %17
  %19 = sub i64 %18, 1
  store i64 %19, ptr %4, align 8, !tbaa !15
  %20 = 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 !14
  store i64 %20, ptr %3, align 8, !tbaa !15
  %21 = load ptr, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 3), align 8, !tbaa !16
  store ptr %21, ptr %2, align 8, !tbaa !17
  store i8 0, ptr %5, align 1, !tbaa !5
  %22 = load ptr, ptr %2, align 8, !tbaa !17
  %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 !18
  %26 = load ptr, ptr @HBufferImage, align 8, !tbaa !21
  %27 = icmp eq ptr %25, %26
  br i1 %27, label %28, label %32

28:                                               ; preds = %13
  %29 = load i64, ptr %3, align 8, !tbaa !15
  %30 = icmp ugt i64 %29, 1
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  store i8 1, ptr %5, align 1, !tbaa !5
  br label %32

32:                                               ; preds = %31, %28, %13
  %33 = load i64, ptr %4, align 8, !tbaa !15
  %34 = sub i64 %33, 1
  %35 = call i64 @HBufferImageDeleteCharacterFromBuffer(i64 noundef %34, i64 noundef 1, ptr noundef null)
  %36 = load i8, ptr %5, align 1, !tbaa !5
  %37 = icmp ne i8 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %32
  store i8 0, ptr @HBufferImageNeedRefresh, align 1, !tbaa !5
  store i8 1, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !5
  br label %40

39:                                               ; preds = %32
  store i8 1, ptr @HBufferImageNeedRefresh, align 1, !tbaa !5
  store i8 0, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !5
  br label %40

40:                                               ; preds = %39, %38
  %41 = load i8, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 9), align 1, !tbaa !22
  %42 = icmp ne i8 %41, 0
  br i1 %42, label %44, label %43

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

44:                                               ; preds = %43, %40
  store i64 0, ptr %1, align 8
  store i32 1, ptr %6, align 4
  br label %45

45:                                               ; preds = %44, %12
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  %46 = load i64, ptr %1, align 8
  ret i64 %46
}

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

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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 64}
!9 = !{!"", !10, i64 0, !10, i64 8, !11, i64 16, !10, i64 24, !12, i64 32, !12, i64 48, !12, i64 64, !11, i64 80, !6, i64 88, !6, i64 89, !13, i64 92, !10, i64 96, !10, i64 104, !10, i64 112}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"long long", !6, i64 0}
!12 = !{!"", !11, i64 0, !11, i64 8}
!13 = !{!"int", !6, i64 0}
!14 = !{!9, !11, i64 72}
!15 = !{!11, !11, i64 0}
!16 = !{!9, !10, i64 24}
!17 = !{!10, !10, i64 0}
!18 = !{!19, !10, i64 32}
!19 = !{!"_HEFI_EDITOR_LINE", !11, i64 0, !6, i64 8, !11, i64 24, !20, i64 32}
!20 = !{!"_LIST_ENTRY", !10, i64 0, !10, i64 8}
!21 = !{!9, !10, i64 0}
!22 = !{!9, !6, 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
define dso_local noundef i64 @HBufferImageDoBackspace() local_unnamed_addr #0 {
  %1 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 64), align 8, !tbaa !5
  %2 = icmp eq i64 %1, 1
  %3 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 72), align 8
  %4 = icmp eq i64 %3, 1
  %or.cond = select i1 %2, i1 %4, i1 false
  br i1 %or.cond, label %18, label %5

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @HBufferImageDeleteCharacterFromBuffer(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #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 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, !7, i64 24}
!14 = !{!15, !7, i64 32}
!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, !7, i64 0}
!18 = !{!8, !8, i64 0}
!19 = !{!6, !8, i64 89}


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