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

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

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

declare zeroext i8 @DebugAssertEnabled() #1

declare void @DebugAssert(ptr noundef, i64 noundef, ptr 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 @HBufferImageScrollDown() #2 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i8, align 1
  %6 = 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
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  %7 = load ptr, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 3), align 8, !tbaa !5
  store ptr %7, ptr %2, align 8, !tbaa !13
  %8 = load i64, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 6), align 8, !tbaa !14
  store i64 %8, ptr %3, align 8, !tbaa !15
  %9 = 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 %9, ptr %4, align 8, !tbaa !15
  %10 = load i8, ptr getelementptr inbounds (%struct.HEFI_EDITOR_BUFFER_IMAGE, ptr @HBufferImage, i32 0, i32 8), align 8, !tbaa !17
  store i8 %10, ptr %5, align 1, !tbaa !18
  %11 = load ptr, ptr %2, align 8, !tbaa !13
  %12 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %11, i32 0, i32 3
  %13 = getelementptr inbounds %struct._LIST_ENTRY, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !19
  %15 = load ptr, ptr @HBufferImage, align 8, !tbaa !22
  %16 = icmp ne ptr %14, %15
  br i1 %16, label %17, label %60

17:                                               ; preds = %0
  %18 = load i64, ptr %3, align 8, !tbaa !15
  %19 = add i64 %18, 1
  store i64 %19, ptr %3, align 8, !tbaa !15
  %20 = call zeroext i8 @DebugAssertEnabled()
  %21 = zext i8 %20 to i32
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %39

23:                                               ; preds = %17
  %24 = load ptr, ptr %2, align 8, !tbaa !13
  %25 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %24, i32 0, i32 3
  %26 = getelementptr inbounds %struct._LIST_ENTRY, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !19
  %28 = ptrtoint ptr %27 to i64
  %29 = sub i64 %28, ptrtoint (ptr getelementptr inbounds (%struct._HEFI_EDITOR_LINE, ptr null, i32 0, i32 3) to i64)
  %30 = inttoptr i64 %29 to ptr
  %31 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %30, i32 0, i32 0
  %32 = load i64, ptr %31, align 8, !tbaa !23
  %33 = icmp ne i64 %32, 1819043173
  br i1 %33, label %34, label %39

34:                                               ; preds = %23
  call void @DebugAssert(ptr noundef @.str.10, i64 noundef 1637, ptr noundef @.str.11)
  %35 = load ptr, ptr %2, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %35, i32 0, i32 3
  %37 = getelementptr inbounds %struct._LIST_ENTRY, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !19
  br label %47

39:                                               ; preds = %23, %17
  %40 = load ptr, ptr %2, align 8, !tbaa !13
  %41 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %40, i32 0, i32 3
  %42 = getelementptr inbounds %struct._LIST_ENTRY, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !19
  %44 = ptrtoint ptr %43 to i64
  %45 = sub i64 %44, ptrtoint (ptr getelementptr inbounds (%struct._HEFI_EDITOR_LINE, ptr null, i32 0, i32 3) to i64)
  %46 = inttoptr i64 %45 to ptr
  br label %47

47:                                               ; preds = %39, %34
  %48 = phi ptr [ %38, %34 ], [ %46, %39 ]
  store ptr %48, ptr %2, align 8, !tbaa !13
  %49 = load i64, ptr %4, align 8, !tbaa !15
  %50 = load ptr, ptr %2, align 8, !tbaa !13
  %51 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %50, i32 0, i32 2
  %52 = load i64, ptr %51, align 8, !tbaa !24
  %53 = icmp ugt i64 %49, %52
  br i1 %53, label %54, label %59

54:                                               ; preds = %47
  %55 = load ptr, ptr %2, align 8, !tbaa !13
  %56 = getelementptr inbounds %struct._HEFI_EDITOR_LINE, ptr %55, i32 0, i32 2
  %57 = load i64, ptr %56, align 8, !tbaa !24
  %58 = add i64 %57, 1
  store i64 %58, ptr %4, align 8, !tbaa !15
  store i8 1, ptr %5, align 1, !tbaa !18
  br label %59

59:                                               ; preds = %54, %47
  br label %61

60:                                               ; preds = %0
  store i64 0, ptr %1, align 8
  store i32 1, ptr %6, align 4
  br label %65

61:                                               ; preds = %59
  %62 = load i64, ptr %3, align 8, !tbaa !15
  %63 = load i64, ptr %4, align 8, !tbaa !15
  %64 = load i8, ptr %5, align 1, !tbaa !18
  call void @HBufferImageMovePosition(i64 noundef %62, i64 noundef %63, i8 noundef zeroext %64)
  store i64 0, ptr %1, align 8
  store i32 1, ptr %6, align 4
  br label %65

65:                                               ; preds = %61, %60
  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
  %66 = load i64, ptr %1, align 8
  ret i64 %66
}

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 = !{!20, !7, i64 32}
!20 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !8, i64 8, !10, i64 24, !21, i64 32}
!21 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!22 = !{!6, !7, i64 0}
!23 = !{!20, !10, i64 0}
!24 = !{!20, !10, i64 24}


; 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
@.str.10 = external hidden unnamed_addr constant [167 x i8], align 1
@.str.11 = 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 @HBufferImageMovePosition(i64 noundef, i64 noundef, i8 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @HBufferImageScrollDown() local_unnamed_addr #1 {
  %1 = load ptr, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 24), 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 = getelementptr inbounds i8, ptr %1, i64 32
  %5 = load ptr, ptr %4, align 8, !tbaa !15
  %6 = load ptr, ptr @HBufferImage, align 8, !tbaa !18
  %.not = icmp eq ptr %5, %6
  br i1 %.not, label %20, label %7

7:                                                ; preds = %0
  %8 = load i64, ptr getelementptr inbounds (i8, ptr @HBufferImage, i64 64), align 8, !tbaa !19
  %9 = add i64 %8, 1
  %10 = tail call zeroext i8 @DebugAssertEnabled() #2
  %.not15 = icmp eq i8 %10, 0
  %.pre = load ptr, ptr %4, align 8, !tbaa !15
  %.pre18 = ptrtoint ptr %.pre to i64
  %.pre19 = add i64 %.pre18, -32
  %.pre21 = inttoptr i64 %.pre19 to ptr
  br i1 %.not15, label %._crit_edge, label %11

11:                                               ; preds = %7
  %12 = load i64, ptr %.pre21, align 8, !tbaa !20
  %.not16 = icmp eq i64 %12, 1819043173
  br i1 %.not16, label %._crit_edge, label %13

13:                                               ; preds = %11
  tail call void @DebugAssert(ptr noundef nonnull @.str.10, i64 noundef 1637, ptr noundef nonnull @.str.11) #2
  %14 = load ptr, ptr %4, align 8, !tbaa !15
  br label %._crit_edge

._crit_edge:                                      ; preds = %7, %11, %13
  %15 = phi ptr [ %14, %13 ], [ %.pre21, %11 ], [ %.pre21, %7 ]
  %16 = getelementptr inbounds i8, ptr %15, i64 24
  %17 = load i64, ptr %16, align 8, !tbaa !21
  %18 = icmp ugt i64 %2, %17
  %19 = add i64 %17, 1
  %spec.select = select i1 %18, i64 %19, i64 %2
  %spec.select17 = select i1 %18, i8 1, i8 %3
  tail call void @HBufferImageMovePosition(i64 noundef %9, i64 noundef %spec.select, i8 noundef zeroext %spec.select17)
  br label %20

20:                                               ; preds = %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 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 72}
!14 = !{!6, !8, i64 88}
!15 = !{!16, !7, i64 32}
!16 = !{!"_HEFI_EDITOR_LINE", !10, i64 0, !8, i64 8, !10, i64 24, !17, i64 32}
!17 = !{!"_LIST_ENTRY", !7, i64 0, !7, i64 8}
!18 = !{!6, !7, i64 0}
!19 = !{!6, !10, i64 64}
!20 = !{!16, !10, i64 0}
!21 = !{!16, !10, i64 24}


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