; LLVM IR from abobija/esp-discord :: src/helpers/estr.ll (estr_cat)

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

; 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: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 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: nounwind uwtable
define dso_local ptr @_estr_cat(ptr noundef %0, ...) #4 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca [1 x %struct.__va_list_tag], align 16
  %7 = alloca [1 x %struct.__va_list_tag], align 16
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %12, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  store i64 0, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 24, ptr %6) #6
  call void @llvm.lifetime.start.p0(i64 24, ptr %7) #6
  %13 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %13)
  %14 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  %15 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  call void @llvm.va_copy.p0(ptr %14, ptr %15)
  br label %16

16:                                               ; preds = %37, %1
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %40

19:                                               ; preds = %16
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = call i64 @strlen(ptr noundef %20) #7
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = add i64 %22, %21
  store i64 %23, ptr %5, align 8, !tbaa !9
  %24 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  %25 = getelementptr inbounds %struct.__va_list_tag, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %25, align 16
  %27 = icmp ule i32 %26, 40
  br i1 %27, label %28, label %33

28:                                               ; preds = %19
  %29 = getelementptr inbounds %struct.__va_list_tag, ptr %24, i32 0, i32 3
  %30 = load ptr, ptr %29, align 16
  %31 = getelementptr i8, ptr %30, i32 %26
  %32 = add i32 %26, 8
  store i32 %32, ptr %25, align 16
  br label %37

33:                                               ; preds = %19
  %34 = getelementptr inbounds %struct.__va_list_tag, ptr %24, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8
  %36 = getelementptr i8, ptr %35, i32 8
  store ptr %36, ptr %34, align 8
  br label %37

37:                                               ; preds = %33, %28
  %38 = phi ptr [ %31, %28 ], [ %35, %33 ]
  %39 = load ptr, ptr %38, align 8, !tbaa !5
  store ptr %39, ptr %3, align 8, !tbaa !5
  br label %16, !llvm.loop !11

40:                                               ; preds = %16
  %41 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %41)
  %42 = load i64, ptr %5, align 8, !tbaa !9
  %43 = icmp ule i64 %42, 0
  br i1 %43, label %44, label %46

44:                                               ; preds = %40
  %45 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %45)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %97

46:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %47 = load i64, ptr %5, align 8, !tbaa !9
  %48 = add i64 %47, 1
  %49 = call noalias ptr @malloc(i64 noundef %48) #8
  store ptr %49, ptr %9, align 8, !tbaa !5
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  %51 = icmp ne ptr %50, null
  br i1 %51, label %54, label %52

52:                                               ; preds = %46
  %53 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %53)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %96

54:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  store i64 0, ptr %10, align 8, !tbaa !9
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %55, ptr %3, align 8, !tbaa !5
  br label %56

56:                                               ; preds = %87, %54
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = icmp ne ptr %57, null
  br i1 %58, label %59, label %90

59:                                               ; preds = %56
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = call i64 @strlen(ptr noundef %60) #7
  store i64 %61, ptr %11, align 8, !tbaa !9
  %62 = load i64, ptr %11, align 8, !tbaa !9
  %63 = icmp ugt i64 %62, 0
  br i1 %63, label %64, label %73

64:                                               ; preds = %59
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  %66 = load i64, ptr %10, align 8, !tbaa !9
  %67 = getelementptr inbounds i8, ptr %65, i64 %66
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = load i64, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %67, ptr align 1 %68, i64 %69, i1 false)
  %70 = load i64, ptr %11, align 8, !tbaa !9
  %71 = load i64, ptr %10, align 8, !tbaa !9
  %72 = add i64 %71, %70
  store i64 %72, ptr %10, align 8, !tbaa !9
  br label %73

73:                                               ; preds = %64, %59
  %74 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  %75 = getelementptr inbounds %struct.__va_list_tag, ptr %74, i32 0, i32 0
  %76 = load i32, ptr %75, align 16
  %77 = icmp ule i32 %76, 40
  br i1 %77, label %78, label %83

78:                                               ; preds = %73
  %79 = getelementptr inbounds %struct.__va_list_tag, ptr %74, i32 0, i32 3
  %80 = load ptr, ptr %79, align 16
  %81 = getelementptr i8, ptr %80, i32 %76
  %82 = add i32 %76, 8
  store i32 %82, ptr %75, align 16
  br label %87

83:                                               ; preds = %73
  %84 = getelementptr inbounds %struct.__va_list_tag, ptr %74, i32 0, i32 2
  %85 = load ptr, ptr %84, align 8
  %86 = getelementptr i8, ptr %85, i32 8
  store ptr %86, ptr %84, align 8
  br label %87

87:                                               ; preds = %83, %78
  %88 = phi ptr [ %81, %78 ], [ %85, %83 ]
  %89 = load ptr, ptr %88, align 8, !tbaa !5
  store ptr %89, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  br label %56, !llvm.loop !13

90:                                               ; preds = %56
  %91 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %91)
  %92 = load ptr, ptr %9, align 8, !tbaa !5
  %93 = load i64, ptr %5, align 8, !tbaa !9
  %94 = getelementptr inbounds i8, ptr %92, i64 %93
  store i8 0, ptr %94, align 1, !tbaa !14
  %95 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %95, ptr %2, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  br label %96

96:                                               ; preds = %90, %52
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  br label %97

97:                                               ; preds = %96, %44
  call void @llvm.lifetime.end.p0(i64 24, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  %98 = load ptr, ptr %2, align 8
  ret ptr %98
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #5

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_copy.p0(ptr, ptr) #5

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 = { nocallback nofree nosync nounwind willreturn }
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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!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"

%struct.__va_list_tag = type { i32, i32, ptr, ptr }

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

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

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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 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: nofree nounwind uwtable
define dso_local noalias noundef ptr @_estr_cat(ptr noundef readonly %0, ...) local_unnamed_addr #4 {
  %2 = alloca [1 x %struct.__va_list_tag], align 16
  %3 = alloca [1 x %struct.__va_list_tag], align 16
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %2) #6
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #6
  call void @llvm.va_start.p0(ptr nonnull %2)
  call void @llvm.va_copy.p0(ptr nonnull %3, ptr nonnull %2)
  %.not32 = icmp eq ptr %0, null
  br i1 %.not32, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %1
  call void @llvm.va_end.p0(ptr nonnull %2)
  br label %25

.lr.ph:                                           ; preds = %1
  %.promoted = load i32, ptr %2, align 16
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  %5 = getelementptr inbounds i8, ptr %2, i64 16
  %6 = load ptr, ptr %5, align 16
  %.promoted35 = load ptr, ptr %4, align 8
  br label %7

7:                                                ; preds = %.lr.ph, %19
  %8 = phi ptr [ %.promoted35, %.lr.ph ], [ %20, %19 ]
  %.02234 = phi ptr [ %0, %.lr.ph ], [ %23, %19 ]
  %.02433 = phi i64 [ 0, %.lr.ph ], [ %11, %19 ]
  %9 = phi i32 [ %.promoted, %.lr.ph ], [ %21, %19 ]
  %10 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %.02234) #7
  %11 = add i64 %10, %.02433
  %12 = icmp ult i32 %9, 41
  br i1 %12, label %13, label %17

13:                                               ; preds = %7
  %14 = zext nneg i32 %9 to i64
  %15 = getelementptr i8, ptr %6, i64 %14
  %16 = add nuw nsw i32 %9, 8
  store i32 %16, ptr %2, align 16
  br label %19

17:                                               ; preds = %7
  %18 = getelementptr i8, ptr %8, i64 8
  store ptr %18, ptr %4, align 8
  br label %19

19:                                               ; preds = %17, %13
  %20 = phi ptr [ %8, %13 ], [ %18, %17 ]
  %21 = phi i32 [ %16, %13 ], [ %9, %17 ]
  %22 = phi ptr [ %15, %13 ], [ %8, %17 ]
  %23 = load ptr, ptr %22, align 8, !tbaa !5
  %.not = icmp eq ptr %23, null
  br i1 %.not, label %._crit_edge, label %7, !llvm.loop !9

._crit_edge:                                      ; preds = %19
  call void @llvm.va_end.p0(ptr nonnull %2)
  %24 = icmp eq i64 %11, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %._crit_edge.thread, %._crit_edge
  call void @llvm.va_end.p0(ptr nonnull %3)
  br label %54

26:                                               ; preds = %._crit_edge
  %27 = add i64 %11, 1
  %28 = call noalias ptr @malloc(i64 noundef %27) #8
  %.not29 = icmp eq ptr %28, null
  br i1 %.not29, label %32, label %.preheader

.preheader:                                       ; preds = %26
  br i1 %.not32, label %._crit_edge41, label %.lr.ph40

.lr.ph40:                                         ; preds = %.preheader
  %.promoted36 = load i32, ptr %3, align 16
  %29 = getelementptr inbounds i8, ptr %3, i64 8
  %30 = getelementptr inbounds i8, ptr %3, i64 16
  %31 = load ptr, ptr %30, align 16
  %.promoted42 = load ptr, ptr %29, align 8
  br label %33

32:                                               ; preds = %26
  call void @llvm.va_end.p0(ptr nonnull %3)
  br label %54

33:                                               ; preds = %.lr.ph40, %48
  %34 = phi ptr [ %.promoted42, %.lr.ph40 ], [ %49, %48 ]
  %.02039 = phi i64 [ 0, %.lr.ph40 ], [ %.121, %48 ]
  %.12338 = phi ptr [ %0, %.lr.ph40 ], [ %52, %48 ]
  %35 = phi i32 [ %.promoted36, %.lr.ph40 ], [ %50, %48 ]
  %36 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %.12338) #7
  %.not31 = icmp eq i64 %36, 0
  br i1 %.not31, label %40, label %37

37:                                               ; preds = %33
  %38 = getelementptr inbounds i8, ptr %28, i64 %.02039
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %38, ptr nonnull align 1 %.12338, i64 %36, i1 false)
  %39 = add i64 %36, %.02039
  br label %40

40:                                               ; preds = %37, %33
  %.121 = phi i64 [ %39, %37 ], [ %.02039, %33 ]
  %41 = icmp ult i32 %35, 41
  br i1 %41, label %42, label %46

42:                                               ; preds = %40
  %43 = zext nneg i32 %35 to i64
  %44 = getelementptr i8, ptr %31, i64 %43
  %45 = add nuw nsw i32 %35, 8
  store i32 %45, ptr %3, align 16
  br label %48

46:                                               ; preds = %40
  %47 = getelementptr i8, ptr %34, i64 8
  store ptr %47, ptr %29, align 8
  br label %48

48:                                               ; preds = %46, %42
  %49 = phi ptr [ %34, %42 ], [ %47, %46 ]
  %50 = phi i32 [ %45, %42 ], [ %35, %46 ]
  %51 = phi ptr [ %44, %42 ], [ %34, %46 ]
  %52 = load ptr, ptr %51, align 8, !tbaa !5
  %.not30 = icmp eq ptr %52, null
  br i1 %.not30, label %._crit_edge41, label %33, !llvm.loop !11

._crit_edge41:                                    ; preds = %48, %.preheader
  call void @llvm.va_end.p0(ptr nonnull %3)
  %53 = getelementptr inbounds i8, ptr %28, i64 %11
  store i8 0, ptr %53, align 1, !tbaa !12
  br label %54

54:                                               ; preds = %32, %._crit_edge41, %25
  %.1 = phi ptr [ null, %25 ], [ %28, %._crit_edge41 ], [ null, %32 ]
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #6
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %2) #6
  ret ptr %.1
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #5

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_copy.p0(ptr, ptr) #5

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #5

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nofree 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = !{!7, !7, i64 0}


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