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

; 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 uwtable
define dso_local noundef ptr @_ZN2rw3ps215writeNativeDataEPNS_6StreamEiPvii(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca i32, align 4
  %16 = alloca ptr, align 8
  %17 = alloca [2 x i32], align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i32 %1, ptr %8, align 4, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %18, ptr %12, align 8, !tbaa !5
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = sub nsw i32 %20, 12
  %22 = call noundef zeroext i1 @_ZN2rw16writeChunkHeaderEPNS_6StreamEii(ptr noundef %19, i32 noundef 1, i32 noundef %21)
  %23 = load ptr, ptr %12, align 8, !tbaa !5
  %24 = getelementptr inbounds %"struct.rw::Geometry", ptr %23, i32 0, i32 13
  %25 = load ptr, ptr %24, align 8, !tbaa !11
  %26 = icmp eq ptr %25, null
  br i1 %26, label %34, label %27

27:                                               ; preds = %5
  %28 = load ptr, ptr %12, 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
  %31 = getelementptr inbounds %"struct.rw::InstanceDataHeader", ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 4, !tbaa !16
  %33 = icmp ne i32 %32, 4
  br i1 %33, label %34, label %36

34:                                               ; preds = %27, %5
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %35, ptr %6, align 8
  store i32 1, ptr %13, align 4
  br label %82

36:                                               ; preds = %27
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = call noundef i32 @_ZN2rw6Stream8writeU32Ej(ptr noundef nonnull align 8 dereferenceable(8) %37, i32 noundef 4)
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %39 = load ptr, ptr %12, align 8, !tbaa !5
  %40 = getelementptr inbounds %"struct.rw::Geometry", ptr %39, i32 0, i32 13
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  store ptr %41, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %77, %36
  %43 = load i32, ptr %15, align 4, !tbaa !9
  %44 = load ptr, ptr %14, align 8, !tbaa !5
  %45 = getelementptr inbounds %"struct.rw::ps2::InstanceDataHeader", ptr %44, i32 0, i32 1
  %46 = load i32, ptr %45, align 4, !tbaa !18
  %47 = icmp ult i32 %43, %46
  br i1 %47, label %49, label %48

48:                                               ; preds = %42
  store i32 2, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %80

49:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %50 = load ptr, ptr %14, align 8, !tbaa !5
  %51 = getelementptr inbounds %"struct.rw::ps2::InstanceDataHeader", ptr %50, i32 0, i32 2
  %52 = load ptr, ptr %51, align 8, !tbaa !20
  %53 = load i32, ptr %15, align 4, !tbaa !9
  %54 = zext i32 %53 to i64
  %55 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %52, i64 %54
  store ptr %55, ptr %16, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #4
  %56 = load ptr, ptr %16, align 8, !tbaa !5
  %57 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %56, i32 0, i32 0
  %58 = load i32, ptr %57, align 8, !tbaa !21
  %59 = getelementptr inbounds [2 x i32], ptr %17, i64 0, i64 0
  store i32 %58, ptr %59, align 4, !tbaa !9
  %60 = load ptr, ptr %16, align 8, !tbaa !5
  %61 = call noundef i32 @_ZN2rw3ps215unfixDmaOffsetsEPNS0_12InstanceDataE(ptr noundef %60)
  %62 = getelementptr inbounds [2 x i32], ptr %17, i64 0, i64 1
  store i32 %61, ptr %62, align 4, !tbaa !9
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = getelementptr inbounds [2 x i32], ptr %17, i64 0, i64 0
  %65 = call noundef i32 @_ZN2rw6Stream7write32EPKvj(ptr noundef nonnull align 8 dereferenceable(8) %63, ptr noundef %64, i32 noundef 8)
  %66 = load ptr, ptr %7, align 8, !tbaa !5
  %67 = load ptr, ptr %16, align 8, !tbaa !5
  %68 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %67, i32 0, i32 2
  %69 = load ptr, ptr %68, align 8, !tbaa !23
  %70 = load ptr, ptr %16, align 8, !tbaa !5
  %71 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %70, i32 0, i32 0
  %72 = load i32, ptr %71, align 8, !tbaa !21
  %73 = load ptr, ptr %66, align 8, !tbaa !24
  %74 = getelementptr inbounds ptr, ptr %73, i64 3
  %75 = load ptr, ptr %74, align 8
  %76 = call noundef i32 %75(ptr noundef nonnull align 8 dereferenceable(8) %66, ptr noundef %69, i32 noundef %72)
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  br label %77

77:                                               ; preds = %49
  %78 = load i32, ptr %15, align 4, !tbaa !9
  %79 = add i32 %78, 1
  store i32 %79, ptr %15, align 4, !tbaa !9
  br label %42, !llvm.loop !26

80:                                               ; preds = %48
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %81, ptr %6, align 8
  store i32 1, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  br label %82

82:                                               ; preds = %80, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  %83 = load ptr, ptr %6, align 8
  ret ptr %83
}

declare noundef zeroext i1 @_ZN2rw16writeChunkHeaderEPNS_6StreamEii(ptr noundef, i32 noundef, i32 noundef) #2

declare noundef i32 @_ZN2rw6Stream8writeU32Ej(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN2rw3ps215unfixDmaOffsetsEPNS0_12InstanceDataE(ptr noundef) #3

declare noundef i32 @_ZN2rw6Stream7write32EPKvj(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef, i32 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 = { 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 #4 = { 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 = !{!22, !6, i64 16}
!24 = !{!25, !25, i64 0}
!25 = !{!"vtable pointer", !8, i64 0}
!26 = distinct !{!26, !27}
!27 = !{!"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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress 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 @_ZN2rw3ps215writeNativeDataEPNS_6StreamEiPvii(ptr noundef returned %0, i32 noundef %1, ptr nocapture noundef readonly %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #1 {
  %6 = alloca [2 x i32], align 4
  %7 = add nsw i32 %1, -12
  %8 = tail call noundef zeroext i1 @_ZN2rw16writeChunkHeaderEPNS_6StreamEii(ptr noundef %0, i32 noundef 1, i32 noundef %7)
  %9 = getelementptr inbounds i8, ptr %2, i64 152
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %.loopexit, label %12

12:                                               ; preds = %5
  %13 = load i32, ptr %10, align 4, !tbaa !14
  %.not = icmp eq i32 %13, 4
  br i1 %.not, label %14, label %.loopexit

14:                                               ; preds = %12
  %15 = tail call noundef i32 @_ZN2rw6Stream8writeU32Ej(ptr noundef nonnull align 8 dereferenceable(8) %0, i32 noundef 4)
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !16
  %.not22 = icmp eq i32 %18, 0
  br i1 %.not22, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %14
  %19 = getelementptr inbounds i8, ptr %16, i64 8
  %20 = getelementptr inbounds i8, ptr %6, i64 4
  br label %21

21:                                               ; preds = %.lr.ph, %21
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %21 ]
  %22 = load ptr, ptr %19, align 8, !tbaa !18
  %23 = getelementptr inbounds %"struct.rw::ps2::InstanceData", ptr %22, i64 %indvars.iv
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #4
  %24 = load i32, ptr %23, align 8, !tbaa !19
  store i32 %24, ptr %6, align 4, !tbaa !21
  %25 = call noundef i32 @_ZN2rw3ps215unfixDmaOffsetsEPNS0_12InstanceDataE(ptr noundef nonnull %23)
  store i32 %25, ptr %20, align 4, !tbaa !21
  %26 = call noundef i32 @_ZN2rw6Stream7write32EPKvj(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef nonnull %6, i32 noundef 8)
  %27 = getelementptr inbounds i8, ptr %23, i64 16
  %28 = load ptr, ptr %27, align 8, !tbaa !22
  %29 = load i32, ptr %23, align 8, !tbaa !19
  %30 = load ptr, ptr %0, align 8, !tbaa !23
  %31 = getelementptr inbounds i8, ptr %30, i64 24
  %32 = load ptr, ptr %31, align 8
  %33 = call noundef i32 %32(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef %28, i32 noundef %29)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #4
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %34 = load i32, ptr %17, align 4, !tbaa !16
  %35 = zext i32 %34 to i64
  %36 = icmp ult i64 %indvars.iv.next, %35
  br i1 %36, label %21, label %.loopexit, !llvm.loop !25

.loopexit:                                        ; preds = %21, %14, %5, %12
  ret ptr %0
}

declare noundef zeroext i1 @_ZN2rw16writeChunkHeaderEPNS_6StreamEii(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

declare noundef i32 @_ZN2rw6Stream8writeU32Ej(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN2rw3ps215unfixDmaOffsetsEPNS0_12InstanceDataE(ptr noundef) local_unnamed_addr #3

declare noundef i32 @_ZN2rw6Stream7write32EPKvj(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef, i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { 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 #4 = { 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, !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 = !{!11, !11, i64 0}
!22 = !{!20, !10, i64 16}
!23 = !{!24, !24, i64 0}
!24 = !{!"vtable pointer", !9, i64 0}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}


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