; 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
@physPageSize = external global i64, align 8

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

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

declare void @treap_removeSpan(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) #0

; Function Attrs: nounwind uwtable
define dso_local void @coalesce_realign(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store ptr @heap_, ptr %7, align 8, !tbaa !5
  %10 = load i64, ptr @physPageSize, align 8, !tbaa !9
  %11 = icmp ule i64 8192, %10
  br i1 %11, label %12, label %13

12:                                               ; preds = %3
  store i32 1, ptr %8, align 4
  br label %92

13:                                               ; preds = %3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.mspan, ptr %14, i32 0, i32 8
  %16 = load i8, ptr %15, align 2, !tbaa !11
  %17 = icmp ne i8 %16, 0
  br i1 %17, label %18, label %22

18:                                               ; preds = %13
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mheap, ptr %19, i32 0, i32 2
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  call void @treap_removeSpan(ptr noundef %20, ptr noundef %21)
  br label %26

22:                                               ; preds = %13
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mheap, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  call void @treap_removeSpan(ptr noundef %24, ptr noundef %25)
  br label %26

26:                                               ; preds = %22, %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.mspan, ptr %27, i32 0, i32 3
  %29 = load i64, ptr %28, align 8, !tbaa !15
  store i64 %29, ptr %9, align 8, !tbaa !9
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.mspan, ptr %30, i32 0, i32 8
  %32 = load i8, ptr %31, align 2, !tbaa !11
  %33 = icmp ne i8 %32, 0
  br i1 %33, label %34, label %40

34:                                               ; preds = %26
  %35 = load i64, ptr %9, align 8, !tbaa !9
  %36 = load i64, ptr @physPageSize, align 8, !tbaa !9
  %37 = sub i64 %36, 1
  %38 = xor i64 %37, -1
  %39 = and i64 %35, %38
  store i64 %39, ptr %9, align 8, !tbaa !9
  br label %49

40:                                               ; preds = %26
  %41 = load i64, ptr %9, align 8, !tbaa !9
  %42 = load i64, ptr @physPageSize, align 8, !tbaa !9
  %43 = add i64 %41, %42
  %44 = sub i64 %43, 1
  %45 = load i64, ptr @physPageSize, align 8, !tbaa !9
  %46 = sub i64 %45, 1
  %47 = xor i64 %46, -1
  %48 = and i64 %44, %47
  store i64 %48, ptr %9, align 8, !tbaa !9
  br label %49

49:                                               ; preds = %40, %34
  %50 = load i64, ptr %9, align 8, !tbaa !9
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.mspan, ptr %51, i32 0, i32 3
  %53 = load i64, ptr %52, align 8, !tbaa !15
  %54 = sub i64 %50, %53
  %55 = udiv i64 %54, 8192
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mspan, ptr %56, i32 0, i32 5
  store i64 %55, ptr %57, align 8, !tbaa !16
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.mspan, ptr %58, i32 0, i32 3
  %60 = load i64, ptr %59, align 8, !tbaa !15
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.mspan, ptr %61, i32 0, i32 5
  %63 = load i64, ptr %62, align 8, !tbaa !16
  %64 = mul i64 %63, 8192
  %65 = add i64 %60, %64
  %66 = load i64, ptr %9, align 8, !tbaa !9
  %67 = sub i64 %65, %66
  %68 = udiv i64 %67, 8192
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.mspan, ptr %69, i32 0, i32 5
  store i64 %68, ptr %70, align 8, !tbaa !16
  %71 = load i64, ptr %9, align 8, !tbaa !9
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.mspan, ptr %72, i32 0, i32 3
  store i64 %71, ptr %73, align 8, !tbaa !15
  %74 = load i64, ptr %9, align 8, !tbaa !9
  %75 = sub i64 %74, 1
  %76 = load ptr, ptr %4, align 8, !tbaa !5
  call void @heap_setSpan(i64 noundef %75, ptr noundef %76)
  %77 = load i64, ptr %9, align 8, !tbaa !9
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  call void @heap_setSpan(i64 noundef %77, ptr noundef %78)
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.mspan, ptr %79, i32 0, i32 8
  %81 = load i8, ptr %80, align 2, !tbaa !11
  %82 = icmp ne i8 %81, 0
  br i1 %82, label %83, label %87

83:                                               ; preds = %49
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.mheap, ptr %84, i32 0, i32 2
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  call void @treap_insert(ptr noundef %85, ptr noundef %86)
  br label %91

87:                                               ; preds = %49
  %88 = load ptr, ptr %7, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.mheap, ptr %88, i32 0, i32 1
  %90 = load ptr, ptr %6, align 8, !tbaa !5
  call void @treap_insert(ptr noundef %89, ptr noundef %90)
  br label %91

91:                                               ; preds = %87, %83
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  store i32 0, ptr %8, align 4
  br label %92

92:                                               ; preds = %91, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %93 = load i32, ptr %8, align 4
  switch i32 %93, label %95 [
    i32 0, label %94
    i32 1, label %94
  ]

94:                                               ; preds = %92, %92
  ret void

95:                                               ; preds = %92
  unreachable
}

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

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

%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
@physPageSize = external local_unnamed_addr global i64, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local void @coalesce_realign(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = load i64, ptr @physPageSize, align 8, !tbaa !5
  %5 = icmp ugt i64 %4, 8191
  br i1 %5, label %30, label %.sink.split

.sink.split:                                      ; preds = %3
  %6 = getelementptr inbounds i8, ptr %2, i64 50
  %7 = load i8, ptr %6, align 2, !tbaa !9
  %.not = icmp eq i8 %7, 0
  %. = select i1 %.not, 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 %2) #2
  %8 = getelementptr inbounds i8, ptr %1, i64 24
  %9 = load i64, ptr %8, align 8, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %0, i64 50
  %11 = load i8, ptr %10, align 2, !tbaa !9
  %.not26 = icmp eq i8 %11, 0
  %12 = load i64, ptr @physPageSize, align 8
  %13 = add i64 %9, -1
  %14 = add i64 %13, %12
  %.sink28 = select i1 %.not26, i64 %14, i64 %9
  %15 = sub i64 0, %12
  %16 = and i64 %.sink28, %15
  %17 = getelementptr inbounds i8, ptr %0, i64 24
  %18 = load i64, ptr %17, align 8, !tbaa !14
  %19 = sub i64 %16, %18
  %20 = lshr i64 %19, 13
  %21 = getelementptr inbounds i8, ptr %0, i64 40
  store i64 %20, ptr %21, align 8, !tbaa !15
  %22 = getelementptr inbounds i8, ptr %1, i64 40
  %23 = load i64, ptr %22, align 8, !tbaa !15
  %24 = sub i64 %9, %16
  %25 = lshr i64 %24, 13
  %26 = add i64 %25, %23
  %27 = and i64 %26, 2251799813685247
  store i64 %27, ptr %22, align 8, !tbaa !15
  store i64 %16, ptr %8, align 8, !tbaa !14
  %28 = add i64 %16, -1
  tail call void @heap_setSpan(i64 noundef %28, ptr noundef nonnull %0)
  tail call void @heap_setSpan(i64 noundef %16, ptr noundef nonnull %1)
  %29 = load i8, ptr %6, align 2, !tbaa !9
  %.not27 = icmp eq i8 %29, 0
  %.sink31 = select i1 %.not27, ptr getelementptr inbounds (i8, ptr @heap_, i64 40), ptr getelementptr inbounds (i8, ptr @heap_, i64 48)
  tail call void @treap_insert(ptr noundef nonnull %.sink31, ptr noundef nonnull %2) #2
  br label %30

30:                                               ; preds = %.sink.split, %3
  ret void
}

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


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