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

; 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 @_ZN3rmg6String11concatenateEPKc(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 i16, align 2
  %6 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  %8 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 0
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

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

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

26:                                               ; preds = %12
  %27 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 1
  %28 = load i16, ptr %27, align 8, !tbaa !13
  %29 = zext i16 %28 to i32
  %30 = load i16, ptr %5, align 2, !tbaa !12
  %31 = zext i16 %30 to i32
  %32 = add nsw i32 %29, %31
  %33 = add nsw i32 %32, 2
  %34 = trunc i32 %33 to i16
  %35 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 2
  store i16 %34, ptr %35, align 2, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %36 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 2
  %37 = load i16, ptr %36, align 2, !tbaa !14
  %38 = zext i16 %37 to i32
  %39 = add nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = call noalias ptr @malloc(i64 noundef %40) #8
  store ptr %41, ptr %6, align 8, !tbaa !5
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !9
  %45 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 1
  %46 = load i16, ptr %45, align 8, !tbaa !13
  %47 = zext i16 %46 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %42, ptr align 1 %44, i64 %47, i1 false)
  %48 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 0
  %49 = load ptr, ptr %48, align 8, !tbaa !9
  call void @free(ptr noundef %49) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  br label %50

50:                                               ; preds = %26, %12
  %51 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !9
  %53 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 1
  %54 = load i16, ptr %53, align 8, !tbaa !13
  %55 = zext i16 %54 to i64
  %56 = getelementptr inbounds i8, ptr %52, i64 %55
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = load i16, ptr %5, align 2, !tbaa !12
  %59 = zext i16 %58 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %56, ptr align 1 %57, i64 %59, i1 false)
  %60 = load i16, ptr %5, align 2, !tbaa !12
  %61 = zext i16 %60 to i32
  %62 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 1
  %63 = load i16, ptr %62, align 8, !tbaa !13
  %64 = zext i16 %63 to i32
  %65 = add nsw i32 %64, %61
  %66 = trunc i32 %65 to i16
  store i16 %66, ptr %62, align 8, !tbaa !13
  %67 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !9
  %69 = getelementptr inbounds %"class.rmg::String", ptr %7, i32 0, i32 1
  %70 = load i16, ptr %69, align 8, !tbaa !13
  %71 = zext i16 %70 to i64
  %72 = getelementptr inbounds i8, ptr %68, i64 %71
  store i8 0, ptr %72, align 1, !tbaa !15
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #6
  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 }
attributes #7 = { nounwind willreturn memory(read) }
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 = !{!11, !11, i64 0}
!13 = !{!10, !11, i64 8}
!14 = !{!10, !11, i64 10}
!15 = !{!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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture 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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3rmg6String11concatenateEPKc(ptr noundef nonnull align 8 dereferenceable(12) %0, ptr nocapture noundef readonly %1) local_unnamed_addr #3 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)
  %.pre7.pre = load ptr, ptr %0, align 8, !tbaa !5
  br label %6

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

18:                                               ; preds = %6
  %19 = trunc i32 %13 to i16
  %20 = add i16 %19, 2
  store i16 %20, ptr %14, align 2, !tbaa !12
  tail call void @free(ptr noundef %.pre7) #5
  %.pre = load ptr, ptr %0, align 8, !tbaa !5
  %.pre8 = load i16, ptr %8, align 8, !tbaa !11
  br label %21

21:                                               ; preds = %18, %6
  %22 = phi i16 [ %.pre8, %18 ], [ %9, %6 ]
  %23 = phi ptr [ %.pre, %18 ], [ %.pre7, %6 ]
  %24 = zext i16 %22 to i64
  %25 = getelementptr inbounds i8, ptr %23, i64 %24
  %26 = and i64 %7, 65535
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %25, ptr align 1 %1, i64 %26, i1 false)
  %27 = load i16, ptr %8, align 8, !tbaa !11
  %28 = trunc i64 %7 to i16
  %29 = add i16 %27, %28
  store i16 %29, ptr %8, align 8, !tbaa !11
  %30 = load ptr, ptr %0, align 8, !tbaa !5
  %31 = zext i16 %29 to i64
  %32 = getelementptr inbounds i8, ptr %30, i64 %31
  store i8 0, ptr %32, align 1, !tbaa !13
  ret void
}

attributes #0 = { 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 #1 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 willreturn memory(read) }
attributes #5 = { 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