; 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
define dso_local zeroext i8 @ugetc(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) #3
  br label %4

4:                                                ; preds = %9, %1
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.uart, ptr %5, i32 0, i32 3
  %7 = load volatile i32, ptr %6, align 4, !tbaa !9
  %8 = icmp ule i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %4
  br label %4, !llvm.loop !12

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

declare void @lock(...) #2

declare void @unlock(...) #2

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 = { "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 140}
!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 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!10, !11, i64 152}
!15 = !{!7, !7, i64 0}
!16 = !{!10, !11, i64 144}


; 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 zeroext i8 @ugetc(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 140
  br label %3

3:                                                ; preds = %3, %1
  %4 = load volatile i32, ptr %2, align 4, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %3, label %6, !llvm.loop !11

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

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

declare void @unlock(...) 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, !10, i64 140}
!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!6, !10, i64 152}
!14 = !{!8, !8, i64 0}
!15 = !{!6, !10, i64 144}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,SROAPass,SimplifyCFGPass,TailCallElimPass