; LLVM IR from brewlin/garbage-collect :: gc-python/malloc (obmalloc)

; 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.pool_header = type { %union.anon, ptr, ptr, ptr, i32, i32, i32, i32 }
%union.anon = type { ptr }
%struct.arena_object = type { i64, ptr, i32, i32, ptr, ptr, ptr }

@arenas = external dso_local global ptr, align 8
@maxarenas = external dso_local global i32, align 4

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

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @PyObject_Free(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @PyObject_Realloc(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %15

12:                                               ; preds = %2
  %13 = load i64, ptr %5, align 8, !tbaa !9
  %14 = call ptr @PyObject_Malloc(i64 noundef %13)
  store ptr %14, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %102

15:                                               ; preds = %2
  %16 = load i64, ptr %5, align 8, !tbaa !9
  %17 = icmp ugt i64 %16, 2147483647
  br i1 %17, label %18, label %19

18:                                               ; preds = %15
  store ptr null, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %102

19:                                               ; preds = %15
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = ptrtoint ptr %20 to i64
  %22 = and i64 %21, -4096
  %23 = inttoptr i64 %22 to ptr
  store ptr %23, ptr %7, align 8, !tbaa !5
  %24 = load ptr, ptr %7, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.pool_header, ptr %24, i32 0, i32 4
  %26 = load i32, ptr %25, align 8, !tbaa !11
  %27 = load i32, ptr @maxarenas, align 4, !tbaa !14
  %28 = icmp ult i32 %26, %27
  br i1 %28, label %29, label %84

29:                                               ; preds = %19
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = ptrtoint ptr %30 to i64
  %32 = load ptr, ptr @arenas, align 8, !tbaa !5
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.pool_header, ptr %33, i32 0, i32 4
  %35 = load i32, ptr %34, align 8, !tbaa !11
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds %struct.arena_object, ptr %32, i64 %36
  %38 = getelementptr inbounds %struct.arena_object, ptr %37, i32 0, i32 0
  %39 = load i64, ptr %38, align 8, !tbaa !15
  %40 = sub i64 %31, %39
  %41 = icmp ult i64 %40, 262144
  br i1 %41, label %42, label %84

42:                                               ; preds = %29
  %43 = load ptr, ptr @arenas, align 8, !tbaa !5
  %44 = load ptr, ptr %7, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.pool_header, ptr %44, i32 0, i32 4
  %46 = load i32, ptr %45, align 8, !tbaa !11
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds %struct.arena_object, ptr %43, i64 %47
  %49 = getelementptr inbounds %struct.arena_object, ptr %48, i32 0, i32 0
  %50 = load i64, ptr %49, align 8, !tbaa !15
  %51 = icmp ne i64 %50, 0
  br i1 %51, label %52, label %84

52:                                               ; preds = %42
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.pool_header, ptr %53, i32 0, i32 5
  %55 = load i32, ptr %54, align 4, !tbaa !17
  %56 = add i32 %55, 1
  %57 = shl i32 %56, 3
  %58 = zext i32 %57 to i64
  store i64 %58, ptr %8, align 8, !tbaa !9
  %59 = load i64, ptr %5, align 8, !tbaa !9
  %60 = load i64, ptr %8, align 8, !tbaa !9
  %61 = icmp ule i64 %59, %60
  br i1 %61, label %62, label %72

62:                                               ; preds = %52
  %63 = load i64, ptr %5, align 8, !tbaa !9
  %64 = mul i64 4, %63
  %65 = load i64, ptr %8, align 8, !tbaa !9
  %66 = mul i64 3, %65
  %67 = icmp ugt i64 %64, %66
  br i1 %67, label %68, label %70

68:                                               ; preds = %62
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %69, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %102

70:                                               ; preds = %62
  %71 = load i64, ptr %5, align 8, !tbaa !9
  store i64 %71, ptr %8, align 8, !tbaa !9
  br label %72

72:                                               ; preds = %70, %52
  %73 = load i64, ptr %5, align 8, !tbaa !9
  %74 = call ptr @PyObject_Malloc(i64 noundef %73)
  store ptr %74, ptr %6, align 8, !tbaa !5
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = icmp ne ptr %75, null
  br i1 %76, label %77, label %82

77:                                               ; preds = %72
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = load ptr, ptr %4, align 8, !tbaa !5
  %80 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %78, ptr align 1 %79, i64 %80, i1 false)
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  call void @PyObject_Free(ptr noundef %81)
  br label %82

82:                                               ; preds = %77, %72
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %83, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %102

84:                                               ; preds = %42, %29, %19
  %85 = load i64, ptr %5, align 8, !tbaa !9
  %86 = icmp ne i64 %85, 0
  br i1 %86, label %87, label %91

87:                                               ; preds = %84
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  %89 = load i64, ptr %5, align 8, !tbaa !9
  %90 = call ptr @realloc(ptr noundef %88, i64 noundef %89) #5
  store ptr %90, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %102

91:                                               ; preds = %84
  %92 = load ptr, ptr %4, align 8, !tbaa !5
  %93 = call ptr @realloc(ptr noundef %92, i64 noundef 1) #5
  store ptr %93, ptr %6, align 8, !tbaa !5
  %94 = load ptr, ptr %6, align 8, !tbaa !5
  %95 = icmp ne ptr %94, null
  br i1 %95, label %96, label %98

96:                                               ; preds = %91
  %97 = load ptr, ptr %6, align 8, !tbaa !5
  br label %100

98:                                               ; preds = %91
  %99 = load ptr, ptr %4, align 8, !tbaa !5
  br label %100

100:                                              ; preds = %98, %96
  %101 = phi ptr [ %97, %96 ], [ %99, %98 ]
  store ptr %101, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %102

102:                                              ; preds = %100, %87, %82, %68, %18, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %103 = load ptr, ptr %3, align 8
  ret ptr %103
}

; 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

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

!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, !13, i64 32}
!12 = !{!"pool_header", !7, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !13, i64 32, !13, i64 36, !13, i64 40, !13, i64 44}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!16, !10, i64 0}
!16 = !{!"arena_object", !10, i64 0, !6, i64 8, !13, i64 16, !13, i64 20, !6, i64 24, !6, i64 32, !6, i64 40}
!17 = !{!12, !13, i64 36}


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

%struct.arena_object = type { i64, ptr, i32, i32, ptr, ptr, ptr }

@arenas = external dso_local local_unnamed_addr global ptr, align 8
@maxarenas = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @PyObject_Malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @PyObject_Free(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @PyObject_Realloc(ptr noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %4, label %6

4:                                                ; preds = %2
  %5 = tail call ptr @PyObject_Malloc(i64 noundef %1)
  br label %42

6:                                                ; preds = %2
  %7 = icmp ugt i64 %1, 2147483647
  br i1 %7, label %42, label %8

8:                                                ; preds = %6
  %9 = ptrtoint ptr %0 to i64
  %10 = and i64 %9, -4096
  %11 = inttoptr i64 %10 to ptr
  %12 = getelementptr inbounds i8, ptr %11, i64 32
  %13 = load i32, ptr %12, align 32, !tbaa !5
  %14 = load i32, ptr @maxarenas, align 4, !tbaa !11
  %15 = icmp ult i32 %13, %14
  br i1 %15, label %16, label %36

16:                                               ; preds = %8
  %17 = load ptr, ptr @arenas, align 8, !tbaa !12
  %18 = zext i32 %13 to i64
  %19 = getelementptr inbounds %struct.arena_object, ptr %17, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = sub i64 %9, %20
  %22 = icmp ugt i64 %21, 262143
  %.not = icmp eq i64 %20, 0
  %or.cond = or i1 %.not, %22
  br i1 %or.cond, label %36, label %23

23:                                               ; preds = %16
  %24 = getelementptr inbounds i8, ptr %11, i64 36
  %25 = load i32, ptr %24, align 4, !tbaa !16
  %26 = shl i32 %25, 3
  %27 = add i32 %26, 8
  %28 = zext i32 %27 to i64
  %.not37 = icmp ult i64 %28, %1
  br i1 %.not37, label %33, label %29

29:                                               ; preds = %23
  %30 = shl nuw nsw i64 %1, 2
  %31 = mul nuw nsw i64 %28, 3
  %32 = icmp ugt i64 %30, %31
  br i1 %32, label %42, label %33

33:                                               ; preds = %29, %23
  %.0 = phi i64 [ %28, %23 ], [ %1, %29 ]
  %34 = tail call ptr @PyObject_Malloc(i64 noundef %1)
  %.not38 = icmp eq ptr %34, null
  br i1 %.not38, label %42, label %35

35:                                               ; preds = %33
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %34, ptr nonnull align 1 %0, i64 %.0, i1 false)
  tail call void @PyObject_Free(ptr noundef nonnull %0)
  br label %42

36:                                               ; preds = %16, %8
  %.not35 = icmp eq i64 %1, 0
  br i1 %.not35, label %39, label %37

37:                                               ; preds = %36
  %38 = tail call ptr @realloc(ptr noundef nonnull %0, i64 noundef %1) #3
  br label %42

39:                                               ; preds = %36
  %40 = tail call dereferenceable_or_null(1) ptr @realloc(ptr noundef nonnull %0, i64 noundef 1) #3
  %.not36 = icmp eq ptr %40, null
  %41 = select i1 %.not36, ptr %0, ptr %40
  br label %42

42:                                               ; preds = %33, %35, %29, %6, %39, %37, %4
  %.029 = phi ptr [ %5, %4 ], [ %38, %37 ], [ %41, %39 ], [ null, %6 ], [ %0, %29 ], [ %34, %35 ], [ null, %33 ]
  ret ptr %.029
}

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

attributes #0 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, 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 #1 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind allocsize(1) }

!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, !10, i64 32}
!6 = !{!"pool_header", !7, i64 0, !9, i64 8, !9, i64 16, !9, i64 24, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"any pointer", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!9, !9, i64 0}
!13 = !{!14, !15, i64 0}
!14 = !{!"arena_object", !15, i64 0, !9, i64 8, !10, i64 16, !10, i64 20, !9, i64 24, !9, i64 32, !9, i64 40}
!15 = !{!"long", !7, i64 0}
!16 = !{!6, !10, i64 36}


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