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

@kernel_main.current_processor_index = external hidden global i32, align 4
@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

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

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

6:                                                ; preds = %5, %1
  br label %7

7:                                                ; preds = %12, %6
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %9 = load i32, ptr @kernel_main.current_processor_index, align 4, !tbaa !9
  %10 = zext i32 %9 to i64
  %11 = icmp ne i64 %8, %10
  br i1 %11, label %12, label %13

12:                                               ; preds = %7
  br label %7, !llvm.loop !11

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

21:                                               ; preds = %13
  br label %22

22:                                               ; preds = %25, %21
  %23 = load i32, ptr @kernel_main.current_processor_index, align 4, !tbaa !9
  %24 = icmp ne i32 %23, 4
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  br label %22, !llvm.loop !13

26:                                               ; preds = %22
  br label %27

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

29:                                               ; preds = %13
  ret void
}

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


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

@kernel_main.current_processor_index = external hidden local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

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

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

4:                                                ; preds = %3, %1
  %5 = load i32, ptr @kernel_main.current_processor_index, align 4, !tbaa !5
  %6 = zext i32 %5 to i64
  %.not = icmp eq i64 %6, %0
  br i1 %.not, label %7, label %.split, !llvm.loop !9

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

7:                                                ; preds = %4
  tail call void @uart_send_string(ptr noundef nonnull @.str) #2
  %8 = trunc i64 %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
  %10 = load i32, ptr @kernel_main.current_processor_index, align 4, !tbaa !5
  %11 = add i32 %10, 1
  store i32 %11, ptr @kernel_main.current_processor_index, align 4, !tbaa !5
  br i1 %2, label %.preheader5, label %13

.preheader5:                                      ; preds = %7
  %.not4 = icmp eq i32 %11, 4
  br i1 %.not4, label %.preheader, label %.preheader5.split, !llvm.loop !11

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

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

13:                                               ; preds = %7
  ret void
}

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


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