; LLVM IR from ponylang/ponyc :: src/libponyrt/sched (mpmcq)

; 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"

%union.aba_protected_mpmcq_node_t = type { i128 }
%struct.mpmcq_t = type { ptr, %union.aba_protected_mpmcq_node_t, [32 x i8] }
%struct.anon = type { ptr, i64 }
%struct.mpmcq_node_t = type { ptr, ptr }

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

; 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
declare hidden void @node_free(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @ponyint_mpmcq_pop(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca %union.aba_protected_mpmcq_node_t, align 16
  %5 = alloca %union.aba_protected_mpmcq_node_t, align 16
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  %11 = alloca i128, align 16
  %12 = alloca i8, align 1
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.mpmcq_t, ptr %17, i32 0, i32 1
  %19 = getelementptr inbounds %struct.anon, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 16, !tbaa !9
  %21 = getelementptr inbounds %struct.anon, ptr %4, i32 0, i32 0
  store ptr %20, ptr %21, align 16, !tbaa !9
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.mpmcq_t, ptr %22, i32 0, i32 1
  %24 = getelementptr inbounds %struct.anon, ptr %23, i32 0, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.anon, ptr %4, i32 0, i32 1
  store i64 %25, ptr %26, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  br label %27

27:                                               ; preds = %54, %1
  %28 = getelementptr inbounds %struct.anon, ptr %4, i32 0, i32 0
  %29 = load ptr, ptr %28, align 16, !tbaa !9
  store ptr %29, ptr %6, align 8, !tbaa !5
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.mpmcq_node_t, ptr %30, i32 0, i32 0
  %32 = load atomic i64, ptr %31 monotonic, align 8
  store i64 %32, ptr %8, align 8
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %33, ptr %7, align 8, !tbaa !5
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = icmp eq ptr %34, null
  br i1 %35, label %36, label %37

36:                                               ; preds = %27
  store ptr null, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %80

37:                                               ; preds = %27
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.anon, ptr %5, i32 0, i32 0
  store ptr %38, ptr %39, align 16, !tbaa !9
  %40 = getelementptr inbounds %struct.anon, ptr %4, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !9
  %42 = add i64 %41, 1
  %43 = getelementptr inbounds %struct.anon, ptr %5, i32 0, i32 1
  store i64 %42, ptr %43, align 8, !tbaa !9
  br label %44

44:                                               ; preds = %37
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.mpmcq_t, ptr %45, i32 0, i32 1
  %47 = load i128, ptr %5, align 16, !tbaa !9
  store i128 %47, ptr %11, align 16, !tbaa !10
  %48 = load i128, ptr %4, align 16
  %49 = load i128, ptr %11, align 16
  %50 = cmpxchg ptr %46, i128 %48, i128 %49 acq_rel acquire, align 16
  %51 = extractvalue { i128, i1 } %50, 0
  %52 = extractvalue { i128, i1 } %50, 1
  br i1 %52, label %54, label %53

53:                                               ; preds = %44
  store i128 %51, ptr %4, align 16
  br label %54

54:                                               ; preds = %53, %44
  %55 = zext i1 %52 to i8
  store i8 %55, ptr %12, align 1, !tbaa !12
  %56 = load i8, ptr %12, align 1, !tbaa !12, !range !14, !noundef !15
  %57 = trunc i8 %56 to i1
  %58 = zext i1 %57 to i8
  store i8 %58, ptr %10, align 1, !tbaa !12
  %59 = load i8, ptr %10, align 1, !tbaa !12, !range !14, !noundef !15
  %60 = trunc i8 %59 to i1
  %61 = xor i1 %60, true
  br i1 %61, label %27, label %62, !llvm.loop !16

62:                                               ; preds = %54
  fence acq_rel
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.mpmcq_node_t, ptr %63, i32 0, i32 1
  %65 = load atomic i64, ptr %64 acquire, align 8
  store i64 %65, ptr %14, align 8
  %66 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %66, ptr %13, align 8, !tbaa !5
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.mpmcq_node_t, ptr %67, i32 0, i32 1
  store ptr null, ptr %15, align 8, !tbaa !5
  %69 = load i64, ptr %15, align 8
  store atomic i64 %69, ptr %68 release, align 8
  br label %70

70:                                               ; preds = %76, %62
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.mpmcq_node_t, ptr %71, i32 0, i32 1
  %73 = load atomic i64, ptr %72 acquire, align 8
  store i64 %73, ptr %16, align 8
  %74 = load ptr, ptr %16, align 8, !tbaa !5
  %75 = icmp ne ptr %74, null
  br i1 %75, label %76, label %77

76:                                               ; preds = %70
  call void (...) @ponyint_cpu_relax()
  br label %70, !llvm.loop !18

77:                                               ; preds = %70
  fence acquire
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  call void @node_free(ptr noundef %78)
  %79 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %79, ptr %2, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  br label %80

80:                                               ; preds = %77, %36
  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 16, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %4) #3
  %81 = load ptr, ptr %2, align 8
  ret ptr %81
}

declare void @ponyint_cpu_relax(...) #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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"__int128", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{i8 0, i8 2}
!15 = !{}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


; 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 hidden void @node_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @ponyint_mpmcq_pop(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load ptr, ptr %2, align 16, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i64, ptr %4, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %9, %1
  %.sroa.016.0 = phi ptr [ %3, %1 ], [ %15, %9 ]
  %.sroa.6.0 = phi i64 [ %5, %1 ], [ %.sroa.6.0.extract.trunc, %9 ]
  %7 = load atomic i64, ptr %.sroa.016.0 monotonic, align 8
  %8 = icmp eq i64 %7, 0
  br i1 %8, label %.loopexit, label %9

9:                                                ; preds = %6
  %10 = add i64 %.sroa.6.0, 1
  %11 = ptrtoint ptr %.sroa.016.0 to i64
  %.sroa.6.0.insert.ext = zext i64 %.sroa.6.0 to i128
  %.sroa.6.0.insert.shift = shl nuw i128 %.sroa.6.0.insert.ext, 64
  %.sroa.016.0.insert.ext = zext i64 %11 to i128
  %.sroa.016.0.insert.insert = or disjoint i128 %.sroa.6.0.insert.shift, %.sroa.016.0.insert.ext
  %.sroa.2.0.insert.ext = zext i64 %10 to i128
  %.sroa.2.0.insert.shift = shl nuw i128 %.sroa.2.0.insert.ext, 64
  %.sroa.015.0.insert.ext = zext i64 %7 to i128
  %.sroa.015.0.insert.insert = or disjoint i128 %.sroa.2.0.insert.shift, %.sroa.015.0.insert.ext
  %12 = cmpxchg ptr %2, i128 %.sroa.016.0.insert.insert, i128 %.sroa.015.0.insert.insert acq_rel acquire, align 16
  %13 = extractvalue { i128, i1 } %12, 1
  %14 = extractvalue { i128, i1 } %12, 0
  %.sroa.016.0.extract.trunc = trunc i128 %14 to i64
  %15 = inttoptr i64 %.sroa.016.0.extract.trunc to ptr
  %.sroa.6.0.extract.shift = lshr i128 %14, 64
  %.sroa.6.0.extract.trunc = trunc nuw i128 %.sroa.6.0.extract.shift to i64
  br i1 %13, label %16, label %6, !llvm.loop !8

16:                                               ; preds = %9
  %17 = inttoptr i64 %7 to ptr
  fence acq_rel
  %18 = getelementptr inbounds i8, ptr %17, i64 8
  %19 = load atomic i64, ptr %18 acquire, align 8
  %20 = inttoptr i64 %19 to ptr
  store atomic i64 0, ptr %18 release, align 8
  %21 = getelementptr inbounds i8, ptr %.sroa.016.0, i64 8
  %22 = load atomic i64, ptr %21 acquire, align 8
  %.not25 = icmp eq i64 %22, 0
  br i1 %.not25, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %16, %.lr.ph
  tail call void (...) @ponyint_cpu_relax() #2
  %23 = load atomic i64, ptr %21 acquire, align 8
  %.not = icmp eq i64 %23, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %16
  fence acquire
  tail call void @node_free(ptr noundef nonnull %.sroa.016.0)
  br label %.loopexit

.loopexit:                                        ; preds = %6, %._crit_edge
  %.0 = phi ptr [ %20, %._crit_edge ], [ null, %6 ]
  ret ptr %.0
}

declare void @ponyint_cpu_relax(...) 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


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