; LLVM IR from jrouwe/JoltPhysics :: Jolt/Core/JobSystemWithBarrier.ll (ZN3JPH20JobSystemWithBarrier4InitEj)

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

%"struct.JPH::AssertLastParam" = type { i8 }
%"class.JPH::JobSystemWithBarrier" = type { %"class.JPH::JobSystem", i32, ptr }
%"class.JPH::JobSystem" = type { ptr }
%"class.JPH::JobSystemWithBarrier::BarrierImpl" = type { %"class.JPH::JobSystem::Barrier", %"struct.std::atomic", [2048 x %"struct.std::atomic.0"], [48 x i8], %"struct.std::atomic.2", [60 x i8], %"struct.std::atomic.2", %"struct.std::atomic.4", [56 x i8], %"class.JPH::Semaphore" }
%"class.JPH::JobSystem::Barrier" = type { ptr }
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i8 }
%"struct.std::atomic.0" = type { %"struct.std::__atomic_base.1" }
%"struct.std::__atomic_base.1" = type { ptr }
%"struct.std::atomic.2" = type { %"struct.std::__atomic_base.3" }
%"struct.std::__atomic_base.3" = type { i32 }
%"struct.std::atomic.4" = type { %"struct.std::__atomic_base.5" }
%"struct.std::__atomic_base.5" = type { i32 }
%"class.JPH::Semaphore" = type { %"struct.std::atomic.4", %union.sem_t, [24 x i8] }
%union.sem_t = type { i64, [24 x i8] }

@.str.1 = external hidden unnamed_addr constant [87 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [21 x i8], align 1

declare i32 @__gxx_personality_v0(...)

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jNS_15AssertLastParamE(ptr noundef, ptr noundef, i32 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3JPH20JobSystemWithBarrier4InitEj(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) #1 align 2 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca %"struct.JPH::AssertLastParam", align 1
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %8 = load ptr, ptr %3, align 8
  br label %9

9:                                                ; preds = %2
  %10 = getelementptr inbounds %"class.JPH::JobSystemWithBarrier", ptr %8, i32 0, i32 2
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %12 = icmp eq ptr %11, null
  br i1 %12, label %16, label %13

13:                                               ; preds = %9
  %14 = call noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jNS_15AssertLastParamE(ptr noundef @.str.5, ptr noundef @.str.1, i32 noundef 172)
  br i1 %14, label %15, label %16

15:                                               ; preds = %13
  call void asm sideeffect "int $$0x3", "~{dirflag},~{fpsr},~{flags}"() #5, !srcloc !14
  br label %16

16:                                               ; preds = %15, %13, %9
  br label %17

17:                                               ; preds = %16
  %18 = load i32, ptr %4, align 4, !tbaa !9
  %19 = getelementptr inbounds %"class.JPH::JobSystemWithBarrier", ptr %8, i32 0, i32 1
  store i32 %18, ptr %19, align 8, !tbaa !15
  %20 = load i32, ptr %4, align 4, !tbaa !9
  %21 = zext i32 %20 to i64
  %22 = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %21, i64 16640)
  %23 = extractvalue { i64, i1 } %22, 1
  %24 = extractvalue { i64, i1 } %22, 0
  %25 = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 %24, i64 64)
  %26 = extractvalue { i64, i1 } %25, 1
  %27 = or i1 %23, %26
  %28 = extractvalue { i64, i1 } %25, 0
  %29 = select i1 %27, i64 -1, i64 %28
  %30 = call noundef ptr @_ZN3JPH20JobSystemWithBarrier11BarrierImplnaEmSt11align_val_t(i64 noundef %29, i64 noundef 64)
  %31 = getelementptr inbounds i8, ptr %30, i64 56
  store i64 %21, ptr %31, align 8
  %32 = getelementptr inbounds i8, ptr %30, i64 64
  %33 = icmp eq i64 %21, 0
  br i1 %33, label %41, label %34

34:                                               ; preds = %17
  %35 = getelementptr inbounds %"class.JPH::JobSystemWithBarrier::BarrierImpl", ptr %32, i64 %21
  br label %36

36:                                               ; preds = %38, %34
  %37 = phi ptr [ %32, %34 ], [ %39, %38 ]
  invoke void @_ZN3JPH20JobSystemWithBarrier11BarrierImplC1Ev(ptr noundef nonnull align 64 dereferenceable(16640) %37)
          to label %38 unwind label %43

38:                                               ; preds = %36
  %39 = getelementptr inbounds %"class.JPH::JobSystemWithBarrier::BarrierImpl", ptr %37, i64 1
  %40 = icmp eq ptr %39, %35
  br i1 %40, label %41, label %36

41:                                               ; preds = %38, %17
  %42 = getelementptr inbounds %"class.JPH::JobSystemWithBarrier", ptr %8, i32 0, i32 2
  store ptr %32, ptr %42, align 8, !tbaa !11
  ret void

43:                                               ; preds = %36
  %44 = landingpad { ptr, i32 }
          cleanup
  %45 = extractvalue { ptr, i32 } %44, 0
  store ptr %45, ptr %6, align 8
  %46 = extractvalue { ptr, i32 } %44, 1
  store i32 %46, ptr %7, align 4
  %47 = icmp eq ptr %32, %37
  br i1 %47, label %52, label %48

48:                                               ; preds = %48, %43
  %49 = phi ptr [ %37, %43 ], [ %50, %48 ]
  %50 = getelementptr inbounds %"class.JPH::JobSystemWithBarrier::BarrierImpl", ptr %49, i64 -1
  call void @_ZN3JPH20JobSystemWithBarrier11BarrierImplD1Ev(ptr noundef nonnull align 64 dereferenceable(16640) %50) #5
  %51 = icmp eq ptr %50, %32
  br i1 %51, label %52, label %48

52:                                               ; preds = %48, %43
  call void @_ZN3JPH20JobSystemWithBarrier11BarrierImpldaEPvSt11align_val_t(ptr noundef %30, i64 noundef 64) #5
  br label %53

53:                                               ; preds = %52
  %54 = load ptr, ptr %6, align 8
  %55 = load i32, ptr %7, align 4
  %56 = insertvalue { ptr, i32 } poison, ptr %54, 0
  %57 = insertvalue { ptr, i32 } %56, i32 %55, 1
  resume { ptr, i32 } %57
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.umul.with.overflow.i64(i64, i64) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare { i64, i1 } @llvm.uadd.with.overflow.i64(i64, i64) #2

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local noundef ptr @_ZN3JPH20JobSystemWithBarrier11BarrierImplnaEmSt11align_val_t(i64 noundef, i64 noundef) #3 align 2

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local void @_ZN3JPH20JobSystemWithBarrier11BarrierImpldaEPvSt11align_val_t(ptr noundef, i64 noundef) #4 align 2

declare void @_ZN3JPH20JobSystemWithBarrier11BarrierImplC1Ev(ptr)

declare void @_ZN3JPH20JobSystemWithBarrier11BarrierImplD1Ev(ptr)

attributes #0 = { inlinehint 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 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { alwaysinline 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 #4 = { alwaysinline 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 #5 = { 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, !6, i64 16}
!12 = !{!"_ZTSN3JPH20JobSystemWithBarrierE", !13, i64 0, !10, i64 8, !6, i64 16}
!13 = !{!"_ZTSN3JPH9JobSystemE"}
!14 = !{i64 2157459562}
!15 = !{!12, !10, i64 8}


; 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.JPH::JobSystemWithBarrier::BarrierImpl" = type { %"class.JPH::JobSystem::Barrier", %"struct.std::atomic", [2048 x %"struct.std::atomic.0"], [48 x i8], %"struct.std::atomic.2", [60 x i8], %"struct.std::atomic.2", %"struct.std::atomic.4", [56 x i8], %"class.JPH::Semaphore" }
%"class.JPH::JobSystem::Barrier" = type { ptr }
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i8 }
%"struct.std::atomic.0" = type { %"struct.std::__atomic_base.1" }
%"struct.std::__atomic_base.1" = type { ptr }
%"struct.std::atomic.2" = type { %"struct.std::__atomic_base.3" }
%"struct.std::__atomic_base.3" = type { i32 }
%"struct.std::atomic.4" = type { %"struct.std::__atomic_base.5" }
%"struct.std::__atomic_base.5" = type { i32 }
%"class.JPH::Semaphore" = type { %"struct.std::atomic.4", %union.sem_t, [24 x i8] }
%union.sem_t = type { i64, [24 x i8] }

@.str.1 = external hidden unnamed_addr constant [87 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [21 x i8], align 1

declare i32 @__gxx_personality_v0(...)

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jNS_15AssertLastParamE(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3JPH20JobSystemWithBarrier4InitEj(ptr nocapture noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) local_unnamed_addr #1 align 2 personality ptr @__gxx_personality_v0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %9, label %6

6:                                                ; preds = %2
  %7 = tail call noundef zeroext i1 @_ZN3JPH23AssertFailedParamHelperEPKcS1_jNS_15AssertLastParamE(ptr noundef nonnull @.str.5, ptr noundef nonnull @.str.1, i32 noundef 172)
  br i1 %7, label %8, label %9

8:                                                ; preds = %6
  tail call void asm sideeffect "int $$0x3", "~{dirflag},~{fpsr},~{flags}"() #4, !srcloc !12
  br label %9

9:                                                ; preds = %2, %6, %8
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %1, ptr %10, align 8, !tbaa !13
  %11 = zext i32 %1 to i64
  %12 = mul nuw nsw i64 %11, 16640
  %13 = or disjoint i64 %12, 64
  %14 = tail call noundef ptr @_ZN3JPH20JobSystemWithBarrier11BarrierImplnaEmSt11align_val_t(i64 noundef %13, i64 noundef 64)
  %15 = getelementptr inbounds i8, ptr %14, i64 56
  store i64 %11, ptr %15, align 8
  %.ptr7 = getelementptr inbounds i8, ptr %14, i64 64
  %16 = icmp eq i32 %1, 0
  br i1 %16, label %.loopexit12, label %17

17:                                               ; preds = %9
  %18 = getelementptr inbounds %"class.JPH::JobSystemWithBarrier::BarrierImpl", ptr %.ptr7, i64 %11
  br label %19

19:                                               ; preds = %20, %17
  %.idx = phi i64 [ 64, %17 ], [ %.add, %20 ]
  %.ptr.ptr = getelementptr inbounds i8, ptr %14, i64 %.idx
  invoke void @_ZN3JPH20JobSystemWithBarrier11BarrierImplC1Ev(ptr noundef nonnull align 64 dereferenceable(16640) %.ptr.ptr)
          to label %20 unwind label %22

20:                                               ; preds = %19
  %.add = add nuw nsw i64 %.idx, 16640
  %.ptr6 = getelementptr inbounds i8, ptr %14, i64 %.add
  %21 = icmp eq ptr %.ptr6, %18
  br i1 %21, label %.loopexit12, label %19

.loopexit12:                                      ; preds = %20, %9
  store ptr %.ptr7, ptr %3, align 8, !tbaa !5
  ret void

22:                                               ; preds = %19
  %23 = landingpad { ptr, i32 }
          cleanup
  %24 = icmp eq i64 %.idx, 64
  br i1 %24, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %22, %.preheader
  %.idx8 = phi i64 [ %.add9, %.preheader ], [ %.idx, %22 ]
  %.add9 = add nsw i64 %.idx8, -16640
  %.ptr11 = getelementptr inbounds i8, ptr %14, i64 %.add9
  tail call void @_ZN3JPH20JobSystemWithBarrier11BarrierImplD1Ev(ptr noundef nonnull align 64 dereferenceable(16640) %.ptr11) #4
  %25 = icmp eq i64 %.add9, 64
  br i1 %25, label %.loopexit, label %.preheader

.loopexit:                                        ; preds = %.preheader, %22
  tail call void @_ZN3JPH20JobSystemWithBarrier11BarrierImpldaEPvSt11align_val_t(ptr noundef %14, i64 noundef 64) #4
  resume { ptr, i32 } %23
}

; Function Attrs: alwaysinline mustprogress uwtable
declare dso_local noundef ptr @_ZN3JPH20JobSystemWithBarrier11BarrierImplnaEmSt11align_val_t(i64 noundef, i64 noundef) local_unnamed_addr #2 align 2

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local void @_ZN3JPH20JobSystemWithBarrier11BarrierImpldaEPvSt11align_val_t(ptr noundef, i64 noundef) local_unnamed_addr #3 align 2

declare void @_ZN3JPH20JobSystemWithBarrier11BarrierImplC1Ev(ptr) local_unnamed_addr

declare void @_ZN3JPH20JobSystemWithBarrier11BarrierImplD1Ev(ptr) local_unnamed_addr

attributes #0 = { inlinehint 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 = { 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 = { alwaysinline 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 #3 = { alwaysinline 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 #4 = { 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, !11, i64 16}
!6 = !{!"_ZTSN3JPH20JobSystemWithBarrierE", !7, i64 0, !8, i64 8, !11, i64 16}
!7 = !{!"_ZTSN3JPH9JobSystemE"}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{i64 2157459562}
!13 = !{!6, !8, i64 8}


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