; LLVM IR from ashishzero/karma :: src/modules/core/lin_maths.ll (Z25mm_rect_enclosing_polygonRK7Polygon)

; 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.Mm_Rect = type { %union.Vec2, %union.Vec2 }
%union.Vec2 = type { %struct.anon.1 }
%struct.anon.1 = type { float, float }
%struct.Polygon = type { i32, [3 x %union.Vec2] }

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local <2 x float> @_Z8vec2_min4Vec2S_(<2 x float>, <2 x float>) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local <2 x float> @_Z8vec2_max4Vec2S_(<2 x float>, <2 x float>) #2

; Function Attrs: mustprogress uwtable
define dso_local { <2 x float>, <2 x float> } @_Z25mm_rect_enclosing_polygonRK7Polygon(ptr noundef nonnull align 4 dereferenceable(28) %0) #3 {
  %2 = alloca %struct.Mm_Rect, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca %union.Vec2, align 4
  %7 = alloca %union.Vec2, align 4
  %8 = alloca %union.Vec2, align 4
  %9 = alloca %union.Vec2, align 4
  %10 = alloca %union.Vec2, align 4
  %11 = alloca %union.Vec2, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.Polygon, ptr %12, i32 0, i32 1
  %14 = getelementptr inbounds [3 x %union.Vec2], ptr %13, i64 0, i64 0
  %15 = getelementptr inbounds %struct.Mm_Rect, ptr %2, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %15, ptr align 4 %14, i64 8, i1 false), !tbaa.struct !9
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Polygon, ptr %16, i32 0, i32 1
  %18 = getelementptr inbounds [3 x %union.Vec2], ptr %17, i64 0, i64 0
  %19 = getelementptr inbounds %struct.Mm_Rect, ptr %2, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %19, ptr align 4 %18, i64 8, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 1, ptr %4, align 4, !tbaa !11
  br label %20

20:                                               ; preds = %52, %1
  %21 = load i32, ptr %4, align 4, !tbaa !11
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.Polygon, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %23, align 4, !tbaa !13
  %25 = icmp ult i32 %21, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %55

27:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Polygon, ptr %28, i32 0, i32 1
  %30 = getelementptr inbounds [3 x %union.Vec2], ptr %29, i64 0, i64 0
  %31 = load i32, ptr %4, align 4, !tbaa !11
  %32 = zext i32 %31 to i64
  %33 = getelementptr inbounds %union.Vec2, ptr %30, i64 %32
  store ptr %33, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %34 = getelementptr inbounds %struct.Mm_Rect, ptr %2, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %7, ptr align 4 %34, i64 8, i1 false), !tbaa.struct !9
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %8, ptr align 4 %35, i64 8, i1 false), !tbaa.struct !9
  %36 = getelementptr inbounds %union.Vec2, ptr %7, i32 0, i32 0
  %37 = load <2 x float>, ptr %36, align 4
  %38 = getelementptr inbounds %union.Vec2, ptr %8, i32 0, i32 0
  %39 = load <2 x float>, ptr %38, align 4
  %40 = call <2 x float> @_Z8vec2_min4Vec2S_(<2 x float> %37, <2 x float> %39)
  %41 = getelementptr inbounds %union.Vec2, ptr %6, i32 0, i32 0
  store <2 x float> %40, ptr %41, align 4
  %42 = getelementptr inbounds %struct.Mm_Rect, ptr %2, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %42, ptr align 4 %6, i64 8, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %43 = getelementptr inbounds %struct.Mm_Rect, ptr %2, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %10, ptr align 4 %43, i64 8, i1 false), !tbaa.struct !9
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %11, ptr align 4 %44, i64 8, i1 false), !tbaa.struct !9
  %45 = getelementptr inbounds %union.Vec2, ptr %10, i32 0, i32 0
  %46 = load <2 x float>, ptr %45, align 4
  %47 = getelementptr inbounds %union.Vec2, ptr %11, i32 0, i32 0
  %48 = load <2 x float>, ptr %47, align 4
  %49 = call <2 x float> @_Z8vec2_max4Vec2S_(<2 x float> %46, <2 x float> %48)
  %50 = getelementptr inbounds %union.Vec2, ptr %9, i32 0, i32 0
  store <2 x float> %49, ptr %50, align 4
  %51 = getelementptr inbounds %struct.Mm_Rect, ptr %2, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %51, ptr align 4 %9, i64 8, i1 false), !tbaa.struct !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  br label %52

52:                                               ; preds = %27
  %53 = load i32, ptr %4, align 4, !tbaa !11
  %54 = add i32 %53, 1
  store i32 %54, ptr %4, align 4, !tbaa !11
  br label %20, !llvm.loop !15

55:                                               ; preds = %26
  %56 = load { <2 x float>, <2 x float> }, ptr %2, align 4
  ret { <2 x float>, <2 x float> } %56
}

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint mustprogress 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 = { mustprogress 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 #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 = !{i64 0, i64 8, !10}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"_ZTS7Polygon", !12, i64 0, !7, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%union.Vec2 = type { %struct.anon.1 }
%struct.anon.1 = type { float, float }

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local <2 x float> @_Z8vec2_min4Vec2S_(<2 x float>, <2 x float>) local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local <2 x float> @_Z8vec2_max4Vec2S_(<2 x float>, <2 x float>) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local { <2 x float>, <2 x float> } @_Z25mm_rect_enclosing_polygonRK7Polygon(ptr nocapture noundef nonnull readonly align 4 dereferenceable(28) %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 4
  %.sroa.013.0.copyload = load <2 x float>, ptr %2, align 4, !tbaa !5
  %3 = load i32, ptr %0, align 4, !tbaa !8
  %4 = icmp ugt i32 %3, 1
  br i1 %4, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %1
  %.sroa.013.0.lcssa = phi <2 x float> [ %.sroa.013.0.copyload, %1 ], [ %6, %.lr.ph ]
  %.sroa.4.0.lcssa = phi <2 x float> [ %.sroa.013.0.copyload, %1 ], [ %7, %.lr.ph ]
  %.fca.0.insert = insertvalue { <2 x float>, <2 x float> } poison, <2 x float> %.sroa.013.0.lcssa, 0
  %.fca.1.insert = insertvalue { <2 x float>, <2 x float> } %.fca.0.insert, <2 x float> %.sroa.4.0.lcssa, 1
  ret { <2 x float>, <2 x float> } %.fca.1.insert

.lr.ph:                                           ; preds = %1, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %1 ]
  %.sroa.4.018 = phi <2 x float> [ %7, %.lr.ph ], [ %.sroa.013.0.copyload, %1 ]
  %.sroa.013.017 = phi <2 x float> [ %6, %.lr.ph ], [ %.sroa.013.0.copyload, %1 ]
  %5 = getelementptr inbounds %union.Vec2, ptr %2, i64 %indvars.iv
  %.sroa.03.0.copyload = load <2 x float>, ptr %5, align 4, !tbaa !5
  %6 = tail call <2 x float> @_Z8vec2_min4Vec2S_(<2 x float> %.sroa.013.017, <2 x float> %.sroa.03.0.copyload)
  %.sroa.0.0.copyload = load <2 x float>, ptr %5, align 4, !tbaa !5
  %7 = tail call <2 x float> @_Z8vec2_max4Vec2S_(<2 x float> %.sroa.4.018, <2 x float> %.sroa.0.0.copyload)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %8 = load i32, ptr %0, align 4, !tbaa !8
  %9 = zext i32 %8 to i64
  %10 = icmp ult i64 %indvars.iv.next, %9
  br i1 %10, label %.lr.ph, label %._crit_edge, !llvm.loop !11
}

attributes #0 = { inlinehint mustprogress 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 = { mustprogress 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" }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !10, i64 0}
!9 = !{!"_ZTS7Polygon", !10, i64 0, !6, i64 4}
!10 = !{!"int", !6, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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