; LLVM IR from s-matyukevich/raspberry-pi-os :: exercises/lesson01/3/adkaster/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"

@.str = external hidden unnamed_addr constant [22 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(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca [2 x i8], align 1
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #3
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = add nsw i32 %4, 48
  %6 = trunc i32 %5 to i8
  %7 = getelementptr inbounds [2 x i8], ptr %3, i64 0, i64 0
  store i8 %6, ptr %7, align 1, !tbaa !9
  %8 = getelementptr inbounds [2 x i8], ptr %3, i64 0, i64 1
  store i8 0, ptr %8, align 1, !tbaa !9
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  call void @uart_init()
  br label %16

12:                                               ; preds = %1
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = mul nsw i32 100000, %13
  %15 = sext i32 %14 to i64
  call void @delay(i64 noundef %15)
  br label %16

16:                                               ; preds = %12, %11
  call void @uart_send_string(ptr noundef @.str)
  %17 = getelementptr inbounds [2 x i8], ptr %3, i64 0, i64 0
  call void @uart_send_string(ptr noundef %17)
  call void @uart_send_string(ptr noundef @.str.1)
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %24

20:                                               ; preds = %16
  br label %21

21:                                               ; preds = %22, %20
  br label %22

22:                                               ; preds = %21
  %23 = call signext i8 @uart_recv()
  call void @uart_send(i8 noundef signext %23)
  br label %21

24:                                               ; preds = %16
  br label %25

25:                                               ; preds = %26, %24
  br label %26

26:                                               ; preds = %25
  br label %25
}

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

declare void @uart_init() #2

declare void @delay(i64 noundef) #2

declare void @uart_send_string(ptr noundef) #2

declare void @uart_send(i8 noundef signext) #2

declare signext i8 @uart_recv() #2

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !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"

@.str = external hidden unnamed_addr constant [22 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(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca [2 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %2) #3
  %3 = trunc i32 %0 to i8
  %4 = add i8 %3, 48
  store i8 %4, ptr %2, align 1, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %2, i64 1
  store i8 0, ptr %5, align 1, !tbaa !5
  %6 = icmp eq i32 %0, 0
  br i1 %6, label %7, label %8

7:                                                ; preds = %1
  tail call void @uart_init() #3
  br label %11

8:                                                ; preds = %1
  %9 = mul nsw i32 %0, 100000
  %10 = sext i32 %9 to i64
  tail call void @delay(i64 noundef %10) #3
  br label %11

11:                                               ; preds = %8, %7
  tail call void @uart_send_string(ptr noundef nonnull @.str) #3
  call void @uart_send_string(ptr noundef nonnull %2) #3
  call void @uart_send_string(ptr noundef nonnull @.str.1) #3
  br i1 %6, label %.preheader, label %.preheader4

.preheader:                                       ; preds = %11, %.preheader
  %12 = call signext i8 @uart_recv() #3
  call void @uart_send(i8 noundef signext %12) #3
  br label %.preheader

.preheader4:                                      ; preds = %11, %.preheader4
  br label %.preheader4
}

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

declare void @uart_init() local_unnamed_addr #2

declare void @delay(i64 noundef) local_unnamed_addr #2

declare void @uart_send_string(ptr noundef) local_unnamed_addr #2

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

declare signext i8 @uart_recv() local_unnamed_addr #2

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 = { mustprogress 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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