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

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

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_heap_mark_shallow(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  call void @maybe_clear_chunk(ptr noundef %7)
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call zeroext i1 @get_chunk_is_small_chunk(ptr noundef %8)
  br i1 %9, label %10, label %38

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

38:                                               ; preds = %2
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  call void @set_large_chunk_shallow(ptr noundef %39, i32 noundef 0)
  br label %40

40:                                               ; preds = %38, %10
  ret void
}

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}


; 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 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
declare hidden void @maybe_clear_chunk(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

; Function Attrs: nounwind uwtable
define dso_local void @ponyint_heap_mark_shallow(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 %20

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 = tail call ptr @get_m(ptr noundef %0)
  %11 = ptrtoint ptr %10 to i64
  %12 = sub i64 %9, %11
  %13 = lshr i64 %12, 5
  %14 = trunc i64 %13 to i32
  %15 = shl nuw i32 1, %14
  %16 = xor i32 %15, -1
  %17 = getelementptr inbounds i8, ptr %0, i64 12
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = and i32 %18, %16
  store i32 %19, ptr %17, align 4, !tbaa !5
  br label %21

20:                                               ; preds = %2
  tail call void @set_large_chunk_shallow(ptr noundef %0, i32 noundef 0)
  br label %21

21:                                               ; preds = %20, %4
  ret void
}

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,TailCallElimPass