; LLVM IR from alekras/ao.rtos.cpp :: Porting/ARM-Raspberry_Pi (rpi-mailbox-interface)

; 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.rpi_mailbox_property_t = type { i32, i32, %union.anon }
%union.anon = type { [64 x i32] }

@pt = external dso_local global [8192 x i32], align 16
@RPI_PropertyGet.property = external hidden global %struct.rpi_mailbox_property_t, align 4

; 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 ptr @RPI_PropertyGet(i32 noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store ptr null, ptr %4, align 8, !tbaa !9
  %9 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %9, ptr @RPI_PropertyGet.property, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 2, ptr %5, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %26, %1
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = load i32, ptr @pt, align 16, !tbaa !5
  %13 = ashr i32 %12, 2
  %14 = icmp slt i32 %11, %13
  br i1 %14, label %15, label %36

15:                                               ; preds = %10
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [8192 x i32], ptr @pt, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = load i32, ptr %3, align 4, !tbaa !5
  %21 = icmp eq i32 %19, %20
  br i1 %21, label %22, label %26

22:                                               ; preds = %15
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [8192 x i32], ptr @pt, i64 0, i64 %24
  store ptr %25, ptr %4, align 8, !tbaa !9
  br label %36

26:                                               ; preds = %15
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [8192 x i32], ptr @pt, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = ashr i32 %31, 2
  %33 = add nsw i32 %32, 3
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = add nsw i32 %34, %33
  store i32 %35, ptr %5, align 4, !tbaa !5
  br label %10, !llvm.loop !13

36:                                               ; preds = %22, %10
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = icmp eq ptr %37, null
  br i1 %38, label %39, label %40

39:                                               ; preds = %36
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %65

40:                                               ; preds = %36
  %41 = load ptr, ptr %4, align 8, !tbaa !9
  %42 = getelementptr inbounds i32, ptr %41, i64 2
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = and i32 %43, 65535
  store i32 %44, ptr getelementptr inbounds (%struct.rpi_mailbox_property_t, ptr @RPI_PropertyGet.property, i32 0, i32 1), align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %45 = load i32, ptr getelementptr inbounds (%struct.rpi_mailbox_property_t, ptr @RPI_PropertyGet.property, i32 0, i32 1), align 4, !tbaa !15
  %46 = ashr i32 %45, 2
  store i32 %46, ptr %8, align 4, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %61, %40
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = load i32, ptr %8, align 4, !tbaa !5
  %50 = icmp slt i32 %48, %49
  br i1 %50, label %51, label %64

51:                                               ; preds = %47
  %52 = load ptr, ptr %4, align 8, !tbaa !9
  %53 = load i32, ptr %7, align 4, !tbaa !5
  %54 = add nsw i32 3, %53
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds i32, ptr %52, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !5
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [64 x i32], ptr getelementptr inbounds (%struct.rpi_mailbox_property_t, ptr @RPI_PropertyGet.property, i32 0, i32 2), i64 0, i64 %59
  store i32 %57, ptr %60, align 4, !tbaa !16
  br label %61

61:                                               ; preds = %51
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %7, align 4, !tbaa !5
  br label %47, !llvm.loop !17

64:                                               ; preds = %47
  store ptr @RPI_PropertyGet.property, ptr %2, align 8
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %65

65:                                               ; preds = %64, %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %66 = load ptr, ptr %2, align 8
  ret ptr %66
}

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


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

%struct.rpi_mailbox_property_t = type { i32, i32, %union.anon }
%union.anon = type { [64 x i32] }

@pt = external dso_local local_unnamed_addr global [8192 x i32], align 16
@RPI_PropertyGet.property = external hidden global %struct.rpi_mailbox_property_t, align 4

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef ptr @RPI_PropertyGet(i32 noundef %0) local_unnamed_addr #0 {
  store i32 %0, ptr @RPI_PropertyGet.property, align 4, !tbaa !5
  %2 = load i32, ptr @pt, align 16, !tbaa !10
  %3 = ashr i32 %2, 2
  %4 = icmp sgt i32 %3, 2
  br i1 %4, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %1, %9
  %.01621 = phi i32 [ %16, %9 ], [ 2, %1 ]
  %5 = sext i32 %.01621 to i64
  %6 = getelementptr inbounds [8192 x i32], ptr @pt, i64 0, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !10
  %8 = icmp eq i32 %7, %0
  br i1 %8, label %18, label %9

9:                                                ; preds = %.lr.ph
  %10 = add nsw i32 %.01621, 1
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [8192 x i32], ptr @pt, i64 0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !10
  %14 = ashr i32 %13, 2
  %15 = add i32 %.01621, 3
  %16 = add i32 %15, %14
  %17 = icmp slt i32 %16, %3
  br i1 %17, label %.lr.ph, label %.thread, !llvm.loop !11

18:                                               ; preds = %.lr.ph
  %19 = getelementptr inbounds [8192 x i32], ptr @pt, i64 0, i64 %5
  %20 = getelementptr inbounds i8, ptr %19, i64 8
  %21 = load i32, ptr %20, align 4, !tbaa !10
  %22 = and i32 %21, 65535
  store i32 %22, ptr getelementptr inbounds (i8, ptr @RPI_PropertyGet.property, i64 4), align 4, !tbaa !13
  %.not = icmp ult i32 %22, 4
  br i1 %.not, label %.thread, label %.lr.ph23.preheader

.lr.ph23.preheader:                               ; preds = %18
  %invariant.gep = getelementptr i8, ptr %19, i64 12
  %23 = lshr i32 %22, 2
  %24 = add nsw i32 %23, -1
  %25 = zext nneg i32 %24 to i64
  %26 = shl nuw nsw i64 %25, 2
  %27 = add nuw nsw i64 %26, 4
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) getelementptr inbounds (i8, ptr @RPI_PropertyGet.property, i64 8), ptr noundef nonnull align 4 dereferenceable(1) %invariant.gep, i64 %27, i1 false), !tbaa !14
  br label %.thread

.thread:                                          ; preds = %9, %.lr.ph23.preheader, %1, %18
  %.0 = phi ptr [ @RPI_PropertyGet.property, %18 ], [ null, %1 ], [ @RPI_PropertyGet.property, %.lr.ph23.preheader ], [ null, %9 ]
  ret ptr %.0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 nounwind willreturn memory(argmem: readwrite) }

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass