; LLVM IR from smwikipedia/EmbeddedAndRTOSSamples :: C2.6 (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"

%struct.uart = type { ptr, i32, [128 x i8], i32, i32, i32, i32, [128 x i8], i32, i32, i32, i32, i32 }

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @do_rx(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @do_tx(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @uart_handler(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.uart, ptr %4, i32 0, i32 0
  %6 = load volatile ptr, ptr %5, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %6, i64 64
  %8 = load i8, ptr %7, align 1, !tbaa !12
  store i8 %8, ptr %3, align 1, !tbaa !12
  %9 = load i8, ptr %3, align 1, !tbaa !12
  %10 = zext i8 %9 to i32
  %11 = and i32 %10, 16
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %13, label %15

13:                                               ; preds = %1
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  call void @do_rx(ptr noundef %14)
  br label %26

15:                                               ; preds = %1
  %16 = load i8, ptr %3, align 1, !tbaa !12
  %17 = zext i8 %16 to i32
  %18 = and i32 %17, 32
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %15
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  call void @do_tx(ptr noundef %21)
  br label %25

22:                                               ; preds = %15
  br label %23

23:                                               ; preds = %24, %22
  br label %24

24:                                               ; preds = %23
  br label %23

25:                                               ; preds = %20
  br label %26

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"uart", !6, i64 0, !11, i64 8, !7, i64 12, !11, i64 140, !11, i64 144, !11, i64 148, !11, i64 152, !7, i64 156, !11, i64 284, !11, i64 288, !11, i64 292, !11, i64 296, !11, i64 300}
!11 = !{!"int", !7, i64 0}
!12 = !{!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"

; Function Attrs: nounwind uwtable
declare dso_local void @do_rx(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @do_tx(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @uart_handler(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load volatile ptr, ptr %0, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 64
  %4 = load i8, ptr %3, align 1, !tbaa !11
  %5 = zext i8 %4 to i32
  %6 = and i32 %5, 16
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %8, label %7

7:                                                ; preds = %1
  tail call void @do_rx(ptr noundef nonnull %0)
  br label %11

8:                                                ; preds = %1
  %9 = and i32 %5, 32
  %.not4 = icmp eq i32 %9, 0
  br i1 %.not4, label %.preheader, label %10

10:                                               ; preds = %8
  tail call void @do_tx(ptr noundef nonnull %0)
  br label %11

.preheader:                                       ; preds = %8, %.preheader
  br label %.preheader

11:                                               ; preds = %10, %7
  ret void
}

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

!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, !7, i64 0}
!6 = !{!"uart", !7, i64 0, !10, i64 8, !8, i64 12, !10, i64 140, !10, i64 144, !10, i64 148, !10, i64 152, !8, i64 156, !10, i64 284, !10, i64 288, !10, i64 292, !10, i64 296, !10, i64 300}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!8, !8, i64 0}


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