; LLVM IR from nickmqb/muon :: bootstrap/mu.ll_ArenaAllocator (realloc__)

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

@top_____abandonFn__ = external dso_local thread_local global ptr, align 8

; 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 speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @ArenaAllocator__alloc__(ptr noundef, i64 noundef) #3

; Function Attrs: nounwind uwtable
define dso_local ptr @ArenaAllocator__realloc__(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) #3 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i8, align 1
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca i64, align 8
  %17 = alloca i64, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i64 %2, ptr %9, align 8, !tbaa !9
  store i64 %3, ptr %10, align 8, !tbaa !9
  store i64 %4, ptr %11, align 8, !tbaa !9
  %18 = load i64, ptr %10, align 8, !tbaa !9
  %19 = icmp ule i64 %18, 9223372036854775800
  br i1 %19, label %23, label %20

20:                                               ; preds = %5
  %21 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  call void %22(i32 noundef -2)
  br label %23

23:                                               ; preds = %20, %5
  %24 = load i64, ptr %10, align 8, !tbaa !9
  %25 = add nsw i64 %24, 7
  %26 = and i64 %25, -8
  store i64 %26, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #4
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = load i64, ptr %10, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %27, i64 %28
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.tag_____ArenaAllocator__, ptr %30, i32 0, i32 2
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  %33 = icmp eq ptr %29, %32
  %34 = zext i1 %33 to i32
  %35 = trunc i32 %34 to i8
  store i8 %35, ptr %12, align 1, !tbaa !13
  %36 = load i8, ptr %12, align 1, !tbaa !13
  %37 = icmp ne i8 %36, 0
  br i1 %37, label %38, label %43

38:                                               ; preds = %23
  %39 = load i64, ptr %10, align 8, !tbaa !9
  %40 = icmp sgt i64 %39, 0
  %41 = zext i1 %40 to i32
  %42 = trunc i32 %41 to i8
  store i8 %42, ptr %12, align 1, !tbaa !13
  br label %43

43:                                               ; preds = %38, %23
  %44 = load i8, ptr %12, align 1, !tbaa !13
  %45 = icmp ne i8 %44, 0
  br i1 %45, label %46, label %73

46:                                               ; preds = %43
  %47 = load i64, ptr %9, align 8, !tbaa !9
  %48 = load i64, ptr %10, align 8, !tbaa !9
  %49 = icmp sgt i64 %47, %48
  br i1 %49, label %50, label %56

50:                                               ; preds = %46
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = load i64, ptr %9, align 8, !tbaa !9
  %53 = load i64, ptr %10, align 8, !tbaa !9
  %54 = sub nsw i64 %52, %53
  %55 = call ptr @ArenaAllocator__alloc__(ptr noundef %51, i64 noundef %54)
  br label %71

56:                                               ; preds = %46
  %57 = load i64, ptr %9, align 8, !tbaa !9
  %58 = icmp sge i64 %57, 0
  br i1 %58, label %62, label %59

59:                                               ; preds = %56
  %60 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  call void %61(i32 noundef -2)
  br label %62

62:                                               ; preds = %59, %56
  %63 = load i64, ptr %9, align 8, !tbaa !9
  %64 = add nsw i64 %63, 7
  %65 = and i64 %64, -8
  store i64 %65, ptr %9, align 8, !tbaa !9
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  %67 = load i64, ptr %9, align 8, !tbaa !9
  %68 = getelementptr inbounds i8, ptr %66, i64 %67
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.tag_____ArenaAllocator__, ptr %69, i32 0, i32 2
  store ptr %68, ptr %70, align 8, !tbaa !11
  br label %71

71:                                               ; preds = %62, %50
  %72 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %72, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %98

73:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = load i64, ptr %9, align 8, !tbaa !9
  %76 = call ptr @ArenaAllocator__alloc__(ptr noundef %74, i64 noundef %75)
  store ptr %76, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %77 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %77, ptr %15, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %78 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %78, ptr %16, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #4
  %79 = load i64, ptr %15, align 8, !tbaa !9
  %80 = load i64, ptr %16, align 8, !tbaa !9
  %81 = icmp slt i64 %79, %80
  br i1 %81, label %82, label %84

82:                                               ; preds = %73
  %83 = load i64, ptr %15, align 8, !tbaa !9
  br label %86

84:                                               ; preds = %73
  %85 = load i64, ptr %16, align 8, !tbaa !9
  br label %86

86:                                               ; preds = %84, %82
  %87 = phi i64 [ %83, %82 ], [ %85, %84 ]
  store i64 %87, ptr %17, align 8, !tbaa !9
  %88 = load i64, ptr %17, align 8, !tbaa !9
  %89 = icmp sle i64 0, %88
  br i1 %89, label %93, label %90

90:                                               ; preds = %86
  %91 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %92 = load ptr, ptr %91, align 8, !tbaa !5
  call void %92(i32 noundef -4)
  br label %93

93:                                               ; preds = %90, %86
  %94 = load ptr, ptr %14, align 8, !tbaa !5
  %95 = load ptr, ptr %8, align 8, !tbaa !5
  %96 = load i64, ptr %17, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %94, ptr align 1 %95, i64 %96, i1 false)
  %97 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %97, ptr %6, align 8
  store i32 1, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  br label %98

98:                                               ; preds = %93, %71
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #4
  %99 = load ptr, ptr %6, align 8
  ret ptr %99
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"tag_____ArenaAllocator__", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!7, !7, i64 0}


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

@top_____abandonFn__ = external dso_local thread_local local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #0

; Function Attrs: mustprogress 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

; Function Attrs: nounwind uwtable
declare dso_local ptr @ArenaAllocator__alloc__(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local ptr @ArenaAllocator__realloc__(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3, i64 noundef %4) local_unnamed_addr #2 {
  %6 = icmp ult i64 %3, 9223372036854775801
  br i1 %6, label %10, label %7

7:                                                ; preds = %5
  %8 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  tail call void %9(i32 noundef -2) #4
  br label %10

10:                                               ; preds = %7, %5
  %11 = add nsw i64 %3, 7
  %12 = and i64 %11, -8
  %13 = getelementptr inbounds i8, ptr %1, i64 %12
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  %16 = icmp ne ptr %13, %15
  %17 = icmp slt i64 %12, 1
  %spec.select = or i1 %17, %16
  br i1 %spec.select, label %32, label %18

18:                                               ; preds = %10
  %19 = icmp slt i64 %12, %2
  br i1 %19, label %20, label %23

20:                                               ; preds = %18
  %21 = sub nsw i64 %2, %12
  %22 = tail call ptr @ArenaAllocator__alloc__(ptr noundef nonnull %0, i64 noundef %21)
  br label %40

23:                                               ; preds = %18
  %24 = icmp sgt i64 %2, -1
  br i1 %24, label %28, label %25

25:                                               ; preds = %23
  %26 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  tail call void %27(i32 noundef -2) #4
  br label %28

28:                                               ; preds = %25, %23
  %29 = add nsw i64 %2, 7
  %30 = and i64 %29, -8
  %31 = getelementptr inbounds i8, ptr %1, i64 %30
  store ptr %31, ptr %14, align 8, !tbaa !9
  br label %40

32:                                               ; preds = %10
  %33 = tail call ptr @ArenaAllocator__alloc__(ptr noundef nonnull %0, i64 noundef %2)
  %34 = tail call i64 @llvm.smin.i64(i64 %4, i64 %2)
  %35 = icmp sgt i64 %34, -1
  br i1 %35, label %39, label %36

36:                                               ; preds = %32
  %37 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  tail call void %38(i32 noundef -4) #4
  br label %39

39:                                               ; preds = %36, %32
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %33, ptr align 1 %1, i64 %34, i1 false)
  br label %40

40:                                               ; preds = %20, %28, %39
  %.0 = phi ptr [ %33, %39 ], [ %1, %28 ], [ %1, %20 ]
  ret ptr %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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/C++ TBAA"}
!9 = !{!10, !6, i64 16}
!10 = !{!"tag_____ArenaAllocator__", !6, i64 0, !6, i64 8, !6, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass