; LLVM IR from brewlin/garbage-collect :: gc-golang/heap (heap_common)

; 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.mheap = type { %union.pthread_mutex_t, %struct.mtreap, %struct.mtreap, %struct.array_t, [2 x %struct.gcSweepBuf], %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.mfixalloc, %struct.lineralloc, %struct.array_t, ptr, [1 x %struct.array_t], i64, i32, i32, i32, ptr, i64, i64, i64, [134 x %struct.mcentral] }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%struct.mtreap = type { ptr }
%struct.gcSweepBuf = type { %union.pthread_mutex_t, ptr, i64, i64, i32 }
%struct.mfixalloc = type { i64, ptr, ptr, ptr, i64, i32, i64, i8 }
%struct.lineralloc = type { i64, i64, i64 }
%struct.array_t = type { ptr, i32, i64, i32 }
%struct.mcentral = type { %union.pthread_mutex_t, i8, %struct.mspanlist, %struct.mspanlist, i64 }
%struct.mspanlist = type { ptr, ptr }
%struct.mspan = type { ptr, ptr, ptr, i64, i64, i64, i8, i8, i8, i16, i32, i64, i8, i16, i8, i8, i16, i64, i64, i64, ptr, ptr, i64 }

@heap_ = external global %struct.mheap, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @coalesce_merge(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #0

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

declare i64 @span_released(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare dso_local void @coalesce_realign(ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @heap_coalesce(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store ptr @heap_, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  store i8 0, ptr %4, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = call i64 @span_released(ptr noundef %8)
  store i64 %9, ptr %5, align 8, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.mspan, ptr %10, i32 0, i32 3
  %12 = load i64, ptr %11, align 8, !tbaa !12
  %13 = sub i64 %12, 1
  %14 = call ptr @heap_spanOf(i64 noundef %13)
  store ptr %14, ptr %6, align 8, !tbaa !5
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %41

17:                                               ; preds = %1
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.mspan, ptr %18, i32 0, i32 6
  %20 = load i8, ptr %19, align 8, !tbaa !16
  %21 = zext i8 %20 to i32
  %22 = icmp eq i32 %21, 4
  br i1 %22, label %23, label %41

23:                                               ; preds = %17
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mspan, ptr %24, i32 0, i32 8
  %26 = load i8, ptr %25, align 2, !tbaa !17
  %27 = zext i8 %26 to i32
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mspan, ptr %28, i32 0, i32 8
  %30 = load i8, ptr %29, align 2, !tbaa !17
  %31 = zext i8 %30 to i32
  %32 = icmp eq i32 %27, %31
  br i1 %32, label %33, label %36

33:                                               ; preds = %23
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  call void @coalesce_merge(ptr noundef %34, ptr noundef %35, ptr noundef %4, ptr noundef %5)
  br label %40

36:                                               ; preds = %23
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  call void @coalesce_realign(ptr noundef %37, ptr noundef %38, ptr noundef %39)
  br label %40

40:                                               ; preds = %36, %33
  br label %41

41:                                               ; preds = %40, %17, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.mspan, ptr %42, i32 0, i32 3
  %44 = load i64, ptr %43, align 8, !tbaa !12
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.mspan, ptr %45, i32 0, i32 5
  %47 = load i64, ptr %46, align 8, !tbaa !18
  %48 = mul i64 %47, 8192
  %49 = add i64 %44, %48
  %50 = call ptr @heap_spanOf(i64 noundef %49)
  store ptr %50, ptr %7, align 8, !tbaa !5
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %77

53:                                               ; preds = %41
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.mspan, ptr %54, i32 0, i32 6
  %56 = load i8, ptr %55, align 8, !tbaa !16
  %57 = zext i8 %56 to i32
  %58 = icmp eq i32 %57, 4
  br i1 %58, label %59, label %77

59:                                               ; preds = %53
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.mspan, ptr %60, i32 0, i32 8
  %62 = load i8, ptr %61, align 2, !tbaa !17
  %63 = zext i8 %62 to i32
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.mspan, ptr %64, i32 0, i32 8
  %66 = load i8, ptr %65, align 2, !tbaa !17
  %67 = zext i8 %66 to i32
  %68 = icmp eq i32 %63, %67
  br i1 %68, label %69, label %72

69:                                               ; preds = %59
  %70 = load ptr, ptr %2, align 8, !tbaa !5
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  call void @coalesce_merge(ptr noundef %70, ptr noundef %71, ptr noundef %4, ptr noundef %5)
  br label %76

72:                                               ; preds = %59
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  call void @coalesce_realign(ptr noundef %73, ptr noundef %74, ptr noundef %75)
  br label %76

76:                                               ; preds = %72, %69
  br label %77

77:                                               ; preds = %76, %53, %41
  %78 = load i8, ptr %4, align 1, !tbaa !9
  %79 = icmp ne i8 %78, 0
  br i1 %79, label %80, label %83

80:                                               ; preds = %77
  %81 = load ptr, ptr %2, align 8, !tbaa !5
  %82 = call i64 @span_scavenge(ptr noundef %81)
  br label %83

83:                                               ; preds = %80, %77
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

; Function Attrs: nounwind uwtable
declare dso_local ptr @heap_spanOf(i64 noundef) #0

declare i64 @span_scavenge(ptr noundef) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !11, i64 24}
!13 = !{!"mspan", !6, i64 0, !6, i64 8, !6, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !7, i64 48, !7, i64 49, !7, i64 50, !14, i64 52, !15, i64 56, !11, i64 64, !7, i64 72, !14, i64 74, !7, i64 76, !7, i64 77, !14, i64 78, !11, i64 80, !11, i64 88, !11, i64 96, !6, i64 104, !6, i64 112, !11, i64 120}
!14 = !{!"short", !7, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{!13, !7, i64 48}
!17 = !{!13, !7, i64 50}
!18 = !{!13, !11, i64 40}


; 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 dso_local void @coalesce_merge(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

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

declare i64 @span_released(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
declare dso_local void @coalesce_realign(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @heap_coalesce(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca i8, align 1
  %3 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %2) #3
  store i8 0, ptr %2, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %4 = tail call i64 @span_released(ptr noundef %0) #3
  store i64 %4, ptr %3, align 8, !tbaa !8
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load i64, ptr %5, align 8, !tbaa !10
  %7 = add i64 %6, -1
  %8 = tail call ptr @heap_spanOf(i64 noundef %7)
  %.not = icmp eq ptr %8, null
  br i1 %.not, label %21, label %9

9:                                                ; preds = %1
  %10 = getelementptr inbounds i8, ptr %8, i64 48
  %11 = load i8, ptr %10, align 8, !tbaa !15
  %12 = icmp eq i8 %11, 4
  br i1 %12, label %13, label %21

13:                                               ; preds = %9
  %14 = getelementptr inbounds i8, ptr %0, i64 50
  %15 = load i8, ptr %14, align 2, !tbaa !16
  %16 = getelementptr inbounds i8, ptr %8, i64 50
  %17 = load i8, ptr %16, align 2, !tbaa !16
  %18 = icmp eq i8 %15, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %13
  call void @coalesce_merge(ptr noundef nonnull %0, ptr noundef nonnull %8, ptr noundef nonnull %2, ptr noundef nonnull %3)
  br label %21

20:                                               ; preds = %13
  tail call void @coalesce_realign(ptr noundef nonnull %8, ptr noundef nonnull %0, ptr noundef nonnull %8)
  br label %21

21:                                               ; preds = %19, %20, %9, %1
  %22 = load i64, ptr %5, align 8, !tbaa !10
  %23 = getelementptr inbounds i8, ptr %0, i64 40
  %24 = load i64, ptr %23, align 8, !tbaa !17
  %25 = shl i64 %24, 13
  %26 = add i64 %25, %22
  %27 = call ptr @heap_spanOf(i64 noundef %26)
  %.not24 = icmp eq ptr %27, null
  br i1 %.not24, label %40, label %28

28:                                               ; preds = %21
  %29 = getelementptr inbounds i8, ptr %27, i64 48
  %30 = load i8, ptr %29, align 8, !tbaa !15
  %31 = icmp eq i8 %30, 4
  br i1 %31, label %32, label %40

32:                                               ; preds = %28
  %33 = getelementptr inbounds i8, ptr %0, i64 50
  %34 = load i8, ptr %33, align 2, !tbaa !16
  %35 = getelementptr inbounds i8, ptr %27, i64 50
  %36 = load i8, ptr %35, align 2, !tbaa !16
  %37 = icmp eq i8 %34, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %32
  call void @coalesce_merge(ptr noundef nonnull %0, ptr noundef nonnull %27, ptr noundef nonnull %2, ptr noundef nonnull %3)
  br label %40

39:                                               ; preds = %32
  call void @coalesce_realign(ptr noundef nonnull %0, ptr noundef nonnull %27, ptr noundef nonnull %27)
  br label %40

40:                                               ; preds = %38, %39, %28, %21
  %41 = load i8, ptr %2, align 1, !tbaa !5
  %.not25 = icmp eq i8 %41, 0
  br i1 %.not25, label %44, label %42

42:                                               ; preds = %40
  %43 = call i64 @span_scavenge(ptr noundef nonnull %0) #3
  br label %44

44:                                               ; preds = %42, %40
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %2) #3
  ret void
}

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

declare i64 @span_scavenge(ptr noundef) local_unnamed_addr #2

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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"}
!8 = !{!9, !9, i64 0}
!9 = !{!"long", !6, i64 0}
!10 = !{!11, !9, i64 24}
!11 = !{!"mspan", !12, i64 0, !12, i64 8, !12, i64 16, !9, i64 24, !9, i64 32, !9, i64 40, !6, i64 48, !6, i64 49, !6, i64 50, !13, i64 52, !14, i64 56, !9, i64 64, !6, i64 72, !13, i64 74, !6, i64 76, !6, i64 77, !13, i64 78, !9, i64 80, !9, i64 88, !9, i64 96, !12, i64 104, !12, i64 112, !9, i64 120}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!"short", !6, i64 0}
!14 = !{!"int", !6, i64 0}
!15 = !{!11, !6, i64 48}
!16 = !{!11, !6, i64 50}
!17 = !{!11, !9, i64 40}


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