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

; 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
@.str.2 = external hidden unnamed_addr constant [32 x i8], align 1

declare i32 @atomic_Xadd(ptr noundef, i32 noundef) #0

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

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

declare void @atomic_Store(ptr noundef, i32 noundef) #0

declare i32 @printf(ptr noundef, ...) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @sweepone() #2 {
  %1 = alloca i64, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %8 = call ptr (...) @getg()
  store ptr %8, ptr %2, align 8, !tbaa !5
  %9 = load i32, ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 17), align 4, !tbaa !9
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %12

11:                                               ; preds = %0
  store i64 -1, ptr %1, align 8
  store i32 1, ptr %3, align 4
  br label %84

12:                                               ; preds = %0
  %13 = call i32 @atomic_Xadd(ptr noundef getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 18), i32 noundef 1)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %14 = load i32, ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 16), align 8, !tbaa !17
  store i32 %14, ptr %5, align 4, !tbaa !18
  br label %15

15:                                               ; preds = %67, %48, %12
  br label %16

16:                                               ; preds = %15
  %17 = load i32, ptr %5, align 4, !tbaa !18
  %18 = udiv i32 %17, 2
  %19 = urem i32 %18, 2
  %20 = sub i32 1, %19
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds [2 x %struct.gcSweepBuf], ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 4), i64 0, i64 %21
  %23 = call ptr @gcsweepbuf_pop(ptr noundef %22)
  store ptr %23, ptr %4, align 8, !tbaa !5
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = icmp eq ptr %24, null
  br i1 %25, label %26, label %27

26:                                               ; preds = %16
  call void @atomic_Store(ptr noundef getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 17), i32 noundef 1)
  br label %68

27:                                               ; preds = %16
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mspan, ptr %28, i32 0, i32 6
  %30 = load i8, ptr %29, align 8, !tbaa !19
  %31 = zext i8 %30 to i32
  %32 = icmp ne i32 %31, 2
  br i1 %32, label %33, label %49

33:                                               ; preds = %27
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.mspan, ptr %34, i32 0, i32 10
  %36 = load i32, ptr %35, align 8, !tbaa !22
  %37 = load i32, ptr %5, align 4, !tbaa !18
  %38 = icmp eq i32 %36, %37
  br i1 %38, label %48, label %39

39:                                               ; preds = %33
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.mspan, ptr %40, i32 0, i32 10
  %42 = load i32, ptr %41, align 8, !tbaa !22
  %43 = load i32, ptr %5, align 4, !tbaa !18
  %44 = add i32 %43, 3
  %45 = icmp eq i32 %42, %44
  br i1 %45, label %48, label %46

46:                                               ; preds = %39
  %47 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 poison, ptr %6, align 4, !tbaa !18
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %48

48:                                               ; preds = %46, %39, %33
  br label %15

49:                                               ; preds = %27
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.mspan, ptr %50, i32 0, i32 10
  %52 = load i32, ptr %51, align 8, !tbaa !22
  %53 = load i32, ptr %5, align 4, !tbaa !18
  %54 = sub i32 %53, 2
  %55 = icmp eq i32 %52, %54
  br i1 %55, label %56, label %67

56:                                               ; preds = %49
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.mspan, ptr %57, i32 0, i32 10
  %59 = load i32, ptr %5, align 4, !tbaa !18
  %60 = sub i32 %59, 2
  %61 = load i32, ptr %5, align 4, !tbaa !18
  %62 = sub i32 %61, 1
  %63 = call zeroext i8 @atomic_Cas(ptr noundef %58, i32 noundef %60, i32 noundef %62)
  %64 = zext i8 %63 to i32
  %65 = icmp ne i32 %64, 0
  br i1 %65, label %66, label %67

66:                                               ; preds = %56
  br label %68

67:                                               ; preds = %56, %49
  br label %15

68:                                               ; preds = %66, %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 -1, ptr %7, align 8, !tbaa !23
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = icmp ne ptr %69, null
  br i1 %70, label %71, label %81

71:                                               ; preds = %68
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.mspan, ptr %72, i32 0, i32 5
  %74 = load i64, ptr %73, align 8, !tbaa !24
  store i64 %74, ptr %7, align 8, !tbaa !23
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = call zeroext i8 @span_sweep(ptr noundef %75, i8 noundef zeroext 0)
  %77 = icmp ne i8 %76, 0
  br i1 %77, label %78, label %79

