; LLVM IR from ponylang/ponyc :: src/libponyrt/mem (heap)

; 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.chunk_t = type { ptr, %union.anon, ptr }
%union.anon = type { %struct.anon.0, [8 x i8] }
%struct.anon.0 = type { i64 }

@.str.1 = external hidden unnamed_addr constant [73 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [14 x i8], align 1
@__func__.ponyint_heap_realloc = external hidden unnamed_addr constant [21 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [13 x i8], align 1

; 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 willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
declare dso_local noalias ptr @ponyint_heap_alloc(ptr noundef, ptr noundef, i64 noundef, i32 noundef) #1

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @get_m(ptr noundef) #1

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

19:                                               ; preds = %5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = load i64, ptr %10, align 8, !tbaa !9
  %23 = call noalias ptr @ponyint_heap_alloc(ptr noundef %20, ptr noundef %21, i64 noundef %22, i32 noundef 0)
  store ptr %23, ptr %6, align 8
  br label %96

24:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %25 = load ptr, ptr %9, align 8, !tbaa !5
  %26 = call ptr @ponyint_pagemap_get_chunk(ptr noundef %25)
  store ptr %26, ptr %12, align 8, !tbaa !5
  %27 = load ptr, ptr %12, align 8, !tbaa !5
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %30

29:                                               ; preds = %24
  br label %31

30:                                               ; preds = %24
  call void @ponyint_assert_fail(ptr noundef @.str.7, ptr noundef @.str.1, i64 noundef 891, ptr noundef @__func__.ponyint_heap_realloc)
  br label %31

31:                                               ; preds = %30, %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %32 = load ptr, ptr %12, align 8, !tbaa !5
  %33 = call zeroext i1 @get_chunk_is_small_chunk(ptr noundef %32)
  br i1 %33, label %34, label %57

34:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %35 = load ptr, ptr %12, align 8, !tbaa !5
  %36 = call i64 @get_small_chunk_size(ptr noundef %35)
  store i64 %36, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = ptrtoint ptr %37 to i64
  %39 = load i64, ptr %14, align 8, !tbaa !9
  %40 = trunc i64 %39 to i32
  %41 = shl i32 32, %40
  %42 = sub nsw i32 %41, 1
  %43 = xor i32 %42, -1
  %44 = sext i32 %43 to i64
  %45 = and i64 %38, %44
  %46 = inttoptr i64 %45 to ptr
  store ptr %46, ptr %15, align 8, !tbaa !5
  %47 = load i64, ptr %14, align 8, !tbaa !9
  %48 = trunc i64 %47 to i32
  %49 = shl i32 32, %48
  %50 = sext i32 %49 to i64
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = ptrtoint ptr %51 to i64
  %53 = load ptr, ptr %15, align 8, !tbaa !5
  %54 = ptrtoint ptr %53 to i64
  %55 = sub i64 %52, %54
  %56 = sub i64 %50, %55
  store i64 %56, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  br label %69

57:                                               ; preds = %31
  %58 = load ptr, ptr %12, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.chunk_t, ptr %58, i32 0, i32 1
  %60 = getelementptr inbounds %struct.anon.0, ptr %59, i32 0, i32 0
  %61 = load i64, ptr %60, align 8, !tbaa !11
  %62 = load ptr, ptr %9, align 8, !tbaa !5
  %63 = ptrtoint ptr %62 to i64
  %64 = load ptr, ptr %12, align 8, !tbaa !5
  %65 = call ptr @get_m(ptr noundef %64)
  %66 = ptrtoint ptr %65 to i64
  %67 = sub i64 %63, %66
  %68 = sub i64 %61, %67
  store i64 %68, ptr %13, align 8, !tbaa !9
  br label %69

69:                                               ; preds = %57, %34
  %70 = load i64, ptr %11, align 8, !tbaa !9
  %71 = load i64, ptr %10, align 8, !tbaa !9
  %72 = icmp ule i64 %70, %71
  br i1 %72, label %73, label %74

73:                                               ; preds = %69
  br label %75

74:                                               ; preds = %69
  call void @ponyint_assert_fail(ptr noundef @.str.10, ptr noundef @.str.1, i64 noundef 908, ptr noundef @__func__.ponyint_heap_realloc)
  br label %75

75:                                               ; preds = %74, %73
  %76 = load i64, ptr %11, align 8, !tbaa !9
  %77 = load i64, ptr %10, align 8, !tbaa !9
  %78 = icmp ugt i64 %76, %77
  br i1 %78, label %79, label %81

79:                                               ; preds = %75
  %80 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %80, ptr %11, align 8, !tbaa !9
  br label %81

81:                                               ; preds = %79, %75
  %82 = load i64, ptr %13, align 8, !tbaa !9
  %83 = load i64, ptr %11, align 8, !tbaa !9
  %84 = icmp ugt i64 %82, %83
  br i1 %84, label %85, label %87

85:                                               ; preds = %81
  %86 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %86, ptr %13, align 8, !tbaa !9
  br label %87

87:                                               ; preds = %85, %81
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %88 = load ptr, ptr %7, align 8, !tbaa !5
  %89 = load ptr, ptr %8, align 8, !tbaa !5
  %90 = load i64, ptr %10, align 8, !tbaa !9
  %91 = call noalias ptr @ponyint_heap_alloc(ptr noundef %88, ptr noundef %89, i64 noundef %90, i32 noundef 0)
  store ptr %91, ptr %16, align 8, !tbaa !5
  %92 = load ptr, ptr %16, align 8, !tbaa !5
  %93 = load ptr, ptr %9, align 8, !tbaa !5
  %94 = load i64, ptr %13, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %92, ptr align 1 %93, i64 %94, i1 false)
  %95 = load ptr, ptr %16, align 8, !tbaa !5
  store ptr %95, ptr %6, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %96

96:                                               ; preds = %87, %19
  %97 = load ptr, ptr %6, align 8
  ret ptr %97
}

declare ptr @ponyint_pagemap_get_chunk(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @get_chunk_is_small_chunk(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i64 @get_small_chunk_size(ptr noundef) #1

; 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 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 = { "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 }

!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 = !{!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"

@.str.1 = external hidden unnamed_addr constant [73 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [14 x i8], align 1
@__func__.ponyint_heap_realloc = external hidden unnamed_addr constant [21 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [13 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local noalias ptr @ponyint_heap_alloc(ptr noundef, ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #0

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @get_m(ptr noundef) local_unnamed_addr #0

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

7:                                                ; preds = %5
  %8 = tail call noalias ptr @ponyint_heap_alloc(ptr noundef %0, ptr noundef %1, i64 noundef %3, i32 noundef 0)
  br label %34

9:                                                ; preds = %5
  %10 = tail call ptr @ponyint_pagemap_get_chunk(ptr noundef nonnull %2) #4
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %11, label %12

11:                                               ; preds = %9
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str.7, ptr noundef nonnull @.str.1, i64 noundef 891, ptr noundef nonnull @__func__.ponyint_heap_realloc) #4
  br label %12

12:                                               ; preds = %9, %11
  %13 = tail call zeroext i1 @get_chunk_is_small_chunk(ptr noundef %10)
  br i1 %13, label %14, label %23

14:                                               ; preds = %12
  %15 = tail call i64 @get_small_chunk_size(ptr noundef %10)
  %16 = ptrtoint ptr %2 to i64
  %17 = trunc i64 %15 to i32
  %18 = shl i32 32, %17
  %19 = sext i32 %18 to i64
  %20 = add i32 %18, -1
  %.not36 = sext i32 %20 to i64
  %21 = and i64 %.not36, %16
  %22 = sub i64 %19, %21
  br label %30

23:                                               ; preds = %12
  %24 = getelementptr inbounds i8, ptr %10, i64 8
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = ptrtoint ptr %2 to i64
  %27 = tail call ptr @get_m(ptr noundef %10)
  %28 = ptrtoint ptr %27 to i64
  %.neg = sub i64 %25, %26
  %29 = add i64 %.neg, %28
  br label %30

30:                                               ; preds = %23, %14
  %.032 = phi i64 [ %22, %14 ], [ %29, %23 ]
  %.not37 = icmp ugt i64 %4, %3
  br i1 %.not37, label %31, label %32

31:                                               ; preds = %30
  tail call void @ponyint_assert_fail(ptr noundef nonnull @.str.10, ptr noundef nonnull @.str.1, i64 noundef 908, ptr noundef nonnull @__func__.ponyint_heap_realloc) #4
  br label %32

32:                                               ; preds = %30, %31
  %spec.select = tail call i64 @llvm.umin.i64(i64 %4, i64 %3)
  %.1 = tail call i64 @llvm.umin.i64(i64 %.032, i64 %spec.select)
  %33 = tail call noalias ptr @ponyint_heap_alloc(ptr noundef %0, ptr noundef %1, i64 noundef %3, i32 noundef 0)
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %33, ptr nonnull align 1 %2, i64 %.1, i1 false)
  br label %34

34:                                               ; preds = %32, %7
  %.0 = phi ptr [ %8, %7 ], [ %33, %32 ]
  ret ptr %.0
}

declare ptr @ponyint_pagemap_get_chunk(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden zeroext i1 @get_chunk_is_small_chunk(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i64 @get_small_chunk_size(ptr noundef) local_unnamed_addr #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) #2

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

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 = { "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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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