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

; 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::InstanceDataHeader" = type { i32 }
%"struct.rw::ps2::InstanceDataHeader" = type { %"struct.rw::InstanceDataHeader", i32, ptr }
%"struct.rw::ps2::InstanceData" = type { i32, ptr, ptr, 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 i32 @_ZN2rw3ps217getSizeNativeDataEPvii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %14, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 16, ptr %9, align 4, !tbaa !9
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = getelementptr inbounds %"struct.rw::Geometry", ptr %15, i32 0, i32 13
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = icmp eq ptr %17, null
  br i1 %18, label %26, label %19

19:                                               ; preds = %3
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  %21 = getelementptr inbounds %"struct.rw::Geometry", ptr %20, i32 0, i32 13
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = getelementptr inbounds %"struct.rw::InstanceDataHeader", ptr %22, i32 0, i32 0
  %24 = load i32, ptr %23, align 4, !tbaa !16
  %25 = icmp ne i32 %24, 4
  br i1 %25, label %26, label %27

26:                                               ; preds = %19, %3
  store i32 0, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %57

27:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = getelementptr inbounds %"struct.rw::Geometry", ptr %28, i32 0, i32 13
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  store ptr %30, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %52, %27
  %32 = load i32, ptr %12, align 4, !tbaa !9
  %33 = load ptr, ptr %11, align 8, !tbaa !5
  %34 = getelementptr inbounds %"struct.rw::ps2::InstanceDataHeader", ptr %33, i32 0, i32 1
  %35 = load i32, ptr %34, align 4, !tbaa !18
  %36 = icmp ult i32 %32, %35
  br i1 %36, label %38, label %37

37:                                               ; preds = %31
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %55

38:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = getelementptr inbounds %"struct.rw::ps2::InstanceDataHeader", ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !20
  %42 = load i32, ptr %12, align 4, !tbaa !9
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %41, i64 %43
  store ptr %44, ptr %13, align 8, !tbaa !5
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = add nsw i32 %45, 8
  store i32 %46, ptr %9, align 4, !tbaa !9
  %47 = load ptr, ptr %13, align 8, !tbaa !5
  %48 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !21
  %50 = load i32, ptr %9, align 4, !tbaa !9
  %51 = add i32 %50, %49
  store i32 %51, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  br label %52

52:                                               ; preds = %38
  %53 = load i32, ptr %12, align 4, !tbaa !9
  %54 = add i32 %53, 1
  store i32 %54, ptr %12, align 4, !tbaa !9
  br label %31, !llvm.loop !23

55:                                               ; preds = %37
  %56 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %56, ptr %4, align 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  br label %57

57:                                               ; preds = %55, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %58 = load i32, ptr %4, align 4
  ret i32 %58
}

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 152}
!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, !10, i64 0}
!17 = !{!"_ZTSN2rw18InstanceDataHeaderE", !10, i64 0}
!18 = !{!19, !10, i64 4}
!19 = !{!"_ZTSN2rw3ps218InstanceDataHeaderE", !17, i64 0, !10, i64 4, !6, i64 8}
!20 = !{!19, !6, i64 8}
!21 = !{!22, !10, i64 0}
!22 = !{!"_ZTSN2rw3ps212InstanceDataE", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!23 = distinct !{!23, !24}
!24 = !{!"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.rw::ps2::InstanceData" = type { i32, ptr, ptr, ptr }

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_ZN2rw3ps217getSizeNativeDataEPvii(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 152
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %.loopexit, label %7

7:                                                ; preds = %3
  %8 = load i32, ptr %5, align 4, !tbaa !14
  %.not = icmp eq i32 %8, 4
  br i1 %.not, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %7
  %9 = getelementptr inbounds i8, ptr %5, i64 4
  %10 = load i32, ptr %9, align 4, !tbaa !16
  %.not18 = icmp eq i32 %10, 0
  br i1 %.not18, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %11 = getelementptr inbounds i8, ptr %5, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !18
  %wide.trip.count = zext i32 %10 to i64
  %min.iters.check = icmp ult i32 %10, 9
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph
  %n.mod.vf = and i64 %wide.trip.count, 7
  %13 = icmp eq i64 %n.mod.vf, 0
  %14 = select i1 %13, i64 8, i64 %n.mod.vf
  %n.vec = sub nsw i64 %wide.trip.count, %14
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ <i32 16, i32 0, i32 0, i32 0>, %vector.ph ], [ %48, %vector.body ]
  %vec.phi20 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %49, %vector.body ]
  %15 = or disjoint i64 %index, 1
  %16 = or disjoint i64 %index, 2
  %17 = or disjoint i64 %index, 3
  %18 = or disjoint i64 %index, 4
  %19 = or disjoint i64 %index, 5
  %20 = or disjoint i64 %index, 6
  %21 = or disjoint i64 %index, 7
  %22 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %index
  %23 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %15
  %24 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %16
  %25 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %17
  %26 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %18
  %27 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %19
  %28 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %20
  %29 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %21
  %30 = add <4 x i32> %vec.phi, <i32 8, i32 8, i32 8, i32 8>
  %31 = add <4 x i32> %vec.phi20, <i32 8, i32 8, i32 8, i32 8>
  %32 = load i32, ptr %22, align 8, !tbaa !19
  %33 = load i32, ptr %23, align 8, !tbaa !19
  %34 = load i32, ptr %24, align 8, !tbaa !19
  %35 = load i32, ptr %25, align 8, !tbaa !19
  %36 = insertelement <4 x i32> poison, i32 %32, i64 0
  %37 = insertelement <4 x i32> %36, i32 %33, i64 1
  %38 = insertelement <4 x i32> %37, i32 %34, i64 2
  %39 = insertelement <4 x i32> %38, i32 %35, i64 3
  %40 = load i32, ptr %26, align 8, !tbaa !19
  %41 = load i32, ptr %27, align 8, !tbaa !19
  %42 = load i32, ptr %28, align 8, !tbaa !19
  %43 = load i32, ptr %29, align 8, !tbaa !19
  %44 = insertelement <4 x i32> poison, i32 %40, i64 0
  %45 = insertelement <4 x i32> %44, i32 %41, i64 1
  %46 = insertelement <4 x i32> %45, i32 %42, i64 2
  %47 = insertelement <4 x i32> %46, i32 %43, i64 3
  %48 = add <4 x i32> %30, %39
  %49 = add <4 x i32> %31, %47
  %index.next = add nuw i64 %index, 8
  %50 = icmp eq i64 %index.next, %n.vec
  br i1 %50, label %middle.block, label %vector.body, !llvm.loop !21

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %49, %48
  %51 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph ], [ %n.vec, %middle.block ]
  %.01416.ph = phi i32 [ 16, %.lr.ph ], [ %51, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %.01416 = phi i32 [ %55, %scalar.ph ], [ %.01416.ph, %scalar.ph.preheader ]
  %52 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %12, i64 %indvars.iv
  %53 = add nsw i32 %.01416, 8
  %54 = load i32, ptr %52, align 8, !tbaa !19
  %55 = add i32 %53, %54
  %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 %scalar.ph, !llvm.loop !25

.loopexit:                                        ; preds = %scalar.ph, %.preheader, %3, %7
  %.0 = phi i32 [ 0, %7 ], [ 0, %3 ], [ 16, %.preheader ], [ %55, %scalar.ph ]
  ret i32 %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 nosync nounwind speculatable willreturn memory(none) }

!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 152}
!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, !11, i64 0}
!15 = !{!"_ZTSN2rw18InstanceDataHeaderE", !11, i64 0}
!16 = !{!17, !11, i64 4}
!17 = !{!"_ZTSN2rw3ps218InstanceDataHeaderE", !15, i64 0, !11, i64 4, !10, i64 8}
!18 = !{!17, !10, i64 8}
!19 = !{!20, !11, i64 0}
!20 = !{!"_ZTSN2rw3ps212InstanceDataE", !11, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!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, !24, !23}


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