78:                                               ; preds = %71
  br label %80

79:                                               ; preds = %71
  store i64 0, ptr %7, align 8, !tbaa !23
  br label %80

80:                                               ; preds = %79, %78
  br label %81

81:                                               ; preds = %80, %68
  %82 = call i32 @atomic_Xadd(ptr noundef getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 18), i32 noundef -1)
  %83 = load i64, ptr %7, align 8, !tbaa !23
  store i64 %83, ptr %1, align 8
  store i32 1, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %84

84:                                               ; preds = %81, %11
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  %85 = load i64, ptr %1, align 8
  ret i64 %85
}

declare ptr @getg(...) #0

declare ptr @gcsweepbuf_pop(ptr noundef) #0

declare zeroext i8 @atomic_Cas(ptr noundef, i32 noundef, i32 noundef) #0

declare zeroext i8 @span_sweep(ptr noundef, i8 noundef zeroext) #0

attributes #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 = { 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 #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, !13, i64 724}
!10 = !{!"mheap", !7, i64 0, !11, i64 40, !11, i64 48, !12, i64 56, !7, i64 88, !15, i64 232, !15, i64 296, !15, i64 360, !15, i64 424, !15, i64 488, !15, i64 552, !16, i64 616, !12, i64 640, !6, i64 672, !7, i64 680, !14, i64 712, !13, i64 720, !13, i64 724, !13, i64 728, !6, i64 736, !14, i64 744, !14, i64 752, !14, i64 760, !7, i64 768}
!11 = !{!"mtreap", !6, i64 0}
!12 = !{!"", !6, i64 0, !13, i64 8, !14, i64 16, !13, i64 24}
!13 = !{!"int", !7, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!"mfixalloc", !14, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !14, i64 32, !13, i64 40, !14, i64 48, !7, i64 56}
!16 = !{!"", !14, i64 0, !14, i64 8, !14, i64 16}
!17 = !{!10, !13, i64 720}
!18 = !{!13, !13, i64 0}
!19 = !{!20, !7, i64 48}
!20 = !{!"mspan", !6, i64 0, !6, i64 8, !6, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !7, i64 48, !7, i64 49, !7, i64 50, !21, i64 52, !13, i64 56, !14, i64 64, !7, i64 72, !21, i64 74, !7, i64 76, !7, i64 77, !21, i64 78, !14, i64 80, !14, i64 88, !14, i64 96, !6, i64 104, !6, i64 112, !14, i64 120}
!21 = !{!"short", !7, i64 0}
!22 = !{!20, !13, i64 56}
!23 = !{!14, !14, i64 0}
!24 = !{!20, !14, 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
@.str.2 = external hidden unnamed_addr constant [32 x i8], align 1

declare i32 @atomic_Xadd(ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @atomic_Store(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @sweepone() local_unnamed_addr #2 {
  %1 = tail call ptr (...) @getg() #3
  %2 = load i32, ptr getelementptr inbounds (i8, ptr @heap_, i64 724), align 4, !tbaa !5
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %3, label %37

3:                                                ; preds = %0
  %4 = tail call i32 @atomic_Xadd(ptr noundef nonnull getelementptr inbounds (i8, ptr @heap_, i64 728), i32 noundef 1) #3
  %5 = load i32, ptr getelementptr inbounds (i8, ptr @heap_, i64 720), align 8, !tbaa !16
  %.mask = lshr i32 %5, 1
  %.mask.lobit = and i32 %.mask, 1
  %6 = xor i32 %.mask.lobit, 1
  %7 = zext nneg i32 %6 to i64
  %8 = getelementptr inbounds [2 x %struct.gcSweepBuf], ptr getelementptr inbounds (i8, ptr @heap_, i64 88), i64 0, i64 %7
  %9 = tail call ptr @gcsweepbuf_pop(ptr noundef nonnull %8) #3
  %10 = icmp eq ptr %9, null
  br i1 %10, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %11 = add i32 %5, 3
  %12 = add i32 %5, -2
  %13 = add i32 %5, -1
  br label %14

.thread:                                          ; preds = %.backedge, %3
  tail call void @atomic_Store(ptr noundef nonnull getelementptr inbounds (i8, ptr @heap_, i64 724), i32 noundef 1) #3
  br label %35

14:                                               ; preds = %.lr.ph, %.backedge
  %15 = phi ptr [ %9, %.lr.ph ], [ %25, %.backedge ]
  %16 = getelementptr inbounds i8, ptr %15, i64 48
  %17 = load i8, ptr %16, align 8, !tbaa !17
  %.not21 = icmp eq i8 %17, 2
  %18 = getelementptr inbounds i8, ptr %15, i64 56
  %19 = load i32, ptr %18, align 8, !tbaa !20
  br i1 %.not21, label %27, label %20

20:                                               ; preds = %14
  %21 = icmp eq i32 %19, %5
  %22 = icmp eq i32 %19, %11
  %or.cond = or i1 %21, %22
  br i1 %or.cond, label %.backedge, label %23

23:                                               ; preds = %20
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  br label %.backedge

.backedge:                                        ; preds = %27, %29, %20, %23
  %25 = tail call ptr @gcsweepbuf_pop(ptr noundef nonnull %8) #3
  %26 = icmp eq ptr %25, null
  br i1 %26, label %.thread, label %14

27:                                               ; preds = %14
  %28 = icmp eq i32 %19, %12
  br i1 %28, label %29, label %.backedge

29:                                               ; preds = %27
  %30 = tail call zeroext i8 @atomic_Cas(ptr noundef nonnull %18, i32 noundef %12, i32 noundef %13) #3
  %.not22 = icmp eq i8 %30, 0
  br i1 %.not22, label %.backedge, label %31

31:                                               ; preds = %29
  %32 = getelementptr inbounds i8, ptr %15, i64 40
  %33 = load i64, ptr %32, align 8, !tbaa !21
  %34 = tail call zeroext i8 @span_sweep(ptr noundef nonnull %15, i8 noundef zeroext 0) #3
  %.not24 = icmp eq i8 %34, 0
  %spec.select = select i1 %.not24, i64 0, i64 %33
  br label %35

35:                                               ; preds = %.thread, %31
  %.0 = phi i64 [ %spec.select, %31 ], [ -1, %.thread ]
  %36 = tail call i32 @atomic_Xadd(ptr noundef nonnull getelementptr inbounds (i8, ptr @heap_, i64 728), i32 noundef -1) #3
  br label %37

37:                                               ; preds = %0, %35
  %.016 = phi i64 [ %.0, %35 ], [ -1, %0 ]
  ret i64 %.016
}

declare ptr @getg(...) local_unnamed_addr #0

declare ptr @gcsweepbuf_pop(ptr noundef) local_unnamed_addr #0

declare zeroext i8 @atomic_Cas(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

declare zeroext i8 @span_sweep(ptr noundef, i8 noundef zeroext) local_unnamed_addr #0

attributes #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 = { nofree nounwind "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 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 #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, !12, i64 724}
!6 = !{!"mheap", !7, i64 0, !9, i64 40, !9, i64 48, !11, i64 56, !7, i64 88, !14, i64 232, !14, i64 296, !14, i64 360, !14, i64 424, !14, i64 488, !14, i64 552, !15, i64 616, !11, i64 640, !10, i64 672, !7, i64 680, !13, i64 712, !12, i64 720, !12, i64 724, !12, i64 728, !10, i64 736, !13, i64 744, !13, i64 752, !13, i64 760, !7, i64 768}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"mtreap", !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!"", !10, i64 0, !12, i64 8, !13, i64 16, !12, i64 24}
!12 = !{!"int", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"mfixalloc", !13, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !13, i64 32, !12, i64 40, !13, i64 48, !7, i64 56}
!15 = !{!"", !13, i64 0, !13, i64 8, !13, i64 16}
!16 = !{!6, !12, i64 720}
!17 = !{!18, !7, i64 48}
!18 = !{!"mspan", !10, i64 0, !10, i64 8, !10, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !7, i64 48, !7, i64 49, !7, i64 50, !19, i64 52, !12, i64 56, !13, i64 64, !7, i64 72, !19, i64 74, !7, i64 76, !7, i64 77, !19, i64 78, !13, i64 80, !13, i64 88, !13, i64 96, !10, i64 104, !10, i64 112, !13, i64 120}
!19 = !{!"short", !7, i64 0}
!20 = !{!18, !12, i64 56}
!21 = !{!18, !13, i64 40}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass