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

; 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::MemoryControlBlock" = type { i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN13MemoryManager6mallocEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %1) #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %9 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %10 = load i64, ptr %4, align 8, !tbaa !9
  %11 = add i64 %10, 8
  %12 = trunc i64 %11 to i32
  store i32 %12, ptr %5, align 4, !tbaa !11
  %13 = load i32, ptr %5, align 4, !tbaa !11
  %14 = urem i32 %13, 4
  %15 = sub i32 4, %14
  %16 = load i32, ptr %5, align 4, !tbaa !11
  %17 = add i32 %16, %15
  store i32 %17, ptr %5, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store ptr null, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %18 = getelementptr inbounds %class.MemoryManager, ptr %9, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  store ptr %19, ptr %8, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %41, %2
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %class.MemoryManager, ptr %9, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = icmp ult ptr %21, %23
  br i1 %24, label %25, label %48

25:                                               ; preds = %20
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %26, ptr %6, align 8, !tbaa !5
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %"struct.MemoryManager::MemoryControlBlock", ptr %27, i32 0, i32 0
  %29 = load i32, ptr %28, align 4, !tbaa !16
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %41

31:                                               ; preds = %25
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %"struct.MemoryManager::MemoryControlBlock", ptr %32, i32 0, i32 1
  %34 = load i32, ptr %33, align 4, !tbaa !18
  %35 = load i32, ptr %5, align 4, !tbaa !11
  %36 = icmp uge i32 %34, %35
  br i1 %36, label %37, label %41

37:                                               ; preds = %31
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = getelementptr inbounds %"struct.MemoryManager::MemoryControlBlock", ptr %38, i32 0, i32 0
  store i32 1, ptr %39, align 4, !tbaa !16
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %40, ptr %7, align 8, !tbaa !5
  br label %48

41:                                               ; preds = %31, %25
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %"struct.MemoryManager::MemoryControlBlock", ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !18
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = zext i32 %44 to i64
  %47 = getelementptr inbounds i8, ptr %45, i64 %46
  store ptr %47, ptr %8, align 8, !tbaa !5
  br label %20, !llvm.loop !19

48:                                               ; preds = %37, %20
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = icmp eq ptr %49, null
  br i1 %50, label %51, label %77

51:                                               ; preds = %48
  %52 = getelementptr inbounds %class.MemoryManager, ptr %9, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !15
  %54 = load i32, ptr %5, align 4, !tbaa !11
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds i8, ptr %53, i64 %55
  %57 = getelementptr inbounds %class.MemoryManager, ptr %9, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !21
  %59 = icmp ule ptr %56, %58
  br i1 %59, label %60, label %76

60:                                               ; preds = %51
  %61 = getelementptr inbounds %class.MemoryManager, ptr %9, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !15
  store ptr %62, ptr %7, align 8, !tbaa !5
  %63 = load i32, ptr %5, align 4, !tbaa !11
  %64 = getelementptr inbounds %class.MemoryManager, ptr %9, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !15
  %66 = zext i32 %63 to i64
  %67 = getelementptr inbounds i8, ptr %65, i64 %66
  store ptr %67, ptr %64, align 8, !tbaa !15
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %68, ptr %6, align 8, !tbaa !5
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %"struct.MemoryManager::MemoryControlBlock", ptr %69, i32 0, i32 0
  store i32 1, ptr %70, align 4, !tbaa !16
  %71 = load i32, ptr %5, align 4, !tbaa !11
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %"struct.MemoryManager::MemoryControlBlock", ptr %72, i32 0, i32 1
  store i32 %71, ptr %73, align 4, !tbaa !18
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = getelementptr inbounds i8, ptr %74, i64 8
  store ptr %75, ptr %7, align 8, !tbaa !5
  br label %76

76:                                               ; preds = %60, %51
  br label %80

77:                                               ; preds = %48
  %78 = load ptr, ptr %7, align 8, !tbaa !5
  %79 = getelementptr inbounds i8, ptr %78, i64 8
  store ptr %79, ptr %7, align 8, !tbaa !5
  br label %80

80:                                               ; preds = %77, %76
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret ptr %81
}

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

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

attributes #0 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTS13MemoryManager", !6, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!14, !6, i64 8}
!16 = !{!17, !12, i64 0}
!17 = !{!"_ZTSN13MemoryManager18MemoryControlBlockE", !12, i64 0, !12, i64 4}
!18 = !{!17, !12, i64 4}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!14, !6, i64 16}


; 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(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @_ZN13MemoryManager6mallocEm(ptr nocapture noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = trunc i64 %1 to i32
  %4 = and i32 %3, -4
  %5 = add i32 %4, 12
  %6 = load ptr, ptr %0, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load ptr, ptr %7, align 8, !tbaa !10
  %9 = icmp ult ptr %6, %8
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2, %.lr.ph._crit_edge
  %.030 = phi ptr [ %15, %.lr.ph._crit_edge ], [ %6, %2 ]
  %10 = load i32, ptr %.030, align 4, !tbaa !11
  %11 = icmp ne i32 %10, 0
  %12 = getelementptr inbounds i8, ptr %.030, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %.not = icmp ult i32 %13, %5
  %or.cond = select i1 %11, i1 true, i1 %.not
  br i1 %or.cond, label %.lr.ph._crit_edge, label %24

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %14 = zext i32 %13 to i64
  %15 = getelementptr inbounds i8, ptr %.030, i64 %14
  %16 = icmp ult ptr %15, %8
  br i1 %16, label %.lr.ph, label %._crit_edge, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph._crit_edge, %2
  %17 = zext i32 %5 to i64
  %18 = getelementptr inbounds i8, ptr %8, i64 %17
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !17
  %.not24 = icmp ugt ptr %18, %20
  br i1 %.not24, label %26, label %21

21:                                               ; preds = %._crit_edge
  store ptr %18, ptr %7, align 8, !tbaa !10
  store i32 1, ptr %8, align 4, !tbaa !11
  %22 = getelementptr inbounds i8, ptr %8, i64 4
  store i32 %5, ptr %22, align 4, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %8, i64 8
  br label %26

24:                                               ; preds = %.lr.ph
  store i32 1, ptr %.030, align 4, !tbaa !11
  %25 = getelementptr inbounds i8, ptr %.030, i64 8
  br label %26

26:                                               ; preds = %._crit_edge, %21, %24
  %.1 = phi ptr [ %23, %21 ], [ null, %._crit_edge ], [ %25, %24 ]
  ret ptr %.1
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(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 = !{!6, !7, i64 8}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTSN13MemoryManager18MemoryControlBlockE", !13, i64 0, !13, i64 4}
!13 = !{!"int", !8, i64 0}
!14 = !{!12, !13, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!6, !7, i64 16}


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