; LLVM IR from aap/librw :: src/d3d/xboxskin.ll (ZN2rw4xbox17getSizeNativeSkinEPvi)

; 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::Skin" = type { i32, i32, i32, ptr, ptr, ptr, ptr, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, i32 }
%"struct.rw::xbox::NativeSkin" = type { [256 x i32], [256 x i32], i32, ptr, i32 }
%"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 }

; 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 i32 @_ZN2rw4xbox17getSizeNativeSkinEPvi(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %10, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr %11, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %15, ptr %7, align 8, !tbaa !5
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %2
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %45

19:                                               ; preds = %2
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds %"struct.rw::Skin", ptr %20, i32 0, i32 14
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %25

24:                                               ; preds = %19
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %45

25:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = getelementptr inbounds %"struct.rw::Skin", ptr %26, i32 0, i32 14
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  store ptr %28, ptr %9, align 8, !tbaa !5
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  %30 = getelementptr inbounds %"struct.rw::xbox::NativeSkin", ptr %29, i32 0, i32 4
  %31 = load i32, ptr %30, align 8, !tbaa !13
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %"struct.rw::Geometry", ptr %32, i32 0, i32 4
  %34 = load i32, ptr %33, align 4, !tbaa !15
  %35 = mul nsw i32 %31, %34
  %36 = add nsw i32 2084, %35
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = getelementptr inbounds %"struct.rw::Skin", ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !20
  %40 = mul nsw i32 %39, 64
  %41 = add nsw i32 %36, %40
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = call noundef i32 @_ZN2rw17skinSplitDataSizeEPNS_4SkinE(ptr noundef %42)
  %44 = add nsw i32 %41, %43
  store i32 %44, ptr %3, align 4
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %45

45:                                               ; preds = %25, %24, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %46 = load i32, ptr %3, align 4
  ret i32 %46
}

declare noundef i32 @_ZN2rw17skinSplitDataSizeEPNS_4SkinE(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 96}
!12 = !{!"_ZTSN2rw4SkinE", !10, i64 0, !10, i64 4, !10, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !10, i64 48, !10, i64 52, !10, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !10, i64 104}
!13 = !{!14, !10, i64 2064}
!14 = !{!"_ZTSN2rw4xbox10NativeSkinE", !7, i64 0, !7, i64 1024, !10, i64 2048, !6, i64 2056, !10, i64 2064}
!15 = !{!16, !10, i64 28}
!16 = !{!"_ZTSN2rw8GeometryE", !17, i64 0, !10, i64 16, !18, 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, !19, i64 128, !6, i64 144, !6, i64 152, !10, i64 160}
!17 = !{!"_ZTSN2rw6ObjectE", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !6, i64 8}
!18 = !{!"short", !7, i64 0}
!19 = !{!"_ZTSN2rw12MaterialListE", !6, i64 0, !10, i64 8, !10, i64 12}
!20 = !{!12, !10, i64 0}


; 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 uwtable
define dso_local noundef i32 @_ZN2rw4xbox17getSizeNativeSkinEPvi(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %1 to i64
  %4 = getelementptr inbounds i8, ptr %0, i64 %3
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %23, label %7

7:                                                ; preds = %2
  %8 = getelementptr inbounds i8, ptr %5, i64 96
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp eq ptr %9, null
  br i1 %10, label %23, label %11

11:                                               ; preds = %7
  %12 = getelementptr inbounds i8, ptr %9, i64 2064
  %13 = load i32, ptr %12, align 8, !tbaa !12
  %14 = getelementptr inbounds i8, ptr %0, i64 28
  %15 = load i32, ptr %14, align 4, !tbaa !14
  %16 = mul nsw i32 %15, %13
  %17 = add nsw i32 %16, 2084
  %18 = load i32, ptr %5, align 8, !tbaa !19
  %19 = shl nsw i32 %18, 6
  %20 = add nsw i32 %17, %19
  %21 = tail call noundef i32 @_ZN2rw17skinSplitDataSizeEPNS_4SkinE(ptr noundef nonnull %5)
  %22 = add nsw i32 %20, %21
  br label %23

23:                                               ; preds = %7, %2, %11
  %.0 = phi i32 [ %22, %11 ], [ -1, %2 ], [ -1, %7 ]
  ret i32 %.0
}

declare noundef i32 @_ZN2rw17skinSplitDataSizeEPNS_4SkinE(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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 96}
!10 = !{!"_ZTSN2rw4SkinE", !11, i64 0, !11, i64 4, !11, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !11, i64 56, !6, i64 64, !6, i64 72, !6, i64 80, !6, i64 88, !6, i64 96, !11, i64 104}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !11, i64 2064}
!13 = !{!"_ZTSN2rw4xbox10NativeSkinE", !7, i64 0, !7, i64 1024, !11, i64 2048, !6, i64 2056, !11, i64 2064}
!14 = !{!15, !11, i64 28}
!15 = !{!"_ZTSN2rw8GeometryE", !16, i64 0, !11, i64 16, !17, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !6, i64 40, !6, i64 48, !7, i64 56, !6, i64 120, !18, i64 128, !6, i64 144, !6, i64 152, !11, i64 160}
!16 = !{!"_ZTSN2rw6ObjectE", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !6, i64 8}
!17 = !{!"short", !7, i64 0}
!18 = !{!"_ZTSN2rw12MaterialListE", !6, i64 0, !11, i64 8, !11, i64 12}
!19 = !{!10, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass