; LLVM IR from jrouwe/JoltPhysics :: Jolt/Physics/IslandBuilder.ll (ZN3JPH13IslandBuilder10LinkBodiesEjj)

; 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.JPH::IslandBuilder" = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i32, i32, i32 }
%"struct.JPH::IslandBuilder::BodyLink" = type { %"struct.std::atomic", i32 }
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i32 }

; 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
declare dso_local noundef i32 @_ZNK3JPH13IslandBuilder18GetLowestBodyIndexEj(ptr noundef nonnull align 8 dereferenceable(112), i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN3JPH13IslandBuilder10LinkBodiesEjj(ptr noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %1, i32 noundef %2) #2 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %10 = load ptr, ptr %4, align 8
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %10, i32 0, i32 11
  %13 = load i32, ptr %12, align 8, !tbaa !11
  %14 = icmp uge i32 %11, %13
  br i1 %14, label %20, label %15

15:                                               ; preds = %3
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %10, i32 0, i32 11
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = icmp uge i32 %16, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %15, %3
  br label %79

21:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %22 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %22, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %23 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %23, ptr %8, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %56, %45, %21
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = call noundef i32 @_ZNK3JPH13IslandBuilder18GetLowestBodyIndexEj(ptr noundef nonnull align 8 dereferenceable(112) %10, i32 noundef %25)
  store i32 %26, ptr %7, align 4, !tbaa !9
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = call noundef i32 @_ZNK3JPH13IslandBuilder18GetLowestBodyIndexEj(ptr noundef nonnull align 8 dereferenceable(112) %10, i32 noundef %27)
  store i32 %28, ptr %8, align 4, !tbaa !9
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = icmp ne i32 %29, %30
  br i1 %31, label %32, label %59

32:                                               ; preds = %24
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = icmp ult i32 %33, %34
  br i1 %35, label %36, label %47

36:                                               ; preds = %32
  %37 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %10, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !13
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = zext i32 %39 to i64
  %41 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %38, i64 %40
  %42 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %41, i32 0, i32 0
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = call noundef zeroext i1 @_ZNSt13__atomic_baseIjE21compare_exchange_weakERjjSt12memory_order(ptr noundef nonnull align 4 dereferenceable(4) %42, ptr noundef nonnull align 4 dereferenceable(4) %8, i32 noundef %43, i32 noundef 0) #5
  br i1 %44, label %46, label %45

45:                                               ; preds = %36
  br label %24, !llvm.loop !14

46:                                               ; preds = %36
  br label %58

47:                                               ; preds = %32
  %48 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %10, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %49, i64 %51
  %53 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %52, i32 0, i32 0
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = call noundef zeroext i1 @_ZNSt13__atomic_baseIjE21compare_exchange_weakERjjSt12memory_order(ptr noundef nonnull align 4 dereferenceable(4) %53, ptr noundef nonnull align 4 dereferenceable(4) %7, i32 noundef %54, i32 noundef 0) #5
  br i1 %55, label %57, label %56

56:                                               ; preds = %47
  br label %24, !llvm.loop !14

57:                                               ; preds = %47
  br label %58

58:                                               ; preds = %57, %46
  br label %59

59:                                               ; preds = %58, %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %60 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIjERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %7, ptr noundef nonnull align 4 dereferenceable(4) %8)
  %61 = load i32, ptr %60, align 4, !tbaa !9
  store i32 %61, ptr %9, align 4, !tbaa !9
  %62 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %10, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !13
  %64 = load i32, ptr %5, align 4, !tbaa !9
  %65 = zext i32 %64 to i64
  %66 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %63, i64 %65
  %67 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %66, i32 0, i32 0
  %68 = load i32, ptr %9, align 4, !tbaa !9
  %69 = call noundef zeroext i1 @_ZN3JPH9AtomicMinIjEEbRSt6atomicIT_ES2_St12memory_order(ptr noundef nonnull align 4 dereferenceable(4) %67, i32 noundef %68, i32 noundef 0)
  %70 = getelementptr inbounds %"class.JPH::IslandBuilder", ptr %10, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !13
  %72 = load i32, ptr %6, align 4, !tbaa !9
  %73 = zext i32 %72 to i64
  %74 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %71, i64 %73
  %75 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %74, i32 0, i32 0
  %76 = load i32, ptr %9, align 4, !tbaa !9
  %77 = call noundef zeroext i1 @_ZN3JPH9AtomicMinIjEEbRSt6atomicIT_ES2_St12memory_order(ptr noundef nonnull align 4 dereferenceable(4) %75, i32 noundef %76, i32 noundef 0)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %78

78:                                               ; preds = %59
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %79

79:                                               ; preds = %78, %20
  ret void
}

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNSt13__atomic_baseIjE21compare_exchange_weakERjjSt12memory_order(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4), i32 noundef, i32 noundef) #3 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIjERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN3JPH9AtomicMinIjEEbRSt6atomicIT_ES2_St12memory_order(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef, i32 noundef) #1

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 = { 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 = { inlinehint 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, !10, i64 88}
!12 = !{!"_ZTSN3JPH13IslandBuilderE", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108}
!13 = !{!12, !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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.JPH::IslandBuilder::BodyLink" = type { %"struct.std::atomic", i32 }
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i32 }

; 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 nounwind uwtable
declare dso_local noundef i32 @_ZNK3JPH13IslandBuilder18GetLowestBodyIndexEj(ptr noundef nonnull align 8 dereferenceable(112), i32 noundef) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3JPH13IslandBuilder10LinkBodiesEjj(ptr noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #1 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = getelementptr inbounds i8, ptr %0, i64 88
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %.not = icmp ugt i32 %7, %1
  %.not12 = icmp ugt i32 %7, %2
  %or.cond = and i1 %.not, %.not12
  br i1 %or.cond, label %8, label %38

8:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #4
  %9 = tail call noundef i32 @_ZNK3JPH13IslandBuilder18GetLowestBodyIndexEj(ptr noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %1)
  store i32 %9, ptr %4, align 4, !tbaa !11
  %10 = tail call noundef i32 @_ZNK3JPH13IslandBuilder18GetLowestBodyIndexEj(ptr noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %2)
  store i32 %10, ptr %5, align 4, !tbaa !11
  %.not1314 = icmp eq i32 %9, %10
  br i1 %.not1314, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %8, %.backedge
  %11 = phi i32 [ %23, %.backedge ], [ %9, %8 ]
  %12 = phi i32 [ %22, %.backedge ], [ %10, %8 ]
  %13 = icmp ult i32 %11, %12
  %14 = load ptr, ptr %0, align 8, !tbaa !12
  br i1 %13, label %15, label %24

15:                                               ; preds = %.lr.ph
  %16 = zext i32 %12 to i64
  %17 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %14, i64 %16
  %18 = call noundef zeroext i1 @_ZNSt13__atomic_baseIjE21compare_exchange_weakERjjSt12memory_order(ptr noundef nonnull align 4 dereferenceable(4) %17, ptr noundef nonnull align 4 dereferenceable(4) %5, i32 noundef %11, i32 noundef 0) #4
  br i1 %18, label %._crit_edge, label %.backedge

.backedge:                                        ; preds = %15, %24
  %19 = load i32, ptr %4, align 4, !tbaa !11
  %20 = call noundef i32 @_ZNK3JPH13IslandBuilder18GetLowestBodyIndexEj(ptr noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %19)
  store i32 %20, ptr %4, align 4, !tbaa !11
  %21 = load i32, ptr %5, align 4, !tbaa !11
  %22 = call noundef i32 @_ZNK3JPH13IslandBuilder18GetLowestBodyIndexEj(ptr noundef nonnull align 8 dereferenceable(112) %0, i32 noundef %21)
  store i32 %22, ptr %5, align 4, !tbaa !11
  %23 = load i32, ptr %4, align 4, !tbaa !11
  %.not13 = icmp eq i32 %23, %22
  br i1 %.not13, label %._crit_edge, label %.lr.ph, !llvm.loop !13

24:                                               ; preds = %.lr.ph
  %25 = zext i32 %11 to i64
  %26 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %14, i64 %25
  %27 = call noundef zeroext i1 @_ZNSt13__atomic_baseIjE21compare_exchange_weakERjjSt12memory_order(ptr noundef nonnull align 4 dereferenceable(4) %26, ptr noundef nonnull align 4 dereferenceable(4) %4, i32 noundef %12, i32 noundef 0) #4
  br i1 %27, label %._crit_edge, label %.backedge

._crit_edge:                                      ; preds = %.backedge, %24, %15, %8
  %28 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIjERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %4, ptr noundef nonnull align 4 dereferenceable(4) %5)
  %29 = load i32, ptr %28, align 4, !tbaa !11
  %30 = load ptr, ptr %0, align 8, !tbaa !12
  %31 = zext i32 %1 to i64
  %32 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %30, i64 %31
  %33 = call noundef zeroext i1 @_ZN3JPH9AtomicMinIjEEbRSt6atomicIT_ES2_St12memory_order(ptr noundef nonnull align 4 dereferenceable(4) %32, i32 noundef %29, i32 noundef 0)
  %34 = load ptr, ptr %0, align 8, !tbaa !12
  %35 = zext i32 %2 to i64
  %36 = getelementptr inbounds %"struct.JPH::IslandBuilder::BodyLink", ptr %34, i64 %35
  %37 = call noundef zeroext i1 @_ZN3JPH9AtomicMinIjEEbRSt6atomicIT_ES2_St12memory_order(ptr noundef nonnull align 4 dereferenceable(4) %36, i32 noundef %29, i32 noundef 0)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #4
  br label %38

38:                                               ; preds = %3, %._crit_edge
  ret void
}

; Function Attrs: alwaysinline mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNSt13__atomic_baseIjE21compare_exchange_weakERjjSt12memory_order(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4), i32 noundef, i32 noundef) local_unnamed_addr #2 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIjERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZN3JPH9AtomicMinIjEEbRSt6atomicIT_ES2_St12memory_order(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { 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 #3 = { inlinehint 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, !10, i64 88}
!6 = !{!"_ZTSN3JPH13IslandBuilderE", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104, !10, i64 108}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!6, !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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