; LLVM IR from shadow-paw/lightcone :: lightcone/threads.ll (ZN9lightcone7Threads4stopEv)

; 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.lightcone::Threads" = type <{ ptr, [128 x %"struct.lightcone::Threads::THREAD_INFO"], i32, [4 x i8] }>
%"struct.lightcone::Threads::THREAD_INFO" = type { i8, %"class.std::thread" }
%"class.std::thread" = type { %"class.std::thread::id" }
%"class.std::thread::id" = type { i64 }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN9lightcone7Threads4stopEv(ptr noundef nonnull align 8 dereferenceable(2060) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %6

6:                                                ; preds = %18, %1
  %7 = load i32, ptr %3, align 4, !tbaa !9
  %8 = getelementptr inbounds %"class.lightcone::Threads", ptr %5, i32 0, i32 2
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = icmp ult i32 %7, %9
  br i1 %10, label %12, label %11

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

12:                                               ; preds = %6
  %13 = getelementptr inbounds %"class.lightcone::Threads", ptr %5, i32 0, i32 1
  %14 = load i32, ptr %3, align 4, !tbaa !9
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %13, i64 0, i64 %15
  %17 = getelementptr inbounds %"struct.lightcone::Threads::THREAD_INFO", ptr %16, i32 0, i32 0
  store i8 0, ptr %17, align 8, !tbaa !13
  br label %18

18:                                               ; preds = %12
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = add i32 %19, 1
  store i32 %20, ptr %3, align 4, !tbaa !9
  br label %6, !llvm.loop !19

21:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %34, %21
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = getelementptr inbounds %"class.lightcone::Threads", ptr %5, i32 0, i32 2
  %25 = load i32, ptr %24, align 8, !tbaa !11
  %26 = icmp ult i32 %23, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %37

28:                                               ; preds = %22
  %29 = getelementptr inbounds %"class.lightcone::Threads", ptr %5, i32 0, i32 1
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = zext i32 %30 to i64
  %32 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %29, i64 0, i64 %31
  %33 = getelementptr inbounds %"struct.lightcone::Threads::THREAD_INFO", ptr %32, i32 0, i32 1
  call void @_ZNSt6thread4joinEv(ptr noundef nonnull align 8 dereferenceable(8) %33)
  br label %34

34:                                               ; preds = %28
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = add i32 %35, 1
  store i32 %36, ptr %4, align 4, !tbaa !9
  br label %22, !llvm.loop !21

37:                                               ; preds = %27
  %38 = getelementptr inbounds %"class.lightcone::Threads", ptr %5, i32 0, i32 2
  store i32 0, ptr %38, align 8, !tbaa !11
  ret void
}

; 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 @_ZNSt6thread4joinEv(ptr noundef nonnull align 8 dereferenceable(8)) #2

attributes #0 = { mustprogress 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 2056}
!12 = !{!"_ZTSN9lightcone7ThreadsE", !7, i64 8, !10, i64 2056}
!13 = !{!14, !15, i64 0}
!14 = !{!"_ZTSN9lightcone7Threads11THREAD_INFOE", !15, i64 0, !16, i64 8}
!15 = !{!"bool", !7, i64 0}
!16 = !{!"_ZTSSt6thread", !17, i64 0}
!17 = !{!"_ZTSNSt6thread2idE", !18, i64 0}
!18 = !{!"long", !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}


; 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.lightcone::Threads::THREAD_INFO" = type { i8, %"class.std::thread" }
%"class.std::thread" = type { %"class.std::thread::id" }
%"class.std::thread::id" = type { i64 }

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN9lightcone7Threads4stopEv(ptr noundef nonnull align 8 dereferenceable(2060) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 2056
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %wide.trip.count = zext i32 %3 to i64
  %xtraiter = and i64 %wide.trip.count, 3
  %5 = icmp ult i32 %3, 4
  br i1 %5, label %.preheader.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 4294967292
  br label %8

.preheader.unr-lcssa:                             ; preds = %8, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.3, %8 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.epil.preheader

.epil.preheader:                                  ; preds = %.preheader.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %.preheader.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %.preheader.unr-lcssa ]
  %6 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %4, i64 0, i64 %indvars.iv.epil
  store i8 0, ptr %6, align 8, !tbaa !10
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.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 %.preheader, label %.epil.preheader, !llvm.loop !16

.preheader:                                       ; preds = %.epil.preheader, %.preheader.unr-lcssa
  br i1 %.not, label %._crit_edge, label %.lr.ph9

.lr.ph9:                                          ; preds = %.preheader
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  br label %13

8:                                                ; preds = %8, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.3, %8 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.3, %8 ]
  %9 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %4, i64 0, i64 %indvars.iv
  store i8 0, ptr %9, align 8, !tbaa !10
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %10 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %4, i64 0, i64 %indvars.iv.next
  store i8 0, ptr %10, align 8, !tbaa !10
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %11 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %4, i64 0, i64 %indvars.iv.next.1
  store i8 0, ptr %11, align 8, !tbaa !10
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %12 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %4, i64 0, i64 %indvars.iv.next.2
  store i8 0, ptr %12, align 8, !tbaa !10
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 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 %.preheader.unr-lcssa, label %8, !llvm.loop !18

._crit_edge:                                      ; preds = %13, %1, %.preheader
  store i32 0, ptr %2, align 8, !tbaa !5
  ret void

13:                                               ; preds = %.lr.ph9, %13
  %indvars.iv12 = phi i64 [ 0, %.lr.ph9 ], [ %indvars.iv.next13, %13 ]
  %14 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %7, i64 0, i64 %indvars.iv12, i32 1
  tail call void @_ZNSt6thread4joinEv(ptr noundef nonnull align 8 dereferenceable(8) %14)
  %indvars.iv.next13 = add nuw nsw i64 %indvars.iv12, 1
  %15 = load i32, ptr %2, align 8, !tbaa !5
  %16 = zext i32 %15 to i64
  %17 = icmp ult i64 %indvars.iv.next13, %16
  br i1 %17, label %13, label %._crit_edge, !llvm.loop !20
}

declare void @_ZNSt6thread4joinEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #1

attributes #0 = { mustprogress 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" }

!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, !9, i64 2056}
!6 = !{!"_ZTSN9lightcone7ThreadsE", !7, i64 8, !9, i64 2056}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !12, i64 0}
!11 = !{!"_ZTSN9lightcone7Threads11THREAD_INFOE", !12, i64 0, !13, i64 8}
!12 = !{!"bool", !7, i64 0}
!13 = !{!"_ZTSSt6thread", !14, i64 0}
!14 = !{!"_ZTSNSt6thread2idE", !15, i64 0}
!15 = !{!"long", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.unroll.disable"}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = distinct !{!20, !19}


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