; LLVM IR from s-matyukevich/raspberry-pi-os :: exercises/lesson01/3/avenito/src (kernel)

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

@processor = external hidden global i32, align 4
@.str = external hidden unnamed_addr constant [29 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @kernel_main(i8 noundef signext %0) #0 {
  %2 = alloca i8, align 1
  store i8 %0, ptr %2, align 1, !tbaa !5
  br label %3

3:                                                ; preds = %8, %1
  %4 = load i32, ptr @processor, align 4, !tbaa !8
  %5 = load i8, ptr %2, align 1, !tbaa !5
  %6 = sext i8 %5 to i32
  %7 = icmp ne i32 %4, %6
  br i1 %7, label %8, label %9

8:                                                ; preds = %3
  br label %3, !llvm.loop !10

9:                                                ; preds = %3
  %10 = load i8, ptr %2, align 1, !tbaa !5
  %11 = sext i8 %10 to i32
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %14

13:                                               ; preds = %9
  call void @uart_init()
  br label %14

14:                                               ; preds = %13, %9
  call void @uart_send_string(ptr noundef @.str)
  %15 = load i8, ptr %2, align 1, !tbaa !5
  %16 = sext i8 %15 to i32
  %17 = add nsw i32 %16, 48
  %18 = trunc i32 %17 to i8
  call void @uart_send(i8 noundef signext %18)
  call void @uart_send_string(ptr noundef @.str.1)
  %19 = load i32, ptr @processor, align 4, !tbaa !8
  %20 = add i32 %19, 1
  store i32 %20, ptr @processor, align 4, !tbaa !8
  %21 = load i8, ptr %2, align 1, !tbaa !5
  %22 = sext i8 %21 to i32
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %27

24:                                               ; preds = %14
  br label %25

25:                                               ; preds = %25, %24
  %26 = call signext i8 @uart_recv()
  call void @uart_send(i8 noundef signext %26)
  br label %25

27:                                               ; preds = %14
  br label %28

28:                                               ; preds = %28, %27
  br label %28
}

declare void @uart_init() #1

declare void @uart_send_string(ptr noundef) #1

declare void @uart_send(i8 noundef signext) #1

declare signext i8 @uart_recv() #1

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

!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 = distinct !{!10, !11}
!11 = !{!"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"

@processor = external hidden local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [29 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: noreturn nounwind uwtable
define dso_local void @kernel_main(i8 noundef signext %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @processor, align 4, !tbaa !5
  %3 = sext i8 %0 to i32
  %.not = icmp eq i32 %2, %3
  br i1 %.not, label %4, label %.split, !llvm.loop !9

.split:                                           ; preds = %1, %.split
  br label %.split

4:                                                ; preds = %1
  %5 = icmp eq i8 %0, 0
  br i1 %5, label %6, label %.critedge

6:                                                ; preds = %4
  tail call void @uart_init() #2
  tail call void @uart_send_string(ptr noundef nonnull @.str) #2
  tail call void @uart_send(i8 noundef signext 48) #2
  tail call void @uart_send_string(ptr noundef nonnull @.str.1) #2
  %7 = load i32, ptr @processor, align 4, !tbaa !5
  %8 = add i32 %7, 1
  store i32 %8, ptr @processor, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %6, %9
  %10 = tail call signext i8 @uart_recv() #2
  tail call void @uart_send(i8 noundef signext %10) #2
  br label %9

.critedge:                                        ; preds = %4
  tail call void @uart_send_string(ptr noundef nonnull @.str) #2
  %11 = add i8 %0, 48
  tail call void @uart_send(i8 noundef signext %11) #2
  tail call void @uart_send_string(ptr noundef nonnull @.str.1) #2
  %12 = load i32, ptr @processor, align 4, !tbaa !5
  %13 = add i32 %12, 1
  store i32 %13, ptr @processor, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %.critedge, %14
  br label %14
}

declare void @uart_init() local_unnamed_addr #1

declare void @uart_send_string(ptr noundef) local_unnamed_addr #1

declare void @uart_send(i8 noundef signext) local_unnamed_addr #1

declare signext i8 @uart_recv() local_unnamed_addr #1

attributes #0 = { noreturn 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InstCombinePass,LICMPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass