; LLVM IR from aap/librw :: src/ps2/ps2.ll (ZN2rw3ps212instanceRGBAEPjPNS_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::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 @_ZN2rw3ps212instanceRGBAEPjPNS_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
  %14 = 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
  %15 = load ptr, ptr %7, align 8, !tbaa !5
  %16 = getelementptr inbounds %"struct.rw::Geometry", ptr %15, i32 0, i32 8
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  store ptr %17, ptr %12, align 8, !tbaa !5
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = getelementptr inbounds %"struct.rw::Geometry", ptr %18, i32 0, i32 1
  %20 = load i32, ptr %19, align 8, !tbaa !16
  %21 = and i32 %20, 8
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %51

23:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %24 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %24, ptr %13, align 4, !tbaa !9
  br label %25

25:                                               ; preds = %47, %23
  %26 = load i32, ptr %13, align 4, !tbaa !9
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = load i32, ptr %10, align 4, !tbaa !9
  %29 = add i32 %27, %28
  %30 = icmp ult i32 %26, %29
  br i1 %30, label %32, label %31

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

32:                                               ; preds = %25
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = getelementptr inbounds %"struct.rw::Mesh", ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !17
  %36 = load i32, ptr %13, align 4, !tbaa !9
  %37 = zext i32 %36 to i64
  %38 = getelementptr inbounds i16, ptr %35, i64 %37
  %39 = load i16, ptr %38, align 2, !tbaa !19
  store i16 %39, ptr %11, align 2, !tbaa !19
  %40 = load ptr, ptr %12, align 8, !tbaa !5
  %41 = load i16, ptr %11, align 2, !tbaa !19
  %42 = zext i16 %41 to i64
  %43 = getelementptr inbounds i32, ptr %40, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !9
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = getelementptr inbounds i32, ptr %45, i32 1
  store ptr %46, ptr %6, align 8, !tbaa !5
  store i32 %44, ptr %45, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %32
  %48 = load i32, ptr %13, align 4, !tbaa !9
  %49 = add i32 %48, 1
  store i32 %49, ptr %13, align 4, !tbaa !9
  br label %25, !llvm.loop !20

50:                                               ; preds = %31
  br label %67

51:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  %52 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %52, ptr %14, align 4, !tbaa !9
  br label %53

53:                                               ; preds = %63, %51
  %54 = load i32, ptr %14, align 4, !tbaa !9
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = add i32 %55, %56
  %58 = icmp ult i32 %54, %57
  br i1 %58, label %60, label %59

59:                                               ; preds = %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  br label %66

60:                                               ; preds = %53
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds i32, ptr %61, i32 1
  store ptr %62, ptr %6, align 8, !tbaa !5
  store i32 -16777216, ptr %61, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %60
  %64 = load i32, ptr %14, align 4, !tbaa !9
  %65 = add i32 %64, 1
  store i32 %65, ptr %14, align 4, !tbaa !9
  br label %53, !llvm.loop !22

66:                                               ; preds = %59
  br label %67

67:                                               ; preds = %66, %50
  br label %68

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

73:                                               ; preds = %68
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds i32, ptr %74, i32 1
  store ptr %75, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %74, align 4, !tbaa !9
  br label %68, !llvm.loop !23

76:                                               ; preds = %68
  %77 = 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 %77
}

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 48}
!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 = !{!12, !10, i64 16}
!17 = !{!18, !6, i64 0}
!18 = !{!"_ZTSN2rw4MeshE", !6, i64 0, !10, i64 8, !6, i64 16}
!19 = !{!14, !14, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}
!23 = distinct !{!23, !21}


; 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 @_ZN2rw3ps212instanceRGBAEPjPNS_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 48
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %1, i64 16
  %9 = load i32, ptr %8, align 8, !tbaa !14
  %10 = and i32 %9, 8
  %.not = icmp eq i32 %10, 0
  %11 = add i32 %4, %3
  %12 = icmp ugt i32 %11, %3
  br i1 %.not, label %.preheader, label %.preheader23

.preheader23:                                     ; preds = %5
  br i1 %12, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader23
  %13 = load ptr, ptr %2, align 8, !tbaa !15
  %14 = zext i32 %3 to i64
  %wide.trip.count = zext i32 %11 to i64
  %15 = sub nsw i64 %wide.trip.count, %14
  %xtraiter = and i64 %15, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.preheader

.prol.preheader:                                  ; preds = %.lr.ph, %.prol.preheader
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.prol.preheader ], [ %14, %.lr.ph ]
  %.02125.prol = phi ptr [ %21, %.prol.preheader ], [ %0, %.lr.ph ]
  %prol.iter = phi i64 [ %prol.iter.next, %.prol.preheader ], [ 0, %.lr.ph ]
  %16 = getelementptr inbounds i16, ptr %13, i64 %indvars.iv.prol
  %17 = load i16, ptr %16, align 2, !tbaa !17
  %18 = zext i16 %17 to i64
  %19 = getelementptr inbounds i32, ptr %7, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !18
  %21 = getelementptr inbounds i8, ptr %.02125.prol, i64 4
  store i32 %20, ptr %.02125.prol, align 4, !tbaa !18
  %indvars.iv.next.prol = add nuw nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.prol.loopexit, label %.prol.preheader, !llvm.loop !19

.prol.loopexit:                                   ; preds = %.prol.preheader, %.lr.ph
  %.lcssa49.unr = phi ptr [ poison, %.lr.ph ], [ %21, %.prol.preheader ]
  %indvars.iv.unr = phi i64 [ %14, %.lr.ph ], [ %indvars.iv.next.prol, %.prol.preheader ]
  %.02125.unr = phi ptr [ %0, %.lr.ph ], [ %21, %.prol.preheader ]
  %22 = sub nsw i64 %14, %wide.trip.count
  %23 = icmp ugt i64 %22, -4
  br i1 %23, label %.loopexit, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.prol.loopexit
  %invariant.gep = getelementptr i8, ptr %13, i64 2
  %invariant.gep52 = getelementptr i8, ptr %13, i64 4
  %invariant.gep54 = getelementptr i8, ptr %13, i64 6
  br label %30

.preheader:                                       ; preds = %5
  br i1 %12, label %.lr.ph29.preheader, label %.loopexit

.lr.ph29.preheader:                               ; preds = %.preheader
  %24 = add i32 %4, -1
  %25 = zext i32 %24 to i64
  %26 = add nuw nsw i64 %25, 1
  %min.iters.check = icmp ult i32 %24, 7
  br i1 %min.iters.check, label %.lr.ph29.preheader47, label %vector.ph

.lr.ph29.preheader47:                             ; preds = %middle.block, %.lr.ph29.preheader
  %.028.ph = phi i32 [ %3, %.lr.ph29.preheader ], [ %ind.end, %middle.block ]
  %.127.ph = phi ptr [ %0, %.lr.ph29.preheader ], [ %ind.end44, %middle.block ]
  br label %.lr.ph29

vector.ph:                                        ; preds = %.lr.ph29.preheader
  %n.vec = and i64 %26, 8589934584
  %.cast = trunc i64 %n.vec to i32
  %ind.end = add i32 %.cast, %3
  %27 = shl nuw nsw i64 %n.vec, 2
  %ind.end44 = getelementptr i8, ptr %0, i64 %27
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %offset.idx = shl i64 %index, 2
  %next.gep = getelementptr i8, ptr %0, i64 %offset.idx
  %28 = getelementptr i8, ptr %next.gep, i64 16
  store <4 x i32> <i32 -16777216, i32 -16777216, i32 -16777216, i32 -16777216>, ptr %next.gep, align 4, !tbaa !18
  store <4 x i32> <i32 -16777216, i32 -16777216, i32 -16777216, i32 -16777216>, ptr %28, align 4, !tbaa !18
  %index.next = add nuw i64 %index, 8
  %29 = icmp eq i64 %index.next, %n.vec
  br i1 %29, label %middle.block, label %vector.body, !llvm.loop !21

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %26, %n.vec
  br i1 %cmp.n, label %.loopexit, label %.lr.ph29.preheader47

30:                                               ; preds = %30, %.lr.ph.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %.lr.ph.new ], [ %indvars.iv.next.3, %30 ]
  %.02125 = phi ptr [ %.02125.unr, %.lr.ph.new ], [ %51, %30 ]
  %31 = getelementptr inbounds i16, ptr %13, i64 %indvars.iv
  %32 = load i16, ptr %31, align 2, !tbaa !17
  %33 = zext i16 %32 to i64
  %34 = getelementptr inbounds i32, ptr %7, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !18
  %36 = getelementptr inbounds i8, ptr %.02125, i64 4
  store i32 %35, ptr %.02125, align 4, !tbaa !18
  %gep = getelementptr i16, ptr %invariant.gep, i64 %indvars.iv
  %37 = load i16, ptr %gep, align 2, !tbaa !17
  %38 = zext i16 %37 to i64
  %39 = getelementptr inbounds i32, ptr %7, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !18
  %41 = getelementptr inbounds i8, ptr %.02125, i64 8
  store i32 %40, ptr %36, align 4, !tbaa !18
  %gep53 = getelementptr i16, ptr %invariant.gep52, i64 %indvars.iv
  %42 = load i16, ptr %gep53, align 2, !tbaa !17
  %43 = zext i16 %42 to i64
  %44 = getelementptr inbounds i32, ptr %7, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !18
  %46 = getelementptr inbounds i8, ptr %.02125, i64 12
  store i32 %45, ptr %41, align 4, !tbaa !18
  %gep55 = getelementptr i16, ptr %invariant.gep54, i64 %indvars.iv
  %47 = load i16, ptr %gep55, align 2, !tbaa !17
  %48 = zext i16 %47 to i64
  %49 = getelementptr inbounds i32, ptr %7, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !18
  %51 = getelementptr inbounds i8, ptr %.02125, i64 16
  store i32 %50, ptr %46, align 4, !tbaa !18
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %exitcond.not.3 = icmp eq i64 %indvars.iv.next.3, %wide.trip.count
  br i1 %exitcond.not.3, label %.loopexit, label %30, !llvm.loop !25

.lr.ph29:                                         ; preds = %.lr.ph29.preheader47, %.lr.ph29
  %.028 = phi i32 [ %53, %.lr.ph29 ], [ %.028.ph, %.lr.ph29.preheader47 ]
  %.127 = phi ptr [ %52, %.lr.ph29 ], [ %.127.ph, %.lr.ph29.preheader47 ]
  %52 = getelementptr inbounds i8, ptr %.127, i64 4
  store i32 -16777216, ptr %.127, align 4, !tbaa !18
  %53 = add nuw i32 %.028, 1
  %exitcond39.not = icmp eq i32 %53, %11
  br i1 %exitcond39.not, label %.loopexit, label %.lr.ph29, !llvm.loop !26

.loopexit:                                        ; preds = %.prol.loopexit, %30, %.lr.ph29, %middle.block, %.preheader23, %.preheader
  %.2 = phi ptr [ %0, %.preheader ], [ %0, %.preheader23 ], [ %ind.end44, %middle.block ], [ %52, %.lr.ph29 ], [ %.lcssa49.unr, %.prol.loopexit ], [ %51, %30 ]
  %54 = ptrtoint ptr %.2 to i64
  %55 = and i64 %54, 15
  %.not2231 = icmp eq i64 %55, 0
  br i1 %.not2231, label %._crit_edge, label %.lr.ph33.preheader

.lr.ph33.preheader:                               ; preds = %.loopexit
  %56 = sub i64 12, %54
  %57 = and i64 %56, 12
  %58 = add nuw nsw i64 %57, 4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %.2, i8 0, i64 %58, i1 false), !tbaa !18
  %scevgep = getelementptr i8, ptr %.2, i64 %58
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph33.preheader, %.loopexit
  %.3.lcssa = phi ptr [ %.2, %.loopexit ], [ %scevgep, %.lr.ph33.preheader ]
  ret ptr %.3.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 48}
!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 = !{!6, !11, i64 16}
!15 = !{!16, !10, i64 0}
!16 = !{!"_ZTSN2rw4MeshE", !10, i64 0, !11, i64 8, !10, i64 16}
!17 = !{!12, !12, i64 0}
!18 = !{!11, !11, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.unroll.disable"}
!21 = distinct !{!21, !22, !23, !24}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!"llvm.loop.isvectorized", i32 1}
!24 = !{!"llvm.loop.unroll.runtime.disable"}
!25 = distinct !{!25, !22}
!26 = distinct !{!26, !22, !24, !23}


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