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

; 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 ptr @HBufferImageCreateLine() #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @HBufferImageCharToHex(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @HBufferImageAddChar(i16 noundef zeroext %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i16, align 2
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i8, align 1
  %11 = alloca i32, align 4
  store i16 %0, ptr %3, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #2
  %12 = load i16, ptr %3, align 2, !tbaa !5
  %13 = call i64 @HBufferImageCharToHex(i16 noundef zeroext %12)
  store i64 %13, ptr %6, align 8, !tbaa !9
  %14 = load i64, ptr %6, align 8, !tbaa !9
  %15 = icmp eq i64 %14, -1
  br i1 %15, label %16, label %17

16:                                               ; preds = %1
  store i64 0, ptr %2, align 8
  store i32 1, ptr %11, align 4
  br label %115

17:                                               ; preds = %1
  %18 = load ptr, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 3), align 8, !tbaa !11
  store ptr %18, ptr %4, align 8, !tbaa !16
  %19 = load i64, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 6), align 8, !tbaa !17
  store i64 %19, ptr %8, align 8, !tbaa !9
  %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 !18
  store i64 %20, ptr %9, align 8, !tbaa !9
  %21 = load i8, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 8), align 8, !tbaa !19
  store i8 %21, ptr %10, align 1, !tbaa !20
  store i8 1, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !20
  %22 = load i64, ptr %9, align 8, !tbaa !9
  %23 = load ptr, ptr %4, align 8, !tbaa !16
  %24 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %23, i32 0, i32 2
  %25 = load i64, ptr %24, align 8, !tbaa !21
  %26 = icmp ugt i64 %22, %25
  br i1 %26, label %27, label %40

27:                                               ; preds = %17
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = trunc i64 %28 to i8
  %30 = load ptr, ptr %4, align 8, !tbaa !16
  %31 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %30, i32 0, i32 1
  %32 = load ptr, ptr %4, align 8, !tbaa !16
  %33 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !21
  %35 = getelementptr inbounds [16 x i8], ptr %31, i64 0, i64 %34
  store i8 %29, ptr %35, align 1, !tbaa !20
  %36 = load ptr, ptr %4, align 8, !tbaa !16
  %37 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %36, i32 0, i32 2
  %38 = load i64, ptr %37, align 8, !tbaa !21
  %39 = add i64 %38, 1
  store i64 %39, ptr %37, align 8, !tbaa !21
  store i8 0, ptr %10, align 1, !tbaa !20
  br label %107

40:                                               ; preds = %17
  %41 = load ptr, ptr %4, align 8, !tbaa !16
  %42 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = sub i64 %43, 1
  %45 = getelementptr inbounds [16 x i8], ptr %42, i64 0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !20
  store i8 %46, ptr %7, align 1, !tbaa !20
  %47 = load i8, ptr %7, align 1, !tbaa !20
  %48 = zext i8 %47 to i32
  %49 = and i32 %48, 15
  %50 = trunc i32 %49 to i8
  store i8 %50, ptr %7, align 1, !tbaa !20
  %51 = load i8, ptr %7, align 1, !tbaa !20
  %52 = zext i8 %51 to i32
  %53 = shl i32 %52, 4
  %54 = trunc i32 %53 to i8
  store i8 %54, ptr %7, align 1, !tbaa !20
  %55 = load i64, ptr %6, align 8, !tbaa !9
  %56 = load i8, ptr %7, align 1, !tbaa !20
  %57 = zext i8 %56 to i64
  %58 = add nsw i64 %55, %57
  %59 = trunc i64 %58 to i8
  store i8 %59, ptr %7, align 1, !tbaa !20
  %60 = load i8, ptr %7, align 1, !tbaa !20
  %61 = load ptr, ptr %4, align 8, !tbaa !16
  %62 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %61, i32 0, i32 1
  %63 = load i64, ptr %9, align 8, !tbaa !9
  %64 = sub i64 %63, 1
  %65 = getelementptr inbounds [16 x i8], ptr %62, i64 0, i64 %64
  store i8 %60, ptr %65, align 1, !tbaa !20
  %66 = load i8, ptr %10, align 1, !tbaa !20
  %67 = icmp ne i8 %66, 0
  br i1 %67, label %85, label %68

68:                                               ; preds = %40
  %69 = load i64, ptr %9, align 8, !tbaa !9
  %70 = icmp eq i64 %69, 16
  br i1 %70, label %71, label %85

71:                                               ; preds = %68
  store i8 0, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !20
  store i8 1, ptr @HBufferImageNeedRefresh, align 1, !tbaa !20
  %72 = load ptr, ptr %4, align 8, !tbaa !16
  %73 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %72, i32 0, i32 3
  %74 = getelementptr inbounds %struct._LIST_ENTRY, ptr %73, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !24
  %76 = load ptr, ptr @HBufferImage, align 8, !tbaa !25
  %77 = icmp eq ptr %75, %76
  br i1 %77, label %78, label %84

78:                                               ; preds = %71
  %79 = call ptr @HBufferImageCreateLine()
  store ptr %79, ptr %5, align 8, !tbaa !16
  %80 = load ptr, ptr %5, align 8, !tbaa !16
  %81 = icmp eq ptr %80, null
  br i1 %81, label %82, label %83

82:                                               ; preds = %78
  store i64 -9223372036854775799, ptr %2, align 8
  store i32 1, ptr %11, align 4
  br label %115

83:                                               ; preds = %78
  br label %84

84:                                               ; preds = %83, %71
  br label %85

85:                                               ; preds = %84, %68, %40
  %86 = load i64, ptr %9, align 8, !tbaa !9
  %87 = icmp eq i64 %86, 16
  br i1 %87, label %88, label %94

88:                                               ; preds = %85
  %89 = load i8, ptr %10, align 1, !tbaa !20
  %90 = icmp ne i8 %89, 0
  br i1 %90, label %94, label %91

91:                                               ; preds = %88
  %92 = load i64, ptr %8, align 8, !tbaa !9
  %93 = add i64 %92, 1
  store i64 %93, ptr %8, align 8, !tbaa !9
  store i64 1, ptr %9, align 8, !tbaa !9
  store i8 1, ptr %10, align 1, !tbaa !20
  br label %106

94:                                               ; preds = %88, %85
  %95 = load i8, ptr %10, align 1, !tbaa !20
  %96 = icmp ne i8 %95, 0
  br i1 %96, label %100, label %97

97:                                               ; preds = %94
  %98 = load i64, ptr %9, align 8, !tbaa !9
  %99 = add i64 %98, 1
  store i64 %99, ptr %9, align 8, !tbaa !9
  br label %100

100:                                              ; preds = %97, %94
  %101 = load i8, ptr %10, align 1, !tbaa !20
  %102 = icmp ne i8 %101, 0
  br i1 %102, label %103, label %104

103:                                              ; preds = %100
  store i8 0, ptr %10, align 1, !tbaa !20
  br label %105

104:                                              ; preds = %100
  store i8 1, ptr %10, align 1, !tbaa !20
  br label %105

105:                                              ; preds = %104, %103
  br label %106

106:                                              ; preds = %105, %91
  br label %107

107:                                              ; preds = %106, %27
  %108 = load i64, ptr %8, align 8, !tbaa !9
  %109 = load i64, ptr %9, align 8, !tbaa !9
  %110 = load i8, ptr %10, align 1, !tbaa !20
  call void @HBufferImageMovePosition(i64 noundef %108, i64 noundef %109, i8 noundef zeroext %110)
  %111 = load i8, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 9), align 1, !tbaa !26
  %112 = icmp ne i8 %111, 0
  br i1 %112, label %114, label %113

113:                                              ; preds = %107
  store i8 1, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 9), align 1, !tbaa !26
  br label %114

114:                                              ; preds = %113, %107
  store i64 0, ptr %2, align 8
  store i32 1, ptr %11, align 4
  br label %115

115:                                              ; preds = %114, %82, %16
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %116 = load i64, ptr %2, align 8
  ret i64 %116
}

; Function Attrs: nounwind uwtable
declare dso_local void @HBufferImageMovePosition(i64 noundef, i64 noundef, i8 noundef zeroext) #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 = !{!"short", !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, !13, i64 24}
!12 = !{!"", !13, i64 0, !13, i64 8, !10, i64 16, !13, i64 24, !14, i64 32, !14, i64 48, !14, i64 64, !10, i64 80, !7, i64 88, !7, i64 89, !15, i64 92, !13, i64 96, !13, i64 104, !13, i64 112}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!"", !10, i64 0, !10, i64 8}
!15 = !{!"int", !7, i64 0}
!16 = !{!13, !13, i64 0}
!17 = !{!12, !10, i64 64}
!18 = !{!12, !10, i64 72}
!19 = !{!12, !7, i64 88}
!20 = !{!7, !7, i64 0}
!21 = !{!22, !10, i64 24}
!22 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !7, i64 8, !10, i64 24, !23, i64 32}
!23 = !{!"_LIST_ENTRY", !13, i64 0, !13, i64 8}
!24 = !{!22, !13, i64 32}
!25 = !{!12, !13, i64 0}
!26 = !{!12, !7, 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 ptr @HBufferImageCreateLine() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @HBufferImageCharToHex(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i64 -9223372036854775799, 1) i64 @HBufferImageAddChar(i16 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = tail call i64 @HBufferImageCharToHex(i16 noundef zeroext %0)
  %3 = icmp eq i64 %2, -1
  br i1 %3, label %46, label %4

4:                                                ; preds = %1
  %5 = load ptr, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 24), align 8, !tbaa !5
  %6 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 64), align 8, !tbaa !13
  %7 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 72), align 8, !tbaa !14
  %8 = load i8, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 88), align 8, !tbaa !15
  store i8 1, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !16
  %9 = getelementptr inbounds i8, ptr %5, i64 24
  %10 = load i64, ptr %9, align 8, !tbaa !17
  %11 = icmp ugt i64 %7, %10
  br i1 %11, label %12, label %18

12:                                               ; preds = %4
  %13 = trunc i64 %2 to i8
  %14 = getelementptr inbounds i8, ptr %5, i64 8
  %15 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 %10
  store i8 %13, ptr %15, align 1, !tbaa !16
  %16 = load i64, ptr %9, align 8, !tbaa !17
  %17 = add i64 %16, 1
  store i64 %17, ptr %9, align 8, !tbaa !17
  br label %43

18:                                               ; preds = %4
  %19 = getelementptr inbounds i8, ptr %5, i64 8
  %20 = add i64 %7, -1
  %21 = getelementptr inbounds [16 x i8], ptr %19, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !16
  %23 = shl i8 %22, 4
  %24 = trunc i64 %2 to i8
  %25 = add i8 %23, %24
  store i8 %25, ptr %21, align 1, !tbaa !16
  %26 = icmp eq i8 %8, 0
  %27 = icmp eq i64 %7, 16
  %or.cond = and i1 %27, %26
  br i1 %or.cond, label %28, label %36

28:                                               ; preds = %18
  store i8 0, ptr @HBufferImageOnlyLineNeedRefresh, align 1, !tbaa !16
  store i8 1, ptr @HBufferImageNeedRefresh, align 1, !tbaa !16
  %29 = getelementptr inbounds i8, ptr %5, i64 32
  %30 = load ptr, ptr %29, align 8, !tbaa !20
  %31 = load ptr, ptr @HBufferImage, align 8, !tbaa !21
  %32 = icmp eq ptr %30, %31
  br i1 %32, label %33, label %36

33:                                               ; preds = %28
  %34 = tail call ptr @HBufferImageCreateLine()
  %35 = icmp eq ptr %34, null
  br i1 %35, label %46, label %36

36:                                               ; preds = %28, %33, %18
  %37 = icmp ne i64 %7, 16
  %38 = icmp ne i8 %8, 0
  %or.cond3 = select i1 %37, i1 true, i1 %38
  br i1 %or.cond3, label %41, label %39

39:                                               ; preds = %36
  %40 = add i64 %6, 1
  br label %43

41:                                               ; preds = %36
  %not. = xor i1 %38, true
  %42 = zext i1 %not. to i64
  %spec.select = add i64 %7, %42
  %spec.select40 = zext i1 %not. to i8
  br label %43

43:                                               ; preds = %41, %39, %12
  %.034 = phi i64 [ %6, %12 ], [ %40, %39 ], [ %6, %41 ]
  %.1 = phi i64 [ %7, %12 ], [ 1, %39 ], [ %spec.select, %41 ]
  %.0 = phi i8 [ 0, %12 ], [ 1, %39 ], [ %spec.select40, %41 ]
  tail call void @HBufferImageMovePosition(i64 noundef %.034, i64 noundef %.1, i8 noundef zeroext %.0)
  %44 = load i8, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 89), align 1, !tbaa !22
  %.not = icmp eq i8 %44, 0
  br i1 %.not, label %45, label %46

45:                                               ; preds = %43
  store i8 1, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 89), align 1, !tbaa !22
  br label %46

46:                                               ; preds = %43, %45, %33, %1
  %.035 = phi i64 [ 0, %1 ], [ -9223372036854775799, %33 ], [ 0, %45 ], [ 0, %43 ]
  ret i64 %.035
}

; Function Attrs: nounwind uwtable
declare dso_local void @HBufferImageMovePosition(i64 noundef, i64 noundef, i8 noundef zeroext) 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, !7, i64 24}
!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 64}
!14 = !{!6, !10, i64 72}
!15 = !{!6, !8, i64 88}
!16 = !{!8, !8, 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 = !{!18, !7, i64 32}
!21 = !{!6, !7, i64 0}
!22 = !{!6, !8, i64 89}


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