; LLVM IR from thuhcsi/Crystal.TTVS :: TinyXML/tinystr.ll (ZN11TiXmlString6appendEPKcm)

; 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.TiXmlString = type { ptr, i64, i64 }

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

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) #1

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24)) #3 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN11TiXmlString6appendEPKcm(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr noundef %1, i64 noundef %2) #4 align 2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  %13 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %14, ptr %11, align 8, !tbaa !5
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %27, %3
  %16 = load ptr, ptr %11, align 8, !tbaa !5
  %17 = load i8, ptr %16, align 1, !tbaa !11
  %18 = icmp ne i8 %17, 0
  br i1 %18, label %19, label %25

19:                                               ; preds = %15
  %20 = load i64, ptr %10, align 8, !tbaa !9
  %21 = load i64, ptr %6, align 8, !tbaa !9
  %22 = trunc i64 %21 to i32
  %23 = zext i32 %22 to i64
  %24 = icmp ult i64 %20, %23
  br label %25

25:                                               ; preds = %19, %15
  %26 = phi i1 [ false, %15 ], [ %24, %19 ]
  br i1 %26, label %27, label %32

27:                                               ; preds = %25
  %28 = load ptr, ptr %11, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i32 1
  store ptr %29, ptr %11, align 8, !tbaa !5
  %30 = load i64, ptr %10, align 8, !tbaa !9
  %31 = add i64 %30, 1
  store i64 %31, ptr %10, align 8, !tbaa !9
  br label %15, !llvm.loop !12

32:                                               ; preds = %25
  %33 = load i64, ptr %10, align 8, !tbaa !9
  %34 = icmp ne i64 %33, 0
  br i1 %34, label %36, label %35

35:                                               ; preds = %32
  store i32 1, ptr %12, align 4
  br label %104

36:                                               ; preds = %32
  %37 = call noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24) %13)
  %38 = load i64, ptr %10, align 8, !tbaa !9
  %39 = add i64 %37, %38
  %40 = add i64 %39, 1
  store i64 %40, ptr %9, align 8, !tbaa !9
  %41 = load i64, ptr %9, align 8, !tbaa !9
  %42 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !14
  %44 = icmp ugt i64 %41, %43
  br i1 %44, label %45, label %88

45:                                               ; preds = %36
  %46 = load i64, ptr %9, align 8, !tbaa !9
  %47 = call noundef i64 @_ZN11TiXmlString15assign_new_sizeEm(ptr noundef nonnull align 8 dereferenceable(24) %13, i64 noundef %46)
  store i64 %47, ptr %8, align 8, !tbaa !9
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = call noalias noundef nonnull ptr @_Znam(i64 noundef %48) #7
  store ptr %49, ptr %7, align 8, !tbaa !5
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds i8, ptr %50, i64 0
  store i8 0, ptr %51, align 1, !tbaa !11
  %52 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !14
  %54 = icmp ne i64 %53, 0
  br i1 %54, label %55, label %64

55:                                               ; preds = %45
  %56 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !16
  %58 = icmp ne ptr %57, null
  br i1 %58, label %59, label %64

59:                                               ; preds = %55
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 0
  %62 = load ptr, ptr %61, align 8, !tbaa !16
  %63 = call noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24) %13)
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %60, ptr align 1 %62, i64 %63, i1 false)
  br label %64

64:                                               ; preds = %59, %55, %45
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = call noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24) %13)
  %67 = getelementptr inbounds i8, ptr %65, i64 %66
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = load i64, ptr %10, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %67, ptr align 1 %68, i64 %69, i1 false)
  %70 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 1
  %71 = load i64, ptr %70, align 8, !tbaa !14
  %72 = icmp ne i64 %71, 0
  br i1 %72, label %73, label %83

73:                                               ; preds = %64
  %74 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !16
  %76 = icmp ne ptr %75, null
  br i1 %76, label %77, label %83

77:                                               ; preds = %73
  %78 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 0
  %79 = load ptr, ptr %78, align 8, !tbaa !16
  %80 = icmp eq ptr %79, null
  br i1 %80, label %82, label %81

81:                                               ; preds = %77
  call void @_ZdaPv(ptr noundef %79) #8
  br label %82

82:                                               ; preds = %81, %77
  br label %83

83:                                               ; preds = %82, %73, %64
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  %85 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 0
  store ptr %84, ptr %85, align 8, !tbaa !16
  %86 = load i64, ptr %8, align 8, !tbaa !9
  %87 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 1
  store i64 %86, ptr %87, align 8, !tbaa !14
  br label %95

88:                                               ; preds = %36
  %89 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 0
  %90 = load ptr, ptr %89, align 8, !tbaa !16
  %91 = call noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24) %13)
  %92 = getelementptr inbounds i8, ptr %90, i64 %91
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  %94 = load i64, ptr %10, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %92, ptr align 1 %93, i64 %94, i1 false)
  br label %95

95:                                               ; preds = %88, %83
  %96 = load i64, ptr %9, align 8, !tbaa !9
  %97 = sub i64 %96, 1
  %98 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 2
  store i64 %97, ptr %98, align 8, !tbaa !17
  %99 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 0
  %100 = load ptr, ptr %99, align 8, !tbaa !16
  %101 = getelementptr inbounds %class.TiXmlString, ptr %13, i32 0, i32 2
  %102 = load i64, ptr %101, align 8, !tbaa !17
  %103 = getelementptr inbounds i8, ptr %100, i64 %102
  store i8 0, ptr %103, align 1, !tbaa !11
  store i32 0, ptr %12, align 4
  br label %104

104:                                              ; preds = %95, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  %105 = load i32, ptr %12, align 4
  switch i32 %105, label %107 [
    i32 0, label %106
    i32 1, label %106
  ]

106:                                              ; preds = %104, %104
  ret void

107:                                              ; preds = %104
  unreachable
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN11TiXmlString15assign_new_sizeEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #3 align 2

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nobuiltin 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 #2 = { 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 = { 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 #5 = { nobuiltin 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 #6 = { nounwind }
attributes #7 = { builtin allocsize(0) }
attributes #8 = { builtin 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 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !10, i64 8}
!15 = !{!"_ZTS11TiXmlString", !6, i64 0, !10, i64 8, !10, i64 16}
!16 = !{!15, !6, i64 0}
!17 = !{!15, !10, i64 16}


; 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: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znam(i64 noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN11TiXmlString6appendEPKcm(ptr noundef nonnull align 8 dereferenceable(24) %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #3 align 2 {
  %4 = and i64 %2, 4294967295
  %5 = load i8, ptr %1, align 1, !tbaa !5
  %6 = icmp ne i8 %5, 0
  %7 = icmp ne i64 %4, 0
  %8 = and i1 %6, %7
  br i1 %8, label %.lr.ph, label %._crit_edge.thread

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.030 = phi ptr [ %9, %.lr.ph ], [ %1, %3 ]
  %.02129 = phi i64 [ %10, %.lr.ph ], [ 0, %3 ]
  %9 = getelementptr inbounds i8, ptr %.030, i64 1
  %10 = add nuw nsw i64 %.02129, 1
  %11 = load i8, ptr %9, align 1, !tbaa !5
  %12 = icmp ne i8 %11, 0
  %13 = icmp ult i64 %10, %4
  %14 = select i1 %12, i1 %13, i1 false
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph
  %15 = tail call noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24) %0)
  %16 = add i64 %15, %10
  %17 = add i64 %16, 1
  %18 = getelementptr inbounds i8, ptr %0, i64 8
  %19 = load i64, ptr %18, align 8, !tbaa !10
  %20 = icmp ugt i64 %17, %19
  br i1 %20, label %21, label %37

21:                                               ; preds = %._crit_edge
  %22 = tail call noundef i64 @_ZN11TiXmlString15assign_new_sizeEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %17)
  %23 = tail call noalias noundef nonnull ptr @_Znam(i64 noundef %22) #5
  store i8 0, ptr %23, align 1, !tbaa !5
  %24 = load i64, ptr %18, align 8, !tbaa !10
  %.not25 = icmp eq i64 %24, 0
  br i1 %.not25, label %29, label %25

25:                                               ; preds = %21
  %26 = load ptr, ptr %0, align 8, !tbaa !14
  %.not26 = icmp eq ptr %26, null
  br i1 %.not26, label %29, label %27

27:                                               ; preds = %25
  %28 = tail call noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24) %0)
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %23, ptr nonnull align 1 %26, i64 %28, i1 false)
  br label %29

29:                                               ; preds = %27, %25, %21
  %30 = tail call noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24) %0)
  %31 = getelementptr inbounds i8, ptr %23, i64 %30
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %31, ptr noundef nonnull align 1 dereferenceable(1) %1, i64 %10, i1 false)
  %32 = load i64, ptr %18, align 8, !tbaa !10
  %.not27 = icmp eq i64 %32, 0
  br i1 %.not27, label %36, label %33

33:                                               ; preds = %29
  %34 = load ptr, ptr %0, align 8, !tbaa !14
  %.not28 = icmp eq ptr %34, null
  br i1 %.not28, label %36, label %35

35:                                               ; preds = %33
  tail call void @_ZdaPv(ptr noundef nonnull %34) #6
  br label %36

36:                                               ; preds = %35, %33, %29
  store ptr %23, ptr %0, align 8, !tbaa !14
  store i64 %22, ptr %18, align 8, !tbaa !10
  br label %41

37:                                               ; preds = %._crit_edge
  %38 = load ptr, ptr %0, align 8, !tbaa !14
  %39 = tail call noundef i64 @_ZNK11TiXmlString6lengthEv(ptr noundef nonnull align 8 dereferenceable(24) %0)
  %40 = getelementptr inbounds i8, ptr %38, i64 %39
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(1) %40, ptr noundef nonnull align 1 dereferenceable(1) %1, i64 %10, i1 false)
  %.pre = load ptr, ptr %0, align 8, !tbaa !14
  br label %41

41:                                               ; preds = %37, %36
  %42 = phi ptr [ %.pre, %37 ], [ %23, %36 ]
  %43 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 %16, ptr %43, align 8, !tbaa !15
  %44 = getelementptr inbounds i8, ptr %42, i64 %16
  store i8 0, ptr %44, align 1, !tbaa !5
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %3, %41
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZN11TiXmlString15assign_new_sizeEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #2 align 2

; Function Attrs: nobuiltin nounwind
declare void @_ZdaPv(ptr noundef) local_unnamed_addr #4

attributes #0 = { nobuiltin 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 #4 = { nobuiltin 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 #5 = { builtin allocsize(0) }
attributes #6 = { builtin 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !13, i64 8}
!11 = !{!"_ZTS11TiXmlString", !12, i64 0, !13, i64 8, !13, i64 16}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!"long", !6, i64 0}
!14 = !{!11, !12, i64 0}
!15 = !{!11, !13, i64 16}


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