; LLVM IR from mtsamis/box2d-optimized :: src/dynamics/b2_body.ll (ZN6b2Body10AddContactEP9b2Contact)

; 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.b2Body = type { i32, i16, i32, %struct.b2Transform, %struct.b2Transform, %struct.b2Sweep, %struct.b2Vec2, float, %struct.b2Vec2, float, ptr, ptr, ptr, ptr, i32, ptr, ptr, i32, i32, float, float, float, float, float, float, float, float, %struct.b2BodyUserData }
%struct.b2Transform = type { %struct.b2Vec2, %struct.b2Rot }
%struct.b2Rot = type { float, float }
%struct.b2Sweep = type { %struct.b2Vec2, %struct.b2Vec2, %struct.b2Vec2, float, float, float }
%struct.b2Vec2 = type { float, float }
%struct.b2BodyUserData = type { i64 }

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_Z7b2Alloci(i32 noundef) #1

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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6b2Body10AddContactEP9b2Contact(ptr noundef nonnull align 8 dereferenceable(208) %0, ptr noundef %1) #3 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8
  %7 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 17
  %8 = load i32, ptr %7, align 8, !tbaa !9
  %9 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 18
  %10 = load i32, ptr %9, align 4, !tbaa !21
  %11 = icmp eq i32 %8, %10
  br i1 %11, label %12, label %33

12:                                               ; preds = %2
  %13 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 18
  %14 = load i32, ptr %13, align 4, !tbaa !21
  %15 = mul nsw i32 %14, 2
  store i32 %15, ptr %13, align 4, !tbaa !21
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %16 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 18
  %17 = load i32, ptr %16, align 4, !tbaa !21
  %18 = sext i32 %17 to i64
  %19 = mul i64 %18, 8
  %20 = trunc i64 %19 to i32
  %21 = call noundef ptr @_Z7b2Alloci(i32 noundef %20)
  store ptr %21, ptr %5, align 8, !tbaa !5
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 16
  %24 = load ptr, ptr %23, align 8, !tbaa !22
  %25 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 17
  %26 = load i32, ptr %25, align 8, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = mul i64 %27, 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %22, ptr align 8 %24, i64 %28, i1 false)
  %29 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 16
  %30 = load ptr, ptr %29, align 8, !tbaa !22
  call void @_Z6b2FreePv(ptr noundef %30)
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 16
  store ptr %31, ptr %32, align 8, !tbaa !22
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  br label %33

33:                                               ; preds = %12, %2
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 16
  %36 = load ptr, ptr %35, align 8, !tbaa !22
  %37 = getelementptr inbounds %class.b2Body, ptr %6, i32 0, i32 17
  %38 = load i32, ptr %37, align 8, !tbaa !9
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %37, align 8, !tbaa !9
  %40 = sext i32 %38 to i64
  %41 = getelementptr inbounds ptr, ptr %36, i64 %40
  store ptr %34, ptr %41, align 8, !tbaa !5
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z6b2FreePv(ptr noundef) #1

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = { 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 160}
!10 = !{!"_ZTS6b2Body", !11, i64 0, !12, i64 4, !13, i64 8, !14, i64 12, !14, i64 28, !18, i64 44, !15, i64 80, !16, i64 88, !15, i64 92, !16, i64 100, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !13, i64 136, !6, i64 144, !6, i64 152, !13, i64 160, !13, i64 164, !16, i64 168, !16, i64 172, !16, i64 176, !16, i64 180, !16, i64 184, !16, i64 188, !16, i64 192, !16, i64 196, !19, i64 200}
!11 = !{!"_ZTS10b2BodyType", !7, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_ZTS11b2Transform", !15, i64 0, !17, i64 8}
!15 = !{!"_ZTS6b2Vec2", !16, i64 0, !16, i64 4}
!16 = !{!"float", !7, i64 0}
!17 = !{!"_ZTS5b2Rot", !16, i64 0, !16, i64 4}
!18 = !{!"_ZTS7b2Sweep", !15, i64 0, !15, i64 8, !15, i64 16, !16, i64 24, !16, i64 28, !16, i64 32}
!19 = !{!"_ZTS14b2BodyUserData", !20, i64 0}
!20 = !{!"long", !7, i64 0}
!21 = !{!10, !13, i64 164}
!22 = !{!10, !6, i64 152}


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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_Z7b2Alloci(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN6b2Body10AddContactEP9b2Contact(ptr nocapture noundef nonnull align 8 dereferenceable(208) %0, ptr noundef %1) local_unnamed_addr #2 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 160
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 164
  %6 = load i32, ptr %5, align 4, !tbaa !20
  %7 = icmp eq i32 %4, %6
  br i1 %7, label %8, label %._crit_edge

._crit_edge:                                      ; preds = %2
  %.phi.trans.insert = getelementptr inbounds i8, ptr %0, i64 152
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !21
  br label %18

8:                                                ; preds = %2
  %9 = shl nsw i32 %4, 1
  store i32 %9, ptr %5, align 4, !tbaa !20
  %10 = shl i32 %4, 4
  %11 = tail call noundef ptr @_Z7b2Alloci(i32 noundef %10)
  %12 = getelementptr inbounds i8, ptr %0, i64 152
  %13 = load ptr, ptr %12, align 8, !tbaa !21
  %14 = load i32, ptr %3, align 8, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = shl nsw i64 %15, 3
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %11, ptr align 8 %13, i64 %16, i1 false)
  %17 = load ptr, ptr %12, align 8, !tbaa !21
  tail call void @_Z6b2FreePv(ptr noundef %17)
  store ptr %11, ptr %12, align 8, !tbaa !21
  %.pre4 = load i32, ptr %3, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %._crit_edge, %8
  %19 = phi i32 [ %4, %._crit_edge ], [ %.pre4, %8 ]
  %20 = phi ptr [ %.pre, %._crit_edge ], [ %11, %8 ]
  %21 = add nsw i32 %19, 1
  store i32 %21, ptr %3, align 8, !tbaa !5
  %22 = sext i32 %19 to i64
  %23 = getelementptr inbounds ptr, ptr %20, i64 %22
  store ptr %1, ptr %23, align 8, !tbaa !22
  ret void
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local void @_Z6b2FreePv(ptr noundef) local_unnamed_addr #1

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

!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 160}
!6 = !{!"_ZTS6b2Body", !7, i64 0, !10, i64 4, !11, i64 8, !12, i64 12, !12, i64 28, !16, i64 44, !13, i64 80, !14, i64 88, !13, i64 92, !14, i64 100, !17, i64 104, !17, i64 112, !17, i64 120, !17, i64 128, !11, i64 136, !17, i64 144, !17, i64 152, !11, i64 160, !11, i64 164, !14, i64 168, !14, i64 172, !14, i64 176, !14, i64 180, !14, i64 184, !14, i64 188, !14, i64 192, !14, i64 196, !18, i64 200}
!7 = !{!"_ZTS10b2BodyType", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"_ZTS11b2Transform", !13, i64 0, !15, i64 8}
!13 = !{!"_ZTS6b2Vec2", !14, i64 0, !14, i64 4}
!14 = !{!"float", !8, i64 0}
!15 = !{!"_ZTS5b2Rot", !14, i64 0, !14, i64 4}
!16 = !{!"_ZTS7b2Sweep", !13, i64 0, !13, i64 8, !13, i64 16, !14, i64 24, !14, i64 28, !14, i64 32}
!17 = !{!"any pointer", !8, i64 0}
!18 = !{!"_ZTS14b2BodyUserData", !19, i64 0}
!19 = !{!"long", !8, i64 0}
!20 = !{!6, !11, i64 164}
!21 = !{!6, !17, i64 152}
!22 = !{!17, !17, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass