; 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 }
%struct.anon = type { i32, i32, i32 }

; 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 hidden ptr @get_m(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @set_large_chunk_slot(ptr noundef, i32 noundef) #1

; 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: nounwind uwtable
define dso_local zeroext i1 @ponyint_heap_mark(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  call void @maybe_clear_chunk(ptr noundef %8)
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = call zeroext i1 @get_chunk_is_small_chunk(ptr noundef %9)
  br i1 %10, label %11, label %60

11:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = ptrtoint ptr %12 to i64
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = call i64 @get_small_chunk_size(ptr noundef %14)
  %16 = trunc i64 %15 to i32
  %17 = shl i32 32, %16
  %18 = sub nsw i32 %17, 1
  %19 = xor i32 %18, -1
  %20 = sext i32 %19 to i64
  %21 = and i64 %13, %20
  %22 = inttoptr i64 %21 to ptr
  store ptr %22, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = call ptr @get_m(ptr noundef %24)
  %26 = ptrtoint ptr %23 to i64
  %27 = ptrtoint ptr %25 to i64
  %28 = sub i64 %26, %27
  %29 = lshr i64 %28, 5
  %30 = trunc i64 %29 to i32
  %31 = shl i32 1, %30
  store i32 %31, ptr %7, align 4, !tbaa !9
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.chunk_t, ptr %32, i32 0, i32 1
  %34 = getelementptr inbounds %struct.anon, ptr %33, i32 0, i32 0
  %35 = load i32, ptr %34, align 8, !tbaa !11
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = and i32 %35, %36
  %38 = icmp eq i32 %37, 0
  %39 = zext i1 %38 to i8
  store i8 %39, ptr %5, align 1, !tbaa !12
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = icmp eq ptr %40, %41
  br i1 %42, label %43, label %51

43:                                               ; preds = %11
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = xor i32 %44, -1
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.chunk_t, ptr %46, i32 0, i32 1
  %48 = getelementptr inbounds %struct.anon, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !11
  %50 = and i32 %49, %45
  store i32 %50, ptr %48, align 8, !tbaa !11
  br label %59

51:                                               ; preds = %11
  %52 = load i32, ptr %7, align 4, !tbaa !9
  %53 = xor i32 %52, -1
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.chunk_t, ptr %54, i32 0, i32 1
  %56 = getelementptr inbounds %struct.anon, ptr %55, i32 0, i32 1
  %57 = load i32, ptr %56, align 4, !tbaa !11
  %58 = and i32 %57, %53
  store i32 %58, ptr %56, align 4, !tbaa !11
  br label %59

59:                                               ; preds = %51, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %74

60:                                               ; preds = %2
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = call i32 @get_large_chunk_slot(ptr noundef %61)
  %63 = icmp eq i32 %62, 0
  %64 = zext i1 %63 to i8
  store i8 %64, ptr %5, align 1, !tbaa !12
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = call ptr @get_m(ptr noundef %66)
  %68 = icmp eq ptr %65, %67
  br i1 %68, label %69, label %71

69:                                               ; preds = %60
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  call void @set_large_chunk_slot(ptr noundef %70, i32 noundef 0)
  br label %73

71:                                               ; preds = %60
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  call void @set_large_chunk_shallow(ptr noundef %72, i32 noundef 0)
  br label %73

73:                                               ; preds = %71, %69
  br label %74

74:                                               ; preds = %73, %59
  %75 = load i8, ptr %5, align 1, !tbaa !12, !range !14, !noundef !15
  %76 = trunc i8 %75 to i1
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #2
  ret i1 %76
}

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

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

; Function Attrs: nounwind uwtable
declare hidden void @set_large_chunk_shallow(ptr noundef, i32 noundef) #1

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 = { 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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}


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

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

; Function Attrs: nounwind uwtable
declare hidden void @set_large_chunk_slot(ptr noundef, i32 noundef) local_unnamed_addr #0

; 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: nounwind uwtable
define dso_local zeroext i1 @ponyint_heap_mark(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  tail call void @maybe_clear_chunk(ptr noundef %0)
  %3 = tail call zeroext i1 @get_chunk_is_small_chunk(ptr noundef %0)
  br i1 %3, label %4, label %28

4:                                                ; preds = %2
  %5 = ptrtoint ptr %1 to i64
  %6 = tail call i64 @get_small_chunk_size(ptr noundef %0)
  %7 = trunc i64 %6 to i32
  %.neg = shl i32 -32, %7
  %8 = sext i32 %.neg to i64
  %9 = and i64 %8, %5
  %10 = inttoptr i64 %9 to ptr
  %11 = tail call ptr @get_m(ptr noundef %0)
  %12 = ptrtoint ptr %11 to i64
  %13 = sub i64 %9, %12
  %14 = lshr i64 %13, 5
  %15 = trunc i64 %14 to i32
  %16 = shl nuw i32 1, %15
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load i32, ptr %17, align 8, !tbaa !5
  %19 = and i32 %16, %18
  %20 = icmp eq ptr %10, %1
  %21 = xor i32 %16, -1
  br i1 %20, label %22, label %24

22:                                               ; preds = %4
  %23 = and i32 %18, %21
  store i32 %23, ptr %17, align 8, !tbaa !5
  br label %34

24:                                               ; preds = %4
  %25 = getelementptr inbounds i8, ptr %0, i64 12
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = and i32 %26, %21
  store i32 %27, ptr %25, align 4, !tbaa !5
  br label %34

28:                                               ; preds = %2
  %29 = tail call i32 @get_large_chunk_slot(ptr noundef %0)
  %30 = tail call ptr @get_m(ptr noundef %0)
  %31 = icmp eq ptr %30, %1
  br i1 %31, label %32, label %33

32:                                               ; preds = %28
  tail call void @set_large_chunk_slot(ptr noundef %0, i32 noundef 0)
  br label %34

33:                                               ; preds = %28
  tail call void @set_large_chunk_shallow(ptr noundef %0, i32 noundef 0)
  br label %34

34:                                               ; preds = %22, %24, %32, %33
  %.0.in.in = phi i32 [ %29, %32 ], [ %29, %33 ], [ %19, %24 ], [ %19, %22 ]
  %.0.in = icmp eq i32 %.0.in.in, 0
  ret i1 %.0.in
}

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

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

; Function Attrs: nounwind uwtable
declare hidden void @set_large_chunk_shallow(ptr noundef, i32 noundef) local_unnamed_addr #0

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

!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,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass