; LLVM IR from kmilo17pet/QuarkTS-cpp :: src/prioqueue.ll (ZN3qOS9prioQueueC2EPNS_2pq12queueStack_tEm)

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

%"class.qOS::prioQueue" = type { ptr, i32, ptr, ptr, i64 }
%"struct.qOS::pq::queueStack_t" = type { ptr, ptr }

@_ZTVN3qOS9prioQueueE = external dso_local unnamed_addr constant { [4 x ptr] }, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3qOS9prioQueueC2EPNS_2pq12queueStack_tEm(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %8 = load ptr, ptr %4, align 8
  store ptr getelementptr inbounds inrange(-16, 16) ({ [4 x ptr] }, ptr @_ZTVN3qOS9prioQueueE, i32 0, i32 0, i32 2), ptr %8, align 8, !tbaa !11
  %9 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 1
  store volatile i32 -1, ptr %9, align 8, !tbaa !13
  %10 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 2
  store ptr null, ptr %10, align 8, !tbaa !16
  %11 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 3
  store ptr null, ptr %11, align 8, !tbaa !17
  %12 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 4
  store i64 0, ptr %12, align 8, !tbaa !18
  call void @_ZN3qOS8critical5enterEv() #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 3
  store ptr %13, ptr %14, align 8, !tbaa !17
  %15 = load i64, ptr %6, align 8, !tbaa !9
  %16 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 4
  store i64 %15, ptr %16, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %29, %3
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 4
  %20 = load i64, ptr %19, align 8, !tbaa !18
  %21 = icmp ult i64 %18, %20
  br i1 %21, label %23, label %22

22:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %32

23:                                               ; preds = %17
  %24 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 3
  %25 = load ptr, ptr %24, align 8, !tbaa !17
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %25, i64 %26
  %28 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %27, i32 0, i32 0
  store ptr null, ptr %28, align 8, !tbaa !19
  br label %29

29:                                               ; preds = %23
  %30 = load i64, ptr %7, align 8, !tbaa !9
  %31 = add i64 %30, 1
  store i64 %31, ptr %7, align 8, !tbaa !9
  br label %17, !llvm.loop !21

32:                                               ; preds = %22
  %33 = getelementptr inbounds %"class.qOS::prioQueue", ptr %8, i32 0, i32 1
  store volatile i32 -1, ptr %33, align 8, !tbaa !13
  call void @_ZN3qOS8critical4exitEv() #3
  ret void
}

; Function Attrs: nounwind
declare void @_ZN3qOS8critical5enterEv() #1

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

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

; Function Attrs: nounwind
declare void @_ZN3qOS8critical4exitEv() #1

attributes #0 = { mustprogress 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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"vtable pointer", !8, i64 0}
!13 = !{!14, !15, i64 8}
!14 = !{!"_ZTSN3qOS9prioQueueE", !15, i64 8, !6, i64 16, !6, i64 24, !10, i64 32}
!15 = !{!"int", !7, i64 0}
!16 = !{!14, !6, i64 16}
!17 = !{!14, !6, i64 24}
!18 = !{!14, !10, i64 32}
!19 = !{!20, !6, i64 0}
!20 = !{!"_ZTSN3qOS2pq12queueStack_tE", !6, i64 0, !6, i64 8}
!21 = distinct !{!21, !22}
!22 = !{!"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.qOS::pq::queueStack_t" = type { ptr, ptr }

@_ZTVN3qOS9prioQueueE = external dso_local unnamed_addr constant { [4 x ptr] }, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3qOS9prioQueueC2EPNS_2pq12queueStack_tEm(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, i64 noundef %2) unnamed_addr #0 align 2 {
  store ptr getelementptr inbounds inrange(-16, 16) (i8, ptr @_ZTVN3qOS9prioQueueE, i64 16), ptr %0, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  store volatile i32 -1, ptr %4, align 8, !tbaa !8
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = getelementptr inbounds i8, ptr %0, i64 32
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %5, i8 0, i64 24, i1 false)
  tail call void @_ZN3qOS8critical5enterEv() #3
  store ptr %1, ptr %6, align 8, !tbaa !14
  store i64 %2, ptr %7, align 8, !tbaa !15
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %xtraiter = and i64 %2, 3
  %8 = icmp ult i64 %2, 4
  br i1 %8, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %2, -4
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.05.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %18, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.05.epil = phi i64 [ %10, %.lr.ph.epil ], [ %.05.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %9 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %1, i64 %.05.epil
  store ptr null, ptr %9, align 8, !tbaa !16
  %10 = add nuw i64 %.05.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !18

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %3
  store volatile i32 -1, ptr %4, align 8, !tbaa !8
  tail call void @_ZN3qOS8critical4exitEv() #3
  ret void

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.05 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %18, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %11 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %1, i64 %.05
  store ptr null, ptr %11, align 8, !tbaa !16
  %12 = or disjoint i64 %.05, 1
  %13 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %1, i64 %12
  store ptr null, ptr %13, align 8, !tbaa !16
  %14 = or disjoint i64 %.05, 2
  %15 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %1, i64 %14
  store ptr null, ptr %15, align 8, !tbaa !16
  %16 = or disjoint i64 %.05, 3
  %17 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %1, i64 %16
  store ptr null, ptr %17, align 8, !tbaa !16
  %18 = add nuw i64 %.05, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !20
}

; Function Attrs: nounwind
declare void @_ZN3qOS8critical5enterEv() local_unnamed_addr #1

; Function Attrs: nounwind
declare void @_ZN3qOS8critical4exitEv() local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { mustprogress 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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"vtable pointer", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !10, i64 8}
!9 = !{!"_ZTSN3qOS9prioQueueE", !10, i64 8, !12, i64 16, !12, i64 24, !13, i64 32}
!10 = !{!"int", !11, i64 0}
!11 = !{!"omnipotent char", !7, i64 0}
!12 = !{!"any pointer", !11, i64 0}
!13 = !{!"long", !11, i64 0}
!14 = !{!9, !12, i64 24}
!15 = !{!9, !13, i64 32}
!16 = !{!17, !12, i64 0}
!17 = !{!"_ZTSN3qOS2pq12queueStack_tE", !12, i64 0, !12, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.unroll.disable"}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass