; LLVM IR from aap/librw :: src/ps2/ps2.ll (ZN2rw3ps211instanceXYZEPjPNS_8GeometryEPNS_4MeshEjj)

; 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.rw::Geometry" = type { %"struct.rw::Object", i32, i16, i32, i32, i32, i32, ptr, ptr, [8 x ptr], ptr, %"struct.rw::MaterialList", ptr, ptr, i32 }
%"struct.rw::Object" = type { i8, i8, i8, i8, ptr }
%"struct.rw::MaterialList" = type { ptr, i32, i32 }
%"struct.rw::MorphTarget" = type { ptr, %"struct.rw::Sphere", ptr, ptr }
%"struct.rw::Sphere" = type { %"struct.rw::V3d", float }
%"struct.rw::V3d" = type { float, float, float }
%"struct.rw::Mesh" = type { ptr, i32, ptr }

; 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 noundef ptr @_ZN2rw3ps211instanceXYZEPjPNS_8GeometryEPNS_4MeshEjj(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i16, align 2
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  %15 = getelementptr inbounds %"struct.rw::Geometry", ptr %14, i32 0, i32 10
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = getelementptr inbounds %"struct.rw::MorphTarget", ptr %16, i64 0
  %18 = getelementptr inbounds %"struct.rw::MorphTarget", ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !16
  store ptr %19, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %20 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %20, ptr %13, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %66, %5
  %22 = load i32, ptr %13, align 4, !tbaa !9
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = load i32, ptr %10, align 4, !tbaa !9
  %25 = add i32 %23, %24
  %26 = icmp ult i32 %22, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %69

28:                                               ; preds = %21
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %"struct.rw::Mesh", ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !21
  %32 = load i32, ptr %13, align 4, !tbaa !9
  %33 = zext i32 %32 to i64
  %34 = getelementptr inbounds i16, ptr %31, i64 %33
  %35 = load i16, ptr %34, align 2, !tbaa !23
  store i16 %35, ptr %11, align 2, !tbaa !23
  %36 = load ptr, ptr %12, align 8, !tbaa !5
  %37 = load i16, ptr %11, align 2, !tbaa !23
  %38 = zext i16 %37 to i32
  %39 = mul nsw i32 %38, 3
  %40 = add nsw i32 %39, 0
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %36, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !9
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds i32, ptr %44, i32 1
  store ptr %45, ptr %6, align 8, !tbaa !5
  store i32 %43, ptr %44, align 4, !tbaa !9
  %46 = load ptr, ptr %12, align 8, !tbaa !5
  %47 = load i16, ptr %11, align 2, !tbaa !23
  %48 = zext i16 %47 to i32
  %49 = mul nsw i32 %48, 3
  %50 = add nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i32, ptr %46, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = getelementptr inbounds i32, ptr %54, i32 1
  store ptr %55, ptr %6, align 8, !tbaa !5
  store i32 %53, ptr %54, align 4, !tbaa !9
  %56 = load ptr, ptr %12, align 8, !tbaa !5
  %57 = load i16, ptr %11, align 2, !tbaa !23
  %58 = zext i16 %57 to i32
  %59 = mul nsw i32 %58, 3
  %60 = add nsw i32 %59, 2
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i32, ptr %56, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !9
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = getelementptr inbounds i32, ptr %64, i32 1
  store ptr %65, ptr %6, align 8, !tbaa !5
  store i32 %63, ptr %64, align 4, !tbaa !9
  br label %66

66:                                               ; preds = %28
  %67 = load i32, ptr %13, align 4, !tbaa !9
  %68 = add i32 %67, 1
  store i32 %68, ptr %13, align 4, !tbaa !9
  br label %21, !llvm.loop !24

69:                                               ; preds = %27
  br label %70

70:                                               ; preds = %75, %69
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = ptrtoint ptr %71 to i64
  %73 = urem i64 %72, 16
  %74 = icmp ne i64 %73, 0
  br i1 %74, label %75, label %78

75:                                               ; preds = %70
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = getelementptr inbounds i32, ptr %76, i32 1
  store ptr %77, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %76, align 4, !tbaa !9
  br label %70, !llvm.loop !26

78:                                               ; preds = %70
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #2
  ret ptr %79
}

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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 120}
!12 = !{!"_ZTSN2rw8GeometryE", !13, i64 0, !10, i64 16, !14, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !6, i64 40, !6, i64 48, !7, i64 56, !6, i64 120, !15, i64 128, !6, i64 144, !6, i64 152, !10, i64 160}
!13 = !{!"_ZTSN2rw6ObjectE", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !6, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!"_ZTSN2rw12MaterialListE", !6, i64 0, !10, i64 8, !10, i64 12}
!16 = !{!17, !6, i64 24}
!17 = !{!"_ZTSN2rw11MorphTargetE", !6, i64 0, !18, i64 8, !6, i64 24, !6, i64 32}
!18 = !{!"_ZTSN2rw6SphereE", !19, i64 0, !20, i64 12}
!19 = !{!"_ZTSN2rw3V3dE", !20, i64 0, !20, i64 4, !20, i64 8}
!20 = !{!"float", !7, i64 0}
!21 = !{!22, !6, i64 0}
!22 = !{!"_ZTSN2rw4MeshE", !6, i64 0, !10, i64 8, !6, i64 16}
!23 = !{!14, !14, i64 0}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = distinct !{!26, !25}


; 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 noundef ptr @_ZN2rw3ps211instanceXYZEPjPNS_8GeometryEPNS_4MeshEjj(ptr noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = getelementptr inbounds i8, ptr %1, i64 120
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 24
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  %10 = add i32 %4, %3
  %11 = icmp ugt i32 %10, %3
  br i1 %11, label %.lr.ph, label %.preheader

.lr.ph:                                           ; preds = %5
  %12 = load ptr, ptr %2, align 8, !tbaa !19
  %13 = zext i32 %3 to i64
  %wide.trip.count = zext i32 %10 to i64
  %14 = sub nsw i64 %wide.trip.count, %13
  %xtraiter = and i64 %14, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.loopexit.unr-lcssa

.prol.loopexit.unr-lcssa:                         ; preds = %.lr.ph
  %15 = getelementptr inbounds i16, ptr %12, i64 %13
  %16 = load i16, ptr %15, align 2, !tbaa !21
  %17 = zext i16 %16 to i64
  %.idx.prol = mul nuw nsw i64 %17, 12
  %18 = getelementptr inbounds i8, ptr %9, i64 %.idx.prol
  %19 = load i32, ptr %18, align 4, !tbaa !22
  %20 = getelementptr inbounds i8, ptr %0, i64 4
  store i32 %19, ptr %0, align 4, !tbaa !22
  %21 = getelementptr inbounds i8, ptr %18, i64 4
  %22 = load i32, ptr %21, align 4, !tbaa !22
  %23 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %22, ptr %20, align 4, !tbaa !22
  %24 = getelementptr inbounds i8, ptr %18, i64 8
  %25 = load i32, ptr %24, align 4, !tbaa !22
  %26 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %25, ptr %23, align 4, !tbaa !22
  %indvars.iv.next.prol = add nuw nsw i64 %13, 1
  br label %.prol.loopexit

.prol.loopexit:                                   ; preds = %.prol.loopexit.unr-lcssa, %.lr.ph
  %.lcssa.unr = phi ptr [ poison, %.lr.ph ], [ %26, %.prol.loopexit.unr-lcssa ]
  %indvars.iv.unr = phi i64 [ %13, %.lr.ph ], [ %indvars.iv.next.prol, %.prol.loopexit.unr-lcssa ]
  %.01920.unr = phi ptr [ %0, %.lr.ph ], [ %26, %.prol.loopexit.unr-lcssa ]
  %27 = add nsw i64 %wide.trip.count, -1
  %28 = icmp eq i64 %27, %13
  br i1 %28, label %.preheader, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %12, i64 2
  br label %34

.preheader:                                       ; preds = %.prol.loopexit, %34, %5
  %.019.lcssa = phi ptr [ %0, %5 ], [ %.lcssa.unr, %.prol.loopexit ], [ %57, %34 ]
  %29 = ptrtoint ptr %.019.lcssa to i64
  %30 = and i64 %29, 15
  %.not22 = icmp eq i64 %30, 0
  br i1 %.not22, label %._crit_edge, label %.lr.ph24.preheader

.lr.ph24.preheader:                               ; preds = %.preheader
  %31 = sub i64 12, %29
  %32 = and i64 %31, 12
  %33 = add nuw nsw i64 %32, 4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %.019.lcssa, i8 0, i64 %33, i1 false), !tbaa !22
  %scevgep = getelementptr i8, ptr %.019.lcssa, i64 %33
  br label %._crit_edge

34:                                               ; preds = %34, %.lr.ph.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %.lr.ph.new ], [ %indvars.iv.next.1, %34 ]
  %.01920 = phi ptr [ %.01920.unr, %.lr.ph.new ], [ %57, %34 ]
  %35 = getelementptr inbounds i16, ptr %12, i64 %indvars.iv
  %36 = load i16, ptr %35, align 2, !tbaa !21
  %37 = zext i16 %36 to i64
  %.idx = mul nuw nsw i64 %37, 12
  %38 = getelementptr inbounds i8, ptr %9, i64 %.idx
  %39 = load i32, ptr %38, align 4, !tbaa !22
  %40 = getelementptr inbounds i8, ptr %.01920, i64 4
  store i32 %39, ptr %.01920, align 4, !tbaa !22
  %41 = getelementptr inbounds i8, ptr %38, i64 4
  %42 = load i32, ptr %41, align 4, !tbaa !22
  %43 = getelementptr inbounds i8, ptr %.01920, i64 8
  store i32 %42, ptr %40, align 4, !tbaa !22
  %44 = getelementptr inbounds i8, ptr %38, i64 8
  %45 = load i32, ptr %44, align 4, !tbaa !22
  %46 = getelementptr inbounds i8, ptr %.01920, i64 12
  store i32 %45, ptr %43, align 4, !tbaa !22
  %gep = getelementptr i16, ptr %invariant.gep, i64 %indvars.iv
  %47 = load i16, ptr %gep, align 2, !tbaa !21
  %48 = zext i16 %47 to i64
  %.idx.1 = mul nuw nsw i64 %48, 12
  %49 = getelementptr inbounds i8, ptr %9, i64 %.idx.1
  %50 = load i32, ptr %49, align 4, !tbaa !22
  %51 = getelementptr inbounds i8, ptr %.01920, i64 16
  store i32 %50, ptr %46, align 4, !tbaa !22
  %52 = getelementptr inbounds i8, ptr %49, i64 4
  %53 = load i32, ptr %52, align 4, !tbaa !22
  %54 = getelementptr inbounds i8, ptr %.01920, i64 20
  store i32 %53, ptr %51, align 4, !tbaa !22
  %55 = getelementptr inbounds i8, ptr %49, i64 8
  %56 = load i32, ptr %55, align 4, !tbaa !22
  %57 = getelementptr inbounds i8, ptr %.01920, i64 24
  store i32 %56, ptr %54, align 4, !tbaa !22
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, %wide.trip.count
  br i1 %exitcond.not.1, label %.preheader, label %34, !llvm.loop !23

._crit_edge:                                      ; preds = %.lr.ph24.preheader, %.preheader
  %.1.lcssa = phi ptr [ %.019.lcssa, %.preheader ], [ %scevgep, %.lr.ph24.preheader ]
  ret ptr %.1.lcssa
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

!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 120}
!6 = !{!"_ZTSN2rw8GeometryE", !7, i64 0, !11, i64 16, !12, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !10, i64 40, !10, i64 48, !8, i64 56, !10, i64 120, !13, i64 128, !10, i64 144, !10, i64 152, !11, i64 160}
!7 = !{!"_ZTSN2rw6ObjectE", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !10, i64 8}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"short", !8, i64 0}
!13 = !{!"_ZTSN2rw12MaterialListE", !10, i64 0, !11, i64 8, !11, i64 12}
!14 = !{!15, !10, i64 24}
!15 = !{!"_ZTSN2rw11MorphTargetE", !10, i64 0, !16, i64 8, !10, i64 24, !10, i64 32}
!16 = !{!"_ZTSN2rw6SphereE", !17, i64 0, !18, i64 12}
!17 = !{!"_ZTSN2rw3V3dE", !18, i64 0, !18, i64 4, !18, i64 8}
!18 = !{!"float", !8, i64 0}
!19 = !{!20, !10, i64 0}
!20 = !{!"_ZTSN2rw4MeshE", !10, i64 0, !11, i64 8, !10, i64 16}
!21 = !{!12, !12, i64 0}
!22 = !{!11, !11, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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