; LLVM IR from lfarcaro/ARINC653_ARMV7A_AM335X :: Hardware (uartStdio)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @UARTPuts(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %2
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %9, %2
  br label %11

11:                                               ; preds = %37, %10
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = load i8, ptr %12, align 1, !tbaa !11
  %14 = sext i8 %13 to i32
  %15 = icmp ne i32 0, %14
  br i1 %15, label %16, label %38

16:                                               ; preds = %11
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load i8, ptr %17, align 1, !tbaa !11
  %19 = sext i8 %18 to i32
  %20 = icmp eq i32 10, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %16
  call void @UARTPutc(i8 noundef zeroext 13)
  call void @UARTPutc(i8 noundef zeroext 10)
  br label %25

22:                                               ; preds = %16
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i8, ptr %23, align 1, !tbaa !11
  call void @UARTPutc(i8 noundef zeroext %24)
  br label %25

25:                                               ; preds = %22, %21
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i32 1
  store ptr %27, ptr %3, align 8, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = add i32 %28, 1
  store i32 %29, ptr %5, align 4, !tbaa !9
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = icmp eq i32 0, %30
  br i1 %31, label %32, label %37

32:                                               ; preds = %25
  %33 = load i32, ptr %5, align 4, !tbaa !9
  %34 = load i32, ptr %4, align 4, !tbaa !9
  %35 = icmp eq i32 %33, %34
  br i1 %35, label %36, label %37

36:                                               ; preds = %32
  br label %38

37:                                               ; preds = %32, %25
  br label %11, !llvm.loop !12

38:                                               ; preds = %36, %11
  %39 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %39
}

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

; Function Attrs: nounwind uwtable
declare dso_local void @UARTPutc(i8 noundef zeroext) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; 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
define dso_local i32 @UARTPuts(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, -1
  br i1 %3, label %.split, label %.split.us

.split.us:                                        ; preds = %2, %6
  %.09.us = phi ptr [ %7, %6 ], [ %0, %2 ]
  %.0.us = phi i32 [ %8, %6 ], [ 0, %2 ]
  %4 = load i8, ptr %.09.us, align 1, !tbaa !5
  switch i8 %4, label %6 [
    i8 0, label %.split13.us
    i8 10, label %5
  ]

5:                                                ; preds = %.split.us
  tail call void @UARTPutc(i8 noundef zeroext 13)
  br label %6

6:                                                ; preds = %.split.us, %5
  %.sink = phi i8 [ 10, %5 ], [ %4, %.split.us ]
  tail call void @UARTPutc(i8 noundef zeroext %.sink)
  %7 = getelementptr inbounds i8, ptr %.09.us, i64 1
  %8 = add i32 %.0.us, 1
  br label %.split.us

.split:                                           ; preds = %2, %11
  %.09 = phi ptr [ %12, %11 ], [ %0, %2 ]
  %.0 = phi i32 [ %13, %11 ], [ 0, %2 ]
  %9 = load i8, ptr %.09, align 1, !tbaa !5
  switch i8 %9, label %11 [
    i8 0, label %.split13.us
    i8 10, label %10
  ]

10:                                               ; preds = %.split
  tail call void @UARTPutc(i8 noundef zeroext 13)
  br label %11

11:                                               ; preds = %.split, %10
  %.sink16 = phi i8 [ 10, %10 ], [ %9, %.split ]
  tail call void @UARTPutc(i8 noundef zeroext %.sink16)
  %12 = getelementptr inbounds i8, ptr %.09, i64 1
  %13 = add i32 %.0, 1
  %14 = icmp eq i32 %13, %1
  br i1 %14, label %.split13.us, label %.split, !llvm.loop !8

.split13.us:                                      ; preds = %.split.us, %.split, %11
  %.us-phi = phi i32 [ %.0, %.split ], [ %1, %11 ], [ %.0.us, %.split.us ]
  ret i32 %.us-phi
}

; Function Attrs: nounwind uwtable
declare dso_local void @UARTPutc(i8 noundef zeroext) local_unnamed_addr #0

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass