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

; 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_ch = external dso_local global i8, align 1
@g_scan_code_chars = external dso_local global <{ [79 x i8], [49 x i8] }>, align 16
@g_caps_lock = external hidden global i32, align 4
@g_shift_pressed = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @alternate_chars(i8 noundef signext) #0

; Function Attrs: nounwind uwtable
define dso_local void @keyboard_handler(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i8 0, ptr @g_ch, align 1, !tbaa !9
  %4 = call i32 @get_scancode()
  store i32 %4, ptr %3, align 4, !tbaa !10
  %5 = load i32, ptr %3, align 4, !tbaa !10
  %6 = and i32 %5, 128
  %7 = icmp ne i32 %6, 0
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  br label %61

9:                                                ; preds = %1
  %10 = load i32, ptr %3, align 4, !tbaa !10
  switch i32 %10, label %22 [
    i32 58, label %11
    i32 28, label %17
    i32 15, label %18
    i32 42, label %19
    i32 72, label %20
    i32 80, label %21
  ]

11:                                               ; preds = %9
  %12 = load i32, ptr @g_caps_lock, align 4, !tbaa !10
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %11
  store i32 1, ptr @g_caps_lock, align 4, !tbaa !10
  br label %16

15:                                               ; preds = %11
  store i32 0, ptr @g_caps_lock, align 4, !tbaa !10
  br label %16

16:                                               ; preds = %15, %14
  br label %60

17:                                               ; preds = %9
  store i8 10, ptr @g_ch, align 1, !tbaa !9
  br label %60

18:                                               ; preds = %9
  store i8 9, ptr @g_ch, align 1, !tbaa !9
  br label %60

19:                                               ; preds = %9
  store i32 1, ptr @g_shift_pressed, align 4, !tbaa !10
  br label %60

20:                                               ; preds = %9
  call void @console_scroll(i32 noundef 1)
  br label %60

21:                                               ; preds = %9
  call void @console_scroll(i32 noundef 2)
  br label %60

22:                                               ; preds = %9
  %23 = load i32, ptr %3, align 4, !tbaa !10
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [128 x i8], ptr @g_scan_code_chars, i64 0, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !9
  store i8 %26, ptr @g_ch, align 1, !tbaa !9
  %27 = load i32, ptr @g_caps_lock, align 4, !tbaa !10
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %39

29:                                               ; preds = %22
  %30 = load i32, ptr @g_shift_pressed, align 4, !tbaa !10
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %35

32:                                               ; preds = %29
  %33 = load i8, ptr @g_ch, align 1, !tbaa !9
  %34 = call signext i8 @alternate_chars(i8 noundef signext %33)
  store i8 %34, ptr @g_ch, align 1, !tbaa !9
  br label %38

35:                                               ; preds = %29
  %36 = load i8, ptr @g_ch, align 1, !tbaa !9
  %37 = call signext i8 @upper(i8 noundef signext %36)
  store i8 %37, ptr @g_ch, align 1, !tbaa !9
  br label %38

38:                                               ; preds = %35, %32
  br label %59

39:                                               ; preds = %22
  %40 = load i32, ptr @g_shift_pressed, align 4, !tbaa !10
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %53

42:                                               ; preds = %39
  %43 = load i8, ptr @g_ch, align 1, !tbaa !9
  %44 = call i32 @isalpha(i8 noundef signext %43)
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %49

46:                                               ; preds = %42
  %47 = load i8, ptr @g_ch, align 1, !tbaa !9
  %48 = call signext i8 @upper(i8 noundef signext %47)
  store i8 %48, ptr @g_ch, align 1, !tbaa !9
  br label %52

49:                                               ; preds = %42
  %50 = load i8, ptr @g_ch, align 1, !tbaa !9
  %51 = call signext i8 @alternate_chars(i8 noundef signext %50)
  store i8 %51, ptr @g_ch, align 1, !tbaa !9
  br label %52

52:                                               ; preds = %49, %46
  br label %58

53:                                               ; preds = %39
  %54 = load i32, ptr %3, align 4, !tbaa !10
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [128 x i8], ptr @g_scan_code_chars, i64 0, i64 %55
  %57 = load i8, ptr %56, align 1, !tbaa !9
  store i8 %57, ptr @g_ch, align 1, !tbaa !9
  br label %58

58:                                               ; preds = %53, %52
  store i32 0, ptr @g_shift_pressed, align 4, !tbaa !10
  br label %59

59:                                               ; preds = %58, %38
  br label %60

60:                                               ; preds = %59, %21, %20, %19, %18, %17, %16
  br label %61

61:                                               ; preds = %60, %8
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

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

declare void @console_scroll(i32 noundef) #2

declare signext i8 @upper(i8 noundef signext) #2

declare i32 @isalpha(i8 noundef signext) #2

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

; Function Attrs: nounwind uwtable
declare hidden i32 @get_scancode() #0

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { "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 = !{!"any pointer", !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 = !{!"int", !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_ch = external dso_local local_unnamed_addr global i8, align 1
@g_scan_code_chars = external dso_local local_unnamed_addr global <{ [79 x i8], [49 x i8] }>, align 16
@g_caps_lock = external hidden local_unnamed_addr global i32, align 4
@g_shift_pressed = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @alternate_chars(i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @keyboard_handler(ptr nocapture noundef readnone %0) local_unnamed_addr #0 {
  store i8 0, ptr @g_ch, align 1, !tbaa !5
  %2 = tail call i32 @get_scancode()
  %3 = and i32 %2, 128
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %4, label %35

4:                                                ; preds = %1
  switch i32 %2, label %15 [
    i32 58, label %5
    i32 28, label %10
    i32 15, label %11
    i32 42, label %12
    i32 72, label %13
    i32 80, label %14
  ]

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

8:                                                ; preds = %5
  store i32 1, ptr @g_caps_lock, align 4, !tbaa !8
  br label %35

9:                                                ; preds = %5
  store i32 0, ptr @g_caps_lock, align 4, !tbaa !8
  br label %35

10:                                               ; preds = %4
  store i8 10, ptr @g_ch, align 1, !tbaa !5
  br label %35

11:                                               ; preds = %4
  store i8 9, ptr @g_ch, align 1, !tbaa !5
  br label %35

12:                                               ; preds = %4
  store i32 1, ptr @g_shift_pressed, align 4, !tbaa !8
  br label %35

13:                                               ; preds = %4
  tail call void @console_scroll(i32 noundef 1) #2
  br label %35

14:                                               ; preds = %4
  tail call void @console_scroll(i32 noundef 2) #2
  br label %35

15:                                               ; preds = %4
  %16 = sext i32 %2 to i64
  %17 = getelementptr inbounds [128 x i8], ptr @g_scan_code_chars, i64 0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !5
  store i8 %18, ptr @g_ch, align 1, !tbaa !5
  %19 = load i32, ptr @g_caps_lock, align 4, !tbaa !8
  %.not4 = icmp eq i32 %19, 0
  %20 = load i32, ptr @g_shift_pressed, align 4, !tbaa !8
  %.not5 = icmp eq i32 %20, 0
  br i1 %.not4, label %26, label %21

21:                                               ; preds = %15
  br i1 %.not5, label %24, label %22

22:                                               ; preds = %21
  %23 = tail call signext i8 @alternate_chars(i8 noundef signext %18)
  store i8 %23, ptr @g_ch, align 1, !tbaa !5
  br label %35

24:                                               ; preds = %21
  %25 = tail call signext i8 @upper(i8 noundef signext %18) #2
  store i8 %25, ptr @g_ch, align 1, !tbaa !5
  br label %35

26:                                               ; preds = %15
  br i1 %.not5, label %34, label %27

27:                                               ; preds = %26
  %28 = tail call i32 @isalpha(i8 noundef signext %18) #2
  %.not6 = icmp eq i32 %28, 0
  %29 = load i8, ptr @g_ch, align 1, !tbaa !5
  br i1 %.not6, label %32, label %30

30:                                               ; preds = %27
  %31 = tail call signext i8 @upper(i8 noundef signext %29) #2
  br label %.sink.split

32:                                               ; preds = %27
  %33 = tail call signext i8 @alternate_chars(i8 noundef signext %29)
  br label %.sink.split

.sink.split:                                      ; preds = %32, %30
  %.sink = phi i8 [ %31, %30 ], [ %33, %32 ]
  store i8 %.sink, ptr @g_ch, align 1, !tbaa !5
  br label %34

34:                                               ; preds = %.sink.split, %26
  store i32 0, ptr @g_shift_pressed, align 4, !tbaa !8
  br label %35

35:                                               ; preds = %10, %11, %12, %13, %14, %9, %8, %22, %24, %34, %1
  ret void
}

declare void @console_scroll(i32 noundef) local_unnamed_addr #1

declare signext i8 @upper(i8 noundef signext) local_unnamed_addr #1

declare i32 @isalpha(i8 noundef signext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i32 @get_scancode() local_unnamed_addr #0

attributes #0 = { 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 #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 }

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


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