; LLVM IR from s-matyukevich/raspberry-pi-os :: exercises/lesson03/2/H-4ND-H/src (mini_uart)

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

@buffer = external hidden global [32 x i8], align 16
@wr_buffer_index = external hidden global i32, align 4
@rd_buffer_index = external hidden global i32, align 4

declare i32 @get32(i64 noundef) #0

declare void @put32(i64 noundef, i32 noundef) #0

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

; 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
define dso_local void @handle_uart_irq() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %3 = call i32 @get32(i64 noundef 1059147848)
  store i32 %3, ptr %1, align 4, !tbaa !5
  %4 = load i32, ptr %1, align 4, !tbaa !5
  %5 = and i32 %4, 6
  %6 = icmp eq i32 %5, 4
  br i1 %6, label %7, label %25

7:                                                ; preds = %0
  br label %8

8:                                                ; preds = %23, %7
  %9 = call i32 @get32(i64 noundef 1059147860)
  %10 = and i32 %9, 1
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %24

12:                                               ; preds = %8
  %13 = call i32 @get32(i64 noundef 1059147840)
  %14 = and i32 %13, 255
  %15 = trunc i32 %14 to i8
  %16 = load i32, ptr @wr_buffer_index, align 4, !tbaa !5
  %17 = add i32 %16, 1
  store i32 %17, ptr @wr_buffer_index, align 4, !tbaa !5
  %18 = zext i32 %16 to i64
  %19 = getelementptr inbounds [32 x i8], ptr @buffer, i64 0, i64 %18
  store i8 %15, ptr %19, align 1, !tbaa !9
  %20 = load i32, ptr @wr_buffer_index, align 4, !tbaa !5
  %21 = icmp eq i32 %20, 32
  br i1 %21, label %22, label %23

22:                                               ; preds = %12
  store i32 0, ptr @wr_buffer_index, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %22, %12
  br label %8, !llvm.loop !10

24:                                               ; preds = %8
  br label %25

25:                                               ; preds = %24, %0
  %26 = load i32, ptr %1, align 4, !tbaa !5
  %27 = and i32 %26, 6
  %28 = icmp eq i32 %27, 2
  br i1 %28, label %29, label %52

29:                                               ; preds = %25
  br label %30

30:                                               ; preds = %50, %29
  %31 = call i32 @get32(i64 noundef 1059147860)
  %32 = and i32 %31, 32
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %51

34:                                               ; preds = %30
  %35 = load i32, ptr @rd_buffer_index, align 4, !tbaa !5
  %36 = load i32, ptr @wr_buffer_index, align 4, !tbaa !5
  %37 = icmp eq i32 %35, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %34
  br label %51

39:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #3
  %40 = load i32, ptr @rd_buffer_index, align 4, !tbaa !5
  %41 = add i32 %40, 1
  store i32 %41, ptr @rd_buffer_index, align 4, !tbaa !5
  %42 = zext i32 %40 to i64
  %43 = getelementptr inbounds [32 x i8], ptr @buffer, i64 0, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !9
  store i8 %44, ptr %2, align 1, !tbaa !9
  %45 = load i8, ptr %2, align 1, !tbaa !9
  %46 = sext i8 %45 to i32
  call void @put32(i64 noundef 1059147840, i32 noundef %46)
  %47 = load i32, ptr @rd_buffer_index, align 4, !tbaa !5
  %48 = icmp eq i32 %47, 32
  br i1 %48, label %49, label %50

49:                                               ; preds = %39
  store i32 0, ptr @rd_buffer_index, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %49, %39
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #3
  br label %30, !llvm.loop !12

51:                                               ; preds = %38, %30
  br label %52

52:                                               ; preds = %51, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

attributes #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 = { 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 #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}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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

@buffer = external hidden local_unnamed_addr global [32 x i8], align 16
@wr_buffer_index = external hidden local_unnamed_addr global i32, align 4
@rd_buffer_index = external hidden local_unnamed_addr global i32, align 4

declare i32 @get32(i64 noundef) local_unnamed_addr #0

declare void @put32(i64 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @handle_uart_irq() local_unnamed_addr #1 {
  %1 = tail call i32 @get32(i64 noundef 1059147848) #2
  %2 = and i32 %1, 6
  switch i32 %2, label %._crit_edge [
    i32 4, label %.preheader4
    i32 2, label %.preheader
  ]

.preheader4:                                      ; preds = %0
  %3 = tail call i32 @get32(i64 noundef 1059147860) #2
  %4 = and i32 %3, 1
  %.not5 = icmp eq i32 %4, 0
  br i1 %.not5, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader4, %13
  %5 = tail call i32 @get32(i64 noundef 1059147840) #2
  %6 = trunc i32 %5 to i8
  %7 = load i32, ptr @wr_buffer_index, align 4, !tbaa !5
  %8 = add i32 %7, 1
  store i32 %8, ptr @wr_buffer_index, align 4, !tbaa !5
  %9 = zext i32 %7 to i64
  %10 = getelementptr inbounds [32 x i8], ptr @buffer, i64 0, i64 %9
  store i8 %6, ptr %10, align 1, !tbaa !9
  %11 = icmp eq i32 %8, 32
  br i1 %11, label %12, label %13

12:                                               ; preds = %.lr.ph
  store i32 0, ptr @wr_buffer_index, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %12, %.lr.ph
  %14 = tail call i32 @get32(i64 noundef 1059147860) #2
  %15 = and i32 %14, 1
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

.preheader:                                       ; preds = %0
  %16 = tail call i32 @get32(i64 noundef 1059147860) #2
  %17 = and i32 %16, 32
  %.not36 = icmp eq i32 %17, 0
  br i1 %.not36, label %._crit_edge, label %.lr.ph7

.lr.ph7:                                          ; preds = %.preheader, %30
  %18 = load i32, ptr @rd_buffer_index, align 4, !tbaa !5
  %19 = load i32, ptr @wr_buffer_index, align 4, !tbaa !5
  %20 = icmp eq i32 %18, %19
  br i1 %20, label %._crit_edge, label %21

21:                                               ; preds = %.lr.ph7
  %22 = add i32 %18, 1
  store i32 %22, ptr @rd_buffer_index, align 4, !tbaa !5
  %23 = zext i32 %18 to i64
  %24 = getelementptr inbounds [32 x i8], ptr @buffer, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9
  %26 = sext i8 %25 to i32
  tail call void @put32(i64 noundef 1059147840, i32 noundef %26) #2
  %27 = load i32, ptr @rd_buffer_index, align 4, !tbaa !5
  %28 = icmp eq i32 %27, 32
  br i1 %28, label %29, label %30

29:                                               ; preds = %21
  store i32 0, ptr @rd_buffer_index, align 4, !tbaa !5
  br label %30

30:                                               ; preds = %29, %21
  %31 = tail call i32 @get32(i64 noundef 1059147860) #2
  %32 = and i32 %31, 32
  %.not3 = icmp eq i32 %32, 0
  br i1 %.not3, label %._crit_edge, label %.lr.ph7, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph7, %30, %13, %.preheader, %.preheader4, %0
  ret void
}

attributes #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 = { 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 #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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


; Effective O3 passes: ConstraintEliminationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass