; LLVM IR from s-matyukevich/raspberry-pi-os :: exercises/lesson01/3/stefanji/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 [18 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @kernel_main(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %5, label %6

5:                                                ; preds = %1
  call void @uart_init()
  br label %10

6:                                                ; preds = %1
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = mul i32 300000, %7
  %9 = zext i32 %8 to i64
  call void @delay(i64 noundef %9)
  br label %10

10:                                               ; preds = %6, %5
  call void @uart_send_string(ptr noundef @.str)
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = add i32 %11, 48
  %13 = trunc i32 %12 to i8
  call void @uart_send(i8 noundef signext %13)
  call void @uart_send_string(ptr noundef @.str.1)
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %10
  br label %17

17:                                               ; preds = %17, %16
  %18 = call signext i8 @uart_recv()
  call void @uart_send(i8 noundef signext %18)
  br label %17

19:                                               ; preds = %10
  br label %20

20:                                               ; preds = %20, %19
  br label %20
}

declare void @uart_init() #1

declare void @delay(i64 noundef) #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; 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 [18 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: noreturn nounwind uwtable
define dso_local void @kernel_main(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq i32 %0, 0
  br i1 %2, label %3, label %4

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

4:                                                ; preds = %1
  %5 = mul i32 %0, 300000
  %6 = zext i32 %5 to i64
  tail call void @delay(i64 noundef %6) #2
  br label %7

7:                                                ; preds = %4, %3
  tail call void @uart_send_string(ptr noundef nonnull @.str) #2
  %8 = trunc i32 %0 to i8
  %9 = add i8 %8, 48
  tail call void @uart_send(i8 noundef signext %9) #2
  tail call void @uart_send_string(ptr noundef nonnull @.str.1) #2
  br i1 %2, label %.preheader, label %.preheader4

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

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

declare void @uart_init() local_unnamed_addr #1

declare void @delay(i64 noundef) 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)"}


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