; LLVM IR from lichen-community-systems/signaletic :: libsignaletic/tests/vendor/unity/extras/memory/src (unity_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.GuardBytes = type { i64, i64 }

@malloc_count = external hidden global i32, align 4
@malloc_fail_countdown = external hidden global i32, align 4
@end = external hidden constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @unity_malloc(i64 noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = add i64 %8, 4
  %10 = call i64 @unity_size_round_up(i64 noundef %9)
  %11 = add i64 16, %10
  store i64 %11, ptr %6, align 8, !tbaa !5
  %12 = load i32, ptr @malloc_fail_countdown, align 4, !tbaa !9
  %13 = icmp ne i32 %12, -1
  br i1 %13, label %14, label %21

14:                                               ; preds = %1
  %15 = load i32, ptr @malloc_fail_countdown, align 4, !tbaa !9
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %45

18:                                               ; preds = %14
  %19 = load i32, ptr @malloc_fail_countdown, align 4, !tbaa !9
  %20 = add nsw i32 %19, -1
  store i32 %20, ptr @malloc_fail_countdown, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %18, %1
  %22 = load i64, ptr %3, align 8, !tbaa !5
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %45

25:                                               ; preds = %21
  %26 = load i64, ptr %6, align 8, !tbaa !5
  %27 = call noalias ptr @malloc(i64 noundef %26) #5
  store ptr %27, ptr %5, align 8, !tbaa !11
  %28 = load ptr, ptr %5, align 8, !tbaa !11
  %29 = icmp eq ptr %28, null
  br i1 %29, label %30, label %31

30:                                               ; preds = %25
  store ptr null, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %45

31:                                               ; preds = %25
  %32 = load i32, ptr @malloc_count, align 4, !tbaa !9
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr @malloc_count, align 4, !tbaa !9
  %34 = load i64, ptr %3, align 8, !tbaa !5
  %35 = load ptr, ptr %5, align 8, !tbaa !11
  %36 = getelementptr inbounds %struct.GuardBytes, ptr %35, i32 0, i32 0
  store i64 %34, ptr %36, align 8, !tbaa !13
  %37 = load ptr, ptr %5, align 8, !tbaa !11
  %38 = getelementptr inbounds %struct.GuardBytes, ptr %37, i32 0, i32 1
  store i64 0, ptr %38, align 8, !tbaa !15
  %39 = load ptr, ptr %5, align 8, !tbaa !11
  %40 = getelementptr inbounds %struct.GuardBytes, ptr %39, i64 1
  store ptr %40, ptr %4, align 8, !tbaa !11
  %41 = load ptr, ptr %4, align 8, !tbaa !11
  %42 = load i64, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %41, i64 %42
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %43, ptr align 1 @end, i64 4, i1 false)
  %44 = load ptr, ptr %4, align 8, !tbaa !11
  store ptr %44, ptr %2, align 8
  store i32 1, ptr %7, align 4
  br label %45

45:                                               ; preds = %31, %30, %24, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %46 = load ptr, ptr %2, align 8
  ret ptr %46
}

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

; Function Attrs: nounwind uwtable
declare hidden i64 @unity_size_round_up(i64 noundef) #0

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; 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) #3

; 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 allocsize(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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"GuardBytes", !6, i64 0, !6, i64 8}
!15 = !{!14, !6, i64 8}


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

@malloc_count = external hidden local_unnamed_addr global i32, align 4
@malloc_fail_countdown = external hidden local_unnamed_addr global i32, align 4
@end = external hidden local_unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @unity_malloc(i64 noundef %0) local_unnamed_addr #0 {
  %2 = add i64 %0, 4
  %3 = tail call i64 @unity_size_round_up(i64 noundef %2)
  %4 = add i64 %3, 16
  %5 = load i32, ptr @malloc_fail_countdown, align 4, !tbaa !5
  switch i32 %5, label %6 [
    i32 -1, label %8
    i32 0, label %20
  ]

6:                                                ; preds = %1
  %7 = add nsw i32 %5, -1
  store i32 %7, ptr @malloc_fail_countdown, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %1, %6
  %9 = icmp eq i64 %0, 0
  br i1 %9, label %20, label %10

10:                                               ; preds = %8
  %11 = tail call noalias ptr @malloc(i64 noundef %4) #2
  %12 = icmp eq ptr %11, null
  br i1 %12, label %20, label %13

13:                                               ; preds = %10
  %14 = load i32, ptr @malloc_count, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr @malloc_count, align 4, !tbaa !5
  store i64 %0, ptr %11, align 8, !tbaa !9
  %16 = getelementptr inbounds i8, ptr %11, i64 8
  store i64 0, ptr %16, align 8, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %11, i64 16
  %18 = getelementptr inbounds i8, ptr %17, i64 %0
  %19 = load i32, ptr @end, align 1
  store i32 %19, ptr %18, align 1
  br label %20

20:                                               ; preds = %10, %8, %1, %13
  %.0 = phi ptr [ %17, %13 ], [ null, %1 ], [ null, %8 ], [ null, %10 ]
  ret ptr %.0
}

; Function Attrs: nounwind uwtable
declare hidden i64 @unity_size_round_up(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

!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, !11, i64 0}
!10 = !{!"GuardBytes", !11, i64 0, !11, i64 8}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass