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

; 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_GLOBAL_EDITOR = type { ptr, %union.EFI_EDITOR_COLOR_UNION, %struct.EFI_EDITOR_POSITION, ptr, i8, ptr, i32, i32 }
%union.EFI_EDITOR_COLOR_UNION = type { 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
@MainEditor = external global %struct.EFI_EDITOR_GLOBAL_EDITOR, align 8

; 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 @MoveLine(i64 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @FileBufferPageDown() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  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
  %5 = load ptr, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 12), align 8, !tbaa !5
  store ptr %5, ptr %1, align 8, !tbaa !13
  %6 = load i64, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 6), align 8, !tbaa !14
  store i64 %6, ptr %2, align 8, !tbaa !15
  %7 = 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 !16
  store i64 %7, ptr %3, align 8, !tbaa !15
  %8 = load i64, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 4), align 8, !tbaa !17
  %9 = load i64, ptr %2, align 8, !tbaa !15
  %10 = load i64, ptr getelementptr inbounds (%struct.EFI_EDITOR_GLOBAL_EDITOR, ptr @MainEditor, i32 0, i32 2), align 8, !tbaa !18
  %11 = sub i64 %10, 2
  %12 = add i64 %9, %11
  %13 = icmp uge i64 %8, %12
  br i1 %13, label %14, label %17

14:                                               ; preds = %0
  %15 = load i64, ptr getelementptr inbounds (%struct.EFI_EDITOR_GLOBAL_EDITOR, ptr @MainEditor, i32 0, i32 2), align 8, !tbaa !18
  %16 = sub i64 %15, 2
  store i64 %16, ptr %4, align 8, !tbaa !15
  br label %21

17:                                               ; preds = %0
  %18 = load i64, ptr getelementptr inbounds (%struct.EFI_EDITOR_FILE_BUFFER, ptr @FileBuffer, i32 0, i32 4), align 8, !tbaa !17
  %19 = load i64, ptr %2, align 8, !tbaa !15
  %20 = sub i64 %18, %19
  store i64 %20, ptr %4, align 8, !tbaa !15
  br label %21

21:                                               ; preds = %17, %14
  %22 = load i64, ptr %4, align 8, !tbaa !15
  %23 = call ptr @MoveLine(i64 noundef %22)
  store ptr %23, ptr %1, align 8, !tbaa !13
  %24 = load ptr, ptr %1, align 8, !tbaa !13
  %25 = icmp ne ptr %24, null
  br i1 %25, label %26, label %37

26:                                               ; preds = %21
  %27 = load i64, ptr %3, align 8, !tbaa !15
  %28 = load ptr, ptr %1, align 8, !tbaa !13
  %29 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %28, i32 0, i32 2
  %30 = load i64, ptr %29, align 8, !tbaa !20
  %31 = icmp ugt i64 %27, %30
  br i1 %31, label %32, label %37

32:                                               ; preds = %26
  %33 = load ptr, ptr %1, align 8, !tbaa !13
  %34 = getelementptr inbounds %struct._EFI_EDITOR_LINE, ptr %33, i32 0, i32 2
  %35 = load i64, ptr %34, align 8, !tbaa !20
  %36 = add i64 %35, 1
  store i64 %36, ptr %3, align 8, !tbaa !15
  br label %37

37:                                               ; preds = %32, %26, %21
  %38 = load i64, ptr %4, align 8, !tbaa !15
  %39 = load i64, ptr %2, align 8, !tbaa !15
  %40 = add i64 %39, %38
  store i64 %40, ptr %2, align 8, !tbaa !15
  %41 = load i64, ptr %2, align 8, !tbaa !15
  %42 = load i64, ptr %3, align 8, !tbaa !15
  call void @FileBufferMovePosition(i64 noundef %41, i64 noundef %42)
  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
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #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, !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 = !{!6, !11, i64 56}
!15 = !{!11, !11, i64 0}
!16 = !{!6, !11, i64 64}
!17 = !{!6, !11, i64 32}
!18 = !{!19, !11, i64 16}
!19 = !{!"", !7, i64 0, !8, i64 8, !12, i64 16, !7, i64 32, !8, i64 40, !7, i64 48, !10, i64 56, !10, i64 60}
!20 = !{!21, !11, i64 16}
!21 = !{!"_EFI_EDITOR_LINE", !11, i64 0, !7, i64 8, !11, i64 16, !11, i64 24, !10, i64 32, !22, i64 40}
!22 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}


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

@FileBuffer = external dso_local local_unnamed_addr global %struct.EFI_EDITOR_FILE_BUFFER, align 8
@MainEditor = external local_unnamed_addr global %struct.EFI_EDITOR_GLOBAL_EDITOR, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @FileBufferPageDown() local_unnamed_addr #0 {
  %1 = load i64, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 56), align 8, !tbaa !5
  %2 = load i64, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 64), align 8, !tbaa !13
  %3 = load i64, ptr getelementptr inbounds (i8, ptr @FileBuffer, i64 32), align 8, !tbaa !14
  %4 = load i64, ptr getelementptr inbounds (i8, ptr @MainEditor, i64 16), align 8, !tbaa !15
  %5 = add i64 %4, -2
  %6 = add i64 %5, %1
  %.not = icmp ult i64 %3, %6
  %7 = sub i64 %3, %1
  %.0 = select i1 %.not, i64 %7, i64 %5
  %8 = tail call ptr @MoveLine(i64 noundef %.0)
  %.not15 = icmp eq ptr %8, null
  br i1 %.not15, label %14, label %9

9:                                                ; preds = %0
  %10 = getelementptr inbounds i8, ptr %8, i64 16
  %11 = load i64, ptr %10, align 8, !tbaa !17
  %12 = icmp ugt i64 %2, %11
  %13 = add i64 %11, 1
  %spec.select = select i1 %12, i64 %13, i64 %2
  br label %14

14:                                               ; preds = %9, %0
  %.010 = phi i64 [ %2, %0 ], [ %spec.select, %9 ]
  %15 = add i64 %.0, %1
  tail call void @FileBufferMovePosition(i64 noundef %15, i64 noundef %.010)
  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, !11, i64 56}
!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 = !{!6, !11, i64 64}
!14 = !{!6, !11, i64 32}
!15 = !{!16, !11, i64 16}
!16 = !{!"", !7, i64 0, !8, i64 8, !12, i64 16, !7, i64 32, !8, i64 40, !7, i64 48, !10, i64 56, !10, i64 60}
!17 = !{!18, !11, i64 16}
!18 = !{!"_EFI_EDITOR_LINE", !11, i64 0, !7, i64 8, !11, i64 16, !11, i64 24, !10, i64 32, !19, i64 40}
!19 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}


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