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

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

@FileBuffer = external dso_local global %struct.EFI_EDITOR_FILE_BUFFER, align 8
@FileBufferOnlyLineNeedRefresh = 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 @FileBufferScrollRight() #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @FileBufferAddChar(i16 noundef zeroext %0) #1 {
  %2 = alloca i16, align 2
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  store i16 %0, ptr %2, align 2, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %5 = load ptr, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 12), align 8, !tbaa !9
  store ptr %5, ptr %3, align 8, !tbaa !15
  store i8 1, ptr @FileBufferOnlyLineNeedRefresh, align 1, !tbaa !16
  %6 = load i64, ptr getelementptr inbounds (%struct.EFI_EDITOR_POSITION, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 6), i32 0, i32 1), align 8, !tbaa !17
  %7 = sub i64 %6, 1
  store i64 %7, ptr %4, align 8, !tbaa !18
  %8 = load i8, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 10), align 1, !tbaa !19
  %9 = zext i8 %8 to i32
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %18, label %11

11:                                               ; preds = %1
  %12 = load i64, ptr %4, align 8, !tbaa !18
  %13 = add i64 %12, 1
  %14 = load ptr, ptr %3, align 8, !tbaa !15
  %15 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %14, i32 0, i32 2
  %16 = load i64, ptr %15, align 8, !tbaa !20
  %17 = icmp ugt i64 %13, %16
  br i1 %17, label %18, label %27

18:                                               ; preds = %11, %1
  %19 = load ptr, ptr %3, align 8, !tbaa !15
  %20 = load i16, ptr %2, align 2, !tbaa !5
  %21 = load i64, ptr %4, align 8, !tbaa !18
  %22 = load ptr, ptr %3, align 8, !tbaa !15
  %23 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %22, i32 0, i32 2
  %24 = load i64, ptr %23, align 8, !tbaa !20
  %25 = add i64 %24, 1
  %26 = call i64 @LineStrInsert(ptr noundef %19, i16 noundef zeroext %20, i64 noundef %21, i64 noundef %25)
  br label %34

27:                                               ; preds = %11
  %28 = load i16, ptr %2, align 2, !tbaa !5
  %29 = load ptr, ptr %3, align 8, !tbaa !15
  %30 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !23
  %32 = load i64, ptr %4, align 8, !tbaa !18
  %33 = getelementptr inbounds i16, ptr %31, i64 %32
  store i16 %28, ptr %33, align 2, !tbaa !5
  br label %34

34:                                               ; preds = %27, %18
  %35 = call i64 @FileBufferScrollRight()
  %36 = load i8, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 9), align 8, !tbaa !24
  %37 = icmp ne i8 %36, 0
  br i1 %37, label %39, label %38

38:                                               ; preds = %34
  store i8 1, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 9), align 8, !tbaa !24
  br label %39

39:                                               ; preds = %38, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i64 0
}

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, !11, i64 112}
!10 = !{!"", !11, i64 0, !12, i64 8, !11, i64 16, !11, i64 24, !13, i64 32, !14, i64 40, !14, i64 56, !14, i64 72, !14, i64 88, !7, i64 104, !7, i64 105, !7, i64 106, !11, i64 112}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long long", !7, i64 0}
!14 = !{!"", !13, i64 0, !13, i64 8}
!15 = !{!11, !11, i64 0}
!16 = !{!7, !7, i64 0}
!17 = !{!10, !13, i64 64}
!18 = !{!13, !13, i64 0}
!19 = !{!10, !7, i64 105}
!20 = !{!21, !13, i64 16}
!21 = !{!"_EFI_EDITOR_LINE", !13, i64 0, !11, i64 8, !13, i64 16, !13, i64 24, !12, i64 32, !22, i64 40}
!22 = !{!"_LIST_ENTRY", !11, i64 0, !11, i64 8}
!23 = !{!21, !11, i64 8}
!24 = !{!10, !7, i64 104}


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

@FileBuffer = external dso_local local_unnamed_addr global %struct.EFI_EDITOR_FILE_BUFFER, align 8
@FileBufferOnlyLineNeedRefresh = external dso_local local_unnamed_addr global i8, align 1

; Function Attrs: nounwind uwtable
declare dso_local i64 @FileBufferScrollRight() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @FileBufferAddChar(i16 noundef zeroext %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 112), align 8, !tbaa !5
  store i8 1, ptr @FileBufferOnlyLineNeedRefresh, align 1, !tbaa !13
  %3 = load i64, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 64), align 8, !tbaa !14
  %4 = add i64 %3, -1
  %5 = load i8, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 105), align 1, !tbaa !15
  %.not = icmp ne i8 %5, 0
  %6 = getelementptr inbounds i8, ptr %2, i64 16
  %7 = load i64, ptr %6, align 8, !tbaa !16
  %8 = icmp ugt i64 %3, %7
  %or.cond = select i1 %.not, i1 true, i1 %8
  br i1 %or.cond, label %._crit_edge, label %11

._crit_edge:                                      ; preds = %1
  %9 = add i64 %7, 1
  %10 = tail call i64 @LineStrInsert(ptr noundef nonnull %2, i16 noundef zeroext %0, i64 noundef %4, i64 noundef %9)
  br label %15

11:                                               ; preds = %1
  %12 = getelementptr inbounds i8, ptr %2, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !19
  %14 = getelementptr inbounds i16, ptr %13, i64 %4
  store i16 %0, ptr %14, align 2, !tbaa !20
  br label %15

15:                                               ; preds = %11, %._crit_edge
  %16 = tail call i64 @FileBufferScrollRight()
  %17 = load i8, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 104), align 8, !tbaa !22
  %.not8 = icmp eq i8 %17, 0
  br i1 %.not8, label %18, label %19

18:                                               ; preds = %15
  store i8 1, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 104), align 8, !tbaa !22
  br label %19

19:                                               ; preds = %18, %15
  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, !7, i64 112}
!6 = !{!"", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24, !11, i64 32, !12, i64 40, !12, i64 56, !12, i64 72, !12, i64 88, !8, i64 104, !8, i64 105, !8, i64 106, !7, i64 112}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long long", !8, i64 0}
!12 = !{!"", !11, i64 0, !11, i64 8}
!13 = !{!8, !8, i64 0}
!14 = !{!6, !11, i64 64}
!15 = !{!6, !8, i64 105}
!16 = !{!17, !11, i64 16}
!17 = !{!"_EFI_EDITOR_LINE", !11, i64 0, !7, i64 8, !11, i64 16, !11, i64 24, !10, i64 32, !18, i64 40}
!18 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!19 = !{!17, !7, i64 8}
!20 = !{!21, !21, i64 0}
!21 = !{!"short", !8, i64 0}
!22 = !{!6, !8, i64 104}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass