; LLVM IR from achilles288/robot-monitor-graphics :: src/base/util/string.ll (ZN3rmg6String4copyEPKc)

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

%"class.rmg::String" = type <{ ptr, i16, i16, [4 x i8] }>

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #0

; Function Attrs: nounwind
declare void @free(ptr noundef) #1

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(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.start.p0(i64 immarg, ptr nocapture) #4

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3rmg6String6resizeEv(ptr noundef nonnull align 8 dereferenceable(12)) #5 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3rmg6String4copyEPKc(ptr noundef nonnull align 8 dereferenceable(12) %0, ptr noundef %1) #5 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8
  %7 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 0
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %11

10:                                               ; preds = %2
  call void @_ZN3rmg6String6resizeEv(ptr noundef nonnull align 8 dereferenceable(12) %6)
  br label %11

11:                                               ; preds = %10, %2
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = call i64 @strlen(ptr noundef %12) #6
  %14 = trunc i64 %13 to i16
  %15 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 1
  store i16 %14, ptr %15, align 8, !tbaa !12
  %16 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 1
  %17 = load i16, ptr %16, align 8, !tbaa !12
  %18 = zext i16 %17 to i32
  %19 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 2
  %20 = load i16, ptr %19, align 2, !tbaa !13
  %21 = zext i16 %20 to i32
  %22 = icmp sgt i32 %18, %21
  br i1 %22, label %23, label %40

23:                                               ; preds = %11
  %24 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 1
  %25 = load i16, ptr %24, align 8, !tbaa !12
  %26 = zext i16 %25 to i32
  %27 = add nsw i32 %26, 2
  %28 = trunc i32 %27 to i16
  %29 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 2
  store i16 %28, ptr %29, align 2, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #7
  %30 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 2
  %31 = load i16, ptr %30, align 2, !tbaa !13
  %32 = zext i16 %31 to i32
  %33 = add nsw i32 %32, 1
  %34 = sext i32 %33 to i64
  %35 = call noalias ptr @malloc(i64 noundef %34) #8
  store ptr %35, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !9
  call void @free(ptr noundef %37) #7
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 0
  store ptr %38, ptr %39, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #7
  br label %40

40:                                               ; preds = %23, %11
  %41 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 1
  %45 = load i16, ptr %44, align 8, !tbaa !12
  %46 = zext i16 %45 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %42, ptr align 1 %43, i64 %46, i1 false)
  %47 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !9
  %49 = getelementptr inbounds %"class.rmg::String", ptr %6, i32 0, i32 1
  %50 = load i16, ptr %49, align 8, !tbaa !12
  %51 = zext i16 %50 to i64
  %52 = getelementptr inbounds i8, ptr %48, i64 %51
  store i8 0, ptr %52, align 1, !tbaa !14
  ret void
}

attributes #0 = { nounwind allocsize(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 = { nounwind "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 = { nounwind willreturn memory(read) "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 #6 = { nounwind willreturn memory(read) }
attributes #7 = { nounwind }
attributes #8 = { nounwind allocsize(0) }

!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 0}
!10 = !{!"_ZTSN3rmg6StringE", !6, i64 0, !11, i64 8, !11, i64 10}
!11 = !{!"short", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 10}
!14 = !{!7, !7, 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

; 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) #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN3rmg6String6resizeEv(ptr noundef nonnull align 8 dereferenceable(12)) local_unnamed_addr #4 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3rmg6String4copyEPKc(ptr noundef nonnull align 8 dereferenceable(12) %0, ptr nocapture noundef readonly %1) local_unnamed_addr #4 align 2 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %6

5:                                                ; preds = %2
  tail call void @_ZN3rmg6String6resizeEv(ptr noundef nonnull align 8 dereferenceable(12) %0)
  %.pre.pre = load ptr, ptr %0, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %5, %2
  %.pre = phi ptr [ %.pre.pre, %5 ], [ %3, %2 ]
  %7 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #5
  %8 = trunc i64 %7 to i16
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  store i16 %8, ptr %9, align 8, !tbaa !11
  %10 = trunc i64 %7 to i32
  %11 = and i32 %10, 65535
  %12 = getelementptr inbounds i8, ptr %0, i64 10
  %13 = load i16, ptr %12, align 2, !tbaa !12
  %14 = zext i16 %13 to i32
  %15 = icmp ugt i32 %11, %14
  br i1 %15, label %16, label %21

16:                                               ; preds = %6
  %17 = add i16 %8, 2
  store i16 %17, ptr %12, align 2, !tbaa !12
  %18 = zext i16 %17 to i64
  %19 = add nuw nsw i64 %18, 1
  %20 = tail call noalias ptr @malloc(i64 noundef %19) #6
  tail call void @free(ptr noundef %.pre) #7
  store ptr %20, ptr %0, align 8, !tbaa !5
  %.pre4 = load i16, ptr %9, align 8, !tbaa !11
  br label %21

21:                                               ; preds = %16, %6
  %22 = phi i16 [ %.pre4, %16 ], [ %8, %6 ]
  %23 = phi ptr [ %20, %16 ], [ %.pre, %6 ]
  %24 = zext i16 %22 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %23, ptr align 1 %1, i64 %24, i1 false)
  %25 = load ptr, ptr %0, align 8, !tbaa !5
  %26 = load i16, ptr %9, align 8, !tbaa !11
  %27 = zext i16 %26 to i64
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  store i8 0, ptr %28, align 1, !tbaa !13
  ret void
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 nofree nounwind willreturn memory(argmem: read) "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 nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { nounwind willreturn memory(read) }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { 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, !7, i64 0}
!6 = !{!"_ZTSN3rmg6StringE", !7, i64 0, !10, i64 8, !10, i64 10}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !10, i64 10}
!13 = !{!8, !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass