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

; 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_GLOBAL_EDITOR = type { ptr, %union.HEFI_EDITOR_COLOR_UNION, %struct.HEFI_EDITOR_POSITION, i8, ptr, i32, i32, i64, i64 }
%union.HEFI_EDITOR_COLOR_UNION = type { 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
@HMainEditor = external global %struct.HEFI_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

declare ptr @HMoveLine(i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @HBufferImageMovePosition(i64 noundef, i64 noundef, i8 noundef zeroext) #2

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

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

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

23:                                               ; preds = %19, %16
  %24 = load i64, ptr %4, align 8, !tbaa !15
  %25 = call ptr @HMoveLine(i64 noundef %24)
  store ptr %25, ptr %1, align 8, !tbaa !13
  %26 = load ptr, ptr %1, align 8, !tbaa !13
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %39

28:                                               ; preds = %23
  %29 = load i64, ptr %3, align 8, !tbaa !15
  %30 = load ptr, ptr %1, align 8, !tbaa !13
  %31 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %30, i32 0, i32 2
  %32 = load i64, ptr %31, align 8, !tbaa !22
  %33 = icmp ugt i64 %29, %32
  br i1 %33, label %34, label %39

34:                                               ; preds = %28
  %35 = load ptr, ptr %1, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %35, i32 0, i32 2
  %37 = load i64, ptr %36, align 8, !tbaa !22
  %38 = add i64 %37, 1
  store i64 %38, ptr %3, align 8, !tbaa !15
  store i8 1, ptr %5, align 1, !tbaa !18
  br label %39

39:                                               ; preds = %34, %28, %23
  %40 = load i64, ptr %4, align 8, !tbaa !15
  %41 = load i64, ptr %2, align 8, !tbaa !15
  %42 = add i64 %41, %40
  store i64 %42, ptr %2, align 8, !tbaa !15
  %43 = load i64, ptr %2, align 8, !tbaa !15
  %44 = load i64, ptr %3, align 8, !tbaa !15
  %45 = load i8, ptr %5, align 1, !tbaa !18
  call void @HBufferImageMovePosition(i64 noundef %43, i64 noundef %44, i8 noundef zeroext %45)
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  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
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret i64 0
}

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 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 = !{!7, !7, i64 0}
!14 = !{!6, !10, i64 64}
!15 = !{!10, !10, i64 0}
!16 = !{!6, !10, i64 72}
!17 = !{!6, !8, i64 88}
!18 = !{!8, !8, i64 0}
!19 = !{!6, !10, i64 16}
!20 = !{!21, !10, i64 16}
!21 = !{!"", !7, i64 0, !8, i64 8, !11, i64 16, !8, i64 32, !7, i64 40, !12, i64 48, !12, i64 52, !10, i64 56, !10, i64 64}
!22 = !{!23, !10, i64 24}
!23 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !8, i64 8, !10, i64 24, !24, i64 32}
!24 = !{!"_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.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_GLOBAL_EDITOR = type { ptr, %union.HEFI_EDITOR_COLOR_UNION, %struct.HEFI_EDITOR_POSITION, i8, ptr, i32, i32, i64, i64 }
%union.HEFI_EDITOR_COLOR_UNION = type { i64 }

@HBufferImage = external dso_local local_unnamed_addr global %struct.HEFI_EDITOR_BUFFER_IMAGE, align 8
@HMainEditor = external local_unnamed_addr global %struct.HEFI_EDITOR_GLOBAL_EDITOR, align 8

declare ptr @HMoveLine(i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @HBufferImagePageDown() local_unnamed_addr #1 {
  %1 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 64), align 8, !tbaa !5
  %2 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 72), align 8, !tbaa !13
  %3 = load i8, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 88), align 8, !tbaa !14
  %4 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 16), align 8, !tbaa !15
  %5 = load i64, ptr getelementptr inbounds (i8, ptr @HMainEditor, i64 16), align 8, !tbaa !16
  %6 = add i64 %5, -2
  %7 = add i64 %6, %1
  %.not = icmp ult i64 %4, %7
  %8 = sub i64 %4, %1
  %.011 = select i1 %.not, i64 %8, i64 %6
  %9 = tail call ptr @HMoveLine(i64 noundef %.011) #2
  %.not17 = icmp eq ptr %9, null
  br i1 %.not17, label %15, label %10

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

15:                                               ; preds = %10, %0
  %.012 = phi i64 [ %2, %0 ], [ %spec.select, %10 ]
  %.0 = phi i8 [ %3, %0 ], [ %spec.select18, %10 ]
  %16 = add i64 %.011, %1
  tail call void @HBufferImageMovePosition(i64 noundef %16, i64 noundef %.012, i8 noundef zeroext %.0)
  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, !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, !10, i64 72}
!14 = !{!6, !8, i64 88}
!15 = !{!6, !10, i64 16}
!16 = !{!17, !10, i64 16}
!17 = !{!"", !7, i64 0, !8, i64 8, !11, i64 16, !8, i64 32, !7, i64 40, !12, i64 48, !12, i64 52, !10, i64 56, !10, i64 64}
!18 = !{!19, !10, i64 24}
!19 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !8, i64 8, !10, i64 24, !20, i64 32}
!20 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}


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