; LLVM IR from aap/librw :: src/d3d/d3d8.ll (ZN2rw4d3d817getSizeNativeDataEPvii)

; 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::d3d8::InstanceDataHeader" = type { %"struct.rw::InstanceDataHeader", i16, i16, ptr }
%"struct.rw::d3d8::InstanceData" = type { i32, i32, i32, i32, ptr, i32, i32, ptr, ptr, i32, i8, i8, i8 }

; 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 @_ZN2rw4d3d817getSizeNativeDataEPvii(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 ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  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
  %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, 8
  br i1 %25, label %26, label %27

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

27:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #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 %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = getelementptr inbounds %"struct.rw::d3d8::InstanceDataHeader", ptr %31, i32 0, i32 3
  %33 = load ptr, ptr %32, align 8, !tbaa !18
  store ptr %33, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = getelementptr inbounds %"struct.rw::d3d8::InstanceDataHeader", ptr %34, i32 0, i32 2
  %36 = load i16, ptr %35, align 2, !tbaa !20
  %37 = zext i16 %36 to i32
  %38 = mul nsw i32 %37, 44
  %39 = add nsw i32 24, %38
  store i32 %39, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %65, %27
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds %"struct.rw::d3d8::InstanceDataHeader", ptr %42, i32 0, i32 2
  %44 = load i16, ptr %43, align 2, !tbaa !20
  %45 = zext i16 %44 to i32
  %46 = icmp slt i32 %41, %45
  br i1 %46, label %48, label %47

47:                                               ; preds = %40
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %68

48:                                               ; preds = %40
  %49 = load ptr, ptr %11, align 8, !tbaa !5
  %50 = getelementptr inbounds %"struct.rw::d3d8::InstanceData", ptr %49, i32 0, i32 3
  %51 = load i32, ptr %50, align 4, !tbaa !21
  %52 = mul nsw i32 %51, 2
  %53 = load ptr, ptr %11, align 8, !tbaa !5
  %54 = getelementptr inbounds %"struct.rw::d3d8::InstanceData", ptr %53, i32 0, i32 2
  %55 = load i32, ptr %54, align 8, !tbaa !23
  %56 = load ptr, ptr %11, align 8, !tbaa !5
  %57 = getelementptr inbounds %"struct.rw::d3d8::InstanceData", ptr %56, i32 0, i32 1
  %58 = load i32, ptr %57, align 4, !tbaa !24
  %59 = mul nsw i32 %55, %58
  %60 = add nsw i32 %52, %59
  %61 = load i32, ptr %12, align 4, !tbaa !9
  %62 = add nsw i32 %61, %60
  store i32 %62, ptr %12, align 4, !tbaa !9
  %63 = load ptr, ptr %11, align 8, !tbaa !5
  %64 = getelementptr inbounds %"struct.rw::d3d8::InstanceData", ptr %63, i32 1
  store ptr %64, ptr %11, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %48
  %66 = load i32, ptr %13, align 4, !tbaa !9
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %13, align 4, !tbaa !9
  br label %40, !llvm.loop !25

68:                                               ; preds = %47
  %69 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %69, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %70

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

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, !6, i64 8}
!19 = !{!"_ZTSN2rw4d3d818InstanceDataHeaderE", !17, i64 0, !14, i64 4, !14, i64 6, !6, i64 8}
!20 = !{!19, !14, i64 6}
!21 = !{!22, !10, i64 12}
!22 = !{!"_ZTSN2rw4d3d812InstanceDataE", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !6, i64 16, !10, i64 24, !10, i64 28, !6, i64 32, !6, i64 40, !10, i64 48, !7, i64 52, !7, i64 53, !7, i64 54}
!23 = !{!22, !10, i64 8}
!24 = !{!22, !10, i64 4}
!25 = distinct !{!25, !26}
!26 = !{!"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"

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_ZN2rw4d3d817getSizeNativeDataEPvii(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, 8
  br i1 %.not, label %9, label %.loopexit

9:                                                ; preds = %7
  %10 = getelementptr inbounds i8, ptr %5, i64 6
  %11 = load i16, ptr %10, align 2, !tbaa !16
  %12 = zext i16 %11 to i32
  %13 = mul nuw nsw i32 %12, 44
  %14 = add nuw nsw i32 %13, 24
  %.not22 = icmp eq i16 %11, 0
  br i1 %.not22, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %9
  %15 = getelementptr inbounds i8, ptr %5, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !18
  %xtraiter = and i32 %12, 1
  %17 = icmp eq i16 %11, 1
  br i1 %17, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i32 %12, 65534
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.01520 = phi i32 [ %14, %.lr.ph.preheader.new ], [ %37, %.lr.ph ]
  %.01619 = phi ptr [ %16, %.lr.ph.preheader.new ], [ %38, %.lr.ph ]
  %niter = phi i32 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %18 = getelementptr inbounds i8, ptr %.01619, i64 12
  %19 = load i32, ptr %18, align 4, !tbaa !19
  %20 = shl nsw i32 %19, 1
  %21 = getelementptr inbounds i8, ptr %.01619, i64 8
  %22 = load i32, ptr %21, align 8, !tbaa !21
  %23 = getelementptr inbounds i8, ptr %.01619, i64 4
  %24 = load i32, ptr %23, align 4, !tbaa !22
  %25 = mul nsw i32 %24, %22
  %26 = add i32 %20, %.01520
  %27 = add i32 %26, %25
  %28 = getelementptr inbounds i8, ptr %.01619, i64 68
  %29 = load i32, ptr %28, align 4, !tbaa !19
  %30 = shl nsw i32 %29, 1
  %31 = getelementptr inbounds i8, ptr %.01619, i64 64
  %32 = load i32, ptr %31, align 8, !tbaa !21
  %33 = getelementptr inbounds i8, ptr %.01619, i64 60
  %34 = load i32, ptr %33, align 4, !tbaa !22
  %35 = mul nsw i32 %34, %32
  %36 = add i32 %30, %27
  %37 = add i32 %36, %35
  %38 = getelementptr inbounds i8, ptr %.01619, i64 112
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !23

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %37, %.lr.ph ]
  %.01520.unr = phi i32 [ %14, %.lr.ph.preheader ], [ %37, %.lr.ph ]
  %.01619.unr = phi ptr [ %16, %.lr.ph.preheader ], [ %38, %.lr.ph ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa
  %39 = getelementptr inbounds i8, ptr %.01619.unr, i64 12
  %40 = load i32, ptr %39, align 4, !tbaa !19
  %41 = shl nsw i32 %40, 1
  %42 = getelementptr inbounds i8, ptr %.01619.unr, i64 8
  %43 = load i32, ptr %42, align 8, !tbaa !21
  %44 = getelementptr inbounds i8, ptr %.01619.unr, i64 4
  %45 = load i32, ptr %44, align 4, !tbaa !22
  %46 = mul nsw i32 %45, %43
  %47 = add i32 %41, %.01520.unr
  %48 = add i32 %47, %46
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.epil, %.loopexit.loopexit.unr-lcssa, %9, %3, %7
  %.017 = phi i32 [ 0, %7 ], [ 0, %3 ], [ %14, %9 ], [ %.lcssa.ph, %.loopexit.loopexit.unr-lcssa ], [ %48, %.lr.ph.epil ]
  ret i32 %.017
}

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" }

!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, !12, i64 6}
!17 = !{!"_ZTSN2rw4d3d818InstanceDataHeaderE", !15, i64 0, !12, i64 4, !12, i64 6, !10, i64 8}
!18 = !{!17, !10, i64 8}
!19 = !{!20, !11, i64 12}
!20 = !{!"_ZTSN2rw4d3d812InstanceDataE", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !10, i64 16, !11, i64 24, !11, i64 28, !10, i64 32, !10, i64 40, !11, i64 48, !8, i64 52, !8, i64 53, !8, i64 54}
!21 = !{!20, !11, i64 8}
!22 = !{!20, !11, i64 4}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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