; 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() #1 {
  %1 = load i32, ptr @g_vga_index, align 4, !tbaa !5
  %2 = icmp ugt i32 %1, 0
  br i1 %2, label %3, label %33

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

17:                                               ; preds = %3
  %18 = load i8, ptr @cursor_pos_x, align 1, !tbaa !9
  %19 = add i8 %18, -1
  store i8 %19, ptr @cursor_pos_x, align 1, !tbaa !9
  %20 = load i8, ptr @cursor_pos_y, align 1, !tbaa !9
  call void @vga_set_cursor_pos(i8 noundef zeroext %18, i8 noundef zeroext %20)
  br label %32

21:                                               ; preds = %3
  store i8 80, ptr @cursor_pos_x, align 1, !tbaa !9
  %22 = load i8, ptr @cursor_pos_y, align 1, !tbaa !9
  %23 = zext i8 %22 to i32
  %24 = icmp sgt i32 %23, 0
  br i1 %24, label %25, label %30

25:                                               ; preds = %21
  %26 = load i8, ptr @cursor_pos_x, align 1, !tbaa !9
  %27 = add i8 %26, -1
  store i8 %27, ptr @cursor_pos_x, align 1, !tbaa !9
  %28 = load i8, ptr @cursor_pos_y, align 1, !tbaa !9
  %29 = add i8 %28, -1
  store i8 %29, ptr @cursor_pos_y, align 1, !tbaa !9
  call void @vga_set_cursor_pos(i8 noundef zeroext %26, i8 noundef zeroext %29)
  br label %31

30:                                               ; preds = %21
  store i8 0, ptr @cursor_pos_y, align 1, !tbaa !9
  br label %31

31:                                               ; preds = %30, %25
  br label %32

32:                                               ; preds = %31, %17
  br label %33

33:                                               ; preds = %32, %0
  %34 = load i8, ptr @g_fore_color, align 1, !tbaa !9
  %35 = zext i8 %34 to i32
  %36 = load i8, ptr @g_back_color, align 1, !tbaa !9
  %37 = zext i8 %36 to i32
  %38 = call zeroext i16 @vga_item_entry(i8 noundef zeroext 0, i32 noundef %35, i32 noundef %37)
  %39 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !10
  %40 = load i32, ptr @g_vga_index, align 4, !tbaa !5
  %41 = zext i32 %40 to i64
  %42 = getelementptr inbounds i16, ptr %39, i64 %41
  store i16 %38, ptr %42, 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 = !{!"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}


; 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() local_unnamed_addr #1 {
  %1 = load i32, ptr @g_vga_index, align 4, !tbaa !5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %21, label %2

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

14:                                               ; preds = %2
  %15 = add i8 %13, -1
  store i8 %15, ptr @cursor_pos_x, align 1, !tbaa !9
  %16 = load i8, ptr @cursor_pos_y, align 1, !tbaa !9
  tail call void @vga_set_cursor_pos(i8 noundef zeroext %13, i8 noundef zeroext %16) #2
  br label %21

17:                                               ; preds = %2
  store i8 80, ptr @cursor_pos_x, align 1, !tbaa !9
  %18 = load i8, ptr @cursor_pos_y, align 1, !tbaa !9
  %.not2 = icmp eq i8 %18, 0
  br i1 %.not2, label %21, label %19

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

21:                                               ; preds = %17, %14, %19, %0
  %22 = load i8, ptr @g_fore_color, align 1, !tbaa !9
  %23 = zext i8 %22 to i32
  %24 = load i8, ptr @g_back_color, align 1, !tbaa !9
  %25 = zext i8 %24 to i32
  %26 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext 0, i32 noundef %23, i32 noundef %25) #2
  %27 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !10
  %28 = load i32, ptr @g_vga_index, align 4, !tbaa !5
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds i16, ptr %27, i64 %29
  store i16 %26, ptr %30, 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,SimplifyCFGPass,TailCallElimPass