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

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

; 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 uwtable
define dso_local noundef zeroext i1 @_ZN9lightcone7Threads5startEj(ptr noundef nonnull align 8 dereferenceable(2060) %0, i32 noundef %1) #1 align 2 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca %"class.std::thread", align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8
  %10 = load i32, ptr %5, align 4, !tbaa !9
  %11 = icmp ule i32 %10, 0
  br i1 %11, label %16, label %12

12:                                               ; preds = %2
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = zext i32 %13 to i64
  %15 = icmp ugt i64 %14, 128
  br i1 %15, label %16, label %17

16:                                               ; preds = %12, %2
  store i1 false, ptr %3, align 1
  br label %41

17:                                               ; preds = %12
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = getelementptr inbounds %"class.lightcone::Threads", ptr %9, i32 0, i32 2
  store i32 %18, ptr %19, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %37, %17
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = icmp ult i32 %21, %22
  br i1 %23, label %25, label %24

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

25:                                               ; preds = %20
  %26 = getelementptr inbounds %"class.lightcone::Threads", ptr %9, i32 0, i32 1
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %26, i64 0, i64 %28
  %30 = getelementptr inbounds %"struct.lightcone::Threads::THREAD_INFO", ptr %29, i32 0, i32 0
  store i8 1, ptr %30, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store ptr %9, ptr %8, align 8, !tbaa !5
  call void @_ZNSt6threadC2IRFvPN9lightcone7ThreadsEjEJS3_RjEvEEOT_DpOT0_(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef nonnull @_ZN9lightcone7Threads4stubEPS0_j, ptr noundef nonnull align 8 dereferenceable(8) %8, ptr noundef nonnull align 4 dereferenceable(4) %6)
  %31 = getelementptr inbounds %"class.lightcone::Threads", ptr %9, i32 0, i32 1
  %32 = load i32, ptr %6, align 4, !tbaa !9
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %31, i64 0, i64 %33
  %35 = getelementptr inbounds %"struct.lightcone::Threads::THREAD_INFO", ptr %34, i32 0, i32 1
  %36 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6threadaSEOS_(ptr noundef nonnull align 8 dereferenceable(8) %35, ptr noundef nonnull align 8 dereferenceable(8) %7) #3
  call void @_ZNSt6threadD2Ev(ptr noundef nonnull align 8 dereferenceable(8) %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %37

37:                                               ; preds = %25
  %38 = load i32, ptr %6, align 4, !tbaa !9
  %39 = add i32 %38, 1
  store i32 %39, ptr %6, align 4, !tbaa !9
  br label %20, !llvm.loop !19

40:                                               ; preds = %24
  store i1 true, ptr %3, align 1
  br label %41

41:                                               ; preds = %40, %16
  %42 = load i1, ptr %3, align 1
  ret i1 %42
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9lightcone7Threads4stubEPS0_j(ptr noundef, i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt6threadC2IRFvPN9lightcone7ThreadsEjEJS3_RjEvEEOT_DpOT0_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull, ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(4)) unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6threadaSEOS_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt6threadD2Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #2 align 2

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


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

%"class.std::thread" = type { %"class.std::thread::id" }
%"class.std::thread::id" = type { i64 }
%"struct.lightcone::Threads::THREAD_INFO" = type { i8, %"class.std::thread" }

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN9lightcone7Threads5startEj(ptr noundef nonnull align 8 dereferenceable(2060) %0, i32 noundef %1) local_unnamed_addr #1 align 2 {
  %3 = alloca i32, align 4
  %4 = alloca %"class.std::thread", align 8
  %5 = alloca ptr, align 8
  %6 = add i32 %1, -1
  %or.cond = icmp ult i32 %6, 128
  br i1 %or.cond, label %.lr.ph, label %19

.lr.ph:                                           ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 2056
  store i32 %1, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #3
  store i32 0, ptr %3, align 4, !tbaa !10
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  br label %9

._crit_edge:                                      ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #3
  br label %19

9:                                                ; preds = %.lr.ph, %9
  %storemerge9 = phi i32 [ 0, %.lr.ph ], [ %17, %9 ]
  %10 = zext i32 %storemerge9 to i64
  %11 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %8, i64 0, i64 %10
  store i8 1, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  store ptr %0, ptr %5, align 8, !tbaa !17
  call void @_ZNSt6threadC2IRFvPN9lightcone7ThreadsEjEJS3_RjEvEEOT_DpOT0_(ptr noundef nonnull align 8 dereferenceable(8) %4, ptr noundef nonnull @_ZN9lightcone7Threads4stubEPS0_j, ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 4 dereferenceable(4) %3)
  %12 = load i32, ptr %3, align 4, !tbaa !10
  %13 = zext i32 %12 to i64
  %14 = getelementptr inbounds [128 x %"struct.lightcone::Threads::THREAD_INFO"], ptr %8, i64 0, i64 %13, i32 1
  %15 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6threadaSEOS_(ptr noundef nonnull align 8 dereferenceable(8) %14, ptr noundef nonnull align 8 dereferenceable(8) %4) #3
  call void @_ZNSt6threadD2Ev(ptr noundef nonnull align 8 dereferenceable(8) %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  %16 = load i32, ptr %3, align 4, !tbaa !10
  %17 = add i32 %16, 1
  store i32 %17, ptr %3, align 4, !tbaa !10
  %18 = icmp ult i32 %17, %1
  br i1 %18, label %9, label %._crit_edge, !llvm.loop !19

19:                                               ; preds = %2, %._crit_edge
  ret i1 %or.cond
}

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9lightcone7Threads4stubEPS0_j(ptr noundef, i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZNSt6threadC2IRFvPN9lightcone7ThreadsEjEJS3_RjEvEEOT_DpOT0_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull, ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 4 dereferenceable(4)) unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZNSt6threadaSEOS_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZNSt6threadD2Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #2 align 2

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass