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

; 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::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 }

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

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

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

25:                                               ; preds = %18, %3
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %26, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %64

27:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %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
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %"struct.rw::Geometry", ptr %31, i32 0, i32 13
  store ptr null, ptr %32, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  %34 = getelementptr inbounds %"struct.rw::d3d8::InstanceDataHeader", ptr %33, i32 0, i32 3
  %35 = load ptr, ptr %34, align 8, !tbaa !18
  store ptr %35, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %53, %27
  %37 = load i32, ptr %12, align 4, !tbaa !9
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = getelementptr inbounds %"struct.rw::d3d8::InstanceDataHeader", ptr %38, i32 0, i32 2
  %40 = load i16, ptr %39, align 2, !tbaa !20
  %41 = zext i16 %40 to i32
  %42 = icmp ult i32 %37, %41
  br i1 %42, label %44, label %43

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

44:                                               ; preds = %36
  %45 = load ptr, ptr %11, align 8, !tbaa !5
  %46 = getelementptr inbounds %"struct.rw::d3d8::InstanceData", ptr %45, i32 0, i32 7
  %47 = load ptr, ptr %46, align 8, !tbaa !21
  call void @_ZN2rw3d3d18destroyIndexBufferEPv(ptr noundef %47)
  %48 = load ptr, ptr %11, align 8, !tbaa !5
  %49 = getelementptr inbounds %"struct.rw::d3d8::InstanceData", ptr %48, i32 0, i32 8
  %50 = load ptr, ptr %49, align 8, !tbaa !23
  call void @_ZN2rw3d3d19destroyVertexBufferEPv(ptr noundef %50)
  %51 = load ptr, ptr %11, align 8, !tbaa !5
  %52 = getelementptr inbounds %"struct.rw::d3d8::InstanceData", ptr %51, i32 1
  store ptr %52, ptr %11, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %44
  %54 = load i32, ptr %12, align 4, !tbaa !9
  %55 = add i32 %54, 1
  store i32 %55, ptr %12, align 4, !tbaa !9
  br label %36, !llvm.loop !24

56:                                               ; preds = %43
  %57 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !26
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = getelementptr inbounds %"struct.rw::d3d8::InstanceDataHeader", ptr %58, i32 0, i32 3
  %60 = load ptr, ptr %59, align 8, !tbaa !18
  call void %57(ptr noundef %60)
  %61 = load ptr, ptr getelementptr inbounds (%"struct.rw::MemoryFunctions", ptr @_ZN2rw6Engine8memfuncsE, i32 0, i32 2), align 8, !tbaa !26
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  call void %61(ptr noundef %62)
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %63, ptr %4, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %64

64:                                               ; preds = %56, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %65 = load ptr, ptr %4, align 8
  ret ptr %65
}

declare void @_ZN2rw3d3d18destroyIndexBufferEPv(ptr noundef) #2

declare void @_ZN2rw3d3d19destroyVertexBufferEPv(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { "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 #3 = { 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, !6, i64 32}
!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, !6, i64 40}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!27, !6, i64 16}
!27 = !{!"_ZTSN2rw15MemoryFunctionsE", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}


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

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

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

9:                                                ; preds = %7
  store ptr null, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %5, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %5, i64 6
  %13 = load i16, ptr %12, align 2, !tbaa !18
  %.not21 = icmp eq i16 %13, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load ptr, ptr %10, align 8, !tbaa !16
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %9
  %14 = phi ptr [ %.pre, %._crit_edge.loopexit ], [ %11, %9 ]
  %15 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZN2rw6Engine8memfuncsE, i64 16), align 8, !tbaa !19
  tail call void %15(ptr noundef %14)
  %16 = load ptr, ptr getelementptr inbounds (i8, ptr @_ZN2rw6Engine8memfuncsE, i64 16), align 8, !tbaa !19
  tail call void %16(ptr noundef nonnull %5)
  br label %26

.lr.ph:                                           ; preds = %9, %.lr.ph
  %.020 = phi i32 [ %22, %.lr.ph ], [ 0, %9 ]
  %.01619 = phi ptr [ %21, %.lr.ph ], [ %11, %9 ]
  %17 = getelementptr inbounds i8, ptr %.01619, i64 32
  %18 = load ptr, ptr %17, align 8, !tbaa !21
  tail call void @_ZN2rw3d3d18destroyIndexBufferEPv(ptr noundef %18)
  %19 = getelementptr inbounds i8, ptr %.01619, i64 40
  %20 = load ptr, ptr %19, align 8, !tbaa !23
  tail call void @_ZN2rw3d3d19destroyVertexBufferEPv(ptr noundef %20)
  %21 = getelementptr inbounds i8, ptr %.01619, i64 56
  %22 = add nuw nsw i32 %.020, 1
  %23 = load i16, ptr %12, align 2, !tbaa !18
  %24 = zext i16 %23 to i32
  %25 = icmp ult i32 %22, %24
  br i1 %25, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !24

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

declare void @_ZN2rw3d3d18destroyIndexBufferEPv(ptr noundef) local_unnamed_addr #1

declare void @_ZN2rw3d3d19destroyVertexBufferEPv(ptr noundef) local_unnamed_addr #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 = { "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, !10, i64 8}
!17 = !{!"_ZTSN2rw4d3d818InstanceDataHeaderE", !15, i64 0, !12, i64 4, !12, i64 6, !10, i64 8}
!18 = !{!17, !12, i64 6}
!19 = !{!20, !10, i64 16}
!20 = !{!"_ZTSN2rw15MemoryFunctionsE", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}
!21 = !{!22, !10, i64 32}
!22 = !{!"_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}
!23 = !{!22, !10, i64 40}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass