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

; 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::MemoryFunctions" = type { ptr, ptr, ptr, ptr, ptr }
%"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 }

@_ZN2rw6Engine8memfuncsE = external global %"struct.rw::MemoryFunctions", align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw3ps217destroyNativeDataEPvii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %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 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
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %12, ptr %8, align 8, !tbaa !5
  %13 = load ptr, ptr %8, align 8, !tbaa !5
  %14 = getelementptr inbounds %"struct.rw::Geometry", ptr %13, i32 0, i32 13
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %16 = icmp eq ptr %15, null
  br i1 %16, label %24, label %17

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

24:                                               ; preds = %17, %3
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %25, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %60

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

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

36:                                               ; preds = %30
  store i32 2, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %50

37:                                               ; preds = %30
  %38 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !20
  %39 = load ptr, ptr %10, 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 !22
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %41, i64 %43
  %45 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !23
  call void %38(ptr noundef %46)
  br label %47

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

50:                                               ; preds = %36
  %51 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !20
  %52 = load ptr, ptr %10, align 8, !tbaa !5
  %53 = getelementptr inbounds %"struct.rw::ps2::InstanceDataHeader", ptr %52, i32 0, i32 2
  %54 = load ptr, ptr %53, align 8, !tbaa !22
  call void %51(ptr noundef %54)
  %55 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !20
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  call void %55(ptr noundef %56)
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds %"struct.rw::Geometry", ptr %57, i32 0, i32 13
  store ptr null, ptr %58, align 8, !tbaa !11
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %59, ptr %4, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %60

60:                                               ; preds = %50, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %61 = load ptr, ptr %4, align 8
  ret ptr %61
}

; 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

attributes #0 = { mustprogress 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 willreturn memory(argmem: readwrite) }
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 = !{!21, !6, i64 16}
!21 = !{!"_ZTSN2rw15MemoryFunctionsE", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!22 = !{!19, !6, i64 8}
!23 = !{!24, !6, i64 8}
!24 = !{!"_ZTSN2rw3ps212InstanceDataE", !10, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!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"

%"struct.rw::MemoryFunctions" = type { ptr, ptr, ptr, ptr, ptr }
%"struct.rw::ps2::InstanceData" = type { i32, ptr, ptr, ptr }

@_ZN2rw6Engine8memfuncsE = external local_unnamed_addr global %"struct.rw::MemoryFunctions", align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw3ps217destroyNativeDataEPvii(ptr noundef returned %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 %24, 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 %24

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

.lr.ph:                                           ; preds = %.preheader
  %11 = getelementptr inbounds i8, ptr %5, i64 8
  br label %16

._crit_edge:                                      ; preds = %16, %.preheader
  %12 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZN2rw6Engine8memfuncsE, i64 16), align 8, !tbaa !18
  %13 = getelementptr inbounds i8, ptr %5, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !20
  tail call void %12(ptr noundef %14)
  %15 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZN2rw6Engine8memfuncsE, i64 16), align 8, !tbaa !18
  tail call void %15(ptr noundef nonnull %5)
  store ptr null, ptr %4, align 8, !tbaa !5
  br label %24

16:                                               ; preds = %.lr.ph, %16
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %16 ]
  %17 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZN2rw6Engine8memfuncsE, i64 16), align 8, !tbaa !18
  %18 = load ptr, ptr %11, align 8, !tbaa !20
  %19 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %18, i64 %indvars.iv, i32 1
  %20 = load ptr, ptr %19, align 8, !tbaa !21
  tail call void %17(ptr noundef %20)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = load i32, ptr %9, align 4, !tbaa !16
  %22 = zext i32 %21 to i64
  %23 = icmp ult i64 %indvars.iv.next, %22
  br i1 %23, label %16, label %._crit_edge, !llvm.loop !23

24:                                               ; preds = %3, %7, %._crit_edge
  ret ptr %0
}

attributes #0 = { mustprogress 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, !11, i64 4}
!17 = !{!"_ZTSN2rw3ps218InstanceDataHeaderE", !15, i64 0, !11, i64 4, !10, i64 8}
!18 = !{!19, !10, i64 16}
!19 = !{!"_ZTSN2rw15MemoryFunctionsE", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!20 = !{!17, !10, i64 8}
!21 = !{!22, !10, i64 8}
!22 = !{!"_ZTSN2rw3ps212InstanceDataE", !11, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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