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

; 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
@.str.1 = external hidden unnamed_addr constant [163 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare zeroext i8 @DebugAssertEnabled() #1

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) #1

; 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 void @FileBufferMovePosition(i64 noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i64 @FileBufferScrollDown() #2 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %6 = load ptr, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 12), align 8, !tbaa !5
  store ptr %6, ptr %2, align 8, !tbaa !13
  %7 = load ptr, ptr %2, align 8, !tbaa !13
  %8 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %7, i32 0, i32 1
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %0
  store i64 0, ptr %1, align 8
  store i32 1, ptr %5, align 4
  br label %68

12:                                               ; preds = %0
  %13 = load i64, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 6), align 8, !tbaa !17
  store i64 %13, ptr %3, align 8, !tbaa !18
  %14 = 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 !19
  store i64 %14, ptr %4, align 8, !tbaa !18
  %15 = load ptr, ptr %2, align 8, !tbaa !13
  %16 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %15, i32 0, i32 5
  %17 = getelementptr inbounds %struct._LIST_ENTRY, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !20
  %19 = load ptr, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 2), align 8, !tbaa !21
  %20 = icmp ne ptr %18, %19
  br i1 %20, label %21, label %64

21:                                               ; preds = %12
  %22 = load i64, ptr %3, align 8, !tbaa !18
  %23 = add i64 %22, 1
  store i64 %23, ptr %3, align 8, !tbaa !18
  %24 = call zeroext i8 @DebugAssertEnabled()
  %25 = zext i8 %24 to i32
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %43

27:                                               ; preds = %21
  %28 = load ptr, ptr %2, align 8, !tbaa !13
  %29 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %28, i32 0, i32 5
  %30 = getelementptr inbounds %struct._LIST_ENTRY, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !20
  %32 = ptrtoint ptr %31 to i64
  %33 = sub i64 %32, ptrtoint (ptr getelementptr inbounds (%struct._EFI_EDITOR_LINE, ptr null, i32 0, i32 5) to i64)
  %34 = inttoptr i64 %33 to ptr
  %35 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %34, i32 0, i32 0
  %36 = load i64, ptr %35, align 8, !tbaa !22
  %37 = icmp ne i64 %36, 1819043173
  br i1 %37, label %38, label %43

38:                                               ; preds = %27
  call void @DebugAssert(ptr noundef @.str.1, i64 noundef 2233, ptr noundef @.str.2)
  %39 = load ptr, ptr %2, align 8, !tbaa !13
  %40 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %39, i32 0, i32 5
  %41 = getelementptr inbounds %struct._LIST_ENTRY, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !20
  br label %51

43:                                               ; preds = %27, %21
  %44 = load ptr, ptr %2, align 8, !tbaa !13
  %45 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %44, i32 0, i32 5
  %46 = getelementptr inbounds %struct._LIST_ENTRY, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !20
  %48 = ptrtoint ptr %47 to i64
  %49 = sub i64 %48, ptrtoint (ptr getelementptr inbounds (%struct._EFI_EDITOR_LINE, ptr null, i32 0, i32 5) to i64)
  %50 = inttoptr i64 %49 to ptr
  br label %51

51:                                               ; preds = %43, %38
  %52 = phi ptr [ %42, %38 ], [ %50, %43 ]
  store ptr %52, ptr %2, align 8, !tbaa !13
  %53 = load i64, ptr %4, align 8, !tbaa !18
  %54 = load ptr, ptr %2, align 8, !tbaa !13
  %55 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %54, i32 0, i32 2
  %56 = load i64, ptr %55, align 8, !tbaa !23
  %57 = icmp ugt i64 %53, %56
  br i1 %57, label %58, label %63

58:                                               ; preds = %51
  %59 = load ptr, ptr %2, align 8, !tbaa !13
  %60 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %59, i32 0, i32 2
  %61 = load i64, ptr %60, align 8, !tbaa !23
  %62 = add i64 %61, 1
  store i64 %62, ptr %4, align 8, !tbaa !18
  br label %63

63:                                               ; preds = %58, %51
  br label %65

64:                                               ; preds = %12
  store i64 0, ptr %1, align 8
  store i32 1, ptr %5, align 4
  br label %68

65:                                               ; preds = %63
  %66 = load i64, ptr %3, align 8, !tbaa !18
  %67 = load i64, ptr %4, align 8, !tbaa !18
  call void @FileBufferMovePosition(i64 noundef %66, i64 noundef %67)
  store i64 0, ptr %1, align 8
  store i32 1, ptr %5, align 4
  br label %68

68:                                               ; preds = %65, %64, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  %69 = load i64, ptr %1, align 8
  ret i64 %69
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !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 = !{!7, !7, i64 0}
!14 = !{!15, !7, i64 8}
!15 = !{!"_EFI_EDITOR_LINE", !11, i64 0, !7, i64 8, !11, i64 16, !11, i64 24, !10, i64 32, !16, i64 40}
!16 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!17 = !{!6, !11, i64 56}
!18 = !{!11, !11, i64 0}
!19 = !{!6, !11, i64 64}
!20 = !{!15, !7, i64 40}
!21 = !{!6, !7, i64 16}
!22 = !{!15, !11, i64 0}
!23 = !{!15, !11, i64 16}


; 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
@.str.1 = external hidden unnamed_addr constant [163 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [21 x i8], align 1

declare zeroext i8 @DebugAssertEnabled() local_unnamed_addr #0

declare void @DebugAssert(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @FileBufferMovePosition(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @FileBufferScrollDown() local_unnamed_addr #1 {
  %1 = load ptr, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 112), align 8, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !13
  %4 = icmp eq ptr %3, null
  br i1 %4, label %23, label %5

5:                                                ; preds = %0
  %6 = load i64, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 64), align 8, !tbaa !16
  %7 = getelementptr inbounds i8, ptr %1, i64 40
  %8 = load ptr, ptr %7, align 8, !tbaa !17
  %9 = load ptr, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 16), align 8, !tbaa !18
  %.not = icmp eq ptr %8, %9
  br i1 %.not, label %23, label %10

10:                                               ; preds = %5
  %11 = load i64, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 56), align 8, !tbaa !19
  %12 = add i64 %11, 1
  %13 = tail call zeroext i8 @DebugAssertEnabled() #2
  %.not14 = icmp eq i8 %13, 0
  %.pre = load ptr, ptr %7, align 8, !tbaa !17
  %.pre16 = ptrtoint ptr %.pre to i64
  %.pre17 = add i64 %.pre16, -40
  %.pre19 = inttoptr i64 %.pre17 to ptr
  br i1 %.not14, label %._crit_edge, label %14

14:                                               ; preds = %10
  %15 = load i64, ptr %.pre19, align 8, !tbaa !20
  %.not15 = icmp eq i64 %15, 1819043173
  br i1 %.not15, label %._crit_edge, label %16

16:                                               ; preds = %14
  tail call void @DebugAssert(ptr noundef nonnull @.str.1, i64 noundef 2233, ptr noundef nonnull @.str.2) #2
  %17 = load ptr, ptr %7, align 8, !tbaa !17
  br label %._crit_edge

._crit_edge:                                      ; preds = %10, %14, %16
  %18 = phi ptr [ %17, %16 ], [ %.pre19, %14 ], [ %.pre19, %10 ]
  %19 = getelementptr inbounds i8, ptr %18, i64 16
  %20 = load i64, ptr %19, align 8, !tbaa !21
  %21 = icmp ugt i64 %6, %20
  %22 = add i64 %20, 1
  %spec.select = select i1 %21, i64 %22, i64 %6
  tail call void @FileBufferMovePosition(i64 noundef %12, i64 noundef %spec.select)
  br label %23

23:                                               ; preds = %5, %0, %._crit_edge
  ret i64 0
}

attributes #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 = { 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, !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 = !{!14, !7, i64 8}
!14 = !{!"_EFI_EDITOR_LINE", !11, i64 0, !7, i64 8, !11, i64 16, !11, i64 24, !10, i64 32, !15, i64 40}
!15 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!16 = !{!6, !11, i64 64}
!17 = !{!14, !7, i64 40}
!18 = !{!6, !7, i64 16}
!19 = !{!6, !11, i64 56}
!20 = !{!14, !11, i64 0}
!21 = !{!14, !11, i64 16}


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