; 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._work = type { i64, i64, i8, i32, %struct.workerSpans, i32 }
%struct.workerSpans = type { %union.pthread_mutex_t, %struct.mspanlist, %struct.mspanlist }
%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.mspanlist = type { ptr, ptr }
%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] }
%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 }

@gcphase = external global i32, align 4
@work = external global %struct._work, align 8
@.str = external hidden unnamed_addr constant [42 x i8], align 1
@heap_ = external global %struct.mheap, align 8
@.str.1 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) #0

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr 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

; Function Attrs: nounwind uwtable
define dso_local void @gcSweep() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = load i32, ptr @gcphase, align 4, !tbaa !5
  %4 = icmp ne i32 %3, 1
  br i1 %4, label %5, label %7

5:                                                ; preds = %0
  %6 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 poison, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  br label %7

7:                                                ; preds = %5, %0
  %8 = call i32 @pthread_mutex_lock(ptr noundef @heap_) #4
  %9 = load i32, ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 16), align 8, !tbaa !9
  %10 = add i32 %9, 2
  store i32 %10, ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 16), align 8, !tbaa !9
  store i32 0, ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 17), align 4, !tbaa !17
  %11 = load i32, ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 16), align 8, !tbaa !9
  %12 = udiv i32 %11, 2
  %13 = urem i32 %12, 2
  %14 = zext i32 %13 to i64
  %15 = getelementptr inbounds [2 x %struct.gcSweepBuf], ptr getelementptr inbounds (%struct.mheap, ptr @heap_, i32 0, i32 4), i64 0, i64 %14
  %16 = getelementptr inbounds %struct.gcSweepBuf, ptr %15, i32 0, i32 4
  %17 = load i32, ptr %16, align 8, !tbaa !18
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %19, label %21

19:                                               ; preds = %7
  %20 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 poison, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %21

21:                                               ; preds = %19, %7
  %22 = call i32 @pthread_mutex_unlock(ptr noundef @heap_) #4
  %23 = load i8, ptr getelementptr inbounds (%struct._work, ptr @work, i32 0, i32 2), align 8, !tbaa !20
  %24 = icmp ne i8 %23, 0
  br i1 %24, label %25, label %31

25:                                               ; preds = %21
  br label %26

26:                                               ; preds = %29, %25
  %27 = call i64 @sweepone()
  %28 = icmp ne i64 %27, -1
  br i1 %28, label %29, label %30

29:                                               ; preds = %26
  br label %26, !llvm.loop !24

30:                                               ; preds = %26
  br label %31

31:                                               ; preds = %30, %21
  ret void
}

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @sweepone() #2

attributes #0 = { 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 #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 = { "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 #4 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 720}
!10 = !{!"mheap", !7, i64 0, !11, i64 40, !11, i64 48, !13, 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, !13, i64 640, !12, i64 672, !7, i64 680, !14, i64 712, !6, i64 720, !6, i64 724, !6, i64 728, !12, i64 736, !14, i64 744, !14, i64 752, !14, i64 760, !7, i64 768}
!11 = !{!"mtreap", !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"", !12, i64 0, !6, i64 8, !14, i64 16, !6, i64 24}
!14 = !{!"long", !7, i64 0}
!15 = !{!"mfixalloc", !14, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !14, i64 32, !6, i64 40, !14, i64 48, !7, i64 56}
!16 = !{!"", !14, i64 0, !14, i64 8, !14, i64 16}
!17 = !{!10, !6, i64 724}
!18 = !{!19, !6, i64 64}
!19 = !{!"", !7, i64 0, !12, i64 40, !14, i64 48, !14, i64 56, !6, i64 64}
!20 = !{!21, !7, i64 16}
!21 = !{!"", !14, i64 0, !14, i64 8, !7, i64 16, !6, i64 20, !22, i64 24, !6, i64 96}
!22 = !{!"", !7, i64 0, !23, i64 40, !23, i64 56}
!23 = !{!"mspanlist", !12, i64 0, !12, i64 8}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}


; 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._work = type { i64, i64, i8, i32, %struct.workerSpans, i32 }
%struct.workerSpans = type { %union.pthread_mutex_t, %struct.mspanlist, %struct.mspanlist }
%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.mspanlist = type { ptr, ptr }
%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] }
%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 }

@gcphase = external local_unnamed_addr global i32, align 4
@work = external local_unnamed_addr global %struct._work, align 8
@.str = external hidden unnamed_addr constant [42 x i8], align 1
@heap_ = external global %struct.mheap, align 8
@.str.1 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: nounwind
declare i32 @pthread_mutex_lock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i32 @pthread_mutex_unlock(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @gcSweep() local_unnamed_addr #1 {
  %1 = load i32, ptr @gcphase, align 4, !tbaa !5
  %.not = icmp eq i32 %1, 1
  br i1 %.not, label %4, label %2

2:                                                ; preds = %0
  %3 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  br label %4

4:                                                ; preds = %2, %0
  %5 = tail call i32 @pthread_mutex_lock(ptr noundef nonnull @heap_) #3
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @heap_, i64 720), align 8, !tbaa !9
  %7 = add i32 %6, 2
  store i32 %7, ptr getelementptr inbounds (i8, ptr @heap_, i64 720), align 8, !tbaa !9
  store i32 0, ptr getelementptr inbounds (i8, ptr @heap_, i64 724), align 4, !tbaa !17
  %8 = lshr i32 %7, 1
  %9 = and i32 %8, 1
  %10 = zext nneg i32 %9 to i64
  %11 = getelementptr inbounds [2 x %struct.gcSweepBuf], ptr getelementptr inbounds (i8, ptr @heap_, i64 88), i64 0, i64 %10, i32 4
  %12 = load i32, ptr %11, align 8, !tbaa !18
  %.not1 = icmp eq i32 %12, 0
  br i1 %.not1, label %15, label %13

13:                                               ; preds = %4
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  br label %15

15:                                               ; preds = %13, %4
  %16 = tail call i32 @pthread_mutex_unlock(ptr noundef nonnull @heap_) #3
  %17 = load i8, ptr getelementptr inbounds (i8, ptr @work, i64 16), align 8, !tbaa !20
  %.not2 = icmp eq i8 %17, 0
  br i1 %.not2, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %15, %.preheader
  %18 = tail call i64 @sweepone()
  %.not3 = icmp eq i64 %18, -1
  br i1 %.not3, label %.loopexit, label %.preheader, !llvm.loop !24

.loopexit:                                        ; preds = %.preheader, %15
  ret void
}

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @sweepone() local_unnamed_addr #1

attributes #0 = { 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 #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 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 720}
!10 = !{!"mheap", !7, i64 0, !11, i64 40, !11, i64 48, !13, 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, !13, i64 640, !12, i64 672, !7, i64 680, !14, i64 712, !6, i64 720, !6, i64 724, !6, i64 728, !12, i64 736, !14, i64 744, !14, i64 752, !14, i64 760, !7, i64 768}
!11 = !{!"mtreap", !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"", !12, i64 0, !6, i64 8, !14, i64 16, !6, i64 24}
!14 = !{!"long", !7, i64 0}
!15 = !{!"mfixalloc", !14, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !14, i64 32, !6, i64 40, !14, i64 48, !7, i64 56}
!16 = !{!"", !14, i64 0, !14, i64 8, !14, i64 16}
!17 = !{!10, !6, i64 724}
!18 = !{!19, !6, i64 64}
!19 = !{!"", !7, i64 0, !12, i64 40, !14, i64 48, !14, i64 56, !6, i64 64}
!20 = !{!21, !7, i64 16}
!21 = !{!"", !14, i64 0, !14, i64 8, !7, i64 16, !6, i64 20, !22, i64 24, !6, i64 96}
!22 = !{!"", !7, i64 0, !23, i64 40, !23, i64 56}
!23 = !{!"mspanlist", !12, i64 0, !12, i64 8}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass