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

declare void @lock(...) #0

declare void @unlock(...) #0

; Function Attrs: nounwind uwtable
define dso_local void @uputc(ptr noundef %0, i8 noundef zeroext %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i8 %1, ptr %4, align 1, !tbaa !9
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.uart, ptr %5, i32 0, i32 12
  %7 = load volatile i32, ptr %6, align 4, !tbaa !10
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %34

9:                                                ; preds = %2
  %10 = load i8, ptr %4, align 1, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.uart, ptr %11, i32 0, i32 7
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.uart, ptr %13, i32 0, i32 10
  %15 = load volatile i32, ptr %14, align 4, !tbaa !13
  %16 = zext i32 %15 to i64
  %17 = getelementptr inbounds [128 x i8], ptr %12, i64 0, i64 %16
  store volatile i8 %10, ptr %17, align 1, !tbaa !9
  call void (...) @lock()
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.uart, ptr %18, i32 0, i32 10
  %20 = load volatile i32, ptr %19, align 4, !tbaa !13
  %21 = add i32 %20, 1
  store volatile i32 %21, ptr %19, align 4, !tbaa !13
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.uart, ptr %22, i32 0, i32 10
  %24 = load volatile i32, ptr %23, align 4, !tbaa !13
  %25 = urem i32 %24, 128
  store volatile i32 %25, ptr %23, align 4, !tbaa !13
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.uart, ptr %26, i32 0, i32 8
  %28 = load volatile i32, ptr %27, align 4, !tbaa !14
  %29 = add i32 %28, 1
  store volatile i32 %29, ptr %27, align 4, !tbaa !14
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.uart, ptr %30, i32 0, i32 9
  %32 = load volatile i32, ptr %31, align 8, !tbaa !15
  %33 = add i32 %32, -1
  store volatile i32 %33, ptr %31, align 8, !tbaa !15
  call void (...) @unlock()
  br label %63

34:                                               ; preds = %2
  br label %35

35:                                               ; preds = %44, %34
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.uart, ptr %36, i32 0, i32 0
  %38 = load volatile ptr, ptr %37, align 8, !tbaa !16
  %39 = getelementptr inbounds i8, ptr %38, i64 24
  %40 = load i8, ptr %39, align 1, !tbaa !9
  %41 = zext i8 %40 to i32
  %42 = and i32 %41, 32
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %35
  br label %35, !llvm.loop !17

45:                                               ; preds = %35
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.uart, ptr %46, i32 0, i32 12
  store volatile i32 1, ptr %47, align 4, !tbaa !10
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.uart, ptr %48, i32 0, i32 0
  %50 = load volatile ptr, ptr %49, align 8, !tbaa !16
  %51 = getelementptr inbounds i8, ptr %50, i64 56
  %52 = load i8, ptr %51, align 1, !tbaa !9
  %53 = zext i8 %52 to i32
  %54 = or i32 %53, 48
  %55 = trunc i32 %54 to i8
  store i8 %55, ptr %51, align 1, !tbaa !9
  %56 = load i8, ptr %4, align 1, !tbaa !9
  %57 = zext i8 %56 to i32
  %58 = trunc i32 %57 to i8
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.uart, ptr %59, i32 0, i32 0
  %61 = load volatile ptr, ptr %60, align 8, !tbaa !16
  %62 = getelementptr inbounds i8, ptr %61, i64 0
  store i8 %58, ptr %62, align 1, !tbaa !9
  br label %63

63:                                               ; preds = %45, %9
  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" }

!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 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 300}
!11 = !{!"uart", !6, i64 0, !12, i64 8, !7, i64 12, !12, i64 140, !12, i64 144, !12, i64 148, !12, i64 152, !7, i64 156, !12, i64 284, !12, i64 288, !12, i64 292, !12, i64 296, !12, i64 300}
!12 = !{!"int", !7, i64 0}
!13 = !{!11, !12, i64 292}
!14 = !{!11, !12, i64 284}
!15 = !{!11, !12, i64 288}
!16 = !{!11, !6, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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"

declare void @lock(...) local_unnamed_addr #0

declare void @unlock(...) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @uputc(ptr noundef %0, i8 noundef zeroext %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 300
  %4 = load volatile i32, ptr %3, align 4, !tbaa !5
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %.preheader, label %5

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %0, i64 156
  %7 = getelementptr inbounds i8, ptr %0, i64 292
  %8 = load volatile i32, ptr %7, align 4, !tbaa !11
  %9 = zext i32 %8 to i64
  %10 = getelementptr inbounds [128 x i8], ptr %6, i64 0, i64 %9
  store volatile i8 %1, ptr %10, align 1, !tbaa !12
  tail call void (...) @lock() #2
  %11 = load volatile i32, ptr %7, align 4, !tbaa !11
  %12 = add i32 %11, 1
  store volatile i32 %12, ptr %7, align 4, !tbaa !11
  %13 = load volatile i32, ptr %7, align 4, !tbaa !11
  %14 = and i32 %13, 127
  store volatile i32 %14, ptr %7, align 4, !tbaa !11
  %15 = getelementptr inbounds i8, ptr %0, i64 284
  %16 = load volatile i32, ptr %15, align 4, !tbaa !13
  %17 = add i32 %16, 1
  store volatile i32 %17, ptr %15, align 4, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %0, i64 288
  %19 = load volatile i32, ptr %18, align 8, !tbaa !14
  %20 = add i32 %19, -1
  store volatile i32 %20, ptr %18, align 8, !tbaa !14
  tail call void (...) @unlock() #2
  br label %31

.preheader:                                       ; preds = %2, %.preheader
  %21 = load volatile ptr, ptr %0, align 8, !tbaa !15
  %22 = getelementptr inbounds i8, ptr %21, i64 24
  %23 = load i8, ptr %22, align 1, !tbaa !12
  %24 = and i8 %23, 32
  %.not12 = icmp eq i8 %24, 0
  br i1 %.not12, label %25, label %.preheader, !llvm.loop !16

25:                                               ; preds = %.preheader
  store volatile i32 1, ptr %3, align 4, !tbaa !5
  %26 = load volatile ptr, ptr %0, align 8, !tbaa !15
  %27 = getelementptr inbounds i8, ptr %26, i64 56
  %28 = load i8, ptr %27, align 1, !tbaa !12
  %29 = or i8 %28, 48
  store i8 %29, ptr %27, align 1, !tbaa !12
  %30 = load volatile ptr, ptr %0, align 8, !tbaa !15
  store i8 %1, ptr %30, align 1, !tbaa !12
  br label %31

31:                                               ; preds = %25, %5
  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, !10, i64 300}
!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 = !{!6, !10, i64 292}
!12 = !{!8, !8, i64 0}
!13 = !{!6, !10, i64 284}
!14 = !{!6, !10, i64 288}
!15 = !{!6, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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