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

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

; 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: mustprogress nounwind uwtable
define dso_local void @_ZN3qOS9prioQueue10clearIndexEm(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  %7 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %8 = getelementptr inbounds %"class.qOS::prioQueue", ptr %7, i32 0, i32 1
  %9 = load volatile i32, ptr %8, align 8, !tbaa !11
  store i32 %9, ptr %5, align 4, !tbaa !14
  %10 = getelementptr inbounds %"class.qOS::prioQueue", ptr %7, i32 0, i32 3
  %11 = load ptr, ptr %10, align 8, !tbaa !15
  %12 = load i64, ptr %4, align 8, !tbaa !9
  %13 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %11, i64 %12
  %14 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %13, i32 0, i32 0
  store ptr null, ptr %14, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %15 = load i64, ptr %4, align 8, !tbaa !9
  store i64 %15, ptr %6, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %32, %2
  %17 = load i64, ptr %6, align 8, !tbaa !9
  %18 = trunc i64 %17 to i32
  %19 = load i32, ptr %5, align 4, !tbaa !14
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %35

22:                                               ; preds = %16
  %23 = getelementptr inbounds %"class.qOS::prioQueue", ptr %7, i32 0, i32 3
  %24 = load ptr, ptr %23, align 8, !tbaa !15
  %25 = load i64, ptr %6, align 8, !tbaa !9
  %26 = add i64 %25, 1
  %27 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %24, i64 %26
  %28 = getelementptr inbounds %"class.qOS::prioQueue", ptr %7, i32 0, i32 3
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = load i64, ptr %6, align 8, !tbaa !9
  %31 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %29, i64 %30
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %31, ptr align 8 %27, i64 16, i1 false), !tbaa.struct !18
  br label %32

32:                                               ; preds = %22
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = add i64 %33, 1
  store i64 %34, ptr %6, align 8, !tbaa !9
  br label %16, !llvm.loop !19

35:                                               ; preds = %21
  %36 = getelementptr inbounds %"class.qOS::prioQueue", ptr %7, i32 0, i32 1
  %37 = load volatile i32, ptr %36, align 8, !tbaa !11
  %38 = sub nsw i32 %37, 1
  %39 = getelementptr inbounds %"class.qOS::prioQueue", ptr %7, i32 0, i32 1
  store volatile i32 %38, ptr %39, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree 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, !13, i64 8}
!12 = !{!"_ZTSN3qOS9prioQueueE", !13, i64 8, !6, i64 16, !6, i64 24, !10, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !6, i64 24}
!16 = !{!17, !6, i64 0}
!17 = !{!"_ZTSN3qOS2pq12queueStack_tE", !6, i64 0, !6, i64 8}
!18 = !{i64 0, i64 8, !5, i64 8, i64 8, !5}
!19 = distinct !{!19, !20}
!20 = !{!"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 }

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local void @_ZN3qOS9prioQueue10clearIndexEm(ptr noundef nonnull align 8 dereferenceable(40) %0, i64 noundef %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load volatile i32, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 24
  %6 = load ptr, ptr %5, align 8, !tbaa !12
  %7 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %6, i64 %1
  store ptr null, ptr %7, align 8, !tbaa !13
  %8 = trunc i64 %1 to i32
  %9 = icmp sgt i32 %4, %8
  br i1 %9, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %10 = sub i32 %4, %8
  %.neg = add i32 %8, 1
  %xtraiter = and i32 %10, 1
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader
  %11 = load ptr, ptr %5, align 8, !tbaa !12
  %12 = add i64 %1, 1
  %13 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %11, i64 %12
  %14 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %11, i64 %1
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %14, ptr noundef nonnull align 8 dereferenceable(16) %13, i64 16, i1 false), !tbaa.struct !15
  br label %.lr.ph.prol.loopexit

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.07.unr = phi i64 [ %1, %.lr.ph.preheader ], [ %12, %.lr.ph.prol ]
  %15 = icmp eq i32 %4, %.neg
  br i1 %15, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %2
  %16 = load volatile i32, ptr %3, align 8, !tbaa !5
  %17 = add nsw i32 %16, -1
  store volatile i32 %17, ptr %3, align 8, !tbaa !5
  ret void

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.07 = phi i64 [ %23, %.lr.ph ], [ %.07.unr, %.lr.ph.prol.loopexit ]
  %18 = load ptr, ptr %5, align 8, !tbaa !12
  %19 = add i64 %.07, 1
  %20 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %18, i64 %19
  %21 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %18, i64 %.07
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %21, ptr noundef nonnull align 8 dereferenceable(16) %20, i64 16, i1 false), !tbaa.struct !15
  %22 = load ptr, ptr %5, align 8, !tbaa !12
  %23 = add i64 %.07, 2
  %24 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %22, i64 %23
  %25 = getelementptr inbounds %"struct.qOS::pq::queueStack_t", ptr %22, i64 %19
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %25, ptr noundef nonnull align 8 dereferenceable(16) %24, i64 16, i1 false), !tbaa.struct !15
  %26 = trunc i64 %23 to i32
  %27 = icmp sgt i32 %4, %26
  br i1 %27, label %.lr.ph, label %._crit_edge, !llvm.loop !17
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

attributes #0 = { mustprogress nofree norecurse 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !7, i64 8}
!6 = !{!"_ZTSN3qOS9prioQueueE", !7, i64 8, !10, i64 16, !10, i64 24, !11, i64 32}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!6, !10, i64 24}
!13 = !{!14, !10, i64 0}
!14 = !{!"_ZTSN3qOS2pq12queueStack_tE", !10, i64 0, !10, i64 8}
!15 = !{i64 0, i64 8, !16, i64 8, i64 8, !16}
!16 = !{!10, !10, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass