; LLVM IR from cw1997/ez-rtos :: os (memory)

; 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.memory_node = type { i32, i32, i8, ptr, ptr }

@heap = external dso_local global [4128 x i8], align 16
@memory_all = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @malloc(i32 noundef %0) #0 {
  %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 ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %11 = load ptr, ptr @memory_all, align 8, !tbaa !9
  store ptr %11, ptr %4, align 8, !tbaa !9
  br label %12

12:                                               ; preds = %88, %1
  %13 = load ptr, ptr %4, align 8, !tbaa !9
  %14 = icmp ne ptr %13, null
  br i1 %14, label %15, label %92

15:                                               ; preds = %12
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.memory_node, ptr %16, i32 0, i32 2
  %18 = load i8, ptr %17, align 8, !tbaa !11
  %19 = sext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %88

21:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = add i64 %23, 32
  %25 = trunc i64 %24 to i32
  store i32 %25, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %26 = load ptr, ptr %4, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.memory_node, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !13
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sub nsw i32 %28, %29
  store i32 %30, ptr %6, align 4, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = icmp sge i32 %31, 0
  br i1 %32, label %33, label %84

33:                                               ; preds = %21
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = load ptr, ptr %4, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.memory_node, ptr %35, i32 0, i32 0
  store i32 %34, ptr %36, align 8, !tbaa !13
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.memory_node, ptr %37, i32 0, i32 2
  store i8 1, ptr %38, align 8, !tbaa !11
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = icmp ugt i64 %40, 32
  br i1 %41, label %42, label %76

42:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %43 = load ptr, ptr %4, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.memory_node, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !14
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = add nsw i32 %45, %46
  store i32 %47, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [4128 x i8], ptr @heap, i64 0, i64 %49
  store ptr %50, ptr %8, align 8, !tbaa !9
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = sext i32 %51 to i64
  %53 = sub i64 %52, 32
  %54 = trunc i64 %53 to i32
  %55 = load ptr, ptr %8, align 8, !tbaa !9
  %56 = getelementptr inbounds %struct.memory_node, ptr %55, i32 0, i32 0
  store i32 %54, ptr %56, align 8, !tbaa !13
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = add i64 %58, 32
  %60 = trunc i64 %59 to i32
  %61 = load ptr, ptr %8, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.memory_node, ptr %61, i32 0, i32 1
  store i32 %60, ptr %62, align 4, !tbaa !14
  %63 = load ptr, ptr %8, align 8, !tbaa !9
  %64 = getelementptr inbounds %struct.memory_node, ptr %63, i32 0, i32 2
  store i8 0, ptr %64, align 8, !tbaa !11
  %65 = load ptr, ptr %4, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.memory_node, ptr %65, i32 0, i32 4
  %67 = load ptr, ptr %66, align 8, !tbaa !15
  %68 = load ptr, ptr %8, align 8, !tbaa !9
  %69 = getelementptr inbounds %struct.memory_node, ptr %68, i32 0, i32 4
  store ptr %67, ptr %69, align 8, !tbaa !15
  %70 = load ptr, ptr %4, align 8, !tbaa !9
  %71 = load ptr, ptr %8, align 8, !tbaa !9
  %72 = getelementptr inbounds %struct.memory_node, ptr %71, i32 0, i32 3
  store ptr %70, ptr %72, align 8, !tbaa !16
  %73 = load ptr, ptr %8, align 8, !tbaa !9
  %74 = load ptr, ptr %4, align 8, !tbaa !9
  %75 = getelementptr inbounds %struct.memory_node, ptr %74, i32 0, i32 4
  store ptr %73, ptr %75, align 8, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %76

76:                                               ; preds = %42, %33
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %77 = load ptr, ptr %4, align 8, !tbaa !9
  %78 = getelementptr inbounds %struct.memory_node, ptr %77, i32 0, i32 1
  %79 = load i32, ptr %78, align 4, !tbaa !14
  %80 = sext i32 %79 to i64
  %81 = add i64 %80, 32
  %82 = getelementptr inbounds [4128 x i8], ptr @heap, i64 0, i64 %81
  store ptr %82, ptr %9, align 8, !tbaa !9
  %83 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %83, ptr %2, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %85

84:                                               ; preds = %21
  store i32 0, ptr %10, align 4
  br label %85

85:                                               ; preds = %84, %76
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  %86 = load i32, ptr %10, align 4
  switch i32 %86, label %93 [
    i32 0, label %87
  ]

87:                                               ; preds = %85
  br label %88

88:                                               ; preds = %87, %15
  %89 = load ptr, ptr %4, align 8, !tbaa !9
  %90 = getelementptr inbounds %struct.memory_node, ptr %89, i32 0, i32 4
  %91 = load ptr, ptr %90, align 8, !tbaa !15
  store ptr %91, ptr %4, align 8, !tbaa !9
  br label %12, !llvm.loop !17

92:                                               ; preds = %12
  store ptr null, ptr %2, align 8
  store i32 1, ptr %10, align 4
  br label %93

93:                                               ; preds = %92, %85
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  %94 = load ptr, ptr %2, align 8
  ret ptr %94
}

; 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 = { 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 = !{!"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, !7, i64 8}
!12 = !{!"memory_node", !6, i64 0, !6, i64 4, !7, i64 8, !10, i64 16, !10, i64 24}
!13 = !{!12, !6, i64 0}
!14 = !{!12, !6, i64 4}
!15 = !{!12, !10, i64 24}
!16 = !{!12, !10, i64 16}
!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"

@heap = external dso_local global [4128 x i8], align 16
@memory_all = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local ptr @malloc(i32 noundef %0) local_unnamed_addr #0 {
  %.02936 = load ptr, ptr @memory_all, align 8, !tbaa !5
  %.not37 = icmp eq ptr %.02936, null
  br i1 %.not37, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %2 = add i32 %0, 32
  br label %3

3:                                                ; preds = %.lr.ph, %.thread
  %.02938 = phi ptr [ %.02936, %.lr.ph ], [ %.029, %.thread ]
  %4 = getelementptr inbounds i8, ptr %.02938, i64 8
  %5 = load i8, ptr %4, align 8, !tbaa !9
  %6 = icmp eq i8 %5, 0
  br i1 %6, label %7, label %.thread

7:                                                ; preds = %3
  %8 = load i32, ptr %.02938, align 8, !tbaa !12
  %9 = sub nsw i32 %8, %2
  %10 = icmp slt i32 %9, 0
  br i1 %10, label %.thread, label %11

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %.02938, i64 8
  store i32 %0, ptr %.02938, align 8, !tbaa !12
  store i8 1, ptr %12, align 8, !tbaa !9
  %13 = icmp ugt i32 %9, 32
  br i1 %13, label %14, label %28

14:                                               ; preds = %11
  %15 = getelementptr inbounds i8, ptr %.02938, i64 4
  %16 = load i32, ptr %15, align 4, !tbaa !13
  %17 = add nsw i32 %16, %2
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [4128 x i8], ptr @heap, i64 0, i64 %18
  %20 = add nsw i32 %9, -32
  store i32 %20, ptr %19, align 8, !tbaa !12
  %21 = add i32 %17, 32
  %22 = getelementptr inbounds i8, ptr %19, i64 4
  store i32 %21, ptr %22, align 4, !tbaa !13
  %23 = getelementptr inbounds i8, ptr %19, i64 8
  store i8 0, ptr %23, align 8, !tbaa !9
  %24 = getelementptr inbounds i8, ptr %.02938, i64 24
  %25 = load ptr, ptr %24, align 8, !tbaa !14
  %26 = getelementptr inbounds i8, ptr %19, i64 24
  store ptr %25, ptr %26, align 8, !tbaa !14
  %27 = getelementptr inbounds i8, ptr %19, i64 16
  store ptr %.02938, ptr %27, align 8, !tbaa !15
  store ptr %19, ptr %24, align 8, !tbaa !14
  br label %28

28:                                               ; preds = %11, %14
  %29 = getelementptr inbounds i8, ptr %.02938, i64 4
  %30 = load i32, ptr %29, align 4, !tbaa !13
  %31 = sext i32 %30 to i64
  %32 = add nsw i64 %31, 32
  %33 = getelementptr inbounds [4128 x i8], ptr @heap, i64 0, i64 %32
  br label %.loopexit

.thread:                                          ; preds = %7, %3
  %34 = getelementptr inbounds i8, ptr %.02938, i64 24
  %.029 = load ptr, ptr %34, align 8, !tbaa !5
  %.not = icmp eq ptr %.029, null
  br i1 %.not, label %.loopexit, label %3, !llvm.loop !16

.loopexit:                                        ; preds = %.thread, %1, %28
  %.3 = phi ptr [ %33, %28 ], [ null, %1 ], [ null, %.thread ]
  ret ptr %.3
}

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


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