; LLVM IR from xing1357/SimpleOS :: src (console)

; 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"

@g_current_temp_page = external dso_local global i32, align 4
@g_vga_buffer = external hidden global ptr, align 8
@g_temp_pages = external hidden global [16 x [2200 x i16]], align 16

; 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
define dso_local void @console_scroll(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 1
  br i1 %5, label %6, label %34

6:                                                ; preds = %1
  %7 = load i32, ptr @g_current_temp_page, align 4, !tbaa !5
  %8 = icmp ugt i32 %7, 0
  br i1 %8, label %9, label %12

9:                                                ; preds = %6
  %10 = load i32, ptr @g_current_temp_page, align 4, !tbaa !5
  %11 = add i32 %10, -1
  store i32 %11, ptr @g_current_temp_page, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %9, %6
  %13 = load i32, ptr @g_current_temp_page, align 4, !tbaa !5
  %14 = urem i32 %13, 16
  store i32 %14, ptr @g_current_temp_page, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %30, %12
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = icmp ult i32 %16, 2200
  br i1 %17, label %18, label %33

18:                                               ; preds = %15
  %19 = load i32, ptr @g_current_temp_page, align 4, !tbaa !5
  %20 = zext i32 %19 to i64
  %21 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %20
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = zext i32 %22 to i64
  %24 = getelementptr inbounds [2200 x i16], ptr %21, i64 0, i64 %23
  %25 = load i16, ptr %24, align 2, !tbaa !9
  %26 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !11
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds i16, ptr %26, i64 %28
  store i16 %25, ptr %29, align 2, !tbaa !9
  br label %30

30:                                               ; preds = %18
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = add i32 %31, 1
  store i32 %32, ptr %3, align 4, !tbaa !5
  br label %15, !llvm.loop !13

33:                                               ; preds = %15
  br label %58

34:                                               ; preds = %1
  %35 = load i32, ptr @g_current_temp_page, align 4, !tbaa !5
  %36 = add i32 %35, 1
  store i32 %36, ptr @g_current_temp_page, align 4, !tbaa !5
  %37 = load i32, ptr @g_current_temp_page, align 4, !tbaa !5
  %38 = urem i32 %37, 16
  store i32 %38, ptr @g_current_temp_page, align 4, !tbaa !5
  store i32 0, ptr %3, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %54, %34
  %40 = load i32, ptr %3, align 4, !tbaa !5
  %41 = icmp ult i32 %40, 2200
  br i1 %41, label %42, label %57

42:                                               ; preds = %39
  %43 = load i32, ptr @g_current_temp_page, align 4, !tbaa !5
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %44
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds [2200 x i16], ptr %45, i64 0, i64 %47
  %49 = load i16, ptr %48, align 2, !tbaa !9
  %50 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !11
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = zext i32 %51 to i64
  %53 = getelementptr inbounds i16, ptr %50, i64 %52
  store i16 %49, ptr %53, align 2, !tbaa !9
  br label %54

54:                                               ; preds = %42
  %55 = load i32, ptr %3, align 4, !tbaa !5
  %56 = add i32 %55, 1
  store i32 %56, ptr %3, align 4, !tbaa !5
  br label %39, !llvm.loop !15

57:                                               ; preds = %39
  br label %58

58:                                               ; preds = %57, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret void
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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"

@g_current_temp_page = external dso_local local_unnamed_addr global i32, align 4
@g_vga_buffer = external hidden local_unnamed_addr global ptr, align 8
@g_temp_pages = external hidden local_unnamed_addr global [16 x [2200 x i16]], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: write, inaccessiblemem: none) uwtable
define dso_local void @console_scroll(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq i32 %0, 1
  %3 = load i32, ptr @g_current_temp_page, align 4, !tbaa !5
  br i1 %2, label %vector.memcheck22, label %vector.memcheck

vector.memcheck22:                                ; preds = %1
  %.not = icmp eq i32 %3, 0
  %4 = add i32 %3, 15
  %5 = and i32 %4, 15
  %6 = select i1 %.not, i32 0, i32 %5
  store i32 %6, ptr @g_current_temp_page, align 4, !tbaa !5
  %7 = zext nneg i32 %6 to i64
  %8 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !9
  %9 = ptrtoint ptr %8 to i64
  %10 = sub i64 %9, ptrtoint (ptr @g_temp_pages to i64)
  %.neg33 = mul nsw i64 %7, -4400
  %11 = add i64 %.neg33, %10
  %diff.check23 = icmp ult i64 %11, 32
  br i1 %diff.check23, label %scalar.ph25.preheader, label %vector.body28

vector.body28:                                    ; preds = %vector.memcheck22, %vector.body28.1
  %index29 = phi i64 [ %index.next32.1, %vector.body28.1 ], [ 0, %vector.memcheck22 ]
  %12 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %7, i64 %index29
  %13 = getelementptr inbounds i8, ptr %12, i64 16
  %wide.load30 = load <8 x i16>, ptr %12, align 16, !tbaa !11
  %wide.load31 = load <8 x i16>, ptr %13, align 16, !tbaa !11
  %14 = getelementptr inbounds i16, ptr %8, i64 %index29
  %15 = getelementptr inbounds i8, ptr %14, i64 16
  store <8 x i16> %wide.load30, ptr %14, align 2, !tbaa !11
  store <8 x i16> %wide.load31, ptr %15, align 2, !tbaa !11
  %16 = icmp eq i64 %index29, 2176
  br i1 %16, label %scalar.ph25.preheader, label %vector.body28.1, !llvm.loop !13

vector.body28.1:                                  ; preds = %vector.body28
  %index.next32 = or disjoint i64 %index29, 16
  %17 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %7, i64 %index.next32
  %18 = getelementptr inbounds i8, ptr %17, i64 16
  %wide.load30.1 = load <8 x i16>, ptr %17, align 16, !tbaa !11
  %wide.load31.1 = load <8 x i16>, ptr %18, align 16, !tbaa !11
  %19 = getelementptr inbounds i16, ptr %8, i64 %index.next32
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  store <8 x i16> %wide.load30.1, ptr %19, align 2, !tbaa !11
  store <8 x i16> %wide.load31.1, ptr %20, align 2, !tbaa !11
  %index.next32.1 = add nuw nsw i64 %index29, 32
  br label %vector.body28

scalar.ph25.preheader:                            ; preds = %vector.body28, %vector.memcheck22
  %indvars.iv15.ph = phi i64 [ 0, %vector.memcheck22 ], [ 2192, %vector.body28 ]
  br label %scalar.ph25

scalar.ph25:                                      ; preds = %scalar.ph25, %scalar.ph25.preheader
  %indvars.iv15 = phi i64 [ %indvars.iv15.ph, %scalar.ph25.preheader ], [ %indvars.iv.next16.3, %scalar.ph25 ]
  %21 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %7, i64 %indvars.iv15
  %22 = load i16, ptr %21, align 8, !tbaa !11
  %23 = getelementptr inbounds i16, ptr %8, i64 %indvars.iv15
  store i16 %22, ptr %23, align 2, !tbaa !11
  %indvars.iv.next16 = or disjoint i64 %indvars.iv15, 1
  %24 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %7, i64 %indvars.iv.next16
  %25 = load i16, ptr %24, align 2, !tbaa !11
  %26 = getelementptr inbounds i16, ptr %8, i64 %indvars.iv.next16
  store i16 %25, ptr %26, align 2, !tbaa !11
  %indvars.iv.next16.1 = or disjoint i64 %indvars.iv15, 2
  %27 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %7, i64 %indvars.iv.next16.1
  %28 = load i16, ptr %27, align 4, !tbaa !11
  %29 = getelementptr inbounds i16, ptr %8, i64 %indvars.iv.next16.1
  store i16 %28, ptr %29, align 2, !tbaa !11
  %indvars.iv.next16.2 = or disjoint i64 %indvars.iv15, 3
  %30 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %7, i64 %indvars.iv.next16.2
  %31 = load i16, ptr %30, align 2, !tbaa !11
  %32 = getelementptr inbounds i16, ptr %8, i64 %indvars.iv.next16.2
  store i16 %31, ptr %32, align 2, !tbaa !11
  %indvars.iv.next16.3 = add nuw nsw i64 %indvars.iv15, 4
  %exitcond18.not.3 = icmp eq i64 %indvars.iv.next16.3, 2200
  br i1 %exitcond18.not.3, label %.loopexit, label %scalar.ph25, !llvm.loop !17

vector.memcheck:                                  ; preds = %1
  %33 = add i32 %3, 1
  %34 = and i32 %33, 15
  store i32 %34, ptr @g_current_temp_page, align 4, !tbaa !5
  %35 = zext nneg i32 %34 to i64
  %36 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !9
  %37 = ptrtoint ptr %36 to i64
  %38 = sub i64 %37, ptrtoint (ptr @g_temp_pages to i64)
  %.neg = mul nsw i64 %35, -4400
  %39 = add i64 %.neg, %38
  %diff.check = icmp ult i64 %39, 32
  br i1 %diff.check, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %vector.memcheck, %vector.body.1
  %index = phi i64 [ %index.next.1, %vector.body.1 ], [ 0, %vector.memcheck ]
  %40 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %35, i64 %index
  %41 = getelementptr inbounds i8, ptr %40, i64 16
  %wide.load = load <8 x i16>, ptr %40, align 16, !tbaa !11
  %wide.load21 = load <8 x i16>, ptr %41, align 16, !tbaa !11
  %42 = getelementptr inbounds i16, ptr %36, i64 %index
  %43 = getelementptr inbounds i8, ptr %42, i64 16
  store <8 x i16> %wide.load, ptr %42, align 2, !tbaa !11
  store <8 x i16> %wide.load21, ptr %43, align 2, !tbaa !11
  %44 = icmp eq i64 %index, 2176
  br i1 %44, label %scalar.ph.preheader, label %vector.body.1, !llvm.loop !18

vector.body.1:                                    ; preds = %vector.body
  %index.next = or disjoint i64 %index, 16
  %45 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %35, i64 %index.next
  %46 = getelementptr inbounds i8, ptr %45, i64 16
  %wide.load.1 = load <8 x i16>, ptr %45, align 16, !tbaa !11
  %wide.load21.1 = load <8 x i16>, ptr %46, align 16, !tbaa !11
  %47 = getelementptr inbounds i16, ptr %36, i64 %index.next
  %48 = getelementptr inbounds i8, ptr %47, i64 16
  store <8 x i16> %wide.load.1, ptr %47, align 2, !tbaa !11
  store <8 x i16> %wide.load21.1, ptr %48, align 2, !tbaa !11
  %index.next.1 = add nuw nsw i64 %index, 32
  br label %vector.body

scalar.ph.preheader:                              ; preds = %vector.body, %vector.memcheck
  %indvars.iv.ph = phi i64 [ 0, %vector.memcheck ], [ 2192, %vector.body ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph, %scalar.ph.preheader
  %indvars.iv = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.3, %scalar.ph ]
  %49 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %35, i64 %indvars.iv
  %50 = load i16, ptr %49, align 8, !tbaa !11
  %51 = getelementptr inbounds i16, ptr %36, i64 %indvars.iv
  store i16 %50, ptr %51, align 2, !tbaa !11
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %52 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %35, i64 %indvars.iv.next
  %53 = load i16, ptr %52, align 2, !tbaa !11
  %54 = getelementptr inbounds i16, ptr %36, i64 %indvars.iv.next
  store i16 %53, ptr %54, align 2, !tbaa !11
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %55 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %35, i64 %indvars.iv.next.1
  %56 = load i16, ptr %55, align 4, !tbaa !11
  %57 = getelementptr inbounds i16, ptr %36, i64 %indvars.iv.next.1
  store i16 %56, ptr %57, align 2, !tbaa !11
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %58 = getelementptr inbounds [16 x [2200 x i16]], ptr @g_temp_pages, i64 0, i64 %35, i64 %indvars.iv.next.2
  %59 = load i16, ptr %58, align 2, !tbaa !11
  %60 = getelementptr inbounds i16, ptr %36, i64 %indvars.iv.next.2
  store i16 %59, ptr %60, align 2, !tbaa !11
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, 2200
  br i1 %exitcond.not.3, label %.loopexit, label %scalar.ph, !llvm.loop !19

.loopexit:                                        ; preds = %scalar.ph, %scalar.ph25
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: write, inaccessiblemem: none) 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = distinct !{!13, !14, !15, !16}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.isvectorized", i32 1}
!16 = !{!"llvm.loop.unroll.runtime.disable"}
!17 = distinct !{!17, !14, !15}
!18 = distinct !{!18, !14, !15, !16}
!19 = distinct !{!19, !14, !15}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass