; LLVM IR from ashishzero/karma :: src/broad_phase.ll (Z27iterate_and_remove_linklistP4GridP10Rigid_Body)

; 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.Grid = type { %union.Vec2, float, i32, ptr }
%union.Vec2 = type { %struct.anon.1 }
%struct.anon.1 = type { float, float }
%struct.Rigid_Body = type { i16, i16, float, float, float, float, %union.Vec2, %union.Vec2, %struct.Transform, i32, ptr, %struct.Mm_Rect, %struct.Entity_Id, ptr, i32, i32 }
%struct.Transform = type { %union.Vec2, %union.Mat2 }
%union.Mat2 = type { [2 x %union.Vec2] }
%struct.Mm_Rect = type { %union.Vec2, %union.Vec2 }
%struct.Entity_Id = type { i64 }

; 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
define dso_local void @_Z27iterate_and_remove_linklistP4GridP10Rigid_Body(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store ptr null, ptr %6, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.Grid, ptr %7, i32 0, i32 3
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %9, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.Grid, ptr %10, i32 0, i32 2
  %12 = load i32, ptr %11, align 4, !tbaa !13
  %13 = add nsw i32 %12, -1
  store i32 %13, ptr %11, align 4, !tbaa !13
  br label %14

14:                                               ; preds = %18, %2
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = icmp ne ptr %15, %16
  br i1 %17, label %18, label %23

18:                                               ; preds = %14
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %19, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Rigid_Body, ptr %20, i32 0, i32 13
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  store ptr %22, ptr %5, align 8, !tbaa !5
  br label %14, !llvm.loop !22

23:                                               ; preds = %14
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = icmp eq ptr %24, null
  br i1 %25, label %26, label %32

26:                                               ; preds = %23
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.Rigid_Body, ptr %27, i32 0, i32 13
  %29 = load ptr, ptr %28, align 8, !tbaa !14
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Grid, ptr %30, i32 0, i32 3
  store ptr %29, ptr %31, align 8, !tbaa !9
  br label %38

32:                                               ; preds = %23
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.Rigid_Body, ptr %33, i32 0, i32 13
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Rigid_Body, ptr %36, i32 0, i32 13
  store ptr %35, ptr %37, align 8, !tbaa !14
  br label %38

38:                                               ; preds = %32, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

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 = { 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, !6, i64 16}
!10 = !{!"_ZTS4Grid", !7, i64 0, !11, i64 8, !12, i64 12, !6, i64 16}
!11 = !{!"float", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !12, i64 12}
!14 = !{!15, !6, i64 96}
!15 = !{!"_ZTS10Rigid_Body", !16, i64 0, !17, i64 2, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !7, i64 20, !7, i64 28, !18, i64 36, !12, i64 60, !6, i64 64, !19, i64 72, !20, i64 88, !6, i64 96, !12, i64 104, !12, i64 108}
!16 = !{!"_ZTS15Rigid_Body_Type", !7, i64 0}
!17 = !{!"short", !7, i64 0}
!18 = !{!"_ZTS9Transform", !7, i64 0, !7, i64 8}
!19 = !{!"_ZTS7Mm_Rect", !7, i64 0, !7, i64 8}
!20 = !{!"_ZTS9Entity_Id", !21, i64 0}
!21 = !{!"long", !7, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_Z27iterate_and_remove_linklistP4GridP10Rigid_Body(ptr nocapture noundef %0, ptr noundef readnone %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 12
  %6 = load i32, ptr %5, align 4, !tbaa !12
  %7 = add nsw i32 %6, -1
  store i32 %7, ptr %5, align 4, !tbaa !12
  %.not12 = icmp eq ptr %4, %1
  br i1 %.not12, label %10, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01113 = phi ptr [ %9, %.lr.ph ], [ %4, %2 ]
  %8 = getelementptr inbounds i8, ptr %.01113, i64 96
  %9 = load ptr, ptr %8, align 8, !tbaa !13
  %.not = icmp eq ptr %9, %1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !21

10:                                               ; preds = %2
  %11 = getelementptr inbounds i8, ptr %4, i64 96
  %12 = load ptr, ptr %11, align 8, !tbaa !13
  store ptr %12, ptr %3, align 8, !tbaa !5
  br label %16

._crit_edge:                                      ; preds = %.lr.ph
  %13 = getelementptr inbounds i8, ptr %9, i64 96
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %.01113, i64 96
  store ptr %14, ptr %15, align 8, !tbaa !13
  br label %16

16:                                               ; preds = %._crit_edge, %10
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 16}
!6 = !{!"_ZTS4Grid", !7, i64 0, !9, i64 8, !10, i64 12, !11, i64 16}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!"float", !7, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!6, !10, i64 12}
!13 = !{!14, !11, i64 96}
!14 = !{!"_ZTS10Rigid_Body", !15, i64 0, !16, i64 2, !9, i64 4, !9, i64 8, !9, i64 12, !9, i64 16, !7, i64 20, !7, i64 28, !17, i64 36, !10, i64 60, !11, i64 64, !18, i64 72, !19, i64 88, !11, i64 96, !10, i64 104, !10, i64 108}
!15 = !{!"_ZTS15Rigid_Body_Type", !7, i64 0}
!16 = !{!"short", !7, i64 0}
!17 = !{!"_ZTS9Transform", !7, i64 0, !7, i64 8}
!18 = !{!"_ZTS7Mm_Rect", !7, i64 0, !7, i64 8}
!19 = !{!"_ZTS9Entity_Id", !20, i64 0}
!20 = !{!"long", !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


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