; LLVM IR from Mithreindeir/VelocityRaptor :: VelocityRaptor/src (vrCollision)

; 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.vrPolygonShape = type { ptr, i32, ptr, i32, %struct.vrVec2 }
%struct.vrVec2 = type { float, float }

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @vrPolyGetFarthestVertex(ptr noundef byval(%struct.vrPolygonShape) align 8 %0, <2 x float> %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca %struct.vrVec2, align 4
  %5 = alloca i32, align 4
  %6 = alloca %struct.vrVec2, align 4
  %7 = alloca float, align 4
  %8 = alloca i32, align 4
  %9 = alloca %struct.vrVec2, align 4
  %10 = alloca float, align 4
  store <2 x float> %1, ptr %4, align 4
  %11 = getelementptr inbounds %struct.vrPolygonShape, ptr %0, i32 0, i32 1
  %12 = load i32, ptr %11, align 8, !tbaa !5
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  store i32 -1, ptr %3, align 4
  br label %49

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %16 = getelementptr inbounds %struct.vrPolygonShape, ptr %0, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !14
  %18 = getelementptr inbounds %struct.vrVec2, ptr %17, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 4 %18, i64 8, i1 false), !tbaa.struct !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %19 = load <2 x float>, ptr %4, align 4
  %20 = load <2 x float>, ptr %6, align 4
  %21 = call float @vrDot(<2 x float> %19, <2 x float> %20)
  store float %21, ptr %7, align 4, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 1, ptr %8, align 4, !tbaa !13
  br label %22

22:                                               ; preds = %44, %15
  %23 = load i32, ptr %8, align 4, !tbaa !13
  %24 = getelementptr inbounds %struct.vrPolygonShape, ptr %0, i32 0, i32 1
  %25 = load i32, ptr %24, align 8, !tbaa !5
  %26 = icmp slt i32 %23, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %47

28:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %29 = getelementptr inbounds %struct.vrPolygonShape, ptr %0, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !14
  %31 = load i32, ptr %8, align 4, !tbaa !13
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds %struct.vrVec2, ptr %30, i64 %32
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %9, ptr align 4 %33, i64 8, i1 false), !tbaa.struct !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %34 = load <2 x float>, ptr %4, align 4
  %35 = load <2 x float>, ptr %9, align 4
  %36 = call float @vrDot(<2 x float> %34, <2 x float> %35)
  store float %36, ptr %10, align 4, !tbaa !16
  %37 = load float, ptr %10, align 4, !tbaa !16
  %38 = load float, ptr %7, align 4, !tbaa !16
  %39 = fcmp ogt float %37, %38
  br i1 %39, label %40, label %43

40:                                               ; preds = %28
  %41 = load i32, ptr %8, align 4, !tbaa !13
  store i32 %41, ptr %5, align 4, !tbaa !13
  %42 = load float, ptr %10, align 4, !tbaa !16
  store float %42, ptr %7, align 4, !tbaa !16
  br label %43

43:                                               ; preds = %40, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %8, align 4, !tbaa !13
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %8, align 4, !tbaa !13
  br label %22, !llvm.loop !17

47:                                               ; preds = %27
  %48 = load i32, ptr %5, align 4, !tbaa !13
  store i32 %48, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %49

49:                                               ; preds = %47, %14
  %50 = load i32, ptr %3, align 4
  ret i32 %50
}

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

declare float @vrDot(<2 x float>, <2 x float>) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind uwtable "min-legal-vector-width"="64" "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 = { "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 8}
!6 = !{!"vrPolygonShape", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24, !11, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"vrVec2", !12, i64 0, !12, i64 4}
!12 = !{!"float", !8, i64 0}
!13 = !{!10, !10, i64 0}
!14 = !{!6, !7, i64 0}
!15 = !{i64 0, i64 4, !16, i64 4, i64 4, !16}
!16 = !{!12, !12, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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.vrPolygonShape = type { ptr, i32, ptr, i32, %struct.vrVec2 }
%struct.vrVec2 = type { float, float }

; Function Attrs: nounwind uwtable
define dso_local i32 @vrPolyGetFarthestVertex(ptr nocapture noundef readonly byval(%struct.vrPolygonShape) align 8 %0, <2 x float> %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i32, ptr %3, align 8, !tbaa !5
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %2
  %7 = load ptr, ptr %0, align 8, !tbaa !13
  %.sroa.06.0.copyload = load <2 x float>, ptr %7, align 4
  %8 = tail call float @vrDot(<2 x float> %1, <2 x float> %.sroa.06.0.copyload) #2
  %9 = icmp sgt i32 %4, 1
  br i1 %9, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %6
  %wide.trip.count = zext nneg i32 %4 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %.01118 = phi i32 [ 0, %.lr.ph.preheader ], [ %.1, %.lr.ph ]
  %.01316 = phi float [ %8, %.lr.ph.preheader ], [ %.114, %.lr.ph ]
  %10 = getelementptr inbounds %struct.vrVec2, ptr %7, i64 %indvars.iv
  %.sroa.0.0.copyload = load <2 x float>, ptr %10, align 4
  %11 = tail call float @vrDot(<2 x float> %1, <2 x float> %.sroa.0.0.copyload) #2
  %12 = fcmp ogt float %11, %.01316
  %.114 = select i1 %12, float %11, float %.01316
  %13 = trunc nuw nsw i64 %indvars.iv to i32
  %.1 = select i1 %12, i32 %13, i32 %.01118
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !14

.loopexit:                                        ; preds = %.lr.ph, %6, %2
  %.0 = phi i32 [ -1, %2 ], [ 0, %6 ], [ %.1, %.lr.ph ]
  ret i32 %.0
}

declare float @vrDot(<2 x float>, <2 x float>) local_unnamed_addr #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="64" "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 = { "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, !10, i64 8}
!6 = !{!"vrPolygonShape", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24, !11, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"vrVec2", !12, i64 0, !12, i64 4}
!12 = !{!"float", !8, i64 0}
!13 = !{!6, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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