; 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
define dso_local void @coalesce_merge(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store ptr @heap_, ptr %9, align 8, !tbaa !5
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.mspan, ptr %10, i32 0, i32 5
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.mspan, ptr %13, i32 0, i32 5
  %15 = load i64, ptr %14, align 8, !tbaa !9
  %16 = add i64 %15, %12
  store i64 %16, ptr %14, align 8, !tbaa !9
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mspan, ptr %17, i32 0, i32 7
  %19 = load i8, ptr %18, align 1, !tbaa !14
  %20 = zext i8 %19 to i32
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mspan, ptr %21, i32 0, i32 7
  %23 = load i8, ptr %22, align 1, !tbaa !14
  %24 = zext i8 %23 to i32
  %25 = or i32 %24, %20
  %26 = trunc i32 %25 to i8
  store i8 %26, ptr %22, align 1, !tbaa !14
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mspan, ptr %27, i32 0, i32 3
  %29 = load i64, ptr %28, align 8, !tbaa !15
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.mspan, ptr %30, i32 0, i32 3
  %32 = load i64, ptr %31, align 8, !tbaa !15
  %33 = icmp ult i64 %29, %32
  br i1 %33, label %34, label %44

34:                                               ; preds = %4
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.mspan, ptr %35, i32 0, i32 3
  %37 = load i64, ptr %36, align 8, !tbaa !15
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.mspan, ptr %38, i32 0, i32 3
  store i64 %37, ptr %39, align 8, !tbaa !15
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.mspan, ptr %40, i32 0, i32 3
  %42 = load i64, ptr %41, align 8, !tbaa !15
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  call void @heap_setSpan(i64 noundef %42, ptr noundef %43)
  br label %55

44:                                               ; preds = %4
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.mspan, ptr %45, i32 0, i32 3
  %47 = load i64, ptr %46, align 8, !tbaa !15
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.mspan, ptr %48, i32 0, i32 5
  %50 = load i64, ptr %49, align 8, !tbaa !9
  %51 = mul i64 %50, 8192
  %52 = add i64 %47, %51
  %53 = sub i64 %52, 1
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  call void @heap_setSpan(i64 noundef %53, ptr noundef %54)
  br label %55

55:                                               ; preds = %44, %34
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load i8, ptr %56, align 1, !tbaa !16
  %58 = zext i8 %57 to i32
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %72, label %60

60:                                               ; preds = %55
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.mspan, ptr %61, i32 0, i32 8
  %63 = load i8, ptr %62, align 2, !tbaa !17
  %64 = zext i8 %63 to i32
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %72, label %66

66:                                               ; preds = %60
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.mspan, ptr %67, i32 0, i32 8
  %69 = load i8, ptr %68, align 2, !tbaa !17
  %70 = zext i8 %69 to i32
  %71 = icmp ne i32 %70, 0
  br label %72

72:                                               ; preds = %66, %60, %55
  %73 = phi i1 [ true, %60 ], [ true, %55 ], [ %71, %66 ]
  %74 = zext i1 %73 to i32
  %75 = trunc i32 %74 to i8
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 %75, ptr %76, align 1, !tbaa !16
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = call i64 @span_released(ptr noundef %77)
  %79 = load ptr, ptr %8, align 8, !tbaa !5
  %80 = load i64, ptr %79, align 8, !tbaa !18
  %81 = add i64 %80, %78
  store i64 %81, ptr %79, align 8, !tbaa !18
  %82 = load ptr, ptr %6, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.mspan, ptr %82, i32 0, i32 8
  %84 = load i8, ptr %83, align 2, !tbaa !17
  %85 = icmp ne i8 %84, 0
  br i1 %85, label %86, label %90

86:                                               ; preds = %72
  %87 = load ptr, ptr %9, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.mheap, ptr %87, i32 0, i32 2
  %89 = load ptr, ptr %6, align 8, !tbaa !5
  call void @treap_removeSpan(ptr noundef %88, ptr noundef %89)
  br label %94

90:                                               ; preds = %72
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.mheap, ptr %91, i32 0, i32 1
  %93 = load ptr, ptr %6, align 8, !tbaa !5
  call void @treap_removeSpan(ptr noundef %92, ptr noundef %93)
  br label %94

94:                                               ; preds = %90, %86
  %95 = load ptr, ptr %6, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.mspan, ptr %95, i32 0, i32 6
  store i8 1, ptr %96, align 8, !tbaa !19
  %97 = load ptr, ptr %9, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.mheap, ptr %97, i32 0, i32 5
  %99 = load ptr, ptr %6, align 8, !tbaa !5
  call void @fixalloc_free(ptr noundef %98, ptr noundef %99)
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret void
}

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

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

declare i64 @span_released(ptr noundef) #2

declare void @treap_removeSpan(ptr noundef, ptr noundef) #2

declare void @fixalloc_free(ptr noundef, ptr noundef) #2

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

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 = !{!10, !11, i64 40}
!10 = !{!"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, !12, i64 52, !13, i64 56, !11, i64 64, !7, i64 72, !12, i64 74, !7, i64 76, !7, i64 77, !12, i64 78, !11, i64 80, !11, i64 88, !11, i64 96, !6, i64 104, !6, i64 112, !11, i64 120}
!11 = !{!"long", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!10, !7, i64 49}
!15 = !{!10, !11, i64 24}
!16 = !{!7, !7, i64 0}
!17 = !{!10, !7, i64 50}
!18 = !{!11, !11, i64 0}
!19 = !{!10, !7, i64 48}


; 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.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 }

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

; Function Attrs: nounwind uwtable
define dso_local void @coalesce_merge(ptr noundef %0, ptr noundef %1, ptr nocapture noundef %2, ptr nocapture noundef %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %1, i64 40
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 40
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = add i64 %8, %6
  store i64 %9, ptr %7, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %1, i64 49
  %11 = load i8, ptr %10, align 1, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %0, i64 49
  %13 = load i8, ptr %12, align 1, !tbaa !13
  %14 = or i8 %13, %11
  store i8 %14, ptr %12, align 1, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %1, i64 24
  %16 = load i64, ptr %15, align 8, !tbaa !14
  %17 = getelementptr inbounds i8, ptr %0, i64 24
  %18 = load i64, ptr %17, align 8, !tbaa !14
  %19 = icmp ult i64 %16, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %4
  store i64 %16, ptr %17, align 8, !tbaa !14
  br label %25

21:                                               ; preds = %4
  %22 = shl i64 %9, 13
  %23 = add i64 %22, -1
  %24 = add i64 %23, %18
  br label %25

25:                                               ; preds = %21, %20
  %.sink = phi i64 [ %24, %21 ], [ %16, %20 ]
  tail call void @heap_setSpan(i64 noundef %.sink, ptr noundef nonnull %0)
  %26 = load i8, ptr %2, align 1, !tbaa !15
  %.not = icmp eq i8 %26, 0
  br i1 %.not, label %27, label %35

27:                                               ; preds = %25
  %28 = getelementptr inbounds i8, ptr %1, i64 50
  %29 = load i8, ptr %28, align 2, !tbaa !16
  %.not28 = icmp eq i8 %29, 0
  br i1 %.not28, label %30, label %35

30:                                               ; preds = %27
  %31 = getelementptr inbounds i8, ptr %0, i64 50
  %32 = load i8, ptr %31, align 2, !tbaa !16
  %33 = icmp ne i8 %32, 0
  %34 = zext i1 %33 to i8
  br label %35

35:                                               ; preds = %30, %27, %25
  %36 = phi i8 [ 1, %27 ], [ 1, %25 ], [ %34, %30 ]
  store i8 %36, ptr %2, align 1, !tbaa !15
  %37 = tail call i64 @span_released(ptr noundef nonnull %1) #2
  %38 = load i64, ptr %3, align 8, !tbaa !17
  %39 = add i64 %38, %37
  store i64 %39, ptr %3, align 8, !tbaa !17
  %40 = getelementptr inbounds i8, ptr %1, i64 50
  %41 = load i8, ptr %40, align 2, !tbaa !16
  %.not29 = icmp eq i8 %41, 0
  %. = select i1 %.not29, ptr getelementptr inbounds (i8, ptr @heap_, i64 40), ptr getelementptr inbounds (i8, ptr @heap_, i64 48)
  tail call void @treap_removeSpan(ptr noundef nonnull %., ptr noundef nonnull %1) #2
  %42 = getelementptr inbounds i8, ptr %1, i64 48
  store i8 1, ptr %42, align 8, !tbaa !18
  tail call void @fixalloc_free(ptr noundef nonnull getelementptr inbounds (i8, ptr @heap_, i64 232), ptr noundef nonnull %1) #2
  ret void
}

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

declare i64 @span_released(ptr noundef) local_unnamed_addr #1

declare void @treap_removeSpan(ptr noundef, ptr noundef) local_unnamed_addr #1

declare void @fixalloc_free(ptr noundef, ptr noundef) local_unnamed_addr #1

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 = { 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, !10, i64 40}
!6 = !{!"mspan", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !8, i64 48, !8, i64 49, !8, i64 50, !11, i64 52, !12, i64 56, !10, i64 64, !8, i64 72, !11, i64 74, !8, i64 76, !8, i64 77, !11, i64 78, !10, i64 80, !10, i64 88, !10, i64 96, !7, i64 104, !7, i64 112, !10, i64 120}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !8, i64 49}
!14 = !{!6, !10, i64 24}
!15 = !{!8, !8, i64 0}
!16 = !{!6, !8, i64 50}
!17 = !{!10, !10, i64 0}
!18 = !{!6, !8, i64 48}


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