; LLVM IR from aap/librw :: src/d3d/d3d9.ll (ZN2rw4d3d923createVertexDeclarationEPNS0_13VertexElementE)

; 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::d3d9::VertexElement" = type { i16, i16, i8, i8, i8, i8 }

@.str = external hidden unnamed_addr constant [75 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw4d3d923createVertexDeclarationEPNS0_13VertexElementE(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %5, ptr %4, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %16, %1
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr %3, align 4, !tbaa !9
  %10 = sext i32 %8 to i64
  %11 = getelementptr inbounds %"struct.rw::d3d9::VertexElement", ptr %7, i64 %10
  %12 = getelementptr inbounds %"struct.rw::d3d9::VertexElement", ptr %11, i32 0, i32 0
  %13 = load i16, ptr %12, align 2, !tbaa !11
  %14 = zext i16 %13 to i32
  %15 = icmp ne i32 %14, 255
  br i1 %15, label %16, label %17

16:                                               ; preds = %6
  br label %6, !llvm.loop !14

17:                                               ; preds = %6
  %18 = load i32, ptr %3, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = mul i64 %19, 8
  %21 = call noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef %20, i32 noundef 199424, ptr noundef @.str)
  store ptr %21, ptr %4, align 8, !tbaa !5
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = mul i64 %25, 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %22, ptr align 2 %23, i64 %26, i1 false)
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret ptr %27
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

; 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 = { inlinehint 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !13, i64 0}
!12 = !{!"_ZTSN2rw4d3d913VertexElementE", !13, i64 0, !13, i64 2, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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::d3d9::VertexElement" = type { i16, i16, i8, i8, i8, i8 }

@.str = external hidden unnamed_addr constant [75 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN2rw4d3d923createVertexDeclarationEPNS0_13VertexElementE(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  br label %2

2:                                                ; preds = %2, %1
  %indvars.iv = phi i64 [ %indvars.iv.next, %2 ], [ 0, %1 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %3 = getelementptr inbounds %"struct.rw::d3d9::VertexElement", ptr %0, i64 %indvars.iv
  %4 = load i16, ptr %3, align 2, !tbaa !5
  %.not = icmp eq i16 %4, 255
  br i1 %.not, label %5, label %2, !llvm.loop !10

5:                                                ; preds = %2
  %6 = shl i64 %indvars.iv.next, 3
  %7 = and i64 %6, 34359738360
  %8 = tail call noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef %7, i32 noundef 199424, ptr noundef nonnull @.str)
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 2 dereferenceable(1) %8, ptr noundef nonnull align 2 dereferenceable(1) %0, i64 %7, i1 false)
  ret ptr %8
}

; Function Attrs: inlinehint mustprogress uwtable
declare dso_local noundef ptr @_ZN2rw14mustmalloc_LOCEmjPKc(i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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 = { inlinehint 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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, !7, i64 0}
!6 = !{!"_ZTSN2rw4d3d913VertexElementE", !7, i64 0, !7, i64 2, !8, i64 4, !8, i64 5, !8, i64 6, !8, i64 7}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass