; LLVM IR from idmillington/cyclone-physics :: src/world.ll (ZN7cyclone5World16generateContactsEv)

; 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.cyclone::World" = type <{ i8, [7 x i8], ptr, %"class.cyclone::ContactResolver", ptr, ptr, i32, [4 x i8] }>
%"class.cyclone::ContactResolver" = type <{ i32, i32, double, double, i32, i32, i8, [7 x i8] }>
%"struct.cyclone::World::ContactGenRegistration" = type { ptr, ptr }
%"class.cyclone::Contact" = type { [2 x ptr], double, double, %"class.cyclone::Vector3", %"class.cyclone::Vector3", double, %"class.cyclone::Matrix3", %"class.cyclone::Vector3", double, [2 x %"class.cyclone::Vector3"] }
%"class.cyclone::Matrix3" = type { [9 x double] }
%"class.cyclone::Vector3" = type { double, double, double, double }

; 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 i32 @_ZN7cyclone5World16generateContactsEv(ptr noundef nonnull align 8 dereferenceable(76) %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %8 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  %9 = getelementptr inbounds %"class.cyclone::World", ptr %8, i32 0, i32 6
  %10 = load i32, ptr %9, align 8, !tbaa !9
  store i32 %10, ptr %3, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %11 = getelementptr inbounds %"class.cyclone::World", ptr %8, i32 0, i32 5
  %12 = load ptr, ptr %11, align 8, !tbaa !16
  store ptr %12, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %13 = getelementptr inbounds %"class.cyclone::World", ptr %8, i32 0, i32 4
  %14 = load ptr, ptr %13, align 8, !tbaa !17
  store ptr %14, ptr %5, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %44, %1
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %45

18:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %"struct.cyclone::World::ContactGenRegistration", ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !18
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load i32, ptr %3, align 4, !tbaa !15
  %24 = load ptr, ptr %21, align 8, !tbaa !20
  %25 = getelementptr inbounds ptr, ptr %24, i64 0
  %26 = load ptr, ptr %25, align 8
  %27 = call noundef i32 %26(ptr noundef nonnull align 8 dereferenceable(8) %21, ptr noundef %22, i32 noundef %23)
  store i32 %27, ptr %6, align 4, !tbaa !15
  %28 = load i32, ptr %6, align 4, !tbaa !15
  %29 = load i32, ptr %3, align 4, !tbaa !15
  %30 = sub i32 %29, %28
  store i32 %30, ptr %3, align 4, !tbaa !15
  %31 = load i32, ptr %6, align 4, !tbaa !15
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = zext i32 %31 to i64
  %34 = getelementptr inbounds %"class.cyclone::Contact", ptr %32, i64 %33
  store ptr %34, ptr %4, align 8, !tbaa !5
  %35 = load i32, ptr %3, align 4, !tbaa !15
  %36 = icmp ule i32 %35, 0
  br i1 %36, label %37, label %38

37:                                               ; preds = %18
  store i32 3, ptr %7, align 4
  br label %42

38:                                               ; preds = %18
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %"struct.cyclone::World::ContactGenRegistration", ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !22
  store ptr %41, ptr %5, align 8, !tbaa !5
  store i32 0, ptr %7, align 4
  br label %42

42:                                               ; preds = %38, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %43 = load i32, ptr %7, align 4
  switch i32 %43, label %50 [
    i32 0, label %44
    i32 3, label %45
  ]

44:                                               ; preds = %42
  br label %15, !llvm.loop !23

45:                                               ; preds = %42, %15
  %46 = getelementptr inbounds %"class.cyclone::World", ptr %8, i32 0, i32 6
  %47 = load i32, ptr %46, align 8, !tbaa !9
  %48 = load i32, ptr %3, align 4, !tbaa !15
  %49 = sub i32 %47, %48
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %49

50:                                               ; preds = %42
  unreachable
}

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 = { 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, !13, i64 72}
!10 = !{!"_ZTSN7cyclone5WorldE", !11, i64 0, !6, i64 8, !12, i64 16, !6, i64 56, !6, i64 64, !13, i64 72}
!11 = !{!"bool", !7, i64 0}
!12 = !{!"_ZTSN7cyclone15ContactResolverE", !13, i64 0, !13, i64 4, !14, i64 8, !14, i64 16, !13, i64 24, !13, i64 28, !11, i64 32}
!13 = !{!"int", !7, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = !{!13, !13, i64 0}
!16 = !{!10, !6, i64 64}
!17 = !{!10, !6, i64 56}
!18 = !{!19, !6, i64 0}
!19 = !{!"_ZTSN7cyclone5World22ContactGenRegistrationE", !6, i64 0, !6, i64 8}
!20 = !{!21, !21, i64 0}
!21 = !{!"vtable pointer", !8, i64 0}
!22 = !{!19, !6, i64 8}
!23 = distinct !{!23, !24}
!24 = !{!"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.cyclone::Contact" = type { [2 x ptr], double, double, %"class.cyclone::Vector3", %"class.cyclone::Vector3", double, %"class.cyclone::Matrix3", %"class.cyclone::Vector3", double, [2 x %"class.cyclone::Vector3"] }
%"class.cyclone::Matrix3" = type { [9 x double] }
%"class.cyclone::Vector3" = type { double, double, double, double }

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7cyclone5World16generateContactsEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(76) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 72
  %3 = load i32, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 56
  %.01319 = load ptr, ptr %4, align 8, !tbaa !14
  %.not20 = icmp eq ptr %.01319, null
  br i1 %.not20, label %.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 64
  %6 = load ptr, ptr %5, align 8, !tbaa !15
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %13
  %.01323 = phi ptr [ %.013, %13 ], [ %.01319, %.lr.ph.preheader ]
  %.01222 = phi i32 [ %11, %13 ], [ %3, %.lr.ph.preheader ]
  %.01521 = phi ptr [ %15, %13 ], [ %6, %.lr.ph.preheader ]
  %7 = load ptr, ptr %.01323, align 8, !tbaa !16
  %8 = load ptr, ptr %7, align 8, !tbaa !18
  %9 = load ptr, ptr %8, align 8
  %10 = tail call noundef i32 %9(ptr noundef nonnull align 8 dereferenceable(8) %7, ptr noundef %.01521, i32 noundef %.01222)
  %11 = sub i32 %.01222, %10
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %.thread.loopexit, label %13

13:                                               ; preds = %.lr.ph
  %14 = zext i32 %10 to i64
  %15 = getelementptr inbounds %"class.cyclone::Contact", ptr %.01521, i64 %14
  %16 = getelementptr inbounds i8, ptr %.01323, i64 8
  %.013 = load ptr, ptr %16, align 8, !tbaa !14
  %.not = icmp eq ptr %.013, null
  br i1 %.not, label %.thread.loopexit, label %.lr.ph

.thread.loopexit:                                 ; preds = %.lr.ph, %13
  %.pre = load i32, ptr %2, align 8, !tbaa !5
  br label %.thread

.thread:                                          ; preds = %.thread.loopexit, %1
  %17 = phi i32 [ %3, %1 ], [ %.pre, %.thread.loopexit ]
  %.1 = phi i32 [ %3, %1 ], [ %11, %.thread.loopexit ]
  %18 = sub i32 %17, %.1
  ret i32 %18
}

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

!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, !12, i64 72}
!6 = !{!"_ZTSN7cyclone5WorldE", !7, i64 0, !10, i64 8, !11, i64 16, !10, i64 56, !10, i64 64, !12, i64 72}
!7 = !{!"bool", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_ZTSN7cyclone15ContactResolverE", !12, i64 0, !12, i64 4, !13, i64 8, !13, i64 16, !12, i64 24, !12, i64 28, !7, i64 32}
!12 = !{!"int", !8, i64 0}
!13 = !{!"double", !8, i64 0}
!14 = !{!10, !10, i64 0}
!15 = !{!6, !10, i64 64}
!16 = !{!17, !10, i64 0}
!17 = !{!"_ZTSN7cyclone5World22ContactGenRegistrationE", !10, i64 0, !10, i64 8}
!18 = !{!19, !19, i64 0}
!19 = !{!"vtable pointer", !9, i64 0}


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