; LLVM IR from alekras/ao.rtos.cpp :: Kernel/memory.ll (ZN13MemoryManager13getStatisticsEPNS_16MemoryStatisticsE)

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

%class.MemoryManager = type { ptr, ptr, ptr }
%"struct.MemoryManager::MemoryStatistics" = type { ptr, ptr, ptr, i32, i32 }
%"struct.MemoryManager::MemoryControlBlock" = type { 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: mustprogress nounwind uwtable
define dso_local void @_ZN13MemoryManager13getStatisticsEPNS_16MemoryStatisticsE(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %8 = load ptr, ptr %3, align 8
  %9 = getelementptr inbounds %class.MemoryManager, ptr %8, i32 0, i32 0
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %"struct.MemoryManager::MemoryStatistics", ptr %11, i32 0, i32 0
  store ptr %10, ptr %12, align 8, !tbaa !11
  %13 = getelementptr inbounds %class.MemoryManager, ptr %8, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %"struct.MemoryManager::MemoryStatistics", ptr %15, i32 0, i32 1
  store ptr %14, ptr %16, align 8, !tbaa !15
  %17 = getelementptr inbounds %class.MemoryManager, ptr %8, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %"struct.MemoryManager::MemoryStatistics", ptr %19, i32 0, i32 2
  store ptr %18, ptr %20, align 8, !tbaa !17
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %"struct.MemoryManager::MemoryStatistics", ptr %21, i32 0, i32 3
  store i32 0, ptr %22, align 8, !tbaa !18
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %"struct.MemoryManager::MemoryStatistics", ptr %23, i32 0, i32 4
  store i32 0, ptr %24, align 4, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %25 = getelementptr inbounds %class.MemoryManager, ptr %8, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !9
  store ptr %26, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !20
  br label %27

27:                                               ; preds = %47, %2
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = getelementptr inbounds %class.MemoryManager, ptr %8, i32 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  %31 = icmp ult ptr %28, %30
  br i1 %31, label %32, label %54

32:                                               ; preds = %27
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %33, ptr %5, align 8, !tbaa !5
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %"struct.MemoryManager::MemoryStatistics", ptr %34, i32 0, i32 3
  %36 = load i32, ptr %35, align 8, !tbaa !18
  %37 = add i32 %36, 1
  store i32 %37, ptr %35, align 8, !tbaa !18
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %"struct.MemoryManager::MemoryControlBlock", ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 4, !tbaa !21
  %41 = icmp eq i32 %40, 1
  br i1 %41, label %42, label %47

42:                                               ; preds = %32
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %"struct.MemoryManager::MemoryStatistics", ptr %43, i32 0, i32 4
  %45 = load i32, ptr %44, align 4, !tbaa !19
  %46 = add i32 %45, 1
  store i32 %46, ptr %44, align 4, !tbaa !19
  br label %47

47:                                               ; preds = %42, %32
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %"struct.MemoryManager::MemoryControlBlock", ptr %48, i32 0, i32 1
  %50 = load i32, ptr %49, align 4, !tbaa !23
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = zext i32 %50 to i64
  %53 = getelementptr inbounds i8, ptr %51, i64 %52
  store ptr %53, ptr %6, align 8, !tbaa !5
  br label %27, !llvm.loop !24

54:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTS13MemoryManager", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTSN13MemoryManager16MemoryStatisticsE", !6, i64 0, !6, i64 8, !6, i64 16, !13, i64 24, !13, i64 28}
!13 = !{!"int", !7, i64 0}
!14 = !{!10, !6, i64 16}
!15 = !{!12, !6, i64 8}
!16 = !{!10, !6, i64 8}
!17 = !{!12, !6, i64 16}
!18 = !{!12, !13, i64 24}
!19 = !{!12, !13, i64 28}
!20 = !{!13, !13, i64 0}
!21 = !{!22, !13, i64 0}
!22 = !{!"_ZTSN13MemoryManager18MemoryControlBlockE", !13, i64 0, !13, i64 4}
!23 = !{!22, !13, i64 4}
!24 = distinct !{!24, !25}
!25 = !{!"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: mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN13MemoryManager13getStatisticsEPNS_16MemoryStatisticsE(ptr nocapture noundef nonnull readonly align 8 dereferenceable(24) %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #0 align 2 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  store ptr %3, ptr %1, align 8, !tbaa !10
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !13
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  store ptr %5, ptr %6, align 8, !tbaa !14
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !15
  %9 = getelementptr inbounds i8, ptr %1, i64 16
  store ptr %8, ptr %9, align 8, !tbaa !16
  %10 = getelementptr inbounds i8, ptr %1, i64 24
  store i32 0, ptr %10, align 8, !tbaa !17
  %11 = getelementptr inbounds i8, ptr %1, i64 28
  store i32 0, ptr %11, align 4, !tbaa !18
  %12 = icmp ult ptr %3, %8
  br i1 %12, label %.lr.ph, label %27

.lr.ph:                                           ; preds = %2, %20
  %13 = phi i32 [ %21, %20 ], [ 0, %2 ]
  %14 = phi i32 [ %15, %20 ], [ 0, %2 ]
  %.013 = phi ptr [ %25, %20 ], [ %3, %2 ]
  %15 = add i32 %14, 1
  %16 = load i32, ptr %.013, align 4, !tbaa !19
  %17 = icmp eq i32 %16, 1
  br i1 %17, label %18, label %20

18:                                               ; preds = %.lr.ph
  %19 = add i32 %13, 1
  store i32 %19, ptr %11, align 4, !tbaa !18
  br label %20

20:                                               ; preds = %18, %.lr.ph
  %21 = phi i32 [ %19, %18 ], [ %13, %.lr.ph ]
  %22 = getelementptr inbounds i8, ptr %.013, i64 4
  %23 = load i32, ptr %22, align 4, !tbaa !21
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %.013, i64 %24
  %26 = icmp ult ptr %25, %8
  br i1 %26, label %.lr.ph, label %._crit_edge, !llvm.loop !22

._crit_edge:                                      ; preds = %20
  store i32 %15, ptr %10, align 8, !tbaa !17
  br label %27

27:                                               ; preds = %._crit_edge, %2
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(read, argmem: readwrite, 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" }

!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 = !{!"_ZTS13MemoryManager", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !7, i64 0}
!11 = !{!"_ZTSN13MemoryManager16MemoryStatisticsE", !7, i64 0, !7, i64 8, !7, i64 16, !12, i64 24, !12, i64 28}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !7, i64 16}
!14 = !{!11, !7, i64 8}
!15 = !{!6, !7, i64 8}
!16 = !{!11, !7, i64 16}
!17 = !{!11, !12, i64 24}
!18 = !{!11, !12, i64 28}
!19 = !{!20, !12, i64 0}
!20 = !{!"_ZTSN13MemoryManager18MemoryControlBlockE", !12, i64 0, !12, i64 4}
!21 = !{!20, !12, i64 4}
!22 = distinct !{!22, !23}
!23 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass