; 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_fore_color = external dso_local global i8, align 1
@g_back_color = external dso_local global i8, align 1
@g_vga_buffer = external hidden global ptr, align 8
@g_vga_index = external hidden global i32, align 4
@cursor_pos_x = external hidden global i8, align 1
@cursor_pos_y = external hidden global i8, align 1

declare zeroext i16 @vga_item_entry(i8 noundef zeroext, i32 noundef, i32 noundef) #0

declare void @vga_set_cursor_pos(i8 noundef zeroext, i8 noundef zeroext) #0

; Function Attrs: nounwind uwtable
define dso_local void @console_ungetchar_bound(i8 noundef zeroext %0) #1 {
  %2 = alloca i8, align 1
  store i8 %0, ptr %2, align 1, !tbaa !5
  %3 = load i32, ptr @g_vga_index, align 4, !tbaa !8
  %4 = urem i32 %3, 80
  %5 = load i8, ptr %2, align 1, !tbaa !5
  %6 = zext i8 %5 to i32
  %7 = icmp ugt i32 %4, %6
  br i1 %7, label %8, label %44

8:                                                ; preds = %1
  %9 = load i8, ptr %2, align 1, !tbaa !5
  %10 = zext i8 %9 to i32
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %12, label %44

12:                                               ; preds = %8
  %13 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %14 = zext i8 %13 to i32
  %15 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %16 = zext i8 %15 to i32
  %17 = call zeroext i16 @vga_item_entry(i8 noundef zeroext 0, i32 noundef %14, i32 noundef %16)
  %18 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !10
  %19 = load i32, ptr @g_vga_index, align 4, !tbaa !8
  %20 = add i32 %19, -1
  store i32 %20, ptr @g_vga_index, align 4, !tbaa !8
  %21 = zext i32 %19 to i64
  %22 = getelementptr inbounds i16, ptr %18, i64 %21
  store i16 %17, ptr %22, align 2, !tbaa !12
  %23 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %24 = zext i8 %23 to i32
  %25 = load i8, ptr %2, align 1, !tbaa !5
  %26 = zext i8 %25 to i32
  %27 = icmp sge i32 %24, %26
  br i1 %27, label %28, label %32

28:                                               ; preds = %12
  %29 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %30 = add i8 %29, -1
  store i8 %30, ptr @cursor_pos_x, align 1, !tbaa !5
  %31 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  call void @vga_set_cursor_pos(i8 noundef zeroext %29, i8 noundef zeroext %31)
  br label %43

32:                                               ; preds = %12
  store i8 80, ptr @cursor_pos_x, align 1, !tbaa !5
  %33 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  %34 = zext i8 %33 to i32
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %36, label %41

36:                                               ; preds = %32
  %37 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %38 = add i8 %37, -1
  store i8 %38, ptr @cursor_pos_x, align 1, !tbaa !5
  %39 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  %40 = add i8 %39, -1
  store i8 %40, ptr @cursor_pos_y, align 1, !tbaa !5
  call void @vga_set_cursor_pos(i8 noundef zeroext %37, i8 noundef zeroext %40)
  br label %42

41:                                               ; preds = %32
  store i8 0, ptr @cursor_pos_y, align 1, !tbaa !5
  br label %42

42:                                               ; preds = %41, %36
  br label %43

43:                                               ; preds = %42, %28
  br label %44

44:                                               ; preds = %43, %8, %1
  %45 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %46 = zext i8 %45 to i32
  %47 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %48 = zext i8 %47 to i32
  %49 = call zeroext i16 @vga_item_entry(i8 noundef zeroext 0, i32 noundef %46, i32 noundef %48)
  %50 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !10
  %51 = load i32, ptr @g_vga_index, align 4, !tbaa !8
  %52 = zext i32 %51 to i64
  %53 = getelementptr inbounds i16, ptr %50, i64 %52
  store i16 %49, ptr %53, align 2, !tbaa !12
  ret void
}

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

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


; 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_fore_color = external dso_local local_unnamed_addr global i8, align 1
@g_back_color = external dso_local local_unnamed_addr global i8, align 1
@g_vga_buffer = external hidden local_unnamed_addr global ptr, align 8
@g_vga_index = external hidden local_unnamed_addr global i32, align 4
@cursor_pos_x = external hidden local_unnamed_addr global i8, align 1
@cursor_pos_y = external hidden local_unnamed_addr global i8, align 1

declare zeroext i16 @vga_item_entry(i8 noundef zeroext, i32 noundef, i32 noundef) local_unnamed_addr #0

declare void @vga_set_cursor_pos(i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @console_ungetchar_bound(i8 noundef zeroext %0) local_unnamed_addr #1 {
  %2 = load i32, ptr @g_vga_index, align 4, !tbaa !5
  %3 = urem i32 %2, 80
  %4 = zext i8 %0 to i32
  %5 = icmp ugt i32 %3, %4
  %6 = icmp ne i8 %0, 0
  %or.cond = and i1 %6, %5
  br i1 %or.cond, label %7, label %26

7:                                                ; preds = %1
  %8 = load i8, ptr @g_fore_color, align 1, !tbaa !9
  %9 = zext i8 %8 to i32
  %10 = load i8, ptr @g_back_color, align 1, !tbaa !9
  %11 = zext i8 %10 to i32
  %12 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext 0, i32 noundef %9, i32 noundef %11) #2
  %13 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !10
  %14 = load i32, ptr @g_vga_index, align 4, !tbaa !5
  %15 = add i32 %14, -1
  store i32 %15, ptr @g_vga_index, align 4, !tbaa !5
  %16 = zext i32 %14 to i64
  %17 = getelementptr inbounds i16, ptr %13, i64 %16
  store i16 %12, ptr %17, align 2, !tbaa !12
  %18 = load i8, ptr @cursor_pos_x, align 1, !tbaa !9
  %.not = icmp ult i8 %18, %0
  br i1 %.not, label %22, label %19

19:                                               ; preds = %7
  %20 = add i8 %18, -1
  store i8 %20, ptr @cursor_pos_x, align 1, !tbaa !9
  %21 = load i8, ptr @cursor_pos_y, align 1, !tbaa !9
  tail call void @vga_set_cursor_pos(i8 noundef zeroext %18, i8 noundef zeroext %21) #2
  br label %26

22:                                               ; preds = %7
  store i8 80, ptr @cursor_pos_x, align 1, !tbaa !9
  %23 = load i8, ptr @cursor_pos_y, align 1, !tbaa !9
  %.not5 = icmp eq i8 %23, 0
  br i1 %.not5, label %26, label %24

24:                                               ; preds = %22
  store i8 79, ptr @cursor_pos_x, align 1, !tbaa !9
  %25 = add i8 %23, -1
  store i8 %25, ptr @cursor_pos_y, align 1, !tbaa !9
  tail call void @vga_set_cursor_pos(i8 noundef zeroext 80, i8 noundef zeroext %25) #2
  br label %26

26:                                               ; preds = %22, %19, %24, %1
  %27 = load i8, ptr @g_fore_color, align 1, !tbaa !9
  %28 = zext i8 %27 to i32
  %29 = load i8, ptr @g_back_color, align 1, !tbaa !9
  %30 = zext i8 %29 to i32
  %31 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext 0, i32 noundef %28, i32 noundef %30) #2
  %32 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !10
  %33 = load i32, ptr @g_vga_index, align 4, !tbaa !5
  %34 = zext i32 %33 to i64
  %35 = getelementptr inbounds i16, ptr %32, i64 %34
  store i16 %31, ptr %35, align 2, !tbaa !12
  ret void
}

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


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