; 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

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

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

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

; 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_putchar(i8 noundef signext %0) #2 {
  %2 = alloca i8, align 1
  %3 = alloca i32, align 4
  store i8 %0, ptr %2, align 1, !tbaa !5
  %4 = load i8, ptr %2, align 1, !tbaa !5
  %5 = sext i8 %4 to i32
  %6 = icmp eq i32 %5, 32
  br i1 %6, label %7, label %21

7:                                                ; preds = %1
  %8 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %9 = zext i8 %8 to i32
  %10 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %11 = zext i8 %10 to i32
  %12 = call zeroext i16 @vga_item_entry(i8 noundef zeroext 32, i32 noundef %9, i32 noundef %11)
  %13 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %14 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %15 = add i32 %14, 1
  store i32 %15, ptr @g_vga_index, align 4, !tbaa !10
  %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 !5
  %19 = add i8 %18, 1
  store i8 %19, ptr @cursor_pos_x, align 1, !tbaa !5
  %20 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  call void @vga_set_cursor_pos(i8 noundef zeroext %18, i8 noundef zeroext %20)
  br label %21

21:                                               ; preds = %7, %1
  %22 = load i8, ptr %2, align 1, !tbaa !5
  %23 = sext i8 %22 to i32
  %24 = icmp eq i32 %23, 9
  br i1 %24, label %25, label %48

25:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !10
  br label %26

26:                                               ; preds = %44, %25
  %27 = load i32, ptr %3, align 4, !tbaa !10
  %28 = icmp slt i32 %27, 4
  br i1 %28, label %30, label %29

29:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %47

30:                                               ; preds = %26
  %31 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %32 = zext i8 %31 to i32
  %33 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %34 = zext i8 %33 to i32
  %35 = call zeroext i16 @vga_item_entry(i8 noundef zeroext 32, i32 noundef %32, i32 noundef %34)
  %36 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %37 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %38 = add i32 %37, 1
  store i32 %38, ptr @g_vga_index, align 4, !tbaa !10
  %39 = zext i32 %37 to i64
  %40 = getelementptr inbounds i16, ptr %36, i64 %39
  store i16 %35, ptr %40, align 2, !tbaa !12
  %41 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %42 = add i8 %41, 1
  store i8 %42, ptr @cursor_pos_x, align 1, !tbaa !5
  %43 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  call void @vga_set_cursor_pos(i8 noundef zeroext %41, i8 noundef zeroext %43)
  br label %44

44:                                               ; preds = %30
  %45 = load i32, ptr %3, align 4, !tbaa !10
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %3, align 4, !tbaa !10
  br label %26, !llvm.loop !14

47:                                               ; preds = %29
  br label %74

48:                                               ; preds = %21
  %49 = load i8, ptr %2, align 1, !tbaa !5
  %50 = sext i8 %49 to i32
  %51 = icmp eq i32 %50, 10
  br i1 %51, label %52, label %53

52:                                               ; preds = %48
  call void @console_newline()
  br label %73

53:                                               ; preds = %48
  %54 = load i8, ptr %2, align 1, !tbaa !5
  %55 = sext i8 %54 to i32
  %56 = icmp sgt i32 %55, 0
  br i1 %56, label %57, label %72

57:                                               ; preds = %53
  %58 = load i8, ptr %2, align 1, !tbaa !5
  %59 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %60 = zext i8 %59 to i32
  %61 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %62 = zext i8 %61 to i32
  %63 = call zeroext i16 @vga_item_entry(i8 noundef zeroext %58, i32 noundef %60, i32 noundef %62)
  %64 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %65 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %66 = add i32 %65, 1
  store i32 %66, ptr @g_vga_index, align 4, !tbaa !10
  %67 = zext i32 %65 to i64
  %68 = getelementptr inbounds i16, ptr %64, i64 %67
  store i16 %63, ptr %68, align 2, !tbaa !12
  %69 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %70 = add i8 %69, 1
  store i8 %70, ptr @cursor_pos_x, align 1, !tbaa !5
  %71 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  call void @vga_set_cursor_pos(i8 noundef zeroext %70, i8 noundef zeroext %71)
  br label %72

72:                                               ; preds = %57, %53
  br label %73

73:                                               ; preds = %72, %52
  br label %74

74:                                               ; preds = %73, %47
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @console_newline() #2

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


; 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_putchar(i8 noundef signext %0) local_unnamed_addr #1 {
  switch i8 %0, label %68 [
    i8 32, label %.thread
    i8 9, label %.preheader.preheader
    i8 10, label %67
  ]

.preheader.preheader:                             ; preds = %1
  %2 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %3 = zext i8 %2 to i32
  %4 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %5 = zext i8 %4 to i32
  %6 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext 32, i32 noundef %3, i32 noundef %5) #2
  %7 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %8 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %9 = add i32 %8, 1
  store i32 %9, ptr @g_vga_index, align 4, !tbaa !10
  %10 = zext i32 %8 to i64
  %11 = getelementptr inbounds i16, ptr %7, i64 %10
  store i16 %6, ptr %11, align 2, !tbaa !12
  %12 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %13 = add i8 %12, 1
  store i8 %13, ptr @cursor_pos_x, align 1, !tbaa !5
  %14 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  tail call void @vga_set_cursor_pos(i8 noundef zeroext %12, i8 noundef zeroext %14) #2
  %15 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %16 = zext i8 %15 to i32
  %17 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %18 = zext i8 %17 to i32
  %19 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext 32, i32 noundef %16, i32 noundef %18) #2
  %20 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %21 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %22 = add i32 %21, 1
  store i32 %22, ptr @g_vga_index, align 4, !tbaa !10
  %23 = zext i32 %21 to i64
  %24 = getelementptr inbounds i16, ptr %20, i64 %23
  store i16 %19, ptr %24, align 2, !tbaa !12
  %25 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %26 = add i8 %25, 1
  store i8 %26, ptr @cursor_pos_x, align 1, !tbaa !5
  %27 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  tail call void @vga_set_cursor_pos(i8 noundef zeroext %25, i8 noundef zeroext %27) #2
  %28 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %29 = zext i8 %28 to i32
  %30 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %31 = zext i8 %30 to i32
  %32 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext 32, i32 noundef %29, i32 noundef %31) #2
  %33 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %34 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %35 = add i32 %34, 1
  store i32 %35, ptr @g_vga_index, align 4, !tbaa !10
  %36 = zext i32 %34 to i64
  %37 = getelementptr inbounds i16, ptr %33, i64 %36
  store i16 %32, ptr %37, align 2, !tbaa !12
  %38 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %39 = add i8 %38, 1
  store i8 %39, ptr @cursor_pos_x, align 1, !tbaa !5
  %40 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  tail call void @vga_set_cursor_pos(i8 noundef zeroext %38, i8 noundef zeroext %40) #2
  %41 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %42 = zext i8 %41 to i32
  %43 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %44 = zext i8 %43 to i32
  %45 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext 32, i32 noundef %42, i32 noundef %44) #2
  %46 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %47 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %48 = add i32 %47, 1
  store i32 %48, ptr @g_vga_index, align 4, !tbaa !10
  %49 = zext i32 %47 to i64
  %50 = getelementptr inbounds i16, ptr %46, i64 %49
  store i16 %45, ptr %50, align 2, !tbaa !12
  %51 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %52 = add i8 %51, 1
  store i8 %52, ptr @cursor_pos_x, align 1, !tbaa !5
  %53 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  tail call void @vga_set_cursor_pos(i8 noundef zeroext %51, i8 noundef zeroext %53) #2
  br label %.loopexit

.thread:                                          ; preds = %1
  %54 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %55 = zext i8 %54 to i32
  %56 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %57 = zext i8 %56 to i32
  %58 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext 32, i32 noundef %55, i32 noundef %57) #2
  %59 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %60 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %61 = add i32 %60, 1
  store i32 %61, ptr @g_vga_index, align 4, !tbaa !10
  %62 = zext i32 %60 to i64
  %63 = getelementptr inbounds i16, ptr %59, i64 %62
  store i16 %58, ptr %63, align 2, !tbaa !12
  %64 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %65 = add i8 %64, 1
  store i8 %65, ptr @cursor_pos_x, align 1, !tbaa !5
  %66 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  tail call void @vga_set_cursor_pos(i8 noundef zeroext %64, i8 noundef zeroext %66) #2
  br label %70

67:                                               ; preds = %1
  tail call void @console_newline()
  br label %.loopexit

68:                                               ; preds = %1
  %69 = icmp sgt i8 %0, 0
  br i1 %69, label %70, label %.loopexit

70:                                               ; preds = %.thread, %68
  %71 = load i8, ptr @g_fore_color, align 1, !tbaa !5
  %72 = zext i8 %71 to i32
  %73 = load i8, ptr @g_back_color, align 1, !tbaa !5
  %74 = zext i8 %73 to i32
  %75 = tail call zeroext i16 @vga_item_entry(i8 noundef zeroext %0, i32 noundef %72, i32 noundef %74) #2
  %76 = load ptr, ptr @g_vga_buffer, align 8, !tbaa !8
  %77 = load i32, ptr @g_vga_index, align 4, !tbaa !10
  %78 = add i32 %77, 1
  store i32 %78, ptr @g_vga_index, align 4, !tbaa !10
  %79 = zext i32 %77 to i64
  %80 = getelementptr inbounds i16, ptr %76, i64 %79
  store i16 %75, ptr %80, align 2, !tbaa !12
  %81 = load i8, ptr @cursor_pos_x, align 1, !tbaa !5
  %82 = add i8 %81, 1
  store i8 %82, ptr @cursor_pos_x, align 1, !tbaa !5
  %83 = load i8, ptr @cursor_pos_y, align 1, !tbaa !5
  tail call void @vga_set_cursor_pos(i8 noundef zeroext %82, i8 noundef zeroext %83) #2
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader.preheader, %67, %70, %68
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @console_newline() local_unnamed_addr #1

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